blob: 9200689f270899a577aa21ed73989210d87976da [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),
Vladimir Markod3e9c622020-08-05 12:20:28 +0100402 has_direct_critical_native_call_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800403 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000404 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000405 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100406 dex_file_(dex_file),
407 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100408 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100409 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800410 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000412 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100413 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
414 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
415 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
416 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000417 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100418 art_method_(nullptr),
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100419 compilation_kind_(compilation_kind),
Vladimir Marko695348f2020-05-19 14:42:02 +0100420 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100421 blocks_.reserve(kDefaultNumberOfBlocks);
422 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000423
Vladimir Markoca6fff82017-10-03 14:49:14 +0100424 ArenaAllocator* GetAllocator() const { return allocator_; }
425 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100426
427 HandleCache* GetHandleCache() { return &handle_cache_; }
428
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100429 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
430
David Brazdil69ba7b72015-06-23 18:27:30 +0100431 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000432 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100433
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000434 HBasicBlock* GetEntryBlock() const { return entry_block_; }
435 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100436 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000437
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000438 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
439 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000440
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000441 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100443 void ComputeDominanceInformation();
444 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000445 void ClearLoopInformation();
446 void FindBackEdges(ArenaBitVector* visited);
447 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100448 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100449 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000450
David Brazdil4833f5a2015-12-16 10:37:39 +0000451 // Analyze all natural loops in this graph. Returns a code specifying that it
452 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000453 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000454 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100455
David Brazdilffee3d32015-07-06 11:48:53 +0100456 // Iterate over blocks to compute try block membership. Needs reverse post
457 // order and loop information.
458 void ComputeTryBlockInformation();
459
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000460 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000461 // Returns the instruction to replace the invoke expression or null if the
462 // invoke is for a void method. Note that the caller is responsible for replacing
463 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000464 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000465
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000466 // Update the loop and try membership of `block`, which was spawned from `reference`.
467 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
468 // should be the new back edge.
469 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
470 HBasicBlock* reference,
471 bool replace_if_back_edge);
472
Mingyao Yang3584bce2015-05-19 16:01:59 -0700473 // Need to add a couple of blocks to test if the loop body is entered and
474 // put deoptimization instructions, etc.
475 void TransformLoopHeaderForBCE(HBasicBlock* header);
476
Aart Bikf8f5a162017-02-06 15:35:29 -0800477 // Adds a new loop directly after the loop with the given header and exit.
478 // Returns the new preheader.
479 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
480 HBasicBlock* body,
481 HBasicBlock* exit);
482
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000483 // Removes `block` from the graph. Assumes `block` has been disconnected from
484 // other blocks and has no instructions or phis.
485 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000486
David Brazdilfc6a86a2015-06-26 10:33:45 +0000487 // Splits the edge between `block` and `successor` while preserving the
488 // indices in the predecessor/successor lists. If there are multiple edges
489 // between the blocks, the lowest indices are used.
490 // Returns the new block which is empty and has the same dex pc as `successor`.
491 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
492
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100494 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000495
496 // Transform a loop into a format with a single preheader.
497 //
498 // Each phi in the header should be split: original one in the header should only hold
499 // inputs reachable from the back edges and a single input from the preheader. The newly created
500 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
501 //
502 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
503 // that no longer have this property.
504 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
505
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506 void SimplifyLoop(HBasicBlock* header);
507
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000508 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100509 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000510 return current_instruction_id_++;
511 }
512
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000513 int32_t GetCurrentInstructionId() const {
514 return current_instruction_id_;
515 }
516
517 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100518 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000519 current_instruction_id_ = id;
520 }
521
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100522 uint16_t GetMaximumNumberOfOutVRegs() const {
523 return maximum_number_of_out_vregs_;
524 }
525
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000526 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
527 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100528 }
529
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100530 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
531 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
532 }
533
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000534 void UpdateTemporariesVRegSlots(size_t slots) {
535 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100536 }
537
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000538 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100539 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000540 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100541 }
542
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100543 void SetNumberOfVRegs(uint16_t number_of_vregs) {
544 number_of_vregs_ = number_of_vregs;
545 }
546
547 uint16_t GetNumberOfVRegs() const {
548 return number_of_vregs_;
549 }
550
551 void SetNumberOfInVRegs(uint16_t value) {
552 number_of_in_vregs_ = value;
553 }
554
David Brazdildee58d62016-04-07 09:54:26 +0000555 uint16_t GetNumberOfInVRegs() const {
556 return number_of_in_vregs_;
557 }
558
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100559 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100560 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100561 return number_of_vregs_ - number_of_in_vregs_;
562 }
563
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100564 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100566 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100567
Eric Holk1868de92020-02-12 09:10:21 -0800568 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() const {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100569 DCHECK(GetReversePostOrder()[0] == entry_block_);
570 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
571 }
572
573 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
574 return ReverseRange(GetReversePostOrder());
575 }
576
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100577 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100578 return linear_order_;
579 }
580
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100581 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
582 return ReverseRange(GetLinearOrder());
583 }
584
Mark Mendell1152c922015-04-24 17:06:35 -0400585 bool HasBoundsChecks() const {
586 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800587 }
588
Mark Mendell1152c922015-04-24 17:06:35 -0400589 void SetHasBoundsChecks(bool value) {
590 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800591 }
592
Hans Boehm206348c2018-12-05 11:11:33 -0800593 // Is the code known to be robust against eliminating dead references
594 // and the effects of early finalization?
595 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
596
597 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
598
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000599 bool IsDebuggable() const { return debuggable_; }
600
David Brazdil8d5b8b22015-03-24 10:51:52 +0000601 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 // already, it is created and inserted into the graph. This method is only for
603 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100604 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000605
606 // TODO: This is problematic for the consistency of reference type propagation
607 // because it can be created anytime after the pass and thus it will be left
608 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600609 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000610
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600611 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
612 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000613 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600614 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
615 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000616 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600617 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
618 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600620 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
621 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000623
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100624 HCurrentMethod* GetCurrentMethod();
625
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100626 const DexFile& GetDexFile() const {
627 return dex_file_;
628 }
629
630 uint32_t GetMethodIdx() const {
631 return method_idx_;
632 }
633
Igor Murashkind01745e2017-04-05 16:40:31 -0700634 // Get the method name (without the signature), e.g. "<init>"
635 const char* GetMethodName() const;
636
637 // Get the pretty method name (class + name + optionally signature).
638 std::string PrettyMethod(bool with_signature = true) const;
639
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100640 InvokeType GetInvokeType() const {
641 return invoke_type_;
642 }
643
Mark Mendellc4701932015-04-10 13:18:51 -0400644 InstructionSet GetInstructionSet() const {
645 return instruction_set_;
646 }
647
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100648 bool IsCompilingOsr() const { return compilation_kind_ == CompilationKind::kOsr; }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000649
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100650 bool IsCompilingBaseline() const { return compilation_kind_ == CompilationKind::kBaseline; }
651
652 CompilationKind GetCompilationKind() const { return compilation_kind_; }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000653
Mingyao Yang063fc772016-08-02 11:02:54 -0700654 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
655 return cha_single_implementation_list_;
656 }
657
658 void AddCHASingleImplementationDependency(ArtMethod* method) {
659 cha_single_implementation_list_.insert(method);
660 }
661
662 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800663 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700664 }
665
David Brazdil77a48ae2015-09-15 12:34:04 +0000666 bool HasTryCatch() const { return has_try_catch_; }
667 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100668
Artem Serov2808be82018-12-20 19:15:11 +0000669 bool HasMonitorOperations() const { return has_monitor_operations_; }
670 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
671
Aart Bikb13c65b2017-03-21 20:14:07 -0700672 bool HasSIMD() const { return has_simd_; }
673 void SetHasSIMD(bool value) { has_simd_ = value; }
674
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800675 bool HasLoops() const { return has_loops_; }
676 void SetHasLoops(bool value) { has_loops_ = value; }
677
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000678 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
679 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
680
Vladimir Markod3e9c622020-08-05 12:20:28 +0100681 bool HasDirectCriticalNativeCall() const { return has_direct_critical_native_call_; }
682 void SetHasDirectCriticalNativeCall(bool value) { has_direct_critical_native_call_ = value; }
683
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100684 ArtMethod* GetArtMethod() const { return art_method_; }
685 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
686
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100687 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500688 // The instruction has been inserted into the graph, either as a constant, or
689 // before cursor.
690 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
691
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100692 ReferenceTypeInfo GetInexactObjectRti() {
693 return ReferenceTypeInfo::Create(handle_cache_.GetObjectClassHandle(), /* is_exact= */ false);
694 }
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000695
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800696 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
697 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
698 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
699
David Brazdil2d7352b2015-04-20 14:52:42 +0100700 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000701 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100702 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000703
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000704 template <class InstructionType, typename ValueType>
705 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600706 ArenaSafeMap<ValueType, InstructionType*>* cache,
707 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000708 // Try to find an existing constant of the given value.
709 InstructionType* constant = nullptr;
710 auto cached_constant = cache->find(value);
711 if (cached_constant != cache->end()) {
712 constant = cached_constant->second;
713 }
714
715 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100716 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000717 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100718 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000719 cache->Overwrite(value, constant);
720 InsertConstant(constant);
721 }
722 return constant;
723 }
724
David Brazdil8d5b8b22015-03-24 10:51:52 +0000725 void InsertConstant(HConstant* instruction);
726
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000727 // Cache a float constant into the graph. This method should only be
728 // called by the SsaBuilder when creating "equivalent" instructions.
729 void CacheFloatConstant(HFloatConstant* constant);
730
731 // See CacheFloatConstant comment.
732 void CacheDoubleConstant(HDoubleConstant* constant);
733
Vladimir Markoca6fff82017-10-03 14:49:14 +0100734 ArenaAllocator* const allocator_;
735 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000736
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100737 HandleCache handle_cache_;
738
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000739 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100740 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000741
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100742 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100743 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000744
Aart Bik281c6812016-08-26 11:31:48 -0700745 // List of blocks to perform a linear order tree traversal. Unlike the reverse
746 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100747 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100748
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000749 HBasicBlock* entry_block_;
750 HBasicBlock* exit_block_;
751
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100752 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100753 uint16_t maximum_number_of_out_vregs_;
754
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100755 // The number of virtual registers in this method. Contains the parameters.
756 uint16_t number_of_vregs_;
757
758 // The number of virtual registers used by parameters of this method.
759 uint16_t number_of_in_vregs_;
760
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000761 // Number of vreg size slots that the temporaries use (used in baseline compiler).
762 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100763
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800764 // Flag whether there are bounds checks in the graph. We can skip
765 // BCE if it's false. It's only best effort to keep it up to date in
766 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400767 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800768
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800769 // Flag whether there are try/catch blocks in the graph. We will skip
770 // try/catch-related passes if it's false. It's only best effort to keep
771 // it up to date in the presence of code elimination so there might be
772 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000773 bool has_try_catch_;
774
Artem Serov2808be82018-12-20 19:15:11 +0000775 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
776 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
777 bool has_monitor_operations_;
778
Aart Bikb13c65b2017-03-21 20:14:07 -0700779 // Flag whether SIMD instructions appear in the graph. If true, the
780 // code generators may have to be more careful spilling the wider
781 // contents of SIMD registers.
782 bool has_simd_;
783
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800784 // Flag whether there are any loops in the graph. We can skip loop
785 // optimization if it's false. It's only best effort to keep it up
786 // to date in the presence of code elimination so there might be false
787 // positives.
788 bool has_loops_;
789
790 // Flag whether there are any irreducible loops in the graph. It's only
791 // best effort to keep it up to date in the presence of code elimination
792 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000793 bool has_irreducible_loops_;
794
Vladimir Markod3e9c622020-08-05 12:20:28 +0100795 // Flag whether there are any direct calls to native code registered
796 // for @CriticalNative methods.
797 bool has_direct_critical_native_call_;
798
Hans Boehm206348c2018-12-05 11:11:33 -0800799 // Is the code known to be robust against eliminating dead references
800 // and the effects of early finalization? If false, dead reference variables
801 // are kept if they might be visible to the garbage collector.
802 // Currently this means that the class was declared to be dead-reference-safe,
803 // the method accesses no reachability-sensitive fields or data, and the same
804 // is true for any methods that were inlined into the current one.
805 bool dead_reference_safe_;
806
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000807 // Indicates whether the graph should be compiled in a way that
808 // ensures full debuggability. If false, we can apply more
809 // aggressive optimizations that may limit the level of debugging.
810 const bool debuggable_;
811
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000812 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000813 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000814
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100815 // The dex file from which the method is from.
816 const DexFile& dex_file_;
817
818 // The method index in the dex file.
819 const uint32_t method_idx_;
820
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100821 // If inlined, this encodes how the callee is being invoked.
822 const InvokeType invoke_type_;
823
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100824 // Whether the graph has been transformed to SSA form. Only used
825 // in debug mode to ensure we are not using properties only valid
826 // for non-SSA form (like the number of temporaries).
827 bool in_ssa_form_;
828
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800829 // Number of CHA guards in the graph. Used to short-circuit the
830 // CHA guard optimization pass when there is no CHA guard left.
831 uint32_t number_of_cha_guards_;
832
Mathieu Chartiere401d142015-04-22 13:56:20 -0700833 const InstructionSet instruction_set_;
834
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000835 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000836 HNullConstant* cached_null_constant_;
837 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000838 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000839 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000840 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000841
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100842 HCurrentMethod* cached_current_method_;
843
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100844 // The ArtMethod this graph is for. Note that for AOT, it may be null,
845 // for example for methods whose declaring class could not be resolved
846 // (such as when the superclass could not be found).
847 ArtMethod* art_method_;
848
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100849 // How we are compiling the graph: either optimized, osr, or baseline.
850 // For osr, we will make all loops seen as irreducible and emit special
851 // stack maps to mark compiled code entries which the interpreter can
852 // directly jump to.
853 const CompilationKind compilation_kind_;
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000854
Mingyao Yang063fc772016-08-02 11:02:54 -0700855 // List of methods that are assumed to have single implementation.
856 ArenaSet<ArtMethod*> cha_single_implementation_list_;
857
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000858 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100859 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000860 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000861 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862 DISALLOW_COPY_AND_ASSIGN(HGraph);
863};
864
Vladimir Markof9f64412015-09-02 14:05:49 +0100865class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000866 public:
867 HLoopInformation(HBasicBlock* header, HGraph* graph)
868 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100869 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000870 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100871 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100872 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100873 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100874 blocks_(graph->GetAllocator(),
875 graph->GetBlocks().size(),
876 true,
877 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100878 back_edges_.reserve(kDefaultNumberOfBackEdges);
879 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100880
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000881 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100882 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000883
884 void Dump(std::ostream& os);
885
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100886 HBasicBlock* GetHeader() const {
887 return header_;
888 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000890 void SetHeader(HBasicBlock* block) {
891 header_ = block;
892 }
893
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100894 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
895 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
896 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
897
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000898 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100899 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000900 }
901
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100903 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 }
905
David Brazdil46e2a392015-03-16 17:31:52 +0000906 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100907 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908 }
909
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000910 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100911 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000912 }
913
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100914 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100915
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100916 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100917 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100918 }
919
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100920 // Returns the lifetime position of the back edge that has the
921 // greatest lifetime position.
922 size_t GetLifetimeEnd() const;
923
924 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100925 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100926 }
927
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000928 // Finds blocks that are part of this loop.
929 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100930
Artem Serov7f4aff62017-06-21 17:02:18 +0100931 // Updates blocks population of the loop and all of its outer' ones recursively after the
932 // population of the inner loop is updated.
933 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
934
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100935 // Returns whether this loop information contains `block`.
936 // Note that this loop information *must* be populated before entering this function.
937 bool Contains(const HBasicBlock& block) const;
938
939 // Returns whether this loop information is an inner loop of `other`.
940 // Note that `other` *must* be populated before entering this function.
941 bool IsIn(const HLoopInformation& other) const;
942
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800943 // Returns true if instruction is not defined within this loop.
944 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700945
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100946 const ArenaBitVector& GetBlocks() const { return blocks_; }
947
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000948 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000949 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000950
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000951 void ClearAllBlocks() {
952 blocks_.ClearAllBits();
953 }
954
David Brazdil3f4a5222016-05-06 12:46:21 +0100955 bool HasBackEdgeNotDominatedByHeader() const;
956
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100957 bool IsPopulated() const {
958 return blocks_.GetHighestBitSet() != -1;
959 }
960
Anton Shaminf89381f2016-05-16 16:44:13 +0600961 bool DominatesAllBackEdges(HBasicBlock* block);
962
David Sehrc757dec2016-11-04 15:48:34 -0700963 bool HasExitEdge() const;
964
Artem Serov7f4aff62017-06-21 17:02:18 +0100965 // Resets back edge and blocks-in-loop data.
966 void ResetBasicBlockData() {
967 back_edges_.clear();
968 ClearAllBlocks();
969 }
970
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000971 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100972 // Internal recursive implementation of `Populate`.
973 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100974 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100975
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000976 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100977 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000978 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100979 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100980 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100981 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000982
983 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
984};
985
David Brazdilec16f792015-08-19 15:04:01 +0100986// Stores try/catch information for basic blocks.
987// Note that HGraph is constructed so that catch blocks cannot simultaneously
988// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100989class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100990 public:
991 // Try block information constructor.
992 explicit TryCatchInformation(const HTryBoundary& try_entry)
993 : try_entry_(&try_entry),
994 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000995 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100996 DCHECK(try_entry_ != nullptr);
997 }
998
999 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001000 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +01001001 : try_entry_(nullptr),
1002 catch_dex_file_(&dex_file),
1003 catch_type_index_(catch_type_index) {}
1004
1005 bool IsTryBlock() const { return try_entry_ != nullptr; }
1006
1007 const HTryBoundary& GetTryEntry() const {
1008 DCHECK(IsTryBlock());
1009 return *try_entry_;
1010 }
1011
1012 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
1013
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001014 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001015 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001016 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +01001017 }
1018
Andreas Gampea5b09a62016-11-17 15:21:22 -08001019 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001020 DCHECK(IsCatchBlock());
1021 return catch_type_index_;
1022 }
1023
1024 const DexFile& GetCatchDexFile() const {
1025 DCHECK(IsCatchBlock());
1026 return *catch_dex_file_;
1027 }
1028
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001029 void SetInvalidTypeIndex() {
1030 catch_type_index_ = dex::TypeIndex::Invalid();
1031 }
1032
David Brazdilec16f792015-08-19 15:04:01 +01001033 private:
1034 // One of possibly several TryBoundary instructions entering the block's try.
1035 // Only set for try blocks.
1036 const HTryBoundary* try_entry_;
1037
1038 // Exception type information. Only set for catch blocks.
1039 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001040 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +01001041};
1042
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001043static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001044static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001045
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001046// A block in a method. Contains the list of instructions represented
1047// as a double linked list. Each block knows its predecessors and
1048// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001049
Vladimir Markof9f64412015-09-02 14:05:49 +01001050class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001051 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07001052 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001054 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
1055 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001056 loop_information_(nullptr),
1057 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001058 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001059 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001060 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001061 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001062 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001063 try_catch_information_(nullptr) {
1064 predecessors_.reserve(kDefaultNumberOfPredecessors);
1065 successors_.reserve(kDefaultNumberOfSuccessors);
1066 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1067 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001068
Vladimir Marko60584552015-09-03 13:35:12 +00001069 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001070 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001071 }
1072
Vladimir Marko60584552015-09-03 13:35:12 +00001073 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001074 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001075 }
1076
David Brazdild26a4112015-11-10 11:07:31 +00001077 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1078 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1079
Vladimir Marko60584552015-09-03 13:35:12 +00001080 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1081 return ContainsElement(successors_, block, start_from);
1082 }
1083
1084 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001085 return dominated_blocks_;
1086 }
1087
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001088 bool IsEntryBlock() const {
1089 return graph_->GetEntryBlock() == this;
1090 }
1091
1092 bool IsExitBlock() const {
1093 return graph_->GetExitBlock() == this;
1094 }
1095
David Brazdil46e2a392015-03-16 17:31:52 +00001096 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001097 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001098 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001099 bool IsSingleTryBoundary() const;
1100
1101 // Returns true if this block emits nothing but a jump.
1102 bool IsSingleJump() const {
1103 HLoopInformation* loop_info = GetLoopInformation();
1104 return (IsSingleGoto() || IsSingleTryBoundary())
1105 // Back edges generate a suspend check.
1106 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1107 }
David Brazdil46e2a392015-03-16 17:31:52 +00001108
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001109 void AddBackEdge(HBasicBlock* back_edge) {
1110 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001111 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001112 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001113 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001114 loop_information_->AddBackEdge(back_edge);
1115 }
1116
Artem Serov7f4aff62017-06-21 17:02:18 +01001117 // Registers a back edge; if the block was not a loop header before the call associates a newly
1118 // created loop info with it.
1119 //
1120 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1121 // info for all blocks during back edges recalculation.
1122 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1123 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1124 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1125 }
1126 loop_information_->AddBackEdge(back_edge);
1127 }
1128
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001129 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001130 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001131
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001132 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001134 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001135
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001136 HBasicBlock* GetDominator() const { return dominator_; }
1137 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001138 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1139
1140 void RemoveDominatedBlock(HBasicBlock* block) {
1141 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001142 }
Vladimir Marko60584552015-09-03 13:35:12 +00001143
1144 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1145 ReplaceElement(dominated_blocks_, existing, new_block);
1146 }
1147
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001148 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001149
1150 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001151 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001152 }
1153
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1155 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001156 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001157 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001158 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1159 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001160
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001161 HInstruction* GetFirstInstructionDisregardMoves() const;
1162
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001163 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001164 successors_.push_back(block);
1165 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001166 }
1167
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001168 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1169 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001170 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001171 new_block->predecessors_.push_back(this);
1172 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001173 }
1174
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001175 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1176 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001177 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001178 new_block->successors_.push_back(this);
1179 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001180 }
1181
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001182 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001183 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001184 // `predecessor` and `successor`.
1185 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1186 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001187 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001188 successor->predecessors_[predecessor_index] = this;
1189 predecessor->successors_[successor_index] = this;
1190 successors_.push_back(successor);
1191 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001192 }
1193
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001194 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001195 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001196 }
1197
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001198 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001199 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001200 }
1201
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001202 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001203 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001204 }
1205
1206 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001207 predecessors_.push_back(block);
1208 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001209 }
1210
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001211 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001212 DCHECK_EQ(predecessors_.size(), 2u);
1213 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001214 }
1215
David Brazdil769c9e52015-04-27 13:54:09 +01001216 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001217 DCHECK_EQ(successors_.size(), 2u);
1218 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001219 }
1220
David Brazdilfc6a86a2015-06-26 10:33:45 +00001221 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001222 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001223 }
1224
David Brazdilfc6a86a2015-06-26 10:33:45 +00001225 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001226 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001227 }
1228
David Brazdilfc6a86a2015-06-26 10:33:45 +00001229 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001230 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001231 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001232 }
1233
1234 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001235 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001236 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001237 }
1238
1239 // Returns whether the first occurrence of `predecessor` in the list of
1240 // predecessors is at index `idx`.
1241 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001242 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001243 return GetPredecessorIndexOf(predecessor) == idx;
1244 }
1245
David Brazdild7558da2015-09-22 13:04:14 +01001246 // Create a new block between this block and its predecessors. The new block
1247 // is added to the graph, all predecessor edges are relinked to it and an edge
1248 // is created to `this`. Returns the new empty block. Reverse post order or
1249 // loop and try/catch information are not updated.
1250 HBasicBlock* CreateImmediateDominator();
1251
David Brazdilfc6a86a2015-06-26 10:33:45 +00001252 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001253 // created, latter block. Note that this method will add the block to the
1254 // graph, create a Goto at the end of the former block and will create an edge
1255 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001256 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001257 HBasicBlock* SplitBefore(HInstruction* cursor);
1258
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001259 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001260 // created block. Note that this method just updates raw block information,
1261 // like predecessors, successors, dominators, and instruction list. It does not
1262 // update the graph, reverse post order, loop information, nor make sure the
1263 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001264 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1265
1266 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1267 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001268
1269 // Merge `other` at the end of `this`. Successors and dominated blocks of
1270 // `other` are changed to be successors and dominated blocks of `this`. Note
1271 // that this method does not update the graph, reverse post order, loop
1272 // information, nor make sure the blocks are consistent (for example ending
1273 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001274 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001275
1276 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1277 // of `this` are moved to `other`.
1278 // Note that this method does not update the graph, reverse post order, loop
1279 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001280 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001281 void ReplaceWith(HBasicBlock* other);
1282
Aart Bik6b69e0a2017-01-11 10:20:43 -08001283 // Merges the instructions of `other` at the end of `this`.
1284 void MergeInstructionsWith(HBasicBlock* other);
1285
David Brazdil2d7352b2015-04-20 14:52:42 +01001286 // Merge `other` at the end of `this`. This method updates loops, reverse post
1287 // order, links to predecessors, successors, dominators and deletes the block
1288 // from the graph. The two blocks must be successive, i.e. `this` the only
1289 // predecessor of `other` and vice versa.
1290 void MergeWith(HBasicBlock* other);
1291
1292 // Disconnects `this` from all its predecessors, successors and dominator,
1293 // removes it from all loops it is included in and eventually from the graph.
1294 // The block must not dominate any other block. Predecessors and successors
1295 // are safely updated.
1296 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001297
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001298 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001299 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001300 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001301 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001302 // Replace phi `initial` with `replacement` within this block.
1303 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001304 // Replace instruction `initial` with `replacement` within this block.
1305 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1306 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001307 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001308 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001309 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1310 // instruction list. With 'ensure_safety' set to true, it verifies that the
1311 // instruction is not in use and removes it from the use lists of its inputs.
1312 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1313 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001314 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001315
1316 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001317 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001318 }
1319
Roland Levillain6b879dd2014-09-22 17:13:44 +01001320 bool IsLoopPreHeaderFirstPredecessor() const {
1321 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001322 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001323 }
1324
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001325 bool IsFirstPredecessorBackEdge() const {
1326 DCHECK(IsLoopHeader());
1327 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1328 }
1329
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001330 HLoopInformation* GetLoopInformation() const {
1331 return loop_information_;
1332 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001333
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001334 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001335 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001336 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001337 void SetInLoop(HLoopInformation* info) {
1338 if (IsLoopHeader()) {
1339 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001340 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001341 loop_information_ = info;
1342 } else if (loop_information_->Contains(*info->GetHeader())) {
1343 // Block is currently part of an outer loop. Make it part of this inner loop.
1344 // Note that a non loop header having a loop information means this loop information
1345 // has already been populated
1346 loop_information_ = info;
1347 } else {
1348 // Block is part of an inner loop. Do not update the loop information.
1349 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1350 // at this point, because this method is being called while populating `info`.
1351 }
1352 }
1353
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001354 // Raw update of the loop information.
1355 void SetLoopInformation(HLoopInformation* info) {
1356 loop_information_ = info;
1357 }
1358
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001359 bool IsInLoop() const { return loop_information_ != nullptr; }
1360
David Brazdilec16f792015-08-19 15:04:01 +01001361 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1362
1363 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1364 try_catch_information_ = try_catch_information;
1365 }
1366
1367 bool IsTryBlock() const {
1368 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1369 }
1370
1371 bool IsCatchBlock() const {
1372 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1373 }
David Brazdilffee3d32015-07-06 11:48:53 +01001374
1375 // Returns the try entry that this block's successors should have. They will
1376 // be in the same try, unless the block ends in a try boundary. In that case,
1377 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001378 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001379
Aart Bik75ff2c92018-04-21 01:28:11 +00001380 bool HasThrowingInstructions() const;
1381
David Brazdila4b8c212015-05-07 09:59:30 +01001382 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001383 bool Dominates(HBasicBlock* block) const;
1384
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001385 size_t GetLifetimeStart() const { return lifetime_start_; }
1386 size_t GetLifetimeEnd() const { return lifetime_end_; }
1387
1388 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1389 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1390
David Brazdil8d5b8b22015-03-24 10:51:52 +00001391 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001392 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001393 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001394 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001395 bool HasSinglePhi() const;
1396
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001397 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001398 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001399 ArenaVector<HBasicBlock*> predecessors_;
1400 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001401 HInstructionList instructions_;
1402 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001403 HLoopInformation* loop_information_;
1404 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001405 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001406 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001407 // The dex program counter of the first instruction of this block.
1408 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001409 size_t lifetime_start_;
1410 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001411 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001413 friend class HGraph;
1414 friend class HInstruction;
1415
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001416 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1417};
1418
David Brazdilb2bd1c52015-03-25 11:17:37 +00001419// Iterates over the LoopInformation of all loops which contain 'block'
1420// from the innermost to the outermost.
1421class HLoopInformationOutwardIterator : public ValueObject {
1422 public:
1423 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1424 : current_(block.GetLoopInformation()) {}
1425
1426 bool Done() const { return current_ == nullptr; }
1427
1428 void Advance() {
1429 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001430 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001431 }
1432
1433 HLoopInformation* Current() const {
1434 DCHECK(!Done());
1435 return current_;
1436 }
1437
1438 private:
1439 HLoopInformation* current_;
1440
1441 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1442};
1443
Artem Serov1a719e42019-07-18 14:24:55 +01001444#define FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001445 M(Above, Condition) \
1446 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001447 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001448 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001449 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001450 M(ArrayGet, Instruction) \
1451 M(ArrayLength, Instruction) \
1452 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001453 M(Below, Condition) \
1454 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001455 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001456 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001457 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001458 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001459 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001460 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001461 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001462 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001463 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001464 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001465 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001466 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001467 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001468 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001469 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001470 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001471 M(Exit, Instruction) \
1472 M(FloatConstant, Constant) \
1473 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001474 M(GreaterThan, Condition) \
1475 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001476 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001477 M(InstanceFieldGet, Instruction) \
1478 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001479 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001480 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001481 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001482 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001483 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001484 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001485 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001486 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001487 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001488 M(LessThan, Condition) \
1489 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001490 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001491 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001492 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001493 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001494 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001495 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001496 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001497 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001498 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001499 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001500 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001501 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001502 M(Neg, UnaryOperation) \
1503 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001504 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001505 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001506 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001507 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001508 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001509 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001510 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001511 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001512 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001513 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001514 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001515 M(Return, Instruction) \
1516 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001517 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001518 M(Shl, BinaryOperation) \
1519 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001520 M(StaticFieldGet, Instruction) \
1521 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001522 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001523 M(UnresolvedInstanceFieldGet, Instruction) \
1524 M(UnresolvedInstanceFieldSet, Instruction) \
1525 M(UnresolvedStaticFieldGet, Instruction) \
1526 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001527 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001528 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001529 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001530 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001531 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001532 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001533 M(UShr, BinaryOperation) \
Artem Serov1a719e42019-07-18 14:24:55 +01001534 M(Xor, BinaryOperation)
1535
1536#define FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001537 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001538 M(VecExtractScalar, VecUnaryOperation) \
1539 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001540 M(VecCnv, VecUnaryOperation) \
1541 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001542 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001543 M(VecNot, VecUnaryOperation) \
1544 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001545 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001546 M(VecSub, VecBinaryOperation) \
1547 M(VecMul, VecBinaryOperation) \
1548 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001549 M(VecMin, VecBinaryOperation) \
1550 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001551 M(VecAnd, VecBinaryOperation) \
1552 M(VecAndNot, VecBinaryOperation) \
1553 M(VecOr, VecBinaryOperation) \
1554 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001555 M(VecSaturationAdd, VecBinaryOperation) \
1556 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001557 M(VecShl, VecBinaryOperation) \
1558 M(VecShr, VecBinaryOperation) \
1559 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001560 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001561 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001562 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001563 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001564 M(VecLoad, VecMemoryOperation) \
1565 M(VecStore, VecMemoryOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001566 M(VecPredSetAll, VecPredSetOperation) \
1567 M(VecPredWhile, VecPredSetOperation) \
1568 M(VecPredCondition, VecOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001569
Artem Serov1a719e42019-07-18 14:24:55 +01001570#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1571 FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
1572 FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M)
1573
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001574/*
1575 * Instructions, shared across several (not all) architectures.
1576 */
1577#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1578#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1579#else
1580#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001581 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001582 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001583 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001584 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001585#endif
1586
Alexandre Ramesef20f712015-06-09 10:29:30 +01001587#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1588
1589#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1590
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001591#ifndef ART_ENABLE_CODEGEN_x86
1592#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1593#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001594#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1595 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001596 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001597 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001598 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001599#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001600
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301601#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1602#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301603 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301604 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301605#else
1606#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1607#endif
1608
Alexandre Ramesef20f712015-06-09 10:29:30 +01001609#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1610
1611#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1612 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001613 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001614 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1615 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1616 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301617 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1618 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001619
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001620#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1621 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001622 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001623 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001624 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001625 M(Invoke, Instruction) \
1626 M(VecOperation, Instruction) \
1627 M(VecUnaryOperation, VecOperation) \
1628 M(VecBinaryOperation, VecOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001629 M(VecMemoryOperation, VecOperation) \
1630 M(VecPredSetOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001631
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001632#define FOR_EACH_INSTRUCTION(M) \
1633 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1634 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1635
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001636#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001637FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1638#undef FORWARD_DECLARATION
1639
Vladimir Marko372f10e2016-05-17 16:30:10 +01001640#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001641 private: \
1642 H##type& operator=(const H##type&) = delete; \
1643 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001644 const char* DebugName() const override { return #type; } \
1645 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001646 DCHECK(IsClonable()); \
1647 return new (arena) H##type(*this->As##type()); \
1648 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001649 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001650
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001651#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001652 private: \
1653 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001654 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001655
Artem Serovcced8ba2017-07-19 18:18:09 +01001656#define DEFAULT_COPY_CONSTRUCTOR(type) \
1657 explicit H##type(const H##type& other) = default;
1658
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001659template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001660class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1661 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001662 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001663 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001664 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001665 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001666 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001667 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001668 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001669
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001670 private:
David Brazdiled596192015-01-23 10:39:45 +00001671 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001672 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001673
1674 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001675 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001676
Vladimir Marko46817b82016-03-29 12:21:58 +01001677 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001678
1679 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1680};
1681
David Brazdiled596192015-01-23 10:39:45 +00001682template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001683using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001684
David Brazdil1abb4192015-02-17 18:33:36 +00001685// This class is used by HEnvironment and HInstruction classes to record the
1686// instructions they use and pointers to the corresponding HUseListNodes kept
1687// by the used instructions.
1688template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001689class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001690 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001691 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1692 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001693
Vladimir Marko46817b82016-03-29 12:21:58 +01001694 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1695 : HUserRecord(old_record.instruction_, before_use_node) {}
1696 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1697 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001698 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001699 }
1700
1701 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001702 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1703 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001704
1705 private:
1706 // Instruction used by the user.
1707 HInstruction* instruction_;
1708
Vladimir Marko46817b82016-03-29 12:21:58 +01001709 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1710 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001711};
1712
Vladimir Markoe9004912016-06-16 16:50:52 +01001713// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1714// This is used for HInstruction::GetInputs() to return a container wrapper providing
1715// HInstruction* values even though the underlying container has HUserRecord<>s.
1716struct HInputExtractor {
1717 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1718 return record.GetInstruction();
1719 }
1720 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1721 return record.GetInstruction();
1722 }
1723};
1724
1725using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1726using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1727
Aart Bik854a02b2015-07-14 16:07:00 -07001728/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001729 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001730 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001731 * For write/read dependences on fields/arrays, the dependence analysis uses
1732 * type disambiguation (e.g. a float field write cannot modify the value of an
1733 * integer field read) and the access type (e.g. a reference array write cannot
1734 * modify the value of a reference field read [although it may modify the
1735 * reference fetch prior to reading the field, which is represented by its own
1736 * write/read dependence]). The analysis makes conservative points-to
1737 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1738 * the same, and any reference read depends on any reference read without
1739 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001740 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001741 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1742 * alive across the point where garbage collection might happen.
1743 *
1744 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1745 *
1746 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1747 * those instructions from the compiler perspective (between this instruction and the next one
1748 * in the IR).
1749 *
1750 * Note: Instructions which can cause GC only on a fatal slow path do not need
1751 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1752 * one. However the execution may return to compiled code if there is a catch block in the
1753 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1754 * set.
1755 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 * The internal representation uses 38-bit and is described in the table below.
1757 * The first line indicates the side effect, and for field/array accesses the
1758 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001760 * The two numbered lines below indicate the bit position in the bitfield (read
1761 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001762 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001763 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1764 * +-------------+---------+---------+--------------+---------+---------+
1765 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1766 * | 3 |333333322|222222221| 1 |111111110|000000000|
1767 * | 7 |654321098|765432109| 8 |765432109|876543210|
1768 *
1769 * Note that, to ease the implementation, 'changes' bits are least significant
1770 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001771 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001772class SideEffects : public ValueObject {
1773 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001774 SideEffects() : flags_(0) {}
1775
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001776 static SideEffects None() {
1777 return SideEffects(0);
1778 }
1779
1780 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001781 return SideEffects(kAllChangeBits | kAllDependOnBits);
1782 }
1783
1784 static SideEffects AllChanges() {
1785 return SideEffects(kAllChangeBits);
1786 }
1787
1788 static SideEffects AllDependencies() {
1789 return SideEffects(kAllDependOnBits);
1790 }
1791
1792 static SideEffects AllExceptGCDependency() {
1793 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1794 }
1795
1796 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001797 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798 }
1799
Aart Bik34c3ba92015-07-20 14:08:59 -07001800 static SideEffects AllWrites() {
1801 return SideEffects(kAllWrites);
1802 }
1803
1804 static SideEffects AllReads() {
1805 return SideEffects(kAllReads);
1806 }
1807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001808 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001809 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001810 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001811 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001812 }
1813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001814 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001815 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001816 }
1817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001819 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001820 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001821 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001822 }
1823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001824 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001825 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001826 }
1827
Artem Serovd1aa7d02018-06-22 11:35:46 +01001828 // Returns whether GC might happen across this instruction from the compiler perspective so
1829 // the next instruction in the IR would see that.
1830 //
1831 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001832 static SideEffects CanTriggerGC() {
1833 return SideEffects(1ULL << kCanTriggerGCBit);
1834 }
1835
Artem Serovd1aa7d02018-06-22 11:35:46 +01001836 // Returns whether the instruction must not be alive across a GC point.
1837 //
1838 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001839 static SideEffects DependsOnGC() {
1840 return SideEffects(1ULL << kDependsOnGCBit);
1841 }
1842
Aart Bik854a02b2015-07-14 16:07:00 -07001843 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001844 SideEffects Union(SideEffects other) const {
1845 return SideEffects(flags_ | other.flags_);
1846 }
1847
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001848 SideEffects Exclusion(SideEffects other) const {
1849 return SideEffects(flags_ & ~other.flags_);
1850 }
1851
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001852 void Add(SideEffects other) {
1853 flags_ |= other.flags_;
1854 }
1855
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001856 bool Includes(SideEffects other) const {
1857 return (other.flags_ & flags_) == other.flags_;
1858 }
1859
1860 bool HasSideEffects() const {
1861 return (flags_ & kAllChangeBits);
1862 }
1863
1864 bool HasDependencies() const {
1865 return (flags_ & kAllDependOnBits);
1866 }
1867
1868 // Returns true if there are no side effects or dependencies.
1869 bool DoesNothing() const {
1870 return flags_ == 0;
1871 }
1872
Aart Bik854a02b2015-07-14 16:07:00 -07001873 // Returns true if something is written.
1874 bool DoesAnyWrite() const {
1875 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001876 }
1877
Aart Bik854a02b2015-07-14 16:07:00 -07001878 // Returns true if something is read.
1879 bool DoesAnyRead() const {
1880 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001881 }
1882
Aart Bik854a02b2015-07-14 16:07:00 -07001883 // Returns true if potentially everything is written and read
1884 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001885 bool DoesAllReadWrite() const {
1886 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1887 }
1888
Aart Bik854a02b2015-07-14 16:07:00 -07001889 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001890 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001891 }
1892
Roland Levillain0d5a2812015-11-13 10:07:31 +00001893 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001894 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001895 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1896 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001897 }
1898
1899 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001900 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001901 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001902 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001903 for (int s = kLastBit; s >= 0; s--) {
1904 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1905 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1906 // This is a bit for the GC side effect.
1907 if (current_bit_is_set) {
1908 flags += "GC";
1909 }
Aart Bik854a02b2015-07-14 16:07:00 -07001910 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001911 } else {
1912 // This is a bit for the array/field analysis.
1913 // The underscore character stands for the 'can trigger GC' bit.
1914 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1915 if (current_bit_is_set) {
1916 flags += kDebug[s];
1917 }
1918 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1919 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1920 flags += "|";
1921 }
1922 }
Aart Bik854a02b2015-07-14 16:07:00 -07001923 }
1924 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001925 }
1926
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001927 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001928
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001929 private:
1930 static constexpr int kFieldArrayAnalysisBits = 9;
1931
1932 static constexpr int kFieldWriteOffset = 0;
1933 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1934 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1935 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1936
1937 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1938
1939 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1940 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1941 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1942 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1943
1944 static constexpr int kLastBit = kDependsOnGCBit;
1945 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1946
1947 // Aliases.
1948
1949 static_assert(kChangeBits == kDependOnBits,
1950 "the 'change' bits should match the 'depend on' bits.");
1951
1952 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1953 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1954 static constexpr uint64_t kAllWrites =
1955 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1956 static constexpr uint64_t kAllReads =
1957 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001958
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001959 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001960 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001961 int shift;
1962 switch (type) {
1963 case DataType::Type::kReference: shift = 0; break;
1964 case DataType::Type::kBool: shift = 1; break;
1965 case DataType::Type::kInt8: shift = 2; break;
1966 case DataType::Type::kUint16: shift = 3; break;
1967 case DataType::Type::kInt16: shift = 4; break;
1968 case DataType::Type::kInt32: shift = 5; break;
1969 case DataType::Type::kInt64: shift = 6; break;
1970 case DataType::Type::kFloat32: shift = 7; break;
1971 case DataType::Type::kFloat64: shift = 8; break;
1972 default:
1973 LOG(FATAL) << "Unexpected data type " << type;
1974 UNREACHABLE();
1975 }
Aart Bik854a02b2015-07-14 16:07:00 -07001976 DCHECK_LE(kFieldWriteOffset, shift);
1977 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001978 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001979 }
1980
1981 // Private constructor on direct flags value.
1982 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1983
1984 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001985};
1986
David Brazdiled596192015-01-23 10:39:45 +00001987// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001988class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001989 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001990 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001991 size_t number_of_vregs,
1992 ArtMethod* method,
1993 uint32_t dex_pc,
1994 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001995 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1996 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001997 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001998 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001999 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002000 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002001 }
2002
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002003 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
2004 const HEnvironment& to_copy,
2005 HInstruction* holder)
2006 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002007 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002008 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002009 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002011
Vladimir Markoec32f642017-06-02 10:51:55 +01002012 void AllocateLocations() {
2013 DCHECK(locations_.empty());
2014 locations_.resize(vregs_.size());
2015 }
2016
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002017 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002018 if (parent_ != nullptr) {
2019 parent_->SetAndCopyParentChain(allocator, parent);
2020 } else {
2021 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
2022 parent_->CopyFrom(parent);
2023 if (parent->GetParent() != nullptr) {
2024 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
2025 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002026 }
David Brazdiled596192015-01-23 10:39:45 +00002027 }
2028
Vladimir Marko69d310e2017-10-09 14:12:23 +01002029 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01002030 void CopyFrom(HEnvironment* environment);
2031
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002032 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
2033 // input to the loop phi instead. This is for inserting instructions that
2034 // require an environment (like HDeoptimization) in the loop pre-header.
2035 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00002036
2037 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002038 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00002039 }
2040
2041 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002042 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00002043 }
2044
David Brazdil1abb4192015-02-17 18:33:36 +00002045 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00002046
Artem Serovca210e32017-12-15 13:43:20 +00002047 // Replaces the input at the position 'index' with the replacement; the replacement and old
2048 // input instructions' env_uses_ lists are adjusted. The function works similar to
2049 // HInstruction::ReplaceInput.
2050 void ReplaceInput(HInstruction* replacement, size_t index);
2051
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002052 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00002053
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002054 HEnvironment* GetParent() const { return parent_; }
2055
2056 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002057 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002058 }
2059
2060 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002061 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002062 }
2063
2064 uint32_t GetDexPc() const {
2065 return dex_pc_;
2066 }
2067
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002068 ArtMethod* GetMethod() const {
2069 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002070 }
2071
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002072 HInstruction* GetHolder() const {
2073 return holder_;
2074 }
2075
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002076
2077 bool IsFromInlinedInvoke() const {
2078 return GetParent() != nullptr;
2079 }
2080
David Brazdiled596192015-01-23 10:39:45 +00002081 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002082 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2083 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002084 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002085 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002086 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002087
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002088 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002089 HInstruction* const holder_;
2090
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002091 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002092
2093 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2094};
2095
Vladimir Markof9f64412015-09-02 14:05:49 +01002096class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002097 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302098#define DECLARE_KIND(type, super) k##type,
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002099 enum InstructionKind { // private marker to avoid generate-operator-out.py from processing.
Vladimir Markoe3946222018-05-04 14:18:47 +01002100 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302101 kLastInstructionKind
2102 };
2103#undef DECLARE_KIND
2104
2105 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002106 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2107
2108 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002109 : previous_(nullptr),
2110 next_(nullptr),
2111 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002112 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002113 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002114 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002115 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002117 locations_(nullptr),
2118 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002119 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002120 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002121 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302122 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002123 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002124 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2125 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002126
Dave Allison20dfc792014-06-16 20:44:29 -07002127 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002130 HInstruction* GetNext() const { return next_; }
2131 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002132
Calin Juravle77520bc2015-01-12 18:45:46 +00002133 HInstruction* GetNextDisregardingMoves() const;
2134 HInstruction* GetPreviousDisregardingMoves() const;
2135
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002136 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002137 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002138 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002139 bool IsInBlock() const { return block_ != nullptr; }
2140 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002141 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2142 bool IsIrreducibleLoopHeaderPhi() const {
2143 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2144 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002145
Vladimir Marko372f10e2016-05-17 16:30:10 +01002146 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2147
2148 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2149 // One virtual method is enough, just const_cast<> and then re-add the const.
2150 return ArrayRef<const HUserRecord<HInstruction*>>(
2151 const_cast<HInstruction*>(this)->GetInputRecords());
2152 }
2153
Vladimir Markoe9004912016-06-16 16:50:52 +01002154 HInputsRef GetInputs() {
2155 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002156 }
2157
Vladimir Markoe9004912016-06-16 16:50:52 +01002158 HConstInputsRef GetInputs() const {
2159 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002160 }
2161
2162 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002163 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164
Aart Bik2767f4b2016-10-28 15:03:53 -07002165 bool HasInput(HInstruction* input) const {
2166 for (const HInstruction* i : GetInputs()) {
2167 if (i == input) {
2168 return true;
2169 }
2170 }
2171 return false;
2172 }
2173
Vladimir Marko372f10e2016-05-17 16:30:10 +01002174 void SetRawInputAt(size_t index, HInstruction* input) {
2175 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2176 }
2177
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002178 virtual void Accept(HGraphVisitor* visitor) = 0;
2179 virtual const char* DebugName() const = 0;
2180
Vladimir Markobd785672018-05-03 17:09:09 +01002181 DataType::Type GetType() const {
2182 return TypeField::Decode(GetPackedFields());
2183 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002184
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002186
2187 uint32_t GetDexPc() const { return dex_pc_; }
2188
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002189 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002190
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002191 // Can the instruction throw?
2192 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2193 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002194 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002195
2196 // Does the instruction always throw an exception unconditionally?
2197 virtual bool AlwaysThrows() const { return false; }
2198
David Brazdilec16f792015-08-19 15:04:01 +01002199 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002200
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002201 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002202 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203
Calin Juravle10e244f2015-01-26 18:54:32 +00002204 // Does not apply for all instructions, but having this at top level greatly
2205 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002206 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002207 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002209 return true;
2210 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002211
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002212 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002213 return false;
2214 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002215
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002216 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2217 // with it. Otherwise return null.
2218 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002219 // Go over previous non-move instructions that are emitted at use site.
2220 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2221 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2222 if (prev_not_move->IsNullCheck()) {
2223 return prev_not_move->AsNullCheck();
2224 }
2225 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002226 }
2227 return nullptr;
2228 }
2229
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002230 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002232 }
2233
Calin Juravle2e768302015-07-28 14:41:11 +00002234 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002235
Calin Juravle61d544b2015-02-23 16:46:57 +00002236 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002237 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002238 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002239 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002240 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002241
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002242 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002243 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002244 // Note: fixup_end remains valid across push_front().
2245 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
Vladimir Marko3224f382020-06-23 14:19:53 +01002246 ArenaAllocator* allocator = user->GetBlock()->GetGraph()->GetAllocator();
Vladimir Marko46817b82016-03-29 12:21:58 +01002247 HUseListNode<HInstruction*>* new_node =
Vladimir Marko3224f382020-06-23 14:19:53 +01002248 new (allocator) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002249 uses_.push_front(*new_node);
2250 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002251 }
2252
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002253 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002254 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002255 // Note: env_fixup_end remains valid across push_front().
2256 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2257 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002258 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002259 env_uses_.push_front(*new_node);
2260 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002261 }
2262
David Brazdil1abb4192015-02-17 18:33:36 +00002263 void RemoveAsUserOfInput(size_t input) {
2264 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002265 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2266 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2267 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002268 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002269
Vladimir Marko372f10e2016-05-17 16:30:10 +01002270 void RemoveAsUserOfAllInputs() {
2271 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2272 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2273 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2274 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2275 }
2276 }
2277
David Brazdil1abb4192015-02-17 18:33:36 +00002278 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2279 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280
Vladimir Marko46817b82016-03-29 12:21:58 +01002281 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2282 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2283 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002284 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002285 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002286 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002287
Aart Bikcc42be02016-10-20 16:14:16 -07002288 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002289 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002290 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002291 !CanThrow() &&
2292 !IsSuspendCheck() &&
2293 !IsControlFlow() &&
2294 !IsNativeDebugInfo() &&
2295 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002296 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002297 !IsMemoryBarrier() &&
2298 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002299 }
2300
Aart Bikcc42be02016-10-20 16:14:16 -07002301 bool IsDeadAndRemovable() const {
2302 return IsRemovable() && !HasUses();
2303 }
2304
Roland Levillain6c82d402014-10-13 16:10:27 +01002305 // Does this instruction strictly dominate `other_instruction`?
2306 // Returns false if this instruction and `other_instruction` are the same.
2307 // Aborts if this instruction and `other_instruction` are both phis.
2308 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002309
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002310 int GetId() const { return id_; }
2311 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002312
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002313 int GetSsaIndex() const { return ssa_index_; }
2314 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2315 bool HasSsaIndex() const { return ssa_index_ != -1; }
2316
2317 bool HasEnvironment() const { return environment_ != nullptr; }
2318 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002319 // Set the `environment_` field. Raw because this method does not
2320 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002321 void SetRawEnvironment(HEnvironment* environment) {
2322 DCHECK(environment_ == nullptr);
2323 DCHECK_EQ(environment->GetHolder(), this);
2324 environment_ = environment;
2325 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002326
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002327 void InsertRawEnvironment(HEnvironment* environment) {
2328 DCHECK(environment_ != nullptr);
2329 DCHECK_EQ(environment->GetHolder(), this);
2330 DCHECK(environment->GetParent() == nullptr);
2331 environment->parent_ = environment_;
2332 environment_ = environment;
2333 }
2334
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002335 void RemoveEnvironment();
2336
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002337 // Set the environment of this instruction, copying it from `environment`. While
2338 // copying, the uses lists are being updated.
2339 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002340 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002341 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002342 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002343 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002344 if (environment->GetParent() != nullptr) {
2345 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2346 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002347 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002348
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002349 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2350 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002351 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002352 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002353 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002354 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002355 if (environment->GetParent() != nullptr) {
2356 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2357 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002358 }
2359
Nicolas Geoffray39468442014-09-02 15:17:15 +01002360 // Returns the number of entries in the environment. Typically, that is the
2361 // number of dex registers in a method. It could be more in case of inlining.
2362 size_t EnvironmentSize() const;
2363
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002364 LocationSummary* GetLocations() const { return locations_; }
2365 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002366
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002367 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002368 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002369 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002370 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002371
Alexandre Rames188d4312015-04-09 18:30:21 +01002372 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2373 // uses of this instruction by `other` are *not* updated.
2374 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2375 ReplaceWith(other);
2376 other->ReplaceInput(this, use_index);
2377 }
2378
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002379 // Move `this` instruction before `cursor`
2380 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002381
Vladimir Markofb337ea2015-11-25 15:25:10 +00002382 // Move `this` before its first user and out of any loops. If there is no
2383 // out-of-loop user that dominates all other users, move the instruction
2384 // to the end of the out-of-loop common dominator of the user's blocks.
2385 //
2386 // This can be used only on non-throwing instructions with no side effects that
2387 // have at least one use but no environment uses.
2388 void MoveBeforeFirstUserAndOutOfLoops();
2389
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002390#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002391 bool Is##type() const;
2392
2393 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2394#undef INSTRUCTION_TYPE_CHECK
2395
2396#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002397 const H##type* As##type() const; \
2398 H##type* As##type();
2399
Vladimir Markoa90dd512018-05-04 15:04:45 +01002400 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2401#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002402
Artem Serovcced8ba2017-07-19 18:18:09 +01002403 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2404 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2405 // the instruction then the behaviour of this function is undefined.
2406 //
2407 // Note: It is semantically valid to create a clone of the instruction only until
2408 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2409 // copied.
2410 //
2411 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2412 // 'explicit HInstruction(const HInstruction& other)' for details.
2413 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2414 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2415 DebugName() << " " << GetId();
2416 UNREACHABLE();
2417 }
2418
2419 // Return whether instruction can be cloned (copied).
2420 virtual bool IsClonable() const { return false; }
2421
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002422 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002423 // TODO: this method is used by LICM and GVN with possibly different
2424 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002425 virtual bool CanBeMoved() const { return false; }
2426
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002427 // Returns whether any data encoded in the two instructions is equal.
2428 // This method does not look at the inputs. Both instructions must be
2429 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002430 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002431 return false;
2432 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002433
2434 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002435 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002436 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002437 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002438
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302439 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002440
2441 virtual size_t ComputeHashCode() const {
2442 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002443 for (const HInstruction* input : GetInputs()) {
2444 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002445 }
2446 return result;
2447 }
2448
2449 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002450 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002451 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002452
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002453 size_t GetLifetimePosition() const { return lifetime_position_; }
2454 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2455 LiveInterval* GetLiveInterval() const { return live_interval_; }
2456 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2457 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2458
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002459 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2460
2461 // Returns whether the code generation of the instruction will require to have access
2462 // to the current method. Such instructions are:
2463 // (1): Instructions that require an environment, as calling the runtime requires
2464 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002465 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2466 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002467 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002468 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002469 }
2470
Vladimir Markodc151b22015-10-15 18:02:30 +01002471 // Returns whether the code generation of the instruction will require to have access
2472 // to the dex cache of the current method's declaring class via the current method.
2473 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002474
Mark Mendellc4701932015-04-10 13:18:51 -04002475 // Does this instruction have any use in an environment before
2476 // control flow hits 'other'?
2477 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2478
2479 // Remove all references to environment uses of this instruction.
2480 // The caller must ensure that this is safe to do.
2481 void RemoveEnvironmentUsers();
2482
Vladimir Markoa1de9182016-02-25 11:37:38 +00002483 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2484 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002485
David Brazdil1abb4192015-02-17 18:33:36 +00002486 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002487 // If set, the machine code for this instruction is assumed to be generated by
2488 // its users. Used by liveness analysis to compute use positions accordingly.
2489 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2490 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302491 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2492 static constexpr size_t kFieldInstructionKindSize =
2493 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002494 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302495 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002496 static constexpr size_t kFieldTypeSize =
2497 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2498 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002499 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2500
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302501 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2502 "Too many generic packed fields");
2503
Vladimir Markobd785672018-05-03 17:09:09 +01002504 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2505
Vladimir Marko372f10e2016-05-17 16:30:10 +01002506 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2507 return GetInputRecords()[i];
2508 }
2509
2510 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2511 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2512 input_records[index] = input;
2513 }
David Brazdil1abb4192015-02-17 18:33:36 +00002514
Vladimir Markoa1de9182016-02-25 11:37:38 +00002515 uint32_t GetPackedFields() const {
2516 return packed_fields_;
2517 }
2518
2519 template <size_t flag>
2520 bool GetPackedFlag() const {
2521 return (packed_fields_ & (1u << flag)) != 0u;
2522 }
2523
2524 template <size_t flag>
2525 void SetPackedFlag(bool value = true) {
2526 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2527 }
2528
2529 template <typename BitFieldType>
2530 typename BitFieldType::value_type GetPackedField() const {
2531 return BitFieldType::Decode(packed_fields_);
2532 }
2533
2534 template <typename BitFieldType>
2535 void SetPackedField(typename BitFieldType::value_type value) {
2536 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2537 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2538 }
2539
Artem Serovcced8ba2017-07-19 18:18:09 +01002540 // Copy construction for the instruction (used for Clone function).
2541 //
2542 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2543 // prepare_for_register_allocator are not copied (set to default values).
2544 //
2545 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2546 // fine for most of them. However for some of the instructions a custom copy constructor must be
2547 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2548 // for copying them).
2549 explicit HInstruction(const HInstruction& other)
2550 : previous_(nullptr),
2551 next_(nullptr),
2552 block_(nullptr),
2553 dex_pc_(other.dex_pc_),
2554 id_(-1),
2555 ssa_index_(-1),
2556 packed_fields_(other.packed_fields_),
2557 environment_(nullptr),
2558 locations_(nullptr),
2559 live_interval_(nullptr),
2560 lifetime_position_(kNoLifetime),
2561 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302562 reference_type_handle_(other.reference_type_handle_) {
2563 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002564
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002565 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302566 using InstructionKindField =
2567 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2568
Vladimir Marko46817b82016-03-29 12:21:58 +01002569 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2570 auto before_use_node = uses_.before_begin();
2571 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2572 HInstruction* user = use_node->GetUser();
2573 size_t input_index = use_node->GetIndex();
2574 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2575 before_use_node = use_node;
2576 }
2577 }
2578
2579 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2580 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2581 if (next != uses_.end()) {
2582 HInstruction* next_user = next->GetUser();
2583 size_t next_index = next->GetIndex();
2584 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2585 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2586 }
2587 }
2588
2589 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2590 auto before_env_use_node = env_uses_.before_begin();
2591 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2592 HEnvironment* user = env_use_node->GetUser();
2593 size_t input_index = env_use_node->GetIndex();
2594 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2595 before_env_use_node = env_use_node;
2596 }
2597 }
2598
2599 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2600 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2601 if (next != env_uses_.end()) {
2602 HEnvironment* next_user = next->GetUser();
2603 size_t next_index = next->GetIndex();
2604 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2605 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2606 }
2607 }
David Brazdil1abb4192015-02-17 18:33:36 +00002608
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002609 HInstruction* previous_;
2610 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002611 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002612 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002613
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002614 // An instruction gets an id when it is added to the graph.
2615 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002616 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002617 int id_;
2618
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002619 // When doing liveness analysis, instructions that have uses get an SSA index.
2620 int ssa_index_;
2621
Vladimir Markoa1de9182016-02-25 11:37:38 +00002622 // Packed fields.
2623 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002624
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002625 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002626 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002627
2628 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002629 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002630
Nicolas Geoffray39468442014-09-02 15:17:15 +01002631 // The environment associated with this instruction. Not null if the instruction
2632 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002633 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002634
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002635 // Set by the code generator.
2636 LocationSummary* locations_;
2637
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002638 // Set by the liveness analysis.
2639 LiveInterval* live_interval_;
2640
2641 // Set by the liveness analysis, this is the position in a linear
2642 // order of blocks where this instruction's live interval start.
2643 size_t lifetime_position_;
2644
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002645 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002646
Vladimir Markoa1de9182016-02-25 11:37:38 +00002647 // The reference handle part of the reference type info.
2648 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002649 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002650 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002651
David Brazdil1abb4192015-02-17 18:33:36 +00002652 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002653 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002654 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002655 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002656 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002657};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002658std::ostream& operator<<(std::ostream& os, HInstruction::InstructionKind rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002660// Iterates over the instructions, while preserving the next instruction
2661// in case the current instruction gets removed from the list by the user
2662// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002663class HInstructionIterator : public ValueObject {
2664 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002665 explicit HInstructionIterator(const HInstructionList& instructions)
2666 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002667 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002668 }
2669
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002670 bool Done() const { return instruction_ == nullptr; }
2671 HInstruction* Current() const { return instruction_; }
2672 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002673 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002674 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002675 }
2676
2677 private:
2678 HInstruction* instruction_;
2679 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002680
2681 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002682};
2683
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002684// Iterates over the instructions without saving the next instruction,
2685// therefore handling changes in the graph potentially made by the user
2686// of this iterator.
2687class HInstructionIteratorHandleChanges : public ValueObject {
2688 public:
2689 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2690 : instruction_(instructions.first_instruction_) {
2691 }
2692
2693 bool Done() const { return instruction_ == nullptr; }
2694 HInstruction* Current() const { return instruction_; }
2695 void Advance() {
2696 instruction_ = instruction_->GetNext();
2697 }
2698
2699 private:
2700 HInstruction* instruction_;
2701
2702 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2703};
2704
2705
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002706class HBackwardInstructionIterator : public ValueObject {
2707 public:
2708 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2709 : instruction_(instructions.last_instruction_) {
2710 next_ = Done() ? nullptr : instruction_->GetPrevious();
2711 }
2712
2713 bool Done() const { return instruction_ == nullptr; }
2714 HInstruction* Current() const { return instruction_; }
2715 void Advance() {
2716 instruction_ = next_;
2717 next_ = Done() ? nullptr : instruction_->GetPrevious();
2718 }
2719
2720 private:
2721 HInstruction* instruction_;
2722 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002723
2724 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002725};
2726
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002727class HVariableInputSizeInstruction : public HInstruction {
2728 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002729 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002730 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002731 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2732 }
2733
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002734 void AddInput(HInstruction* input);
2735 void InsertInputAt(size_t index, HInstruction* input);
2736 void RemoveInputAt(size_t index);
2737
Igor Murashkind01745e2017-04-05 16:40:31 -07002738 // Removes all the inputs.
2739 // Also removes this instructions from each input's use list
2740 // (for non-environment uses only).
2741 void RemoveAllInputs();
2742
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002743 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302744 HVariableInputSizeInstruction(InstructionKind inst_kind,
2745 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002746 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002747 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002748 size_t number_of_inputs,
2749 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302750 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002751 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002752 HVariableInputSizeInstruction(InstructionKind inst_kind,
2753 DataType::Type type,
2754 SideEffects side_effects,
2755 uint32_t dex_pc,
2756 ArenaAllocator* allocator,
2757 size_t number_of_inputs,
2758 ArenaAllocKind kind)
2759 : HInstruction(inst_kind, type, side_effects, dex_pc),
2760 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002761
Artem Serovcced8ba2017-07-19 18:18:09 +01002762 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002763
Artem Serovcced8ba2017-07-19 18:18:09 +01002764 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002765};
2766
Vladimir Markof9f64412015-09-02 14:05:49 +01002767template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002768class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002769 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002770 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302771 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002772 HExpression<N>(InstructionKind kind,
2773 DataType::Type type,
2774 SideEffects side_effects,
2775 uint32_t dex_pc)
2776 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2777 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002778
Vladimir Marko372f10e2016-05-17 16:30:10 +01002779 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002780 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002781 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002782 }
2783
Artem Serovcced8ba2017-07-19 18:18:09 +01002784 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002785 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002786
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002787 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002788 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002789
2790 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002791};
2792
Vladimir Markobd785672018-05-03 17:09:09 +01002793// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002794template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002795class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002796 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002797 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002798
Vladimir Markobd785672018-05-03 17:09:09 +01002799 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002800
Vladimir Marko372f10e2016-05-17 16:30:10 +01002801 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002802 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002803 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002804 }
2805
Artem Serovcced8ba2017-07-19 18:18:09 +01002806 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002807 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002808
Vladimir Markof9f64412015-09-02 14:05:49 +01002809 private:
2810 friend class SsaBuilder;
2811};
2812
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002813// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2814// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002815class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002816 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002817 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002818 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302819 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002820
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002821 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002822
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002823 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002824
Artem Serovcced8ba2017-07-19 18:18:09 +01002825 protected:
2826 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002827};
2828
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002829// Represents dex's RETURN opcodes. A HReturn is a control flow
2830// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002831class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002832 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002833 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002834 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002835 SetRawInputAt(0, value);
2836 }
2837
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002838 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002839
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002840 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002841
Artem Serovcced8ba2017-07-19 18:18:09 +01002842 protected:
2843 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002844};
2845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002846class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002847 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002848 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002849 uint32_t reg_number,
2850 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002852 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002853 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302854 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002855 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002856 SideEffects::None(),
2857 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002858 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002859 number_of_inputs,
2860 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002861 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002863 // Phis are constructed live and marked dead if conflicting or unused.
2864 // Individual steps of SsaBuilder should assume that if a phi has been
2865 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2866 SetPackedFlag<kFlagIsLive>(true);
2867 SetPackedFlag<kFlagCanBeNull>(true);
2868 }
2869
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002870 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002871
David Brazdildee58d62016-04-07 09:54:26 +00002872 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 static DataType::Type ToPhiType(DataType::Type type) {
2874 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002875 }
2876
2877 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002880 // Make sure that only valid type changes occur. The following are allowed:
2881 // (1) int -> float/ref (primitive type propagation),
2882 // (2) long -> double (primitive type propagation).
2883 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002884 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2885 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2886 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002887 SetPackedField<TypeField>(new_type);
2888 }
2889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002890 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002891 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2892
2893 uint32_t GetRegNumber() const { return reg_number_; }
2894
2895 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2896 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2897 bool IsDead() const { return !IsLive(); }
2898 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2899
Vladimir Markoe9004912016-06-16 16:50:52 +01002900 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002901 return other != nullptr
2902 && other->IsPhi()
2903 && other->AsPhi()->GetBlock() == GetBlock()
2904 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2905 }
2906
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002907 bool HasEquivalentPhi() const {
2908 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2909 return true;
2910 }
2911 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2912 return true;
2913 }
2914 return false;
2915 }
2916
David Brazdildee58d62016-04-07 09:54:26 +00002917 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2918 // An equivalent phi is a phi having the same dex register and type.
2919 // It assumes that phis with the same dex register are adjacent.
2920 HPhi* GetNextEquivalentPhiWithSameType() {
2921 HInstruction* next = GetNext();
2922 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2923 if (next->GetType() == GetType()) {
2924 return next->AsPhi();
2925 }
2926 next = next->GetNext();
2927 }
2928 return nullptr;
2929 }
2930
2931 DECLARE_INSTRUCTION(Phi);
2932
Artem Serovcced8ba2017-07-19 18:18:09 +01002933 protected:
2934 DEFAULT_COPY_CONSTRUCTOR(Phi);
2935
David Brazdildee58d62016-04-07 09:54:26 +00002936 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002937 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002938 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2939 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2940 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002941
David Brazdildee58d62016-04-07 09:54:26 +00002942 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002943};
2944
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002945// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002946// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002947// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002948class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002949 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302950 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002951 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302952 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002954 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002955
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002956 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002957
Artem Serovcced8ba2017-07-19 18:18:09 +01002958 protected:
2959 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002960};
2961
2962// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002963class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002964 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302965 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002966 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302967 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002968
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002969 bool IsClonable() const override { return true; }
2970 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002971
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002972 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002973 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002974 }
2975
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002976 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002977
Artem Serovcced8ba2017-07-19 18:18:09 +01002978 protected:
2979 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002980};
2981
Roland Levillain9867bc72015-08-05 10:21:34 +01002982class HConstant : public HExpression<0> {
2983 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302984 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2985 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2986 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002987
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002988 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002989
Roland Levillain1a653882016-03-18 18:05:57 +00002990 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002991 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002992 // Is this constant 0 in the arithmetic sense?
2993 virtual bool IsArithmeticZero() const { return false; }
2994 // Is this constant a 0-bit pattern?
2995 virtual bool IsZeroBitPattern() const { return false; }
2996 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002997 virtual bool IsOne() const { return false; }
2998
David Brazdil77a48ae2015-09-15 12:34:04 +00002999 virtual uint64_t GetValueAsUint64() const = 0;
3000
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003001 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003002
Artem Serovcced8ba2017-07-19 18:18:09 +01003003 protected:
3004 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003005};
3006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003007class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003008 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003009 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01003010 return true;
3011 }
3012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003013 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003015 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003016
Roland Levillain1a653882016-03-18 18:05:57 +00003017 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07003018 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00003019
Roland Levillain9867bc72015-08-05 10:21:34 +01003020 DECLARE_INSTRUCTION(NullConstant);
3021
Artem Serovcced8ba2017-07-19 18:18:09 +01003022 protected:
3023 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
3024
Roland Levillain9867bc72015-08-05 10:21:34 +01003025 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303027 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
3028 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003029
3030 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003031};
3032
3033// Constants of the type int. Those can be from Dex instructions, or
3034// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003035class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003036 public:
3037 int32_t GetValue() const { return value_; }
3038
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003039 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01003040 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
3041 }
David Brazdil77a48ae2015-09-15 12:34:04 +00003042
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003043 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003044 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003045 return other->AsIntConstant()->value_ == value_;
3046 }
3047
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003048 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003049
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003050 bool IsMinusOne() const override { return GetValue() == -1; }
3051 bool IsArithmeticZero() const override { return GetValue() == 0; }
3052 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3053 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003054
Roland Levillain1a653882016-03-18 18:05:57 +00003055 // Integer constants are used to encode Boolean values as well,
3056 // where 1 means true and 0 means false.
3057 bool IsTrue() const { return GetValue() == 1; }
3058 bool IsFalse() const { return GetValue() == 0; }
3059
Roland Levillain9867bc72015-08-05 10:21:34 +01003060 DECLARE_INSTRUCTION(IntConstant);
3061
Artem Serovcced8ba2017-07-19 18:18:09 +01003062 protected:
3063 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3064
Roland Levillain9867bc72015-08-05 10:21:34 +01003065 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003066 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303067 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3068 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003069 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303070 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3071 value_(value ? 1 : 0) {
3072 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003073
3074 const int32_t value_;
3075
3076 friend class HGraph;
3077 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3078 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003079};
3080
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003081class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003082 public:
3083 int64_t GetValue() const { return value_; }
3084
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003085 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003086
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003087 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003088 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003089 return other->AsLongConstant()->value_ == value_;
3090 }
3091
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003092 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003093
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003094 bool IsMinusOne() const override { return GetValue() == -1; }
3095 bool IsArithmeticZero() const override { return GetValue() == 0; }
3096 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3097 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003098
3099 DECLARE_INSTRUCTION(LongConstant);
3100
Artem Serovcced8ba2017-07-19 18:18:09 +01003101 protected:
3102 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3103
Roland Levillain9867bc72015-08-05 10:21:34 +01003104 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003105 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303106 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3107 value_(value) {
3108 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003109
3110 const int64_t value_;
3111
3112 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003113};
Dave Allison20dfc792014-06-16 20:44:29 -07003114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003115class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003116 public:
3117 float GetValue() const { return value_; }
3118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003119 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3121 }
3122
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003123 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003124 DCHECK(other->IsFloatConstant()) << other->DebugName();
3125 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3126 }
3127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003128 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003130 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003131 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3132 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003133 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003134 return std::fpclassify(value_) == FP_ZERO;
3135 }
3136 bool IsArithmeticPositiveZero() const {
3137 return IsArithmeticZero() && !std::signbit(value_);
3138 }
3139 bool IsArithmeticNegativeZero() const {
3140 return IsArithmeticZero() && std::signbit(value_);
3141 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003142 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003143 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003144 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003145 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003146 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3147 }
3148 bool IsNaN() const {
3149 return std::isnan(value_);
3150 }
3151
3152 DECLARE_INSTRUCTION(FloatConstant);
3153
Artem Serovcced8ba2017-07-19 18:18:09 +01003154 protected:
3155 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3156
Roland Levillain31dd3d62016-02-16 12:21:02 +00003157 private:
3158 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303159 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3160 value_(value) {
3161 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003162 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303163 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3164 value_(bit_cast<float, int32_t>(value)) {
3165 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003166
3167 const float value_;
3168
3169 // Only the SsaBuilder and HGraph can create floating-point constants.
3170 friend class SsaBuilder;
3171 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003172};
3173
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003174class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003175 public:
3176 double GetValue() const { return value_; }
3177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003178 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003180 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003181 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3182 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3183 }
3184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003185 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003187 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003188 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3189 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003190 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003191 return std::fpclassify(value_) == FP_ZERO;
3192 }
3193 bool IsArithmeticPositiveZero() const {
3194 return IsArithmeticZero() && !std::signbit(value_);
3195 }
3196 bool IsArithmeticNegativeZero() const {
3197 return IsArithmeticZero() && std::signbit(value_);
3198 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003199 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003200 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003201 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003202 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003203 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3204 }
3205 bool IsNaN() const {
3206 return std::isnan(value_);
3207 }
3208
3209 DECLARE_INSTRUCTION(DoubleConstant);
3210
Artem Serovcced8ba2017-07-19 18:18:09 +01003211 protected:
3212 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3213
Roland Levillain31dd3d62016-02-16 12:21:02 +00003214 private:
3215 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303216 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3217 value_(value) {
3218 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003219 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303220 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3221 value_(bit_cast<double, int64_t>(value)) {
3222 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003223
3224 const double value_;
3225
3226 // Only the SsaBuilder and HGraph can create floating-point constants.
3227 friend class SsaBuilder;
3228 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003229};
3230
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003231// Conditional branch. A block ending with an HIf instruction must have
3232// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003233class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003234 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003235 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003236 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003237 SetRawInputAt(0, input);
3238 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003240 bool IsClonable() const override { return true; }
3241 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003242
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003243 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003244 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003245 }
3246
3247 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003248 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003249 }
3250
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003251 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003252
Artem Serovcced8ba2017-07-19 18:18:09 +01003253 protected:
3254 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003255};
3256
David Brazdilfc6a86a2015-06-26 10:33:45 +00003257
3258// Abstract instruction which marks the beginning and/or end of a try block and
3259// links it to the respective exception handlers. Behaves the same as a Goto in
3260// non-exceptional control flow.
3261// Normal-flow successor is stored at index zero, exception handlers under
3262// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003263class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003264 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003265 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003266 kEntry,
3267 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003268 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003269 };
3270
Artem Serovd1aa7d02018-06-22 11:35:46 +01003271 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3272 // across the catch block entering edges as GC might happen during throwing an exception.
3273 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3274 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003275 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003276 : HExpression(kTryBoundary,
3277 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3278 : SideEffects::None(),
3279 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003280 SetPackedField<BoundaryKindField>(kind);
3281 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003283 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003284
3285 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003286 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003287
David Brazdild26a4112015-11-10 11:07:31 +00003288 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3289 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3290 }
3291
David Brazdilfc6a86a2015-06-26 10:33:45 +00003292 // Returns whether `handler` is among its exception handlers (non-zero index
3293 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003294 bool HasExceptionHandler(const HBasicBlock& handler) const {
3295 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003296 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003297 }
3298
3299 // If not present already, adds `handler` to its block's list of exception
3300 // handlers.
3301 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003302 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003303 GetBlock()->AddSuccessor(handler);
3304 }
3305 }
3306
Vladimir Markoa1de9182016-02-25 11:37:38 +00003307 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3308 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003309
David Brazdilffee3d32015-07-06 11:48:53 +01003310 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3311
David Brazdilfc6a86a2015-06-26 10:33:45 +00003312 DECLARE_INSTRUCTION(TryBoundary);
3313
Artem Serovcced8ba2017-07-19 18:18:09 +01003314 protected:
3315 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3316
David Brazdilfc6a86a2015-06-26 10:33:45 +00003317 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003318 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3319 static constexpr size_t kFieldBoundaryKindSize =
3320 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3321 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3322 kFieldBoundaryKind + kFieldBoundaryKindSize;
3323 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3324 "Too many packed fields.");
3325 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003326};
3327
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003328// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003329class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003330 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003331 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3332 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003333 HDeoptimize(ArenaAllocator* allocator,
3334 HInstruction* cond,
3335 DeoptimizationKind kind,
3336 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003337 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303338 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003339 SideEffects::All(),
3340 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003341 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003342 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003343 kArenaAllocMisc) {
3344 SetPackedFlag<kFieldCanBeMoved>(false);
3345 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003346 SetRawInputAt(0, cond);
3347 }
3348
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003349 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003350
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003351 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3352 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3353 // instead of `guard`.
3354 // We set CanTriggerGC to prevent any intermediate address to be live
3355 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003356 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003357 HInstruction* cond,
3358 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003359 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003360 uint32_t dex_pc)
3361 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303362 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003363 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003364 SideEffects::CanTriggerGC(),
3365 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003366 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003367 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003368 kArenaAllocMisc) {
3369 SetPackedFlag<kFieldCanBeMoved>(true);
3370 SetPackedField<DeoptimizeKindField>(kind);
3371 SetRawInputAt(0, cond);
3372 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003373 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003374
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003375 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003376
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003377 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003378 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3379 }
3380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003381 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003382
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003383 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003384
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003385 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003386
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003387 bool GuardsAnInput() const {
3388 return InputCount() == 2;
3389 }
3390
3391 HInstruction* GuardedInput() const {
3392 DCHECK(GuardsAnInput());
3393 return InputAt(1);
3394 }
3395
3396 void RemoveGuard() {
3397 RemoveInputAt(1);
3398 }
3399
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003400 DECLARE_INSTRUCTION(Deoptimize);
3401
Artem Serovcced8ba2017-07-19 18:18:09 +01003402 protected:
3403 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3404
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003405 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003406 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3407 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3408 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003409 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003410 static constexpr size_t kNumberOfDeoptimizePackedBits =
3411 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3412 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3413 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003414 using DeoptimizeKindField =
3415 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003416};
3417
Mingyao Yang063fc772016-08-02 11:02:54 -07003418// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3419// The compiled code checks this flag value in a guard before devirtualized call and
3420// if it's true, starts to do deoptimization.
3421// It has a 4-byte slot on stack.
3422// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003423class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003424 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003425 // CHA guards are only optimized in a separate pass and it has no side effects
3426 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003427 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303428 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003429 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303430 SideEffects::None(),
3431 dex_pc,
3432 allocator,
3433 0,
3434 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003435 }
3436
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003437 // We do all CHA guard elimination/motion in a single pass, after which there is no
3438 // further guard elimination/motion since a guard might have been used for justification
3439 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3440 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003441 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003442
3443 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3444
Artem Serovcced8ba2017-07-19 18:18:09 +01003445 protected:
3446 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003447};
3448
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003449// Represents the ArtMethod that was passed as a first argument to
3450// the method. It is used by instructions that depend on it, like
3451// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003452class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003453 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003454 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303455 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3456 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003457
3458 DECLARE_INSTRUCTION(CurrentMethod);
3459
Artem Serovcced8ba2017-07-19 18:18:09 +01003460 protected:
3461 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003462};
3463
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003464// Fetches an ArtMethod from the virtual table or the interface method table
3465// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003466class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003467 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003468 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003469 kVTable,
3470 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003471 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003472 };
3473 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003474 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003475 TableKind kind,
3476 size_t index,
3477 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303478 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003479 index_(index) {
3480 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003481 SetRawInputAt(0, cls);
3482 }
3483
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003484 bool IsClonable() const override { return true; }
3485 bool CanBeMoved() const override { return true; }
3486 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003487 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003488 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003489 }
3490
Vladimir Markoa1de9182016-02-25 11:37:38 +00003491 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003492 size_t GetIndex() const { return index_; }
3493
3494 DECLARE_INSTRUCTION(ClassTableGet);
3495
Artem Serovcced8ba2017-07-19 18:18:09 +01003496 protected:
3497 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3498
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003499 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003500 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003501 static constexpr size_t kFieldTableKindSize =
3502 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3503 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3504 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3505 "Too many packed fields.");
3506 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3507
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003508 // The index of the ArtMethod in the table.
3509 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003510};
3511
Mark Mendellfe57faa2015-09-18 09:26:15 -04003512// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3513// have one successor for each entry in the switch table, and the final successor
3514// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003515class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003516 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003517 HPackedSwitch(int32_t start_value,
3518 uint32_t num_entries,
3519 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003520 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003521 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003522 start_value_(start_value),
3523 num_entries_(num_entries) {
3524 SetRawInputAt(0, input);
3525 }
3526
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003527 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003528
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003529 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003530
3531 int32_t GetStartValue() const { return start_value_; }
3532
Vladimir Marko211c2112015-09-24 16:52:33 +01003533 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003534
3535 HBasicBlock* GetDefaultBlock() const {
3536 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003537 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003538 }
3539 DECLARE_INSTRUCTION(PackedSwitch);
3540
Artem Serovcced8ba2017-07-19 18:18:09 +01003541 protected:
3542 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3543
Mark Mendellfe57faa2015-09-18 09:26:15 -04003544 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003545 const int32_t start_value_;
3546 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003547};
3548
Roland Levillain88cb1752014-10-20 16:36:47 +01003549class HUnaryOperation : public HExpression<1> {
3550 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303551 HUnaryOperation(InstructionKind kind,
3552 DataType::Type result_type,
3553 HInstruction* input,
3554 uint32_t dex_pc = kNoDexPc)
3555 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003556 SetRawInputAt(0, input);
3557 }
3558
Artem Serovcced8ba2017-07-19 18:18:09 +01003559 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003560 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003561
Roland Levillain88cb1752014-10-20 16:36:47 +01003562 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003563 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003565 bool CanBeMoved() const override { return true; }
3566 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003567 return true;
3568 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003569
Roland Levillain31dd3d62016-02-16 12:21:02 +00003570 // Try to statically evaluate `this` and return a HConstant
3571 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003572 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003573 HConstant* TryStaticEvaluation() const;
3574
3575 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003576 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3577 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003578 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3579 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003580
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003581 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003582
Artem Serovcced8ba2017-07-19 18:18:09 +01003583 protected:
3584 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003585};
3586
Dave Allison20dfc792014-06-16 20:44:29 -07003587class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003588 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303589 HBinaryOperation(InstructionKind kind,
3590 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003591 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003592 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003593 SideEffects side_effects = SideEffects::None(),
3594 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303595 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003596 SetRawInputAt(0, left);
3597 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003598 }
3599
Artem Serovcced8ba2017-07-19 18:18:09 +01003600 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003601 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003602
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003603 HInstruction* GetLeft() const { return InputAt(0); }
3604 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003605 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003606
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003607 virtual bool IsCommutative() const { return false; }
3608
3609 // Put constant on the right.
3610 // Returns whether order is changed.
3611 bool OrderInputsWithConstantOnTheRight() {
3612 HInstruction* left = InputAt(0);
3613 HInstruction* right = InputAt(1);
3614 if (left->IsConstant() && !right->IsConstant()) {
3615 ReplaceInput(right, 0);
3616 ReplaceInput(left, 1);
3617 return true;
3618 }
3619 return false;
3620 }
3621
3622 // Order inputs by instruction id, but favor constant on the right side.
3623 // This helps GVN for commutative ops.
3624 void OrderInputs() {
3625 DCHECK(IsCommutative());
3626 HInstruction* left = InputAt(0);
3627 HInstruction* right = InputAt(1);
3628 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3629 return;
3630 }
3631 if (OrderInputsWithConstantOnTheRight()) {
3632 return;
3633 }
3634 // Order according to instruction id.
3635 if (left->GetId() > right->GetId()) {
3636 ReplaceInput(right, 0);
3637 ReplaceInput(left, 1);
3638 }
3639 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003640
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003641 bool CanBeMoved() const override { return true; }
3642 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003643 return true;
3644 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003645
Roland Levillain31dd3d62016-02-16 12:21:02 +00003646 // Try to statically evaluate `this` and return a HConstant
3647 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003648 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003649 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003650
3651 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003652 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3653 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003654 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3655 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003656 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003657 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3658 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003659 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3660 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003661 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3662 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003663 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003664 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3665 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003666
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003667 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003668 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003669 HConstant* GetConstantRight() const;
3670
3671 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003672 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003673 HInstruction* GetLeastConstantLeft() const;
3674
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003675 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003676
Artem Serovcced8ba2017-07-19 18:18:09 +01003677 protected:
3678 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003679};
3680
Mark Mendellc4701932015-04-10 13:18:51 -04003681// The comparison bias applies for floating point operations and indicates how NaN
3682// comparisons are treated:
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003683enum class ComparisonBias { // private marker to avoid generate-operator-out.py from processing.
Mark Mendellc4701932015-04-10 13:18:51 -04003684 kNoBias, // bias is not applicable (i.e. for long operation)
3685 kGtBias, // return 1 for NaN comparisons
3686 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003687 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003688};
3689
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003690std::ostream& operator<<(std::ostream& os, ComparisonBias rhs);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003691
Dave Allison20dfc792014-06-16 20:44:29 -07003692class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003693 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303694 HCondition(InstructionKind kind,
3695 HInstruction* first,
3696 HInstruction* second,
3697 uint32_t dex_pc = kNoDexPc)
3698 : HBinaryOperation(kind,
3699 DataType::Type::kBool,
3700 first,
3701 second,
3702 SideEffects::None(),
3703 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003704 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3705 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003706
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003707 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003708 // `instruction`, and disregard moves in between.
3709 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003710
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003711 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003712
3713 virtual IfCondition GetCondition() const = 0;
3714
Mark Mendellc4701932015-04-10 13:18:51 -04003715 virtual IfCondition GetOppositeCondition() const = 0;
3716
Vladimir Markoa1de9182016-02-25 11:37:38 +00003717 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003718 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3719
Vladimir Markoa1de9182016-02-25 11:37:38 +00003720 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3721 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003722
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003723 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003724 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003725 }
3726
Roland Levillain4fa13f62015-07-06 18:11:54 +01003727 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003728 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003729 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003730 if (if_cond == kCondNE) {
3731 return true;
3732 } else if (if_cond == kCondEQ) {
3733 return false;
3734 }
3735 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003736 }
3737
3738 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003739 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003740 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003741 if (if_cond == kCondEQ) {
3742 return true;
3743 } else if (if_cond == kCondNE) {
3744 return false;
3745 }
3746 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003747 }
3748
Roland Levillain31dd3d62016-02-16 12:21:02 +00003749 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003750 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003751 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003752 static constexpr size_t kFieldComparisonBiasSize =
3753 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3754 static constexpr size_t kNumberOfConditionPackedBits =
3755 kFieldComparisonBias + kFieldComparisonBiasSize;
3756 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3757 using ComparisonBiasField =
3758 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3759
Roland Levillain31dd3d62016-02-16 12:21:02 +00003760 template <typename T>
3761 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3762
3763 template <typename T>
3764 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003766 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3767 // Handle the bias.
3768 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3769 }
3770
3771 // Return an integer constant containing the result of a condition evaluated at compile time.
3772 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3773 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3774 }
3775
Artem Serovcced8ba2017-07-19 18:18:09 +01003776 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003777};
3778
3779// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003780class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003781 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003782 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303783 : HCondition(kEqual, first, second, dex_pc) {
3784 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003786 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003787
Vladimir Marko9e23df52015-11-10 17:14:35 +00003788 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003789 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003790 return MakeConstantCondition(true, GetDexPc());
3791 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003792 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003793 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3794 }
3795 // In the following Evaluate methods, a HCompare instruction has
3796 // been merged into this HEqual instruction; evaluate it as
3797 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003798 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003799 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3800 GetDexPc());
3801 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003802 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003803 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3804 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003805 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003806 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003807 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003808
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003809 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003811 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003812 return kCondEQ;
3813 }
3814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003815 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003816 return kCondNE;
3817 }
3818
Artem Serovcced8ba2017-07-19 18:18:09 +01003819 protected:
3820 DEFAULT_COPY_CONSTRUCTOR(Equal);
3821
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003822 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003823 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003824};
3825
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003826class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003827 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303828 HNotEqual(HInstruction* first, HInstruction* second,
3829 uint32_t dex_pc = kNoDexPc)
3830 : HCondition(kNotEqual, first, second, dex_pc) {
3831 }
Dave Allison20dfc792014-06-16 20:44:29 -07003832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003833 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003834
Vladimir Marko9e23df52015-11-10 17:14:35 +00003835 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003836 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003837 return MakeConstantCondition(false, GetDexPc());
3838 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003839 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3841 }
3842 // In the following Evaluate methods, a HCompare instruction has
3843 // been merged into this HNotEqual instruction; evaluate it as
3844 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003846 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3847 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003848 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003849 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3850 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003851 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003852 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003853 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003854
Dave Allison20dfc792014-06-16 20:44:29 -07003855 DECLARE_INSTRUCTION(NotEqual);
3856
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003857 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003858 return kCondNE;
3859 }
3860
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003861 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003862 return kCondEQ;
3863 }
3864
Artem Serovcced8ba2017-07-19 18:18:09 +01003865 protected:
3866 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3867
Dave Allison20dfc792014-06-16 20:44:29 -07003868 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003869 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003870};
3871
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003872class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003873 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303874 HLessThan(HInstruction* first, HInstruction* second,
3875 uint32_t dex_pc = kNoDexPc)
3876 : HCondition(kLessThan, first, second, dex_pc) {
3877 }
Dave Allison20dfc792014-06-16 20:44:29 -07003878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003879 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003880 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003881 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003882 // In the following Evaluate methods, a HCompare instruction has
3883 // been merged into this HLessThan instruction; evaluate it as
3884 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003885 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003886 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3887 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003888 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003889 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3890 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003891 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003892 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003893 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003894
Dave Allison20dfc792014-06-16 20:44:29 -07003895 DECLARE_INSTRUCTION(LessThan);
3896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003897 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003898 return kCondLT;
3899 }
3900
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003901 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003902 return kCondGE;
3903 }
3904
Artem Serovcced8ba2017-07-19 18:18:09 +01003905 protected:
3906 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3907
Dave Allison20dfc792014-06-16 20:44:29 -07003908 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003909 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003910};
3911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003912class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003913 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303914 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3915 uint32_t dex_pc = kNoDexPc)
3916 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3917 }
Dave Allison20dfc792014-06-16 20:44:29 -07003918
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003919 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003920 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003921 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003922 // In the following Evaluate methods, a HCompare instruction has
3923 // been merged into this HLessThanOrEqual instruction; evaluate it as
3924 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003925 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003926 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3927 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003928 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003929 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3930 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003931 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003932 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003933 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003934
Dave Allison20dfc792014-06-16 20:44:29 -07003935 DECLARE_INSTRUCTION(LessThanOrEqual);
3936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003937 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003938 return kCondLE;
3939 }
3940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003941 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003942 return kCondGT;
3943 }
3944
Artem Serovcced8ba2017-07-19 18:18:09 +01003945 protected:
3946 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3947
Dave Allison20dfc792014-06-16 20:44:29 -07003948 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003949 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003950};
3951
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003952class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003953 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003954 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303955 : HCondition(kGreaterThan, first, second, dex_pc) {
3956 }
Dave Allison20dfc792014-06-16 20:44:29 -07003957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003958 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003959 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003960 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003961 // In the following Evaluate methods, a HCompare instruction has
3962 // been merged into this HGreaterThan instruction; evaluate it as
3963 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003964 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003965 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3966 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003967 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003968 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003970 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003971 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003972 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003973
Dave Allison20dfc792014-06-16 20:44:29 -07003974 DECLARE_INSTRUCTION(GreaterThan);
3975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003976 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003977 return kCondGT;
3978 }
3979
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003980 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003981 return kCondLE;
3982 }
3983
Artem Serovcced8ba2017-07-19 18:18:09 +01003984 protected:
3985 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3986
Dave Allison20dfc792014-06-16 20:44:29 -07003987 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003988 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003989};
3990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003991class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003992 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003993 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303994 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3995 }
Dave Allison20dfc792014-06-16 20:44:29 -07003996
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003997 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003998 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003999 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004000 // In the following Evaluate methods, a HCompare instruction has
4001 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
4002 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004003 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004004 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4005 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004006 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004007 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4008 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004009 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004010 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004011 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004012
Dave Allison20dfc792014-06-16 20:44:29 -07004013 DECLARE_INSTRUCTION(GreaterThanOrEqual);
4014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004015 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004016 return kCondGE;
4017 }
4018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004019 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004020 return kCondLT;
4021 }
4022
Artem Serovcced8ba2017-07-19 18:18:09 +01004023 protected:
4024 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
4025
Dave Allison20dfc792014-06-16 20:44:29 -07004026 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004027 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004028};
4029
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004030class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004031 public:
4032 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304033 : HCondition(kBelow, first, second, dex_pc) {
4034 }
Aart Bike9f37602015-10-09 11:15:55 -07004035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004036 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004037 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004038 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004039 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004040 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4041 }
4042 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004043 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004044 LOG(FATAL) << DebugName() << " is not defined for float values";
4045 UNREACHABLE();
4046 }
4047 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004048 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004049 LOG(FATAL) << DebugName() << " is not defined for double values";
4050 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004051 }
4052
4053 DECLARE_INSTRUCTION(Below);
4054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004055 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004056 return kCondB;
4057 }
4058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004059 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004060 return kCondAE;
4061 }
4062
Artem Serovcced8ba2017-07-19 18:18:09 +01004063 protected:
4064 DEFAULT_COPY_CONSTRUCTOR(Below);
4065
Aart Bike9f37602015-10-09 11:15:55 -07004066 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004067 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004068 return MakeUnsigned(x) < MakeUnsigned(y);
4069 }
Aart Bike9f37602015-10-09 11:15:55 -07004070};
4071
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004072class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004073 public:
4074 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304075 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4076 }
Aart Bike9f37602015-10-09 11:15:55 -07004077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004078 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004079 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004080 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004081 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004082 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4083 }
4084 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004085 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004086 LOG(FATAL) << DebugName() << " is not defined for float values";
4087 UNREACHABLE();
4088 }
4089 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004090 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004091 LOG(FATAL) << DebugName() << " is not defined for double values";
4092 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004093 }
4094
4095 DECLARE_INSTRUCTION(BelowOrEqual);
4096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004097 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004098 return kCondBE;
4099 }
4100
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004101 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004102 return kCondA;
4103 }
4104
Artem Serovcced8ba2017-07-19 18:18:09 +01004105 protected:
4106 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4107
Aart Bike9f37602015-10-09 11:15:55 -07004108 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004109 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004110 return MakeUnsigned(x) <= MakeUnsigned(y);
4111 }
Aart Bike9f37602015-10-09 11:15:55 -07004112};
4113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004114class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004115 public:
4116 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304117 : HCondition(kAbove, first, second, dex_pc) {
4118 }
Aart Bike9f37602015-10-09 11:15:55 -07004119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004120 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004121 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004122 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004123 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004124 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4125 }
4126 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004127 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004128 LOG(FATAL) << DebugName() << " is not defined for float values";
4129 UNREACHABLE();
4130 }
4131 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004132 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004133 LOG(FATAL) << DebugName() << " is not defined for double values";
4134 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004135 }
4136
4137 DECLARE_INSTRUCTION(Above);
4138
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004139 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004140 return kCondA;
4141 }
4142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004143 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004144 return kCondBE;
4145 }
4146
Artem Serovcced8ba2017-07-19 18:18:09 +01004147 protected:
4148 DEFAULT_COPY_CONSTRUCTOR(Above);
4149
Aart Bike9f37602015-10-09 11:15:55 -07004150 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004151 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004152 return MakeUnsigned(x) > MakeUnsigned(y);
4153 }
Aart Bike9f37602015-10-09 11:15:55 -07004154};
4155
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004156class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004157 public:
4158 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304159 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4160 }
Aart Bike9f37602015-10-09 11:15:55 -07004161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004162 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004163 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004164 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004165 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004166 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4167 }
4168 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004169 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004170 LOG(FATAL) << DebugName() << " is not defined for float values";
4171 UNREACHABLE();
4172 }
4173 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004174 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004175 LOG(FATAL) << DebugName() << " is not defined for double values";
4176 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004177 }
4178
4179 DECLARE_INSTRUCTION(AboveOrEqual);
4180
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004181 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004182 return kCondAE;
4183 }
4184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004185 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004186 return kCondB;
4187 }
4188
Artem Serovcced8ba2017-07-19 18:18:09 +01004189 protected:
4190 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4191
Aart Bike9f37602015-10-09 11:15:55 -07004192 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004193 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004194 return MakeUnsigned(x) >= MakeUnsigned(y);
4195 }
Aart Bike9f37602015-10-09 11:15:55 -07004196};
Dave Allison20dfc792014-06-16 20:44:29 -07004197
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004198// Instruction to check how two inputs compare to each other.
4199// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004200class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004201 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004202 // Note that `comparison_type` is the type of comparison performed
4203 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004204 // HCompare instruction (which is always DataType::Type::kInt).
4205 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004206 HInstruction* first,
4207 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004208 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004209 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304210 : HBinaryOperation(kCompare,
4211 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004212 first,
4213 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004214 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004215 dex_pc) {
4216 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004217 }
4218
Roland Levillain9867bc72015-08-05 10:21:34 +01004219 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004220 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4221
4222 template <typename T>
4223 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004224 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004225 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4226 // Handle the bias.
4227 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4228 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004230 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004231 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4232 // reach this code path when processing a freshly built HIR
4233 // graph. However HCompare integer instructions can be synthesized
4234 // by the instruction simplifier to implement IntegerCompare and
4235 // IntegerSignum intrinsics, so we have to handle this case.
4236 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004237 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004238 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004239 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4240 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004241 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004242 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4243 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004244 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004245 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004246 }
4247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004248 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004249 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004250 }
4251
Vladimir Markoa1de9182016-02-25 11:37:38 +00004252 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004253
Roland Levillain31dd3d62016-02-16 12:21:02 +00004254 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004255 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004256 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004257 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004258 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004259 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004261 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004262 // Comparisons do not require a runtime call in any back end.
4263 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004264 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004265
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004266 DECLARE_INSTRUCTION(Compare);
4267
Roland Levillain31dd3d62016-02-16 12:21:02 +00004268 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004269 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004270 static constexpr size_t kFieldComparisonBiasSize =
4271 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4272 static constexpr size_t kNumberOfComparePackedBits =
4273 kFieldComparisonBias + kFieldComparisonBiasSize;
4274 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4275 using ComparisonBiasField =
4276 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4277
Roland Levillain31dd3d62016-02-16 12:21:02 +00004278 // Return an integer constant containing the result of a comparison evaluated at compile time.
4279 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4280 DCHECK(value == -1 || value == 0 || value == 1) << value;
4281 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4282 }
4283
Artem Serovcced8ba2017-07-19 18:18:09 +01004284 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004285};
4286
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004287class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004288 public:
4289 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004290 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004291 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004292 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004293 bool finalizable,
4294 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304295 : HExpression(kNewInstance,
4296 DataType::Type::kReference,
4297 SideEffects::CanTriggerGC(),
4298 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004299 type_index_(type_index),
4300 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004301 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004302 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004303 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004304 }
4305
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004306 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004307
Andreas Gampea5b09a62016-11-17 15:21:22 -08004308 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004309 const DexFile& GetDexFile() const { return dex_file_; }
4310
4311 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004312 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004313
Mingyao Yang062157f2016-03-02 10:15:36 -08004314 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004315 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004316
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004317 bool NeedsChecks() const {
4318 return entrypoint_ == kQuickAllocObjectWithChecks;
4319 }
David Brazdil6de19382016-01-08 17:37:10 +00004320
Vladimir Markoa1de9182016-02-25 11:37:38 +00004321 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004323 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004324
4325 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4326
4327 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4328 entrypoint_ = entrypoint;
4329 }
4330
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004331 HLoadClass* GetLoadClass() const {
4332 HInstruction* input = InputAt(0);
4333 if (input->IsClinitCheck()) {
4334 input = input->InputAt(0);
4335 }
4336 DCHECK(input->IsLoadClass());
4337 return input->AsLoadClass();
4338 }
4339
David Brazdil6de19382016-01-08 17:37:10 +00004340 bool IsStringAlloc() const;
4341
4342 DECLARE_INSTRUCTION(NewInstance);
4343
Artem Serovcced8ba2017-07-19 18:18:09 +01004344 protected:
4345 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4346
David Brazdil6de19382016-01-08 17:37:10 +00004347 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004348 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004349 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4350 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4351 "Too many packed fields.");
4352
Andreas Gampea5b09a62016-11-17 15:21:22 -08004353 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004354 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004355 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004356};
4357
Agi Csaki05f20562015-08-19 14:58:14 -07004358enum IntrinsicNeedsEnvironmentOrCache {
4359 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4360 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004361};
4362
Aart Bik5d75afe2015-12-14 11:57:01 -08004363enum IntrinsicSideEffects {
4364 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4365 kReadSideEffects, // Intrinsic may read heap memory.
4366 kWriteSideEffects, // Intrinsic may write heap memory.
4367 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4368};
4369
4370enum IntrinsicExceptions {
4371 kNoThrow, // Intrinsic does not throw any exceptions.
4372 kCanThrow // Intrinsic may throw exceptions.
4373};
4374
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004375// Determines how to load an ArtMethod*.
4376enum class MethodLoadKind {
4377 // Use a String init ArtMethod* loaded from Thread entrypoints.
4378 kStringInit,
4379
4380 // Use the method's own ArtMethod* loaded by the register allocator.
4381 kRecursive,
4382
4383 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4384 // Used for boot image methods referenced by boot image code.
4385 kBootImageLinkTimePcRelative,
4386
4387 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4388 // Used for app->boot calls with relocatable image.
4389 kBootImageRelRo,
4390
4391 // Load from an entry in the .bss section using a PC-relative load.
4392 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
4393 kBssEntry,
4394
4395 // Use ArtMethod* at a known address, embed the direct address in the code.
4396 // Used for for JIT-compiled calls.
4397 kJitDirectAddress,
4398
4399 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4400 // used when other kinds are unimplemented on a particular architecture.
4401 kRuntimeCall,
4402};
4403
4404// Determines the location of the code pointer of an invoke.
4405enum class CodePtrLocation {
4406 // Recursive call, use local PC-relative call instruction.
4407 kCallSelf,
4408
4409 // Use native pointer from the Artmethod*.
4410 // Used for @CriticalNative to avoid going through the compiled stub. This call goes through
4411 // a special resolution stub if the class is not initialized or no native code is registered.
4412 kCallCriticalNative,
4413
4414 // Use code pointer from the ArtMethod*.
4415 // Used when we don't know the target code. This is also the last-resort-kind used when
4416 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4417 kCallArtMethod,
4418};
4419
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004420static inline bool IsPcRelativeMethodLoadKind(MethodLoadKind load_kind) {
4421 return load_kind == MethodLoadKind::kBootImageLinkTimePcRelative ||
4422 load_kind == MethodLoadKind::kBootImageRelRo ||
4423 load_kind == MethodLoadKind::kBssEntry;
4424}
4425
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004426class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004427 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004428 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004429
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004430 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004431 SetRawInputAt(index, argument);
4432 }
4433
Roland Levillain3e3d7332015-04-28 11:00:54 +01004434 // Return the number of arguments. This number can be lower than
4435 // the number of inputs returned by InputCount(), as some invoke
4436 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4437 // inputs at the end of their list of inputs.
4438 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4439
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004440 InvokeType GetInvokeType() const {
4441 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004442 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004443
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004444 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004445 return intrinsic_;
4446 }
4447
Aart Bik5d75afe2015-12-14 11:57:01 -08004448 void SetIntrinsic(Intrinsics intrinsic,
4449 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4450 IntrinsicSideEffects side_effects,
4451 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004452
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004453 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004454 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004455 }
4456
Aart Bikff7d89c2016-11-07 08:49:28 -08004457 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4458
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004459 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004460
Aart Bika8b8e9b2018-01-09 11:01:02 -08004461 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4462
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004463 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004465 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004467 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004468 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4469 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004470
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004471 uint32_t* GetIntrinsicOptimizations() {
4472 return &intrinsic_optimizations_;
4473 }
4474
4475 const uint32_t* GetIntrinsicOptimizations() const {
4476 return &intrinsic_optimizations_;
4477 }
4478
4479 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4480
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004481 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004482 void SetResolvedMethod(ArtMethod* method);
4483
4484 MethodReference GetMethodReference() const { return method_reference_; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004485
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004486 const MethodReference GetResolvedMethodReference() const {
4487 return resolved_method_reference_;
4488 }
4489
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004490 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004491
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004492 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004493 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4494 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004495 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004496 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004497 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4498 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004499 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004500 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004501
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304502 HInvoke(InstructionKind kind,
4503 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004504 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004505 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004506 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004507 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004508 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004509 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004510 MethodReference resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004511 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004512 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304513 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004514 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004515 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4516 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004517 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004518 number_of_arguments + number_of_other_inputs,
4519 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004520 number_of_arguments_(number_of_arguments),
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004521 method_reference_(method_reference),
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004522 resolved_method_reference_(resolved_method_reference),
agicsaki57b81ec2015-08-11 17:39:37 -07004523 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004524 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004525 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004526 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004527 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004528 }
4529
Artem Serovcced8ba2017-07-19 18:18:09 +01004530 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4531
Roland Levillain3e3d7332015-04-28 11:00:54 +01004532 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004533 ArtMethod* resolved_method_;
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004534 const MethodReference method_reference_;
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004535 // Cached values of the resolved method, to avoid needing the mutator lock.
4536 const MethodReference resolved_method_reference_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004537 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004538
4539 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4540 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004541};
4542
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004543class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004544 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004545 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004546 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004547 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004548 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004549 MethodReference method_reference,
Calin Juravle175dc732015-08-25 15:42:32 +01004550 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304551 : HInvoke(kInvokeUnresolved,
4552 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004553 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004554 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004555 return_type,
4556 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004557 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004558 nullptr,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004559 MethodReference(nullptr, 0u),
Calin Juravle175dc732015-08-25 15:42:32 +01004560 invoke_type) {
4561 }
4562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004563 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004564
Calin Juravle175dc732015-08-25 15:42:32 +01004565 DECLARE_INSTRUCTION(InvokeUnresolved);
4566
Artem Serovcced8ba2017-07-19 18:18:09 +01004567 protected:
4568 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004569};
4570
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004571class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004572 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004573 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004574 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004575 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004576 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004577 MethodReference method_reference,
Andra Danciub931b8c2020-07-27 13:16:37 +00004578 // resolved_method is the ArtMethod object corresponding to the polymorphic
4579 // method (e.g. VarHandle.get), resolved using the class linker. It is needed
4580 // to pass intrinsic information to the HInvokePolymorphic node.
Andra Danciu73c31802020-09-01 13:17:05 +00004581 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004582 MethodReference resolved_method_reference,
Andra Danciu73c31802020-09-01 13:17:05 +00004583 dex::ProtoIndex proto_idx)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304584 : HInvoke(kInvokePolymorphic,
4585 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004586 number_of_arguments,
Andra Danciu49a19f32020-08-27 12:44:25 +00004587 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004588 return_type,
4589 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004590 method_reference,
Andra Danciua0130e82020-07-23 12:34:56 +00004591 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004592 resolved_method_reference,
Andra Danciu73c31802020-09-01 13:17:05 +00004593 kPolymorphic),
4594 proto_idx_(proto_idx) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304595 }
Orion Hodsonac141392017-01-13 11:53:47 +00004596
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004597 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004598
Andra Danciu73c31802020-09-01 13:17:05 +00004599 dex::ProtoIndex GetProtoIndex() { return proto_idx_; }
4600
Orion Hodsonac141392017-01-13 11:53:47 +00004601 DECLARE_INSTRUCTION(InvokePolymorphic);
4602
Artem Serovcced8ba2017-07-19 18:18:09 +01004603 protected:
Andra Danciu73c31802020-09-01 13:17:05 +00004604 dex::ProtoIndex proto_idx_;
Artem Serovcced8ba2017-07-19 18:18:09 +01004605 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004606};
4607
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004608class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004609 public:
4610 HInvokeCustom(ArenaAllocator* allocator,
4611 uint32_t number_of_arguments,
4612 uint32_t call_site_index,
4613 DataType::Type return_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004614 uint32_t dex_pc,
4615 MethodReference method_reference)
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004616 : HInvoke(kInvokeCustom,
4617 allocator,
4618 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004619 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004620 return_type,
4621 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004622 method_reference,
Andreas Gampe3db70682018-12-26 15:12:03 -08004623 /* resolved_method= */ nullptr,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004624 MethodReference(nullptr, 0u),
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004625 kStatic),
4626 call_site_index_(call_site_index) {
4627 }
4628
4629 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4630
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004631 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004632
4633 DECLARE_INSTRUCTION(InvokeCustom);
4634
4635 protected:
4636 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4637
4638 private:
4639 uint32_t call_site_index_;
4640};
4641
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004642class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004643 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004644 // Requirements of this method call regarding the class
4645 // initialization (clinit) check of its declaring class.
Vladimir Marko9974e3c2020-06-10 16:27:06 +01004646 enum class ClinitCheckRequirement { // private marker to avoid generate-operator-out.py from processing.
Roland Levillain4c0eb422015-04-24 16:43:49 +01004647 kNone, // Class already initialized.
4648 kExplicit, // Static call having explicit clinit check as last input.
4649 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004650 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004651 };
4652
Vladimir Marko58155012015-08-19 12:49:41 +00004653 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004654 MethodLoadKind method_load_kind;
4655 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004656 // The method load data holds
4657 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4658 // Note that there are multiple string init methods, each having its own offset.
4659 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004660 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004661 };
4662
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004663 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004664 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004665 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004666 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004667 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004668 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004669 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004670 InvokeType invoke_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004671 MethodReference resolved_method_reference,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004672 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304673 : HInvoke(kInvokeStaticOrDirect,
4674 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004675 number_of_arguments,
Vladimir Marko86c87522020-05-11 16:55:55 +01004676 // There is potentially one extra argument for the HCurrentMethod input,
4677 // and one other if the clinit check is explicit. These can be removed later.
4678 (NeedsCurrentMethodInput(dispatch_info) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004679 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004680 return_type,
4681 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004682 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004683 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004684 resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004685 invoke_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004686 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004687 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4688 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004689
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004690 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004691
Vladimir Marko86c87522020-05-11 16:55:55 +01004692 void SetDispatchInfo(DispatchInfo dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004693 bool had_current_method_input = HasCurrentMethodInput();
Vladimir Marko86c87522020-05-11 16:55:55 +01004694 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info);
Vladimir Markob554b5a2015-11-06 12:57:55 +00004695
4696 // Using the current method is the default and once we find a better
4697 // method load kind, we should not go back to using the current method.
4698 DCHECK(had_current_method_input || !needs_current_method_input);
4699
4700 if (had_current_method_input && !needs_current_method_input) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004701 DCHECK_EQ(InputAt(GetCurrentMethodIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4702 RemoveInputAt(GetCurrentMethodIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004703 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004704 dispatch_info_ = dispatch_info;
4705 }
4706
Aart Bik6daebeb2017-04-03 14:35:41 -07004707 DispatchInfo GetDispatchInfo() const {
4708 return dispatch_info_;
4709 }
4710
Vladimir Marko372f10e2016-05-17 16:30:10 +01004711 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004712 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004713 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4714 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4715 DCHECK(!input_records.empty());
4716 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4717 HInstruction* last_input = input_records.back().GetInstruction();
4718 // Note: `last_input` may be null during arguments setup.
4719 if (last_input != nullptr) {
4720 // `last_input` is the last input of a static invoke marked as having
4721 // an explicit clinit check. It must either be:
4722 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4723 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4724 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4725 }
4726 }
4727 return input_records;
4728 }
4729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004730 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Vladimir Marko86c87522020-05-11 16:55:55 +01004731 // We do not access the method via object reference, so we cannot do an implicit null check.
Calin Juravle77520bc2015-01-12 18:45:46 +00004732 // TODO: for intrinsics we can generate implicit null checks.
4733 return false;
4734 }
4735
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004736 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004737 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004738 }
4739
Vladimir Marko58155012015-08-19 12:49:41 +00004740 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4741 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4742 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004743 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004744 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004745 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004746 bool HasPcRelativeMethodLoadKind() const {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004747 return IsPcRelativeMethodLoadKind(GetMethodLoadKind());
Vladimir Marko65979462017-05-19 17:25:12 +01004748 }
Vladimir Marko58155012015-08-19 12:49:41 +00004749
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004750 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004751 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004752 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004753 }
4754
4755 uint64_t GetMethodAddress() const {
4756 DCHECK(HasMethodAddress());
4757 return dispatch_info_.method_load_data;
4758 }
4759
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004760 const DexFile& GetDexFileForPcRelativeDexCache() const;
4761
Vladimir Markoa1de9182016-02-25 11:37:38 +00004762 ClinitCheckRequirement GetClinitCheckRequirement() const {
4763 return GetPackedField<ClinitCheckRequirementField>();
4764 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004765
Roland Levillain4c0eb422015-04-24 16:43:49 +01004766 // Is this instruction a call to a static method?
4767 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004768 return GetInvokeType() == kStatic;
4769 }
4770
Vladimir Marko86c87522020-05-11 16:55:55 +01004771 // Does this method load kind need the current method as an input?
4772 static bool NeedsCurrentMethodInput(DispatchInfo dispatch_info) {
4773 return dispatch_info.method_load_kind == MethodLoadKind::kRecursive ||
4774 dispatch_info.method_load_kind == MethodLoadKind::kRuntimeCall ||
4775 dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative;
4776 }
4777
4778 // Get the index of the current method input.
4779 size_t GetCurrentMethodIndex() const {
4780 DCHECK(HasCurrentMethodInput());
4781 return GetCurrentMethodIndexUnchecked();
4782 }
4783 size_t GetCurrentMethodIndexUnchecked() const {
4784 return GetNumberOfArguments();
4785 }
4786
4787 // Check if the method has a current method input.
4788 bool HasCurrentMethodInput() const {
4789 if (NeedsCurrentMethodInput(GetDispatchInfo())) {
4790 DCHECK(InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4791 InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4792 return true;
4793 } else {
4794 DCHECK(InputCount() == GetCurrentMethodIndexUnchecked() ||
4795 InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4796 !InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4797 return false;
4798 }
4799 }
4800
4801 // Get the index of the special input.
4802 size_t GetSpecialInputIndex() const {
4803 DCHECK(HasSpecialInput());
4804 return GetSpecialInputIndexUnchecked();
4805 }
4806 size_t GetSpecialInputIndexUnchecked() const {
4807 return GetNumberOfArguments() + (HasCurrentMethodInput() ? 1u : 0u);
4808 }
4809
4810 // Check if the method has a special input.
4811 bool HasSpecialInput() const {
4812 size_t other_inputs =
4813 GetSpecialInputIndexUnchecked() + (IsStaticWithExplicitClinitCheck() ? 1u : 0u);
4814 size_t input_count = InputCount();
4815 DCHECK_LE(input_count - other_inputs, 1u) << other_inputs << " " << input_count;
4816 return other_inputs != input_count;
4817 }
4818
4819 void AddSpecialInput(HInstruction* input) {
4820 // We allow only one special input.
4821 DCHECK(!HasSpecialInput());
4822 InsertInputAt(GetSpecialInputIndexUnchecked(), input);
4823 }
4824
Vladimir Markofbb184a2015-11-13 14:47:00 +00004825 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4826 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4827 // instruction; only relevant for static calls with explicit clinit check.
4828 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004829 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004830 size_t last_input_index = inputs_.size() - 1u;
4831 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004832 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004833 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004834 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004835 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004836 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004837 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004838 }
4839
4840 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004841 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004842 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004843 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004844 }
4845
4846 // Is this a call to a static method whose declaring class has an
4847 // implicit intialization check requirement?
4848 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004849 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004850 }
4851
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004852 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004853
Artem Serovcced8ba2017-07-19 18:18:09 +01004854 protected:
4855 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4856
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004857 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004858 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004859 static constexpr size_t kFieldClinitCheckRequirementSize =
4860 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4861 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4862 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4863 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4864 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004865 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4866 kFieldClinitCheckRequirement,
4867 kFieldClinitCheckRequirementSize>;
4868
Vladimir Marko58155012015-08-19 12:49:41 +00004869 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004870};
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004871std::ostream& operator<<(std::ostream& os, MethodLoadKind rhs);
4872std::ostream& operator<<(std::ostream& os, CodePtrLocation rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004873std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004874
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004875class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004876 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004877 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004878 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004879 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004880 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004881 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004882 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004883 MethodReference resolved_method_reference,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004884 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304885 : HInvoke(kInvokeVirtual,
4886 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004887 number_of_arguments,
4888 0u,
4889 return_type,
4890 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004891 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004892 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004893 resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004894 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304895 vtable_index_(vtable_index) {
4896 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004898 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004899
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004900 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004901 switch (GetIntrinsic()) {
4902 case Intrinsics::kThreadCurrentThread:
4903 case Intrinsics::kStringBufferAppend:
4904 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004905 case Intrinsics::kStringBuilderAppendObject:
4906 case Intrinsics::kStringBuilderAppendString:
4907 case Intrinsics::kStringBuilderAppendCharSequence:
4908 case Intrinsics::kStringBuilderAppendCharArray:
4909 case Intrinsics::kStringBuilderAppendBoolean:
4910 case Intrinsics::kStringBuilderAppendChar:
4911 case Intrinsics::kStringBuilderAppendInt:
4912 case Intrinsics::kStringBuilderAppendLong:
4913 case Intrinsics::kStringBuilderAppendFloat:
4914 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004915 case Intrinsics::kStringBuilderToString:
4916 return false;
4917 default:
4918 return HInvoke::CanBeNull();
4919 }
4920 }
4921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004922 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004923 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004924 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004925 }
4926
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004927 uint32_t GetVTableIndex() const { return vtable_index_; }
4928
4929 DECLARE_INSTRUCTION(InvokeVirtual);
4930
Artem Serovcced8ba2017-07-19 18:18:09 +01004931 protected:
4932 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4933
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004934 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004935 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004936 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004937};
4938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004939class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004940 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004941 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004942 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004943 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004944 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004945 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004946 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004947 MethodReference resolved_method_reference,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004948 uint32_t imt_index,
4949 MethodLoadKind load_kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304950 : HInvoke(kInvokeInterface,
4951 allocator,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004952 number_of_arguments + (NeedsCurrentMethod(load_kind) ? 1 : 0),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004953 0u,
4954 return_type,
4955 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004956 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004957 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004958 resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004959 kInterface),
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004960 imt_index_(imt_index),
4961 hidden_argument_load_kind_(load_kind) {
4962 }
4963
4964 static bool NeedsCurrentMethod(MethodLoadKind load_kind) {
4965 return load_kind == MethodLoadKind::kRecursive;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304966 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004968 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004969
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004971 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004972 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004973 }
4974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004975 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004976 // The assembly stub currently needs it.
4977 return true;
4978 }
4979
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004980 size_t GetSpecialInputIndex() const {
4981 return GetNumberOfArguments();
4982 }
4983
4984 void AddSpecialInput(HInstruction* input) {
4985 InsertInputAt(GetSpecialInputIndex(), input);
4986 }
4987
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004988 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004989 MethodLoadKind GetHiddenArgumentLoadKind() const { return hidden_argument_load_kind_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004990
4991 DECLARE_INSTRUCTION(InvokeInterface);
4992
Artem Serovcced8ba2017-07-19 18:18:09 +01004993 protected:
4994 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4995
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004996 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004997 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004998 const uint32_t imt_index_;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004999
5000 // How the hidden argument (the interface method) is being loaded.
5001 const MethodLoadKind hidden_argument_load_kind_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005002};
5003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005004class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01005005 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005006 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305007 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005008 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00005009 }
Roland Levillain88cb1752014-10-20 16:36:47 +01005010
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005011 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005013 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005014 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005015 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005016 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005017 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005018 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005019 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005020 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
5021 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005022 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005023 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
5024 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01005025
Roland Levillain88cb1752014-10-20 16:36:47 +01005026 DECLARE_INSTRUCTION(Neg);
5027
Artem Serovcced8ba2017-07-19 18:18:09 +01005028 protected:
5029 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01005030};
5031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005032class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005033 public:
Vladimir Markob5461632018-10-15 14:24:21 +01005034 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305035 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005036 SetRawInputAt(0, cls);
5037 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01005038 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005039 }
5040
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005041 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005042
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005043 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005044 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005045
Mingyao Yang0c365e62015-03-31 15:09:29 -07005046 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005047 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07005048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005049 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005050
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005051 HLoadClass* GetLoadClass() const {
5052 DCHECK(InputAt(0)->IsLoadClass());
5053 return InputAt(0)->AsLoadClass();
5054 }
5055
5056 HInstruction* GetLength() const {
5057 return InputAt(1);
5058 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00005059
Vladimir Markob5461632018-10-15 14:24:21 +01005060 size_t GetComponentSizeShift() {
5061 return GetPackedField<ComponentSizeShiftField>();
5062 }
5063
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005064 DECLARE_INSTRUCTION(NewArray);
5065
Artem Serovcced8ba2017-07-19 18:18:09 +01005066 protected:
5067 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01005068
5069 private:
5070 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
5071 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
5072 static constexpr size_t kNumberOfNewArrayPackedBits =
5073 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
5074 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5075 using ComponentSizeShiftField =
5076 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005077};
5078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005079class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005080 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005081 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005082 HInstruction* left,
5083 HInstruction* right,
5084 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305085 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5086 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005087
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005088 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005089
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005090 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005091
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005092 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005093 return GetBlock()->GetGraph()->GetIntConstant(
5094 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005095 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005096 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005097 return GetBlock()->GetGraph()->GetLongConstant(
5098 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005099 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005100 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005101 return GetBlock()->GetGraph()->GetFloatConstant(
5102 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5103 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005104 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005105 return GetBlock()->GetGraph()->GetDoubleConstant(
5106 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5107 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005108
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005109 DECLARE_INSTRUCTION(Add);
5110
Artem Serovcced8ba2017-07-19 18:18:09 +01005111 protected:
5112 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005113};
5114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005115class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005116 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005117 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005118 HInstruction* left,
5119 HInstruction* right,
5120 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305121 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5122 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005123
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005124 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005126 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005127 return GetBlock()->GetGraph()->GetIntConstant(
5128 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005129 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005130 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005131 return GetBlock()->GetGraph()->GetLongConstant(
5132 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005133 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005134 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005135 return GetBlock()->GetGraph()->GetFloatConstant(
5136 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5137 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005138 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005139 return GetBlock()->GetGraph()->GetDoubleConstant(
5140 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5141 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005142
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005143 DECLARE_INSTRUCTION(Sub);
5144
Artem Serovcced8ba2017-07-19 18:18:09 +01005145 protected:
5146 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005147};
5148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005149class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005150 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005151 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005152 HInstruction* left,
5153 HInstruction* right,
5154 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305155 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5156 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005157
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005158 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005159
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005160 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005162 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005163 return GetBlock()->GetGraph()->GetIntConstant(
5164 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005165 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005166 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005167 return GetBlock()->GetGraph()->GetLongConstant(
5168 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005169 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005170 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005171 return GetBlock()->GetGraph()->GetFloatConstant(
5172 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5173 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005174 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005175 return GetBlock()->GetGraph()->GetDoubleConstant(
5176 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5177 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005178
5179 DECLARE_INSTRUCTION(Mul);
5180
Artem Serovcced8ba2017-07-19 18:18:09 +01005181 protected:
5182 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005183};
5184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005185class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005186 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005187 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005188 HInstruction* left,
5189 HInstruction* right,
5190 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305191 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5192 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005193
Roland Levillain9867bc72015-08-05 10:21:34 +01005194 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005195 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005196 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005197 // Our graph structure ensures we never have 0 for `y` during
5198 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005199 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005200 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005201 return (y == -1) ? -x : x / y;
5202 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005203
Roland Levillain31dd3d62016-02-16 12:21:02 +00005204 template <typename T>
5205 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005206 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005207 return x / y;
5208 }
5209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005210 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005211 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005212 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005213 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005214 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005215 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005216 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5217 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005218 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005219 return GetBlock()->GetGraph()->GetFloatConstant(
5220 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5221 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005222 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005223 return GetBlock()->GetGraph()->GetDoubleConstant(
5224 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005225 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005226
5227 DECLARE_INSTRUCTION(Div);
5228
Artem Serovcced8ba2017-07-19 18:18:09 +01005229 protected:
5230 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005231};
5232
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005233class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005234 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005235 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005236 HInstruction* left,
5237 HInstruction* right,
5238 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305239 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5240 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005241
Roland Levillain9867bc72015-08-05 10:21:34 +01005242 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005243 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005244 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005245 // Our graph structure ensures we never have 0 for `y` during
5246 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005247 DCHECK_NE(y, 0);
5248 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5249 return (y == -1) ? 0 : x % y;
5250 }
5251
Roland Levillain31dd3d62016-02-16 12:21:02 +00005252 template <typename T>
5253 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005254 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005255 return std::fmod(x, y);
5256 }
5257
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005258 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005259 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005260 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005261 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005262 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005263 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005264 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005265 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005266 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005267 return GetBlock()->GetGraph()->GetFloatConstant(
5268 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5269 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005270 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005271 return GetBlock()->GetGraph()->GetDoubleConstant(
5272 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5273 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005274
5275 DECLARE_INSTRUCTION(Rem);
5276
Artem Serovcced8ba2017-07-19 18:18:09 +01005277 protected:
5278 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005279};
5280
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005281class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005282 public:
5283 HMin(DataType::Type result_type,
5284 HInstruction* left,
5285 HInstruction* right,
5286 uint32_t dex_pc)
5287 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5288
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005289 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005290
5291 // Evaluation for integral values.
5292 template <typename T> static T ComputeIntegral(T x, T y) {
5293 return (x <= y) ? x : y;
5294 }
5295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005296 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005297 return GetBlock()->GetGraph()->GetIntConstant(
5298 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5299 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005300 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005301 return GetBlock()->GetGraph()->GetLongConstant(
5302 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5303 }
5304 // TODO: Evaluation for floating-point values.
5305 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005306 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005307 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005308 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005309
5310 DECLARE_INSTRUCTION(Min);
5311
5312 protected:
5313 DEFAULT_COPY_CONSTRUCTOR(Min);
5314};
5315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005316class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005317 public:
5318 HMax(DataType::Type result_type,
5319 HInstruction* left,
5320 HInstruction* right,
5321 uint32_t dex_pc)
5322 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5323
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005324 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005325
5326 // Evaluation for integral values.
5327 template <typename T> static T ComputeIntegral(T x, T y) {
5328 return (x >= y) ? x : y;
5329 }
5330
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005331 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005332 return GetBlock()->GetGraph()->GetIntConstant(
5333 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5334 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005335 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005336 return GetBlock()->GetGraph()->GetLongConstant(
5337 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5338 }
5339 // TODO: Evaluation for floating-point values.
5340 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005341 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005342 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005343 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005344
5345 DECLARE_INSTRUCTION(Max);
5346
5347 protected:
5348 DEFAULT_COPY_CONSTRUCTOR(Max);
5349};
5350
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005351class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005352 public:
5353 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5354 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5355
5356 // Evaluation for integral values.
5357 template <typename T> static T ComputeIntegral(T x) {
5358 return x < 0 ? -x : x;
5359 }
5360
5361 // Evaluation for floating-point values.
5362 // Note, as a "quality of implementation", rather than pure "spec compliance",
5363 // we require that Math.abs() clears the sign bit (but changes nothing else)
5364 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5365 // http://b/30758343
5366 template <typename T, typename S> static T ComputeFP(T x) {
5367 S bits = bit_cast<S, T>(x);
5368 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5369 }
5370
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005371 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005372 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5373 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005374 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005375 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5376 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005377 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005378 return GetBlock()->GetGraph()->GetFloatConstant(
5379 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5380 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005381 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005382 return GetBlock()->GetGraph()->GetDoubleConstant(
5383 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5384 }
5385
5386 DECLARE_INSTRUCTION(Abs);
5387
5388 protected:
5389 DEFAULT_COPY_CONSTRUCTOR(Abs);
5390};
5391
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005392class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005393 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005394 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005395 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5396 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005397 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005398 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005399 SetRawInputAt(0, value);
5400 }
5401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005402 bool IsClonable() const override { return true; }
5403 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005405 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005406 return true;
5407 }
5408
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005409 bool NeedsEnvironment() const override { return true; }
5410 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005411
Calin Juravled0d48522014-11-04 16:40:20 +00005412 DECLARE_INSTRUCTION(DivZeroCheck);
5413
Artem Serovcced8ba2017-07-19 18:18:09 +01005414 protected:
5415 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005416};
5417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005418class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005419 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005420 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005421 HInstruction* value,
5422 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005423 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305424 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005425 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5426 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005427 }
5428
Roland Levillain5b5b9312016-03-22 14:57:31 +00005429 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005430 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005431 return value << (distance & max_shift_distance);
5432 }
5433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005434 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005435 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005436 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005437 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005438 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005439 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005440 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005441 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005442 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005443 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005444 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5445 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005446 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005447 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005448 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005449 LOG(FATAL) << DebugName() << " is not defined for float values";
5450 UNREACHABLE();
5451 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005452 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005453 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005454 LOG(FATAL) << DebugName() << " is not defined for double values";
5455 UNREACHABLE();
5456 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005457
5458 DECLARE_INSTRUCTION(Shl);
5459
Artem Serovcced8ba2017-07-19 18:18:09 +01005460 protected:
5461 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005462};
5463
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005464class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005465 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005466 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005467 HInstruction* value,
5468 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005469 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305470 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005471 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5472 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005473 }
5474
Roland Levillain5b5b9312016-03-22 14:57:31 +00005475 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005476 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005477 return value >> (distance & max_shift_distance);
5478 }
5479
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005480 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005481 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005482 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005483 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005484 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005485 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005486 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005487 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005488 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005489 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005490 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5491 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005492 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005493 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005494 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005495 LOG(FATAL) << DebugName() << " is not defined for float values";
5496 UNREACHABLE();
5497 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005498 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005499 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005500 LOG(FATAL) << DebugName() << " is not defined for double values";
5501 UNREACHABLE();
5502 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005503
5504 DECLARE_INSTRUCTION(Shr);
5505
Artem Serovcced8ba2017-07-19 18:18:09 +01005506 protected:
5507 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005508};
5509
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005510class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005511 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005512 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005513 HInstruction* value,
5514 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005515 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305516 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005517 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5518 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005519 }
5520
Roland Levillain5b5b9312016-03-22 14:57:31 +00005521 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005522 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005523 typedef typename std::make_unsigned<T>::type V;
5524 V ux = static_cast<V>(value);
5525 return static_cast<T>(ux >> (distance & max_shift_distance));
5526 }
5527
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005528 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005529 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005530 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005531 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005532 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005533 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005534 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005535 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005536 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005537 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005538 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5539 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005540 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005541 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005542 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005543 LOG(FATAL) << DebugName() << " is not defined for float values";
5544 UNREACHABLE();
5545 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005546 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005547 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005548 LOG(FATAL) << DebugName() << " is not defined for double values";
5549 UNREACHABLE();
5550 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005551
5552 DECLARE_INSTRUCTION(UShr);
5553
Artem Serovcced8ba2017-07-19 18:18:09 +01005554 protected:
5555 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005556};
5557
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005558class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005559 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005560 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005561 HInstruction* left,
5562 HInstruction* right,
5563 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305564 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5565 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005566
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005567 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005568
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005569 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005570
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005571 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005572 return GetBlock()->GetGraph()->GetIntConstant(
5573 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005574 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005576 return GetBlock()->GetGraph()->GetLongConstant(
5577 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005578 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005579 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005580 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005581 LOG(FATAL) << DebugName() << " is not defined for float values";
5582 UNREACHABLE();
5583 }
5584 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005585 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005586 LOG(FATAL) << DebugName() << " is not defined for double values";
5587 UNREACHABLE();
5588 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005589
5590 DECLARE_INSTRUCTION(And);
5591
Artem Serovcced8ba2017-07-19 18:18:09 +01005592 protected:
5593 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005594};
5595
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005596class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005597 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005598 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005599 HInstruction* left,
5600 HInstruction* right,
5601 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305602 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5603 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005604
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005605 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005606
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005607 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005608
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005609 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005610 return GetBlock()->GetGraph()->GetIntConstant(
5611 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005612 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005613 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005614 return GetBlock()->GetGraph()->GetLongConstant(
5615 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005616 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005617 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005618 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005619 LOG(FATAL) << DebugName() << " is not defined for float values";
5620 UNREACHABLE();
5621 }
5622 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005623 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005624 LOG(FATAL) << DebugName() << " is not defined for double values";
5625 UNREACHABLE();
5626 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005627
5628 DECLARE_INSTRUCTION(Or);
5629
Artem Serovcced8ba2017-07-19 18:18:09 +01005630 protected:
5631 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005632};
5633
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005634class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005635 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005636 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005637 HInstruction* left,
5638 HInstruction* right,
5639 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305640 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5641 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005642
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005643 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005644
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005645 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005647 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005648 return GetBlock()->GetGraph()->GetIntConstant(
5649 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005650 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005651 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005652 return GetBlock()->GetGraph()->GetLongConstant(
5653 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005654 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005655 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005656 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005657 LOG(FATAL) << DebugName() << " is not defined for float values";
5658 UNREACHABLE();
5659 }
5660 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005661 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005662 LOG(FATAL) << DebugName() << " is not defined for double values";
5663 UNREACHABLE();
5664 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005665
5666 DECLARE_INSTRUCTION(Xor);
5667
Artem Serovcced8ba2017-07-19 18:18:09 +01005668 protected:
5669 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005670};
5671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005672class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005673 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005674 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305675 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005676 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005677
Roland Levillain5b5b9312016-03-22 14:57:31 +00005678 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005679 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005680 typedef typename std::make_unsigned<T>::type V;
5681 V ux = static_cast<V>(value);
5682 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005683 return static_cast<T>(ux);
5684 } else {
5685 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005686 return static_cast<T>(ux >> (distance & max_shift_value)) |
5687 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005688 }
5689 }
5690
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005691 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005692 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005693 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005694 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005695 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005696 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005697 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005698 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005699 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005700 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005701 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5702 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005703 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005704 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005705 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005706 LOG(FATAL) << DebugName() << " is not defined for float values";
5707 UNREACHABLE();
5708 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005709 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005710 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005711 LOG(FATAL) << DebugName() << " is not defined for double values";
5712 UNREACHABLE();
5713 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005714
5715 DECLARE_INSTRUCTION(Ror);
5716
Artem Serovcced8ba2017-07-19 18:18:09 +01005717 protected:
5718 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005719};
5720
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005721// The value of a parameter in this method. Its location depends on
5722// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005723class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005724 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005725 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005726 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005727 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005729 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305730 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005731 dex_file_(dex_file),
5732 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005733 index_(index) {
5734 SetPackedFlag<kFlagIsThis>(is_this);
5735 SetPackedFlag<kFlagCanBeNull>(!is_this);
5736 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005737
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005738 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005739 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005740 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005741 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005742
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005743 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005744 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005745
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005746 DECLARE_INSTRUCTION(ParameterValue);
5747
Artem Serovcced8ba2017-07-19 18:18:09 +01005748 protected:
5749 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5750
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005751 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005752 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005753 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005754 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5755 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5756 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5757 "Too many packed fields.");
5758
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005759 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005760 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005761 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005762 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005763 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005764};
5765
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005766class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005767 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005768 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305769 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5770 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005772 bool CanBeMoved() const override { return true; }
5773 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005774 return true;
5775 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005776
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005777 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005778
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005779 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005780 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005781 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005782 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005783 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005784 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005785 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005786 LOG(FATAL) << DebugName() << " is not defined for float values";
5787 UNREACHABLE();
5788 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005789 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005790 LOG(FATAL) << DebugName() << " is not defined for double values";
5791 UNREACHABLE();
5792 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005793
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005794 DECLARE_INSTRUCTION(Not);
5795
Artem Serovcced8ba2017-07-19 18:18:09 +01005796 protected:
5797 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005798};
5799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005800class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005802 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305803 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5804 }
David Brazdil66d126e2015-04-03 16:02:44 +01005805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005806 bool CanBeMoved() const override { return true; }
5807 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005808 return true;
5809 }
5810
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005811 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005812 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005813 return !x;
5814 }
5815
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005816 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005817 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005818 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005819 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005820 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005821 UNREACHABLE();
5822 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005823 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005824 LOG(FATAL) << DebugName() << " is not defined for float values";
5825 UNREACHABLE();
5826 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005827 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005828 LOG(FATAL) << DebugName() << " is not defined for double values";
5829 UNREACHABLE();
5830 }
David Brazdil66d126e2015-04-03 16:02:44 +01005831
5832 DECLARE_INSTRUCTION(BooleanNot);
5833
Artem Serovcced8ba2017-07-19 18:18:09 +01005834 protected:
5835 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005836};
5837
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005838class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005839 public:
5840 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005841 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305842 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005843 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005844 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005845 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005846 }
5847
5848 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005849 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5850 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005851
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005852 bool IsClonable() const override { return true; }
5853 bool CanBeMoved() const override { return true; }
5854 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005855 return true;
5856 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005857 // Return whether the conversion is implicit. This includes conversion to the same type.
5858 bool IsImplicitConversion() const {
5859 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5860 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005861
Mark Mendelle82549b2015-05-06 10:55:34 -04005862 // Try to statically evaluate the conversion and return a HConstant
5863 // containing the result. If the input cannot be converted, return nullptr.
5864 HConstant* TryStaticEvaluation() const;
5865
Roland Levillaindff1f282014-11-05 14:15:05 +00005866 DECLARE_INSTRUCTION(TypeConversion);
5867
Artem Serovcced8ba2017-07-19 18:18:09 +01005868 protected:
5869 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005870};
5871
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005872static constexpr uint32_t kNoRegNumber = -1;
5873
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005874class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005875 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005876 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005877 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5878 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005879 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005880 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005881 SetRawInputAt(0, value);
5882 }
5883
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005884 bool IsClonable() const override { return true; }
5885 bool CanBeMoved() const override { return true; }
5886 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005887 return true;
5888 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005890 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005892 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005893
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005894 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005895
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005896 DECLARE_INSTRUCTION(NullCheck);
5897
Artem Serovcced8ba2017-07-19 18:18:09 +01005898 protected:
5899 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005900};
5901
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005902// Embeds an ArtField and all the information required by the compiler. We cache
5903// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005904class FieldInfo : public ValueObject {
5905 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005906 FieldInfo(ArtField* field,
5907 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005909 bool is_volatile,
5910 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005911 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005912 const DexFile& dex_file)
5913 : field_(field),
5914 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005915 field_type_(field_type),
5916 is_volatile_(is_volatile),
5917 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005918 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005919 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005920
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005921 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005922 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005923 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005924 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005925 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005926 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005927 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005928
5929 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005930 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005931 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005932 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005933 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005934 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005935 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005936 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005937};
5938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005939class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005940 public:
5941 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005942 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005943 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005944 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005945 bool is_volatile,
5946 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005947 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005948 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005949 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305950 : HExpression(kInstanceFieldGet,
5951 field_type,
5952 SideEffects::FieldReadOfType(field_type, is_volatile),
5953 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005954 field_info_(field,
5955 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005956 field_type,
5957 is_volatile,
5958 field_idx,
5959 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005960 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005961 SetRawInputAt(0, value);
5962 }
5963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005964 bool IsClonable() const override { return true; }
5965 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005966
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005967 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005968 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005969 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005970 }
5971
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005972 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005973 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005974 }
5975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005976 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005977 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5978 }
5979
Calin Juravle52c48962014-12-16 17:02:57 +00005980 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005981 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005982 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005983 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005984
Vladimir Marko61b92282017-10-11 13:23:17 +01005985 void SetType(DataType::Type new_type) {
5986 DCHECK(DataType::IsIntegralType(GetType()));
5987 DCHECK(DataType::IsIntegralType(new_type));
5988 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5989 SetPackedField<TypeField>(new_type);
5990 }
5991
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005992 DECLARE_INSTRUCTION(InstanceFieldGet);
5993
Artem Serovcced8ba2017-07-19 18:18:09 +01005994 protected:
5995 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5996
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005997 private:
5998 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005999};
6000
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006001class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006002 public:
6003 HInstanceFieldSet(HInstruction* object,
6004 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006005 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006006 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006007 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006008 bool is_volatile,
6009 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006010 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006011 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006012 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006013 : HExpression(kInstanceFieldSet,
6014 SideEffects::FieldWriteOfType(field_type, is_volatile),
6015 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006016 field_info_(field,
6017 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006018 field_type,
6019 is_volatile,
6020 field_idx,
6021 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006022 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006023 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006024 SetRawInputAt(0, object);
6025 SetRawInputAt(1, value);
6026 }
6027
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006028 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006029
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006030 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01006031 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00006032 }
6033
Calin Juravle52c48962014-12-16 17:02:57 +00006034 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006035 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006036 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006037 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006038 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006039 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6040 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006041
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006042 DECLARE_INSTRUCTION(InstanceFieldSet);
6043
Artem Serovcced8ba2017-07-19 18:18:09 +01006044 protected:
6045 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
6046
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006047 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006048 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6049 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
6050 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
6051 "Too many packed fields.");
6052
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006053 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006054};
6055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006056class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006057 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006058 HArrayGet(HInstruction* array,
6059 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006060 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006061 uint32_t dex_pc)
6062 : HArrayGet(array,
6063 index,
6064 type,
6065 SideEffects::ArrayReadOfType(type),
6066 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08006067 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306068 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006069
6070 HArrayGet(HInstruction* array,
6071 HInstruction* index,
6072 DataType::Type type,
6073 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006074 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006075 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306076 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006077 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006078 SetRawInputAt(0, array);
6079 SetRawInputAt(1, index);
6080 }
6081
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006082 bool IsClonable() const override { return true; }
6083 bool CanBeMoved() const override { return true; }
6084 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006085 return true;
6086 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006087 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006088 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006089 // Currently, unless the array is the result of NewArray, the array access is always
6090 // preceded by some form of null NullCheck necessary for the bounds check, usually
6091 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6092 // dynamic BCE. There are cases when these could be removed to produce better code.
6093 // If we ever add optimizations to do so we should allow an implicit check here
6094 // (as long as the address falls in the first page).
6095 //
6096 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6097 // a = cond ? new int[1] : null;
6098 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006099 return false;
6100 }
6101
David Brazdil4833f5a2015-12-16 10:37:39 +00006102 bool IsEquivalentOf(HArrayGet* other) const {
6103 bool result = (GetDexPc() == other->GetDexPc());
6104 if (kIsDebugBuild && result) {
6105 DCHECK_EQ(GetBlock(), other->GetBlock());
6106 DCHECK_EQ(GetArray(), other->GetArray());
6107 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006108 if (DataType::IsIntOrLongType(GetType())) {
6109 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006110 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006111 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6112 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006113 }
6114 }
6115 return result;
6116 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006117
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006118 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6119
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006120 HInstruction* GetArray() const { return InputAt(0); }
6121 HInstruction* GetIndex() const { return InputAt(1); }
6122
Vladimir Marko61b92282017-10-11 13:23:17 +01006123 void SetType(DataType::Type new_type) {
6124 DCHECK(DataType::IsIntegralType(GetType()));
6125 DCHECK(DataType::IsIntegralType(new_type));
6126 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6127 SetPackedField<TypeField>(new_type);
6128 }
6129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006130 DECLARE_INSTRUCTION(ArrayGet);
6131
Artem Serovcced8ba2017-07-19 18:18:09 +01006132 protected:
6133 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6134
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006135 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006136 // We treat a String as an array, creating the HArrayGet from String.charAt()
6137 // intrinsic in the instruction simplifier. We can always determine whether
6138 // a particular HArrayGet is actually a String.charAt() by looking at the type
6139 // of the input but that requires holding the mutator lock, so we prefer to use
6140 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006141 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006142 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6143 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6144 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006145};
6146
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006147class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006148 public:
6149 HArraySet(HInstruction* array,
6150 HInstruction* index,
6151 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006152 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006153 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006154 : HArraySet(array,
6155 index,
6156 value,
6157 expected_component_type,
6158 // Make a best guess for side effects now, may be refined during SSA building.
6159 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306160 dex_pc) {
6161 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006162
6163 HArraySet(HInstruction* array,
6164 HInstruction* index,
6165 HInstruction* value,
6166 DataType::Type expected_component_type,
6167 SideEffects side_effects,
6168 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006169 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006170 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006171 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006172 SetPackedFlag<kFlagValueCanBeNull>(true);
6173 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006174 SetRawInputAt(0, array);
6175 SetRawInputAt(1, index);
6176 SetRawInputAt(2, value);
6177 }
6178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006179 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006180
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006181 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006182 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006183 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006184 }
6185
Mingyao Yang81014cb2015-06-02 03:16:27 -07006186 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006187 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006188
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006189 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006190 // TODO: Same as for ArrayGet.
6191 return false;
6192 }
6193
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006194 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006195 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006196 }
6197
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006198 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006199 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006200 }
6201
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006202 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006203 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006204 }
6205
Vladimir Markoa1de9182016-02-25 11:37:38 +00006206 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6207 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6208 bool StaticTypeOfArrayIsObjectArray() const {
6209 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6210 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006211
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006212 HInstruction* GetArray() const { return InputAt(0); }
6213 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006214 HInstruction* GetValue() const { return InputAt(2); }
6215
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006216 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006217 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6218 }
6219
6220 static DataType::Type GetComponentType(DataType::Type value_type,
6221 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006222 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006223 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006224 // be correct, we also check what is the value type. If it is a floating
6225 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006226 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006227 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006228 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006229 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006231 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006232 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006233 }
6234
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006235 static SideEffects ComputeSideEffects(DataType::Type type) {
6236 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006237 }
6238
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006239 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6240 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6241 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006242 }
6243
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006244 DECLARE_INSTRUCTION(ArraySet);
6245
Artem Serovcced8ba2017-07-19 18:18:09 +01006246 protected:
6247 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6248
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006249 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006250 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6251 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006252 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006253 static constexpr size_t kFlagNeedsTypeCheck =
6254 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6255 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006256 // Cached information for the reference_type_info_ so that codegen
6257 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006258 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6259 static constexpr size_t kNumberOfArraySetPackedBits =
6260 kFlagStaticTypeOfArrayIsObjectArray + 1;
6261 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6262 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006263 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006264};
6265
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006266class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006267 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006268 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306269 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006270 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006271 // Note that arrays do not change length, so the instruction does not
6272 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006273 SetRawInputAt(0, array);
6274 }
6275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006276 bool IsClonable() const override { return true; }
6277 bool CanBeMoved() const override { return true; }
6278 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006279 return true;
6280 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006281 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006282 return obj == InputAt(0);
6283 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006284
Vladimir Markodce016e2016-04-28 13:10:02 +01006285 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6286
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006287 DECLARE_INSTRUCTION(ArrayLength);
6288
Artem Serovcced8ba2017-07-19 18:18:09 +01006289 protected:
6290 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6291
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006292 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006293 // We treat a String as an array, creating the HArrayLength from String.length()
6294 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6295 // determine whether a particular HArrayLength is actually a String.length() by
6296 // looking at the type of the input but that requires holding the mutator lock, so
6297 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006298 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006299 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6300 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6301 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006302};
6303
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006304class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006305 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006306 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006307 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6308 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006309 HBoundsCheck(HInstruction* index,
6310 HInstruction* length,
6311 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006312 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006313 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006314 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006315 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006316 SetRawInputAt(0, index);
6317 SetRawInputAt(1, length);
6318 }
6319
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006320 bool IsClonable() const override { return true; }
6321 bool CanBeMoved() const override { return true; }
6322 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006323 return true;
6324 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006325
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006326 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006327
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006328 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006329
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006330 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006331
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006332 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006333
6334 DECLARE_INSTRUCTION(BoundsCheck);
6335
Artem Serovcced8ba2017-07-19 18:18:09 +01006336 protected:
6337 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006339 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006340 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006341 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6342 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6343 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006344};
6345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006346class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006347 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006348 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006349 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306350 slow_path_(nullptr) {
6351 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006352
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006353 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006355 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006356 return true;
6357 }
6358
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006359 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6360 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006361
6362 DECLARE_INSTRUCTION(SuspendCheck);
6363
Artem Serovcced8ba2017-07-19 18:18:09 +01006364 protected:
6365 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6366
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006367 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006368 // Only used for code generation, in order to share the same slow path between back edges
6369 // of a same loop.
6370 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006371};
6372
David Srbecky0cf44932015-12-09 14:09:59 +00006373// Pseudo-instruction which provides the native debugger with mapping information.
6374// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006375class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006376 public:
6377 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006378 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306379 }
David Srbecky0cf44932015-12-09 14:09:59 +00006380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006381 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006382 return true;
6383 }
6384
6385 DECLARE_INSTRUCTION(NativeDebugInfo);
6386
Artem Serovcced8ba2017-07-19 18:18:09 +01006387 protected:
6388 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006389};
6390
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006391/**
6392 * Instruction to load a Class object.
6393 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006394class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006395 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006396 // Determines how to load the Class.
6397 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006398 // We cannot load this class. See HSharpening::SharpenLoadClass.
6399 kInvalid = -1,
6400
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006401 // Use the Class* from the method's own ArtMethod*.
6402 kReferrersClass,
6403
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006404 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006405 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006406 kBootImageLinkTimePcRelative,
6407
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006408 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006409 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006410 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006411
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006412 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006413 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006414 kBssEntry,
6415
Vladimir Marko8f63f102020-09-28 12:10:28 +01006416 // Load from an entry for public class in the .bss section using a PC-relative load.
6417 // Used for classes that were unresolved during AOT-compilation outside the literal
6418 // package of the compiling class. Such classes are accessible only if they are public
6419 // and the .bss entry shall therefore be filled only if the resolved class is public.
6420 kBssEntryPublic,
6421
6422 // Load from an entry for package class in the .bss section using a PC-relative load.
6423 // Used for classes that were unresolved during AOT-compilation but within the literal
6424 // package of the compiling class. Such classes are accessible if they are public or
6425 // in the same package which, given the literal package match, requires only matching
6426 // defining class loader and the .bss entry shall therefore be filled only if at least
6427 // one of those conditions holds. Note that all code in an oat file belongs to classes
6428 // with the same defining class loader.
6429 kBssEntryPackage,
6430
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006431 // Use a known boot image Class* address, embedded in the code by the codegen.
6432 // Used for boot image classes referenced by apps in JIT-compiled code.
6433 kJitBootImageAddress,
6434
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006435 // Load from the root table associated with the JIT compiled method.
6436 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006437
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006438 // Load using a simple runtime call. This is the fall-back load kind when
6439 // the codegen is unable to use another appropriate kind.
6440 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006441
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006442 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006443 };
6444
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006445 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006446 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006447 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006448 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006449 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006450 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006451 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006452 : HInstruction(kLoadClass,
6453 DataType::Type::kReference,
6454 SideEffectsForArchRuntimeCalls(),
6455 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006456 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006457 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006458 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006459 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006460 // Referrers class should not need access check. We never inline unverified
6461 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006462 DCHECK(!is_referrers_class || !needs_access_check);
6463
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006464 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006465 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006466 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006467 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006468 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006469 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006470 }
6471
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006472 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006473
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006474 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006475
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006476 LoadKind GetLoadKind() const {
6477 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006478 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006479
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006480 bool HasPcRelativeLoadKind() const {
6481 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6482 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006483 GetLoadKind() == LoadKind::kBssEntry ||
6484 GetLoadKind() == LoadKind::kBssEntryPublic ||
6485 GetLoadKind() == LoadKind::kBssEntryPackage;
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006486 }
6487
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006488 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006489
Yi Kong39402542019-03-24 02:47:16 -07006490 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006491
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006492 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006493
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006494 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006495
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006496 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006497 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006498 }
6499
Calin Juravle0ba218d2015-05-19 18:46:01 +01006500 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006501 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006502 }
6503
6504 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006505 return NeedsAccessCheck() ||
6506 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006507 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006508 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006509 }
6510
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006511 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006512 return NeedsAccessCheck() ||
6513 MustGenerateClinitCheck() ||
6514 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006515 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006516 GetLoadKind() == LoadKind::kBssEntry) &&
6517 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006518 }
6519
Calin Juravleacf735c2015-02-12 15:25:22 +00006520 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006521 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6522 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006523 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006524 } else {
6525 return ReferenceTypeInfo::CreateInvalid();
6526 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006527 }
6528
Vladimir Marko175e7862018-03-27 09:03:13 +00006529 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6530 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6531 DCHECK(klass_ != nullptr);
6532 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006533 }
6534
Andreas Gampea5b09a62016-11-17 15:21:22 -08006535 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006536 const DexFile& GetDexFile() const { return dex_file_; }
6537
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006538 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006539 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006540 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006541
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006542 static SideEffects SideEffectsForArchRuntimeCalls() {
6543 return SideEffects::CanTriggerGC();
6544 }
6545
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006546 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006547 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006548 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006549 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006550
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006551 bool MustResolveTypeOnSlowPath() const {
6552 // Check that this instruction has a slow path.
Vladimir Marko8f63f102020-09-28 12:10:28 +01006553 LoadKind load_kind = GetLoadKind();
6554 DCHECK(load_kind != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6555 bool must_resolve_type_on_slow_path =
6556 load_kind == LoadKind::kBssEntry ||
6557 load_kind == LoadKind::kBssEntryPublic ||
6558 load_kind == LoadKind::kBssEntryPackage;
6559 DCHECK(must_resolve_type_on_slow_path || MustGenerateClinitCheck());
6560 return must_resolve_type_on_slow_path;
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006561 }
6562
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006563 void MarkInBootImage() {
6564 SetPackedFlag<kFlagIsInBootImage>(true);
6565 }
6566
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006567 void AddSpecialInput(HInstruction* special_input);
6568
6569 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006570 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006571 return ArrayRef<HUserRecord<HInstruction*>>(
6572 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6573 }
6574
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006575 Handle<mirror::Class> GetClass() const {
6576 return klass_;
6577 }
6578
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006579 DECLARE_INSTRUCTION(LoadClass);
6580
Artem Serovcced8ba2017-07-19 18:18:09 +01006581 protected:
6582 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6583
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006584 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006585 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006586 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006587 // Whether this instruction must generate the initialization check.
6588 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006589 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006590 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6591 static constexpr size_t kFieldLoadKindSize =
6592 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006593 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6594 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006595 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006596 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6597
6598 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006599 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006600 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006601 load_kind == LoadKind::kBssEntry ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006602 load_kind == LoadKind::kBssEntryPublic ||
6603 load_kind == LoadKind::kBssEntryPackage ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006604 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006605 }
6606
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006607 void SetLoadKindInternal(LoadKind load_kind);
6608
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006609 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006610 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko8f63f102020-09-28 12:10:28 +01006611 // for PC-relative loads, i.e. kBssEntry* or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006612 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006613
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006614 // A type index and dex file where the class can be accessed. The dex file can be:
6615 // - The compiling method's dex file if the class is defined there too.
6616 // - The compiling method's dex file if the class is referenced there.
6617 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko8f63f102020-09-28 12:10:28 +01006618 // kBssEntry* or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006619 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006620 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006621
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006622 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006623};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006624std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6625
6626// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006627inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6628 // The load kind should be determined before inserting the instruction to the graph.
6629 DCHECK(GetBlock() == nullptr);
6630 DCHECK(GetEnvironment() == nullptr);
6631 SetPackedField<LoadKindField>(load_kind);
6632 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6633 special_input_ = HUserRecord<HInstruction*>(nullptr);
6634 }
6635 if (!NeedsEnvironment()) {
6636 SetSideEffects(SideEffects::None());
6637 }
6638}
6639
6640// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006641inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006642 // The special input is used for PC-relative loads on some architectures,
6643 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006644 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006645 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006646 GetLoadKind() == LoadKind::kBssEntry ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006647 GetLoadKind() == LoadKind::kBssEntryPublic ||
6648 GetLoadKind() == LoadKind::kBssEntryPackage ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006649 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006650 DCHECK(special_input_.GetInstruction() == nullptr);
6651 special_input_ = HUserRecord<HInstruction*>(special_input);
6652 special_input->AddUseAt(this, 0);
6653}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006655class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006656 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006657 // Determines how to load the String.
6658 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006659 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006660 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006661 kBootImageLinkTimePcRelative,
6662
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006663 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006664 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006665 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006666
Vladimir Markoaad75c62016-10-03 08:46:48 +00006667 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006668 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006669 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006670
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006671 // Use a known boot image String* address, embedded in the code by the codegen.
6672 // Used for boot image strings referenced by apps in JIT-compiled code.
6673 kJitBootImageAddress,
6674
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006675 // Load from the root table associated with the JIT compiled method.
6676 kJitTableAddress,
6677
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006678 // Load using a simple runtime call. This is the fall-back load kind when
6679 // the codegen is unable to use another appropriate kind.
6680 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006681
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006682 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006683 };
6684
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006685 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006686 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006687 const DexFile& dex_file,
6688 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006689 : HInstruction(kLoadString,
6690 DataType::Type::kReference,
6691 SideEffectsForArchRuntimeCalls(),
6692 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006693 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006694 string_index_(string_index),
6695 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006696 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006697 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006698
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006699 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006700
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006701 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006702
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006703 LoadKind GetLoadKind() const {
6704 return GetPackedField<LoadKindField>();
6705 }
6706
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006707 bool HasPcRelativeLoadKind() const {
6708 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6709 GetLoadKind() == LoadKind::kBootImageRelRo ||
6710 GetLoadKind() == LoadKind::kBssEntry;
6711 }
6712
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006713 const DexFile& GetDexFile() const {
6714 return dex_file_;
6715 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006716
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006717 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006718 return string_index_;
6719 }
6720
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006721 Handle<mirror::String> GetString() const {
6722 return string_;
6723 }
6724
6725 void SetString(Handle<mirror::String> str) {
6726 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006727 }
6728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006729 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006730
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006731 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006732
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006733 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006734
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006735 // Will call the runtime if we need to load the string through
6736 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006737 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006738 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006739 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006740 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006741 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006742 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006743 return false;
6744 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006745 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006746 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006748 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006749 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006750 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006752 bool CanBeNull() const override { return false; }
6753 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006754
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006755 static SideEffects SideEffectsForArchRuntimeCalls() {
6756 return SideEffects::CanTriggerGC();
6757 }
6758
Vladimir Marko372f10e2016-05-17 16:30:10 +01006759 void AddSpecialInput(HInstruction* special_input);
6760
6761 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006762 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006763 return ArrayRef<HUserRecord<HInstruction*>>(
6764 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006765 }
6766
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006767 DECLARE_INSTRUCTION(LoadString);
6768
Artem Serovcced8ba2017-07-19 18:18:09 +01006769 protected:
6770 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6771
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006772 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006773 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006774 static constexpr size_t kFieldLoadKindSize =
6775 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6776 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006777 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006778 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006779
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006780 void SetLoadKindInternal(LoadKind load_kind);
6781
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006782 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006783 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006784 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006785 HUserRecord<HInstruction*> special_input_;
6786
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006787 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006788 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006789
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006790 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006791};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006792std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6793
6794// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006795inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6796 // The load kind should be determined before inserting the instruction to the graph.
6797 DCHECK(GetBlock() == nullptr);
6798 DCHECK(GetEnvironment() == nullptr);
6799 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6800 SetPackedField<LoadKindField>(load_kind);
6801 if (load_kind != LoadKind::kRuntimeCall) {
6802 special_input_ = HUserRecord<HInstruction*>(nullptr);
6803 }
6804 if (!NeedsEnvironment()) {
6805 SetSideEffects(SideEffects::None());
6806 }
6807}
6808
6809// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006810inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006811 // The special input is used for PC-relative loads on some architectures,
6812 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006813 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006814 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006815 GetLoadKind() == LoadKind::kBssEntry ||
6816 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006817 // HLoadString::GetInputRecords() returns an empty array at this point,
6818 // so use the GetInputRecords() from the base class to set the input record.
6819 DCHECK(special_input_.GetInstruction() == nullptr);
6820 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006821 special_input->AddUseAt(this, 0);
6822}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006823
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006824class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006825 public:
6826 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006827 uint16_t method_handle_idx,
6828 const DexFile& dex_file,
6829 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006830 : HInstruction(kLoadMethodHandle,
6831 DataType::Type::kReference,
6832 SideEffectsForArchRuntimeCalls(),
6833 dex_pc),
6834 special_input_(HUserRecord<HInstruction*>(current_method)),
6835 method_handle_idx_(method_handle_idx),
6836 dex_file_(dex_file) {
6837 }
6838
6839 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006840 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006841 return ArrayRef<HUserRecord<HInstruction*>>(
6842 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6843 }
6844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006845 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006846
6847 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6848
6849 const DexFile& GetDexFile() const { return dex_file_; }
6850
6851 static SideEffects SideEffectsForArchRuntimeCalls() {
6852 return SideEffects::CanTriggerGC();
6853 }
6854
6855 DECLARE_INSTRUCTION(LoadMethodHandle);
6856
6857 protected:
6858 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6859
6860 private:
6861 // The special input is the HCurrentMethod for kRuntimeCall.
6862 HUserRecord<HInstruction*> special_input_;
6863
6864 const uint16_t method_handle_idx_;
6865 const DexFile& dex_file_;
6866};
6867
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006868class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006869 public:
6870 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006871 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006872 const DexFile& dex_file,
6873 uint32_t dex_pc)
6874 : HInstruction(kLoadMethodType,
6875 DataType::Type::kReference,
6876 SideEffectsForArchRuntimeCalls(),
6877 dex_pc),
6878 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006879 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006880 dex_file_(dex_file) {
6881 }
6882
6883 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006884 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006885 return ArrayRef<HUserRecord<HInstruction*>>(
6886 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6887 }
6888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006889 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006890
Orion Hodson06d10a72018-05-14 08:53:38 +01006891 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006892
6893 const DexFile& GetDexFile() const { return dex_file_; }
6894
6895 static SideEffects SideEffectsForArchRuntimeCalls() {
6896 return SideEffects::CanTriggerGC();
6897 }
6898
6899 DECLARE_INSTRUCTION(LoadMethodType);
6900
6901 protected:
6902 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6903
6904 private:
6905 // The special input is the HCurrentMethod for kRuntimeCall.
6906 HUserRecord<HInstruction*> special_input_;
6907
Orion Hodson06d10a72018-05-14 08:53:38 +01006908 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006909 const DexFile& dex_file_;
6910};
6911
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006912/**
6913 * Performs an initialization check on its Class object input.
6914 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006915class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006916 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006917 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006918 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306919 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006920 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006921 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006922 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006923 SetRawInputAt(0, constant);
6924 }
Artem Serova6e26142018-06-19 14:55:17 +01006925 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006926 bool CanBeMoved() const override { return true; }
6927 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006928 return true;
6929 }
6930
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006931 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006932 // May call runtime to initialize the class.
6933 return true;
6934 }
6935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006936 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006937
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006938 HLoadClass* GetLoadClass() const {
6939 DCHECK(InputAt(0)->IsLoadClass());
6940 return InputAt(0)->AsLoadClass();
6941 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006942
6943 DECLARE_INSTRUCTION(ClinitCheck);
6944
Artem Serovcced8ba2017-07-19 18:18:09 +01006945
6946 protected:
6947 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006948};
6949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006950class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006951 public:
6952 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006953 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006954 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006955 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006956 bool is_volatile,
6957 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006958 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006959 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006960 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306961 : HExpression(kStaticFieldGet,
6962 field_type,
6963 SideEffects::FieldReadOfType(field_type, is_volatile),
6964 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006965 field_info_(field,
6966 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006967 field_type,
6968 is_volatile,
6969 field_idx,
6970 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006971 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006972 SetRawInputAt(0, cls);
6973 }
6974
Calin Juravle52c48962014-12-16 17:02:57 +00006975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006976 bool IsClonable() const override { return true; }
6977 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006979 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006980 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006981 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006982 }
6983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006984 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006985 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6986 }
6987
Calin Juravle52c48962014-12-16 17:02:57 +00006988 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006989 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006990 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006991 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006992
Vladimir Marko61b92282017-10-11 13:23:17 +01006993 void SetType(DataType::Type new_type) {
6994 DCHECK(DataType::IsIntegralType(GetType()));
6995 DCHECK(DataType::IsIntegralType(new_type));
6996 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6997 SetPackedField<TypeField>(new_type);
6998 }
6999
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007000 DECLARE_INSTRUCTION(StaticFieldGet);
7001
Artem Serovcced8ba2017-07-19 18:18:09 +01007002 protected:
7003 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
7004
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007005 private:
7006 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007007};
7008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007009class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007010 public:
7011 HStaticFieldSet(HInstruction* cls,
7012 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007013 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007014 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00007015 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01007016 bool is_volatile,
7017 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007018 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07007019 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01007020 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007021 : HExpression(kStaticFieldSet,
7022 SideEffects::FieldWriteOfType(field_type, is_volatile),
7023 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007024 field_info_(field,
7025 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007026 field_type,
7027 is_volatile,
7028 field_idx,
7029 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007030 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007031 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007032 SetRawInputAt(0, cls);
7033 SetRawInputAt(1, value);
7034 }
7035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007036 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00007037 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007038 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007039 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007040 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007041
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00007042 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007043 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
7044 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00007045
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007046 DECLARE_INSTRUCTION(StaticFieldSet);
7047
Artem Serovcced8ba2017-07-19 18:18:09 +01007048 protected:
7049 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
7050
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007051 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007052 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
7053 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
7054 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
7055 "Too many packed fields.");
7056
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007057 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007058};
7059
Vladimir Marko552a1342017-10-31 10:56:47 +00007060class HStringBuilderAppend final : public HVariableInputSizeInstruction {
7061 public:
7062 HStringBuilderAppend(HIntConstant* format,
7063 uint32_t number_of_arguments,
7064 ArenaAllocator* allocator,
7065 uint32_t dex_pc)
7066 : HVariableInputSizeInstruction(
7067 kStringBuilderAppend,
7068 DataType::Type::kReference,
7069 // The runtime call may read memory from inputs. It never writes outside
7070 // of the newly allocated result object (or newly allocated helper objects).
7071 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
7072 dex_pc,
7073 allocator,
7074 number_of_arguments + /* format */ 1u,
7075 kArenaAllocInvokeInputs) {
7076 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
7077 SetRawInputAt(FormatIndex(), format);
7078 }
7079
7080 void SetArgumentAt(size_t index, HInstruction* argument) {
7081 DCHECK_LE(index, GetNumberOfArguments());
7082 SetRawInputAt(index, argument);
7083 }
7084
7085 // Return the number of arguments, excluding the format.
7086 size_t GetNumberOfArguments() const {
7087 DCHECK_GE(InputCount(), 1u);
7088 return InputCount() - 1u;
7089 }
7090
7091 size_t FormatIndex() const {
7092 return GetNumberOfArguments();
7093 }
7094
7095 HIntConstant* GetFormat() {
7096 return InputAt(FormatIndex())->AsIntConstant();
7097 }
7098
7099 bool NeedsEnvironment() const override { return true; }
7100
7101 bool CanThrow() const override { return true; }
7102
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007103 bool CanBeNull() const override { return false; }
7104
Vladimir Marko552a1342017-10-31 10:56:47 +00007105 DECLARE_INSTRUCTION(StringBuilderAppend);
7106
7107 protected:
7108 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7109};
7110
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007111class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007112 public:
7113 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007114 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007115 uint32_t field_index,
7116 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307117 : HExpression(kUnresolvedInstanceFieldGet,
7118 field_type,
7119 SideEffects::AllExceptGCDependency(),
7120 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007121 field_index_(field_index) {
7122 SetRawInputAt(0, obj);
7123 }
7124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007125 bool IsClonable() const override { return true; }
7126 bool NeedsEnvironment() const override { return true; }
7127 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007129 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007130 uint32_t GetFieldIndex() const { return field_index_; }
7131
7132 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7133
Artem Serovcced8ba2017-07-19 18:18:09 +01007134 protected:
7135 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7136
Calin Juravlee460d1d2015-09-29 04:52:17 +01007137 private:
7138 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007139};
7140
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007141class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007142 public:
7143 HUnresolvedInstanceFieldSet(HInstruction* obj,
7144 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007145 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007146 uint32_t field_index,
7147 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007148 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007149 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007150 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007151 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007152 SetRawInputAt(0, obj);
7153 SetRawInputAt(1, value);
7154 }
7155
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007156 bool IsClonable() const override { return true; }
7157 bool NeedsEnvironment() const override { return true; }
7158 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007160 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007161 uint32_t GetFieldIndex() const { return field_index_; }
7162
7163 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7164
Artem Serovcced8ba2017-07-19 18:18:09 +01007165 protected:
7166 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7167
Calin Juravlee460d1d2015-09-29 04:52:17 +01007168 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007169 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7170 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007171 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007172 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7173 kFieldFieldType + kFieldFieldTypeSize;
7174 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7175 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007176 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007177
Calin Juravlee460d1d2015-09-29 04:52:17 +01007178 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007179};
7180
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007181class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007182 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007183 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007184 uint32_t field_index,
7185 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307186 : HExpression(kUnresolvedStaticFieldGet,
7187 field_type,
7188 SideEffects::AllExceptGCDependency(),
7189 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007190 field_index_(field_index) {
7191 }
7192
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007193 bool IsClonable() const override { return true; }
7194 bool NeedsEnvironment() const override { return true; }
7195 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007197 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007198 uint32_t GetFieldIndex() const { return field_index_; }
7199
7200 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7201
Artem Serovcced8ba2017-07-19 18:18:09 +01007202 protected:
7203 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7204
Calin Juravlee460d1d2015-09-29 04:52:17 +01007205 private:
7206 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007207};
7208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007209class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007210 public:
7211 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007212 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007213 uint32_t field_index,
7214 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007215 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007216 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007217 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007218 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007219 SetRawInputAt(0, value);
7220 }
7221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007222 bool IsClonable() const override { return true; }
7223 bool NeedsEnvironment() const override { return true; }
7224 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007226 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007227 uint32_t GetFieldIndex() const { return field_index_; }
7228
7229 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7230
Artem Serovcced8ba2017-07-19 18:18:09 +01007231 protected:
7232 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7233
Calin Juravlee460d1d2015-09-29 04:52:17 +01007234 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007235 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7236 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007237 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007238 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7239 kFieldFieldType + kFieldFieldTypeSize;
7240 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7241 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007242 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007243
Calin Juravlee460d1d2015-09-29 04:52:17 +01007244 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007245};
7246
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007247// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007248class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007249 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007250 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307251 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7252 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007253
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007254 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007255
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007256 DECLARE_INSTRUCTION(LoadException);
7257
Artem Serovcced8ba2017-07-19 18:18:09 +01007258 protected:
7259 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007260};
7261
David Brazdilcb1c0552015-08-04 16:22:25 +01007262// Implicit part of move-exception which clears thread-local exception storage.
7263// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007264class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007265 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007266 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007267 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307268 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007269
7270 DECLARE_INSTRUCTION(ClearException);
7271
Artem Serovcced8ba2017-07-19 18:18:09 +01007272 protected:
7273 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007274};
7275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007276class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007277 public:
7278 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007279 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007280 SetRawInputAt(0, exception);
7281 }
7282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007283 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007284
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007285 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007286
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007287 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007288
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007289 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007290
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007291 DECLARE_INSTRUCTION(Throw);
7292
Artem Serovcced8ba2017-07-19 18:18:09 +01007293 protected:
7294 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007295};
7296
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007297/**
7298 * Implementation strategies for the code generator of a HInstanceOf
7299 * or `HCheckCast`.
7300 */
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007301enum class TypeCheckKind { // private marker to avoid generate-operator-out.py from processing.
Calin Juravle98893e12015-10-02 21:05:03 +01007302 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007303 kExactCheck, // Can do a single class compare.
7304 kClassHierarchyCheck, // Can just walk the super class chain.
7305 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7306 kInterfaceCheck, // No optimization yet when checking against an interface.
7307 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007308 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007309 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007310 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007311};
7312
Roland Levillain86503782016-02-11 19:07:30 +00007313std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7314
Vladimir Marko175e7862018-03-27 09:03:13 +00007315// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7316// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7317class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007318 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007319 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007320 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007321 HInstruction* object,
7322 HInstruction* target_class_or_null,
7323 TypeCheckKind check_kind,
7324 Handle<mirror::Class> klass,
7325 uint32_t dex_pc,
7326 ArenaAllocator* allocator,
7327 HIntConstant* bitstring_path_to_root,
7328 HIntConstant* bitstring_mask,
7329 SideEffects side_effects)
7330 : HVariableInputSizeInstruction(
7331 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007332 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007333 side_effects,
7334 dex_pc,
7335 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007336 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007337 kArenaAllocTypeCheckInputs),
7338 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007339 SetPackedField<TypeCheckKindField>(check_kind);
7340 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007341 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007342 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007343 SetRawInputAt(1, target_class_or_null);
7344 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7345 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7346 if (check_kind == TypeCheckKind::kBitstringCheck) {
7347 DCHECK(target_class_or_null->IsNullConstant());
7348 SetRawInputAt(2, bitstring_path_to_root);
7349 SetRawInputAt(3, bitstring_mask);
7350 } else {
7351 DCHECK(target_class_or_null->IsLoadClass());
7352 }
Vladimir Marko87584542017-12-12 17:47:52 +00007353 }
7354
7355 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007356 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007357 HInstruction* load_class = InputAt(1);
7358 DCHECK(load_class->IsLoadClass());
7359 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007360 }
7361
Vladimir Marko175e7862018-03-27 09:03:13 +00007362 uint32_t GetBitstringPathToRoot() const {
7363 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7364 HInstruction* path_to_root = InputAt(2);
7365 DCHECK(path_to_root->IsIntConstant());
7366 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7367 }
7368
7369 uint32_t GetBitstringMask() const {
7370 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7371 HInstruction* mask = InputAt(3);
7372 DCHECK(mask->IsIntConstant());
7373 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7374 }
7375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007376 bool IsClonable() const override { return true; }
7377 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007378
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007379 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007380 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7381 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007382 }
7383
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007384 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7385 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7386 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7387 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7388
Vladimir Marko175e7862018-03-27 09:03:13 +00007389 ReferenceTypeInfo GetTargetClassRTI() {
7390 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7391 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007392 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007393 } else {
7394 return ReferenceTypeInfo::CreateInvalid();
7395 }
7396 }
7397
7398 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7399 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7400 DCHECK(klass_ != nullptr);
7401 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7402 }
7403
7404 Handle<mirror::Class> GetClass() const {
7405 return klass_;
7406 }
7407
7408 protected:
7409 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7410
7411 private:
7412 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7413 static constexpr size_t kFieldTypeCheckKindSize =
7414 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7415 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7416 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7417 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7418 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7419 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7420
7421 Handle<mirror::Class> klass_;
7422};
7423
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007424class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007425 public:
7426 HInstanceOf(HInstruction* object,
7427 HInstruction* target_class_or_null,
7428 TypeCheckKind check_kind,
7429 Handle<mirror::Class> klass,
7430 uint32_t dex_pc,
7431 ArenaAllocator* allocator,
7432 HIntConstant* bitstring_path_to_root,
7433 HIntConstant* bitstring_mask)
7434 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007435 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007436 object,
7437 target_class_or_null,
7438 check_kind,
7439 klass,
7440 dex_pc,
7441 allocator,
7442 bitstring_path_to_root,
7443 bitstring_mask,
7444 SideEffectsForArchRuntimeCalls(check_kind)) {}
7445
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007446 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007448 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007449 return CanCallRuntime(GetTypeCheckKind());
7450 }
7451
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007452 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007453 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007454 return check_kind != TypeCheckKind::kExactCheck;
7455 }
7456
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007457 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007458 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007459 }
7460
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007461 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007462
Artem Serovcced8ba2017-07-19 18:18:09 +01007463 protected:
7464 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007465};
7466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007467class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007468 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007469 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307470 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007471 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7472 SetPackedFlag<kFlagUpperCanBeNull>(true);
7473 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007474 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007475 SetRawInputAt(0, input);
7476 }
7477
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007478 bool InstructionDataEquals(const HInstruction* other) const override;
7479 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007480
David Brazdilf5552582015-12-27 13:36:12 +00007481 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007482 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007483 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007484 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007485
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007486 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007487 DCHECK(GetUpperCanBeNull() || !can_be_null);
7488 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007489 }
7490
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007491 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007492
Calin Juravleb1498f62015-02-16 13:13:29 +00007493 DECLARE_INSTRUCTION(BoundType);
7494
Artem Serovcced8ba2017-07-19 18:18:09 +01007495 protected:
7496 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7497
Calin Juravleb1498f62015-02-16 13:13:29 +00007498 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007499 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7500 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007501 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007502 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7503 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7504 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7505
Calin Juravleb1498f62015-02-16 13:13:29 +00007506 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007507 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7508 // It is used to bound the type in cases like:
7509 // if (x instanceof ClassX) {
7510 // // uper_bound_ will be ClassX
7511 // }
David Brazdilf5552582015-12-27 13:36:12 +00007512 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007513};
7514
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007515class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007516 public:
7517 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007518 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007519 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007520 Handle<mirror::Class> klass,
7521 uint32_t dex_pc,
7522 ArenaAllocator* allocator,
7523 HIntConstant* bitstring_path_to_root,
7524 HIntConstant* bitstring_mask)
7525 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007526 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007527 object,
7528 target_class_or_null,
7529 check_kind,
7530 klass,
7531 dex_pc,
7532 allocator,
7533 bitstring_path_to_root,
7534 bitstring_mask,
7535 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007536
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007537 bool IsClonable() const override { return true; }
7538 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007539 // Instruction may throw a CheckCastError.
7540 return true;
7541 }
7542
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007543 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007544
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007545 DECLARE_INSTRUCTION(CheckCast);
7546
Artem Serovcced8ba2017-07-19 18:18:09 +01007547 protected:
7548 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007549};
7550
Andreas Gampe26de38b2016-07-27 17:53:11 -07007551/**
7552 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7553 * @details We define the combined barrier types that are actually required
7554 * by the Java Memory Model, rather than using exactly the terminology from
7555 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7556 * primitives. Note that the JSR-133 cookbook generally does not deal with
7557 * store atomicity issues, and the recipes there are not always entirely sufficient.
7558 * The current recipe is as follows:
7559 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7560 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7561 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7562 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7563 * class has final fields.
7564 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7565 * store-to-memory instructions. Only generated together with non-temporal stores.
7566 */
7567enum MemBarrierKind {
7568 kAnyStore,
7569 kLoadAny,
7570 kStoreStore,
7571 kAnyAny,
7572 kNTStoreStore,
7573 kLastBarrierKind = kNTStoreStore
7574};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007575std::ostream& operator<<(std::ostream& os, MemBarrierKind kind);
Andreas Gampe26de38b2016-07-27 17:53:11 -07007576
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007577class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007578 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007579 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007580 : HExpression(kMemoryBarrier,
7581 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7582 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007583 SetPackedField<BarrierKindField>(barrier_kind);
7584 }
Calin Juravle27df7582015-04-17 19:12:31 +01007585
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007586 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007587
Vladimir Markoa1de9182016-02-25 11:37:38 +00007588 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007589
7590 DECLARE_INSTRUCTION(MemoryBarrier);
7591
Artem Serovcced8ba2017-07-19 18:18:09 +01007592 protected:
7593 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7594
Calin Juravle27df7582015-04-17 19:12:31 +01007595 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007596 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7597 static constexpr size_t kFieldBarrierKindSize =
7598 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7599 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7600 kFieldBarrierKind + kFieldBarrierKindSize;
7601 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7602 "Too many packed fields.");
7603 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007604};
7605
Igor Murashkind01745e2017-04-05 16:40:31 -07007606// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7607// the specified object(s), with respect to any subsequent store that might "publish"
7608// (i.e. make visible) the specified object to another thread.
7609//
7610// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7611// for all final fields (that were set) at the end of the invoked constructor.
7612//
7613// The constructor fence models the freeze actions for the final fields of an object
7614// being constructed (semantically at the end of the constructor). Constructor fences
7615// have a per-object affinity; two separate objects being constructed get two separate
7616// constructor fences.
7617//
7618// (Note: that if calling a super-constructor or forwarding to another constructor,
7619// the freezes would happen at the end of *that* constructor being invoked).
7620//
7621// The memory model guarantees that when the object being constructed is "published" after
7622// constructor completion (i.e. escapes the current thread via a store), then any final field
7623// writes must be observable on other threads (once they observe that publication).
7624//
7625// Further, anything written before the freeze, and read by dereferencing through the final field,
7626// must also be visible (so final object field could itself have an object with non-final fields;
7627// yet the freeze must also extend to them).
7628//
7629// Constructor example:
7630//
7631// class HasFinal {
7632// final int field; Optimizing IR for <init>()V:
7633// HasFinal() {
7634// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7635// // freeze(this.field); HConstructorFence(this)
7636// } HReturn
7637// }
7638//
7639// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7640// already-initialized classes; in that case the allocation must act as a "default-initializer"
7641// of the object which effectively writes the class pointer "final field".
7642//
7643// For example, we can model default-initialiation as roughly the equivalent of the following:
7644//
7645// class Object {
7646// private final Class header;
7647// }
7648//
7649// Java code: Optimizing IR:
7650//
7651// T new_instance<T>() {
7652// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7653// obj.header = T.class; // header write is done by above call.
7654// // freeze(obj.header) HConstructorFence(obj)
7655// return (T)obj;
7656// }
7657//
7658// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007659// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007660// * QuasiAtomic::ThreadFenceForConstructor
7661//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007662class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007663 // A fence has variable inputs because the inputs can be removed
7664 // after prepare_for_register_allocation phase.
7665 // (TODO: In the future a fence could freeze multiple objects
7666 // after merging two fences together.)
7667 public:
7668 // `fence_object` is the reference that needs to be protected for correct publication.
7669 //
7670 // It makes sense in the following situations:
7671 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7672 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7673 //
7674 // After construction the `fence_object` becomes the 0th input.
7675 // This is not an input in a real sense, but just a convenient place to stash the information
7676 // about the associated object.
7677 HConstructorFence(HInstruction* fence_object,
7678 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007679 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007680 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7681 // constraints described in the class header. We claim that these SideEffects constraints
7682 // enforce a superset of the real constraints.
7683 //
7684 // The ordering described above is conservatively modeled with SideEffects as follows:
7685 //
7686 // * To prevent reordering of the publication stores:
7687 // ----> "Reads of objects" is the initial SideEffect.
7688 // * For every primitive final field store in the constructor:
7689 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7690 // * If there are any stores to reference final fields in the constructor:
7691 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7692 // that are reachable from `fence_object` also need to be prevented for reordering
7693 // (and we do not want to do alias analysis to figure out what those stores are).
7694 //
7695 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7696 // even more conservative approach than the one described above, and prevents all of the
7697 // above reordering without analyzing any of the instructions in the constructor.
7698 //
7699 // If in a later phase we discover that there are no writes to reference final fields,
7700 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307701 : HVariableInputSizeInstruction(kConstructorFence,
7702 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007703 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007704 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007705 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007706 kArenaAllocConstructorFenceInputs) {
7707 DCHECK(fence_object != nullptr);
7708 SetRawInputAt(0, fence_object);
7709 }
7710
7711 // The object associated with this constructor fence.
7712 //
7713 // (Note: This will be null after the prepare_for_register_allocation phase,
7714 // as all constructor fence inputs are removed there).
7715 HInstruction* GetFenceObject() const {
7716 return InputAt(0);
7717 }
7718
7719 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7720 // - Delete `fence_use` from `this`'s use list.
7721 // - Delete `this` from `fence_use`'s inputs list.
7722 // - If the `fence_use` is dead, remove it from the graph.
7723 //
7724 // A fence is considered dead once it no longer has any uses
7725 // and all of the inputs are dead.
7726 //
7727 // This must *not* be called during/after prepare_for_register_allocation,
7728 // because that removes all the inputs to the fences but the fence is actually
7729 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007730 //
7731 // Returns how many HConstructorFence instructions were removed from graph.
7732 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007733
Igor Murashkindd018df2017-08-09 10:38:31 -07007734 // Combine all inputs of `this` and `other` instruction and remove
7735 // `other` from the graph.
7736 //
7737 // Inputs are unique after the merge.
7738 //
7739 // Requirement: `this` must not be the same as `other.
7740 void Merge(HConstructorFence* other);
7741
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007742 // Check if this constructor fence is protecting
7743 // an HNewInstance or HNewArray that is also the immediate
7744 // predecessor of `this`.
7745 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007746 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7747 // to be one of the inputs of `this`.
7748 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007749 // Returns the associated HNewArray or HNewInstance,
7750 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007751 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007752
Igor Murashkind01745e2017-04-05 16:40:31 -07007753 DECLARE_INSTRUCTION(ConstructorFence);
7754
Artem Serovcced8ba2017-07-19 18:18:09 +01007755 protected:
7756 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007757};
7758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007759class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007760 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007761 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007762 kEnter,
7763 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007764 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007765 };
7766
7767 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007768 : HExpression(kMonitorOperation,
7769 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7770 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007771 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007772 SetRawInputAt(0, object);
7773 }
7774
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007775 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007776 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007778 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007779 // Verifier guarantees that monitor-exit cannot throw.
7780 // This is important because it allows the HGraphBuilder to remove
7781 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7782 return IsEnter();
7783 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007784
Vladimir Markoa1de9182016-02-25 11:37:38 +00007785 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7786 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007787
7788 DECLARE_INSTRUCTION(MonitorOperation);
7789
Artem Serovcced8ba2017-07-19 18:18:09 +01007790 protected:
7791 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7792
Calin Juravle52c48962014-12-16 17:02:57 +00007793 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007794 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7795 static constexpr size_t kFieldOperationKindSize =
7796 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7797 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7798 kFieldOperationKind + kFieldOperationKindSize;
7799 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7800 "Too many packed fields.");
7801 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007802};
7803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007804class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007805 public:
7806 HSelect(HInstruction* condition,
7807 HInstruction* true_value,
7808 HInstruction* false_value,
7809 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307810 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007811 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7812
7813 // First input must be `true_value` or `false_value` to allow codegens to
7814 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7815 // that architectures which implement HSelect as a conditional move also
7816 // will not need to invert the condition.
7817 SetRawInputAt(0, false_value);
7818 SetRawInputAt(1, true_value);
7819 SetRawInputAt(2, condition);
7820 }
7821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007822 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007823 HInstruction* GetFalseValue() const { return InputAt(0); }
7824 HInstruction* GetTrueValue() const { return InputAt(1); }
7825 HInstruction* GetCondition() const { return InputAt(2); }
7826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007827 bool CanBeMoved() const override { return true; }
7828 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007829 return true;
7830 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007832 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007833 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7834 }
7835
7836 DECLARE_INSTRUCTION(Select);
7837
Artem Serovcced8ba2017-07-19 18:18:09 +01007838 protected:
7839 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007840};
7841
Vladimir Markof9f64412015-09-02 14:05:49 +01007842class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007843 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007844 MoveOperands(Location source,
7845 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007846 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007847 HInstruction* instruction)
7848 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007849
7850 Location GetSource() const { return source_; }
7851 Location GetDestination() const { return destination_; }
7852
7853 void SetSource(Location value) { source_ = value; }
7854 void SetDestination(Location value) { destination_ = value; }
7855
7856 // The parallel move resolver marks moves as "in-progress" by clearing the
7857 // destination (but not the source).
7858 Location MarkPending() {
7859 DCHECK(!IsPending());
7860 Location dest = destination_;
7861 destination_ = Location::NoLocation();
7862 return dest;
7863 }
7864
7865 void ClearPending(Location dest) {
7866 DCHECK(IsPending());
7867 destination_ = dest;
7868 }
7869
7870 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007871 DCHECK(source_.IsValid() || destination_.IsInvalid());
7872 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007873 }
7874
7875 // True if this blocks a move from the given location.
7876 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007877 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007878 }
7879
7880 // A move is redundant if it's been eliminated, if its source and
7881 // destination are the same, or if its destination is unneeded.
7882 bool IsRedundant() const {
7883 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7884 }
7885
7886 // We clear both operands to indicate move that's been eliminated.
7887 void Eliminate() {
7888 source_ = destination_ = Location::NoLocation();
7889 }
7890
7891 bool IsEliminated() const {
7892 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7893 return source_.IsInvalid();
7894 }
7895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007896 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007897
Nicolas Geoffray90218252015-04-15 11:56:51 +01007898 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007899 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007900 }
7901
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007902 HInstruction* GetInstruction() const { return instruction_; }
7903
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007904 private:
7905 Location source_;
7906 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007907 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007908 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007909 // The instruction this move is assocatied with. Null when this move is
7910 // for moving an input in the expected locations of user (including a phi user).
7911 // This is only used in debug mode, to ensure we do not connect interval siblings
7912 // in the same parallel move.
7913 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007914};
7915
Roland Levillainc9285912015-12-18 10:38:42 +00007916std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7917
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007918static constexpr size_t kDefaultNumberOfMoves = 4;
7919
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007920class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007921 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007922 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007923 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007924 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007925 moves_.reserve(kDefaultNumberOfMoves);
7926 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007927
Nicolas Geoffray90218252015-04-15 11:56:51 +01007928 void AddMove(Location source,
7929 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007930 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007931 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007932 DCHECK(source.IsValid());
7933 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007934 if (kIsDebugBuild) {
7935 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007936 for (const MoveOperands& move : moves_) {
7937 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007938 // Special case the situation where the move is for the spill slot
7939 // of the instruction.
7940 if ((GetPrevious() == instruction)
7941 || ((GetPrevious() == nullptr)
7942 && instruction->IsPhi()
7943 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007944 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007945 << "Doing parallel moves for the same instruction.";
7946 } else {
7947 DCHECK(false) << "Doing parallel moves for the same instruction.";
7948 }
7949 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007950 }
7951 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007952 for (const MoveOperands& move : moves_) {
7953 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007954 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007955 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007956 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007957 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007958 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007959 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007960 }
7961
Vladimir Marko225b6462015-09-28 12:17:40 +01007962 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007963 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007964 }
7965
Vladimir Marko225b6462015-09-28 12:17:40 +01007966 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007967
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007968 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007969
Artem Serovcced8ba2017-07-19 18:18:09 +01007970 protected:
7971 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7972
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007973 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007974 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007975};
7976
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007977// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7978// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7979// never used across anything that can trigger GC.
7980// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7981// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007982class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007983 public:
7984 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307985 : HExpression(kIntermediateAddress,
7986 DataType::Type::kInt32,
7987 SideEffects::DependsOnGC(),
7988 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007989 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7990 DataType::Size(DataType::Type::kReference))
7991 << "kPrimInt and kPrimNot have different sizes.";
7992 SetRawInputAt(0, base_address);
7993 SetRawInputAt(1, offset);
7994 }
7995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007996 bool IsClonable() const override { return true; }
7997 bool CanBeMoved() const override { return true; }
7998 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007999 return true;
8000 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008001 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008002
8003 HInstruction* GetBaseAddress() const { return InputAt(0); }
8004 HInstruction* GetOffset() const { return InputAt(1); }
8005
8006 DECLARE_INSTRUCTION(IntermediateAddress);
8007
Artem Serovcced8ba2017-07-19 18:18:09 +01008008 protected:
8009 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008010};
8011
8012
Mark Mendell0616ae02015-04-17 12:49:27 -04008013} // namespace art
8014
Aart Bikf8f5a162017-02-06 15:35:29 -08008015#include "nodes_vector.h"
8016
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008017#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
8018#include "nodes_shared.h"
8019#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05308020#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04008021#include "nodes_x86.h"
8022#endif
8023
Vladimir Marko0a516052019-10-14 13:00:44 +00008024namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04008025
Igor Murashkin6ef45672017-08-08 13:59:55 -07008026class OptimizingCompilerStats;
8027
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008028class HGraphVisitor : public ValueObject {
8029 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008030 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8031 : stats_(stats),
8032 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07008033 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008034
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008035 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008036 virtual void VisitBasicBlock(HBasicBlock* block);
8037
Roland Levillain633021e2014-10-01 14:12:25 +01008038 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008039 void VisitInsertionOrder();
8040
Roland Levillain633021e2014-10-01 14:12:25 +01008041 // Visit the graph following dominator tree reverse post-order.
8042 void VisitReversePostOrder();
8043
Nicolas Geoffray787c3072014-03-17 10:20:19 +00008044 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008045
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008046 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008047#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008048 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
8049
8050 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8051
8052#undef DECLARE_VISIT_INSTRUCTION
8053
Igor Murashkin6ef45672017-08-08 13:59:55 -07008054 protected:
8055 OptimizingCompilerStats* stats_;
8056
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008057 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07008058 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008059
8060 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
8061};
8062
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008063class HGraphDelegateVisitor : public HGraphVisitor {
8064 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008065 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8066 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008067 virtual ~HGraphDelegateVisitor() {}
8068
8069 // Visit functions that delegate to to super class.
8070#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008071 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008072
8073 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8074
8075#undef DECLARE_VISIT_INSTRUCTION
8076
8077 private:
8078 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
8079};
8080
Artem Serovcced8ba2017-07-19 18:18:09 +01008081// Create a clone of the instruction, insert it into the graph; replace the old one with a new
8082// and remove the old instruction.
8083HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
8084
8085// Create a clone for each clonable instructions/phis and replace the original with the clone.
8086//
8087// Used for testing individual instruction cloner.
8088class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
8089 public:
8090 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01008091 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01008092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008093 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01008094 if (instruction->IsClonable()) {
8095 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01008096 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01008097 }
8098 }
8099
Artem Serov7f4aff62017-06-21 17:02:18 +01008100 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008101
8102 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008103 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008104
8105 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8106};
8107
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008108// Iterator over the blocks that art part of the loop. Includes blocks part
8109// of an inner loop. The order in which the blocks are iterated is on their
8110// block id.
8111class HBlocksInLoopIterator : public ValueObject {
8112 public:
8113 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8114 : blocks_in_loop_(info.GetBlocks()),
8115 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8116 index_(0) {
8117 if (!blocks_in_loop_.IsBitSet(index_)) {
8118 Advance();
8119 }
8120 }
8121
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008122 bool Done() const { return index_ == blocks_.size(); }
8123 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008124 void Advance() {
8125 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008126 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008127 if (blocks_in_loop_.IsBitSet(index_)) {
8128 break;
8129 }
8130 }
8131 }
8132
8133 private:
8134 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008135 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008136 size_t index_;
8137
8138 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8139};
8140
Mingyao Yang3584bce2015-05-19 16:01:59 -07008141// Iterator over the blocks that art part of the loop. Includes blocks part
8142// of an inner loop. The order in which the blocks are iterated is reverse
8143// post order.
8144class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8145 public:
8146 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8147 : blocks_in_loop_(info.GetBlocks()),
8148 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8149 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008150 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008151 Advance();
8152 }
8153 }
8154
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008155 bool Done() const { return index_ == blocks_.size(); }
8156 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008157 void Advance() {
8158 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008159 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8160 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008161 break;
8162 }
8163 }
8164 }
8165
8166 private:
8167 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008168 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008169 size_t index_;
8170
8171 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8172};
8173
Aart Bikf3e61ee2017-04-12 17:09:20 -07008174// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008175inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008176 if (constant->IsIntConstant()) {
8177 return constant->AsIntConstant()->GetValue();
8178 } else if (constant->IsLongConstant()) {
8179 return constant->AsLongConstant()->GetValue();
8180 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008181 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008182 return 0;
8183 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008184}
8185
Aart Bikf3e61ee2017-04-12 17:09:20 -07008186// Returns true iff instruction is an integral constant (and sets value on success).
8187inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8188 if (instruction->IsIntConstant()) {
8189 *value = instruction->AsIntConstant()->GetValue();
8190 return true;
8191 } else if (instruction->IsLongConstant()) {
8192 *value = instruction->AsLongConstant()->GetValue();
8193 return true;
8194 } else if (instruction->IsNullConstant()) {
8195 *value = 0;
8196 return true;
8197 }
8198 return false;
8199}
8200
Aart Bik0148de42017-09-05 09:25:01 -07008201// Returns true iff instruction is the given integral constant.
8202inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8203 int64_t val = 0;
8204 return IsInt64AndGet(instruction, &val) && val == value;
8205}
8206
8207// Returns true iff instruction is a zero bit pattern.
8208inline bool IsZeroBitPattern(HInstruction* instruction) {
8209 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8210}
8211
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008212// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008213#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008214 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8215 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8216#undef INSTRUCTION_TYPE_CHECK
8217
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008218// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008219#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8220 std::is_base_of<BaseType, H##type>::value,
8221#define INSTRUCTION_TYPE_CHECK(type, super) \
8222 inline bool HInstruction::Is##type() const { \
8223 DCHECK_LT(GetKind(), kLastInstructionKind); \
8224 using BaseType = H##type; \
8225 static constexpr bool results[] = { \
8226 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8227 }; \
8228 return results[static_cast<size_t>(GetKind())]; \
8229 }
8230
8231 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8232#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008233#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008234
8235#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008236 inline const H##type* HInstruction::As##type() const { \
8237 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8238 } \
8239 inline H##type* HInstruction::As##type() { \
8240 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8241 }
8242
Vladimir Markoa90dd512018-05-04 15:04:45 +01008243 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8244#undef INSTRUCTION_TYPE_CAST
8245
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008246
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008247// Create space in `blocks` for adding `number_of_new_blocks` entries
8248// starting at location `at`. Blocks after `at` are moved accordingly.
8249inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8250 size_t number_of_new_blocks,
8251 size_t after) {
8252 DCHECK_LT(after, blocks->size());
8253 size_t old_size = blocks->size();
8254 size_t new_size = old_size + number_of_new_blocks;
8255 blocks->resize(new_size);
8256 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8257}
8258
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008259/*
8260 * Hunt "under the hood" of array lengths (leading to array references),
8261 * null checks (also leading to array references), and new arrays
8262 * (leading to the actual length). This makes it more likely related
8263 * instructions become actually comparable.
8264 */
8265inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8266 while (instruction->IsArrayLength() ||
8267 instruction->IsNullCheck() ||
8268 instruction->IsNewArray()) {
8269 instruction = instruction->IsNewArray()
8270 ? instruction->AsNewArray()->GetLength()
8271 : instruction->InputAt(0);
8272 }
8273 return instruction;
8274}
8275
Artem Serovb47b9782019-12-04 21:02:09 +00008276inline bool IsAddOrSub(const HInstruction* instruction) {
8277 return instruction->IsAdd() || instruction->IsSub();
8278}
8279
Artem Serov21c7e6f2017-07-27 16:04:42 +01008280void RemoveEnvironmentUses(HInstruction* instruction);
8281bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8282void ResetEnvironmentInputRecords(HInstruction* instruction);
8283
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01008284// Detects an instruction that is >= 0. As long as the value is carried by
8285// a single instruction, arithmetic wrap-around cannot occur.
8286bool IsGEZero(HInstruction* instruction);
8287
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008288} // namespace art
8289
8290#endif // ART_COMPILER_OPTIMIZING_NODES_H_