blob: 80d4a2f39568045d779e56f3045390a605035bf2 [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
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004375class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004376 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004377 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004378
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004379 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004380 SetRawInputAt(index, argument);
4381 }
4382
Roland Levillain3e3d7332015-04-28 11:00:54 +01004383 // Return the number of arguments. This number can be lower than
4384 // the number of inputs returned by InputCount(), as some invoke
4385 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4386 // inputs at the end of their list of inputs.
4387 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4388
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004389 InvokeType GetInvokeType() const {
4390 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004391 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004392
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004393 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004394 return intrinsic_;
4395 }
4396
Aart Bik5d75afe2015-12-14 11:57:01 -08004397 void SetIntrinsic(Intrinsics intrinsic,
4398 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4399 IntrinsicSideEffects side_effects,
4400 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004401
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004402 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004403 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004404 }
4405
Aart Bikff7d89c2016-11-07 08:49:28 -08004406 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4407
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004408 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004409
Aart Bika8b8e9b2018-01-09 11:01:02 -08004410 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4411
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004412 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004413
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004414 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004415
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004416 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004417 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4418 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004419
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004420 uint32_t* GetIntrinsicOptimizations() {
4421 return &intrinsic_optimizations_;
4422 }
4423
4424 const uint32_t* GetIntrinsicOptimizations() const {
4425 return &intrinsic_optimizations_;
4426 }
4427
4428 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4429
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004430 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004431 void SetResolvedMethod(ArtMethod* method);
4432
4433 MethodReference GetMethodReference() const { return method_reference_; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004434
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004435 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004436
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004437 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004438 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4439 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004440 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004441 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004442 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4443 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004444 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004445 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004446
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304447 HInvoke(InstructionKind kind,
4448 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004449 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004450 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004451 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004452 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004453 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004454 ArtMethod* resolved_method,
4455 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004456 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304457 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004458 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004459 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4460 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004461 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004462 number_of_arguments + number_of_other_inputs,
4463 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004464 number_of_arguments_(number_of_arguments),
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004465 method_reference_(method_reference),
agicsaki57b81ec2015-08-11 17:39:37 -07004466 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004467 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004468 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004469 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004470 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004471 }
4472
Artem Serovcced8ba2017-07-19 18:18:09 +01004473 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4474
Roland Levillain3e3d7332015-04-28 11:00:54 +01004475 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004476 ArtMethod* resolved_method_;
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004477 const MethodReference method_reference_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004478 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004479
4480 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4481 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004482};
4483
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004484class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004485 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004486 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004487 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004488 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004489 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004490 MethodReference method_reference,
Calin Juravle175dc732015-08-25 15:42:32 +01004491 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304492 : HInvoke(kInvokeUnresolved,
4493 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004494 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004495 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004496 return_type,
4497 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004498 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004499 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004500 invoke_type) {
4501 }
4502
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004503 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004504
Calin Juravle175dc732015-08-25 15:42:32 +01004505 DECLARE_INSTRUCTION(InvokeUnresolved);
4506
Artem Serovcced8ba2017-07-19 18:18:09 +01004507 protected:
4508 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004509};
4510
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004511class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004512 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004513 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004514 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004515 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004516 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004517 MethodReference method_reference,
Andra Danciub931b8c2020-07-27 13:16:37 +00004518 // resolved_method is the ArtMethod object corresponding to the polymorphic
4519 // method (e.g. VarHandle.get), resolved using the class linker. It is needed
4520 // to pass intrinsic information to the HInvokePolymorphic node.
Andra Danciu73c31802020-09-01 13:17:05 +00004521 ArtMethod* resolved_method,
4522 dex::ProtoIndex proto_idx)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304523 : HInvoke(kInvokePolymorphic,
4524 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004525 number_of_arguments,
Andra Danciu49a19f32020-08-27 12:44:25 +00004526 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004527 return_type,
4528 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004529 method_reference,
Andra Danciua0130e82020-07-23 12:34:56 +00004530 resolved_method,
Andra Danciu73c31802020-09-01 13:17:05 +00004531 kPolymorphic),
4532 proto_idx_(proto_idx) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304533 }
Orion Hodsonac141392017-01-13 11:53:47 +00004534
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004535 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004536
Andra Danciu73c31802020-09-01 13:17:05 +00004537 dex::ProtoIndex GetProtoIndex() { return proto_idx_; }
4538
Orion Hodsonac141392017-01-13 11:53:47 +00004539 DECLARE_INSTRUCTION(InvokePolymorphic);
4540
Artem Serovcced8ba2017-07-19 18:18:09 +01004541 protected:
Andra Danciu73c31802020-09-01 13:17:05 +00004542 dex::ProtoIndex proto_idx_;
Artem Serovcced8ba2017-07-19 18:18:09 +01004543 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004544};
4545
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004546class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004547 public:
4548 HInvokeCustom(ArenaAllocator* allocator,
4549 uint32_t number_of_arguments,
4550 uint32_t call_site_index,
4551 DataType::Type return_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004552 uint32_t dex_pc,
4553 MethodReference method_reference)
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004554 : HInvoke(kInvokeCustom,
4555 allocator,
4556 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004557 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004558 return_type,
4559 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004560 method_reference,
Andreas Gampe3db70682018-12-26 15:12:03 -08004561 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004562 kStatic),
4563 call_site_index_(call_site_index) {
4564 }
4565
4566 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4567
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004568 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004569
4570 DECLARE_INSTRUCTION(InvokeCustom);
4571
4572 protected:
4573 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4574
4575 private:
4576 uint32_t call_site_index_;
4577};
4578
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004579class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004580 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004581 // Requirements of this method call regarding the class
4582 // initialization (clinit) check of its declaring class.
Vladimir Marko9974e3c2020-06-10 16:27:06 +01004583 enum class ClinitCheckRequirement { // private marker to avoid generate-operator-out.py from processing.
Roland Levillain4c0eb422015-04-24 16:43:49 +01004584 kNone, // Class already initialized.
4585 kExplicit, // Static call having explicit clinit check as last input.
4586 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004587 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004588 };
4589
Vladimir Marko58155012015-08-19 12:49:41 +00004590 // Determines how to load the target ArtMethod*.
4591 enum class MethodLoadKind {
4592 // Use a String init ArtMethod* loaded from Thread entrypoints.
4593 kStringInit,
4594
4595 // Use the method's own ArtMethod* loaded by the register allocator.
4596 kRecursive,
4597
Vladimir Marko65979462017-05-19 17:25:12 +01004598 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4599 // Used for boot image methods referenced by boot image code.
4600 kBootImageLinkTimePcRelative,
4601
Vladimir Markob066d432018-01-03 13:14:37 +00004602 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4603 // Used for app->boot calls with relocatable image.
4604 kBootImageRelRo,
4605
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004606 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004607 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004608 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004609
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004610 // Use ArtMethod* at a known address, embed the direct address in the code.
4611 // Used for for JIT-compiled calls.
4612 kJitDirectAddress,
4613
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004614 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4615 // used when other kinds are unimplemented on a particular architecture.
4616 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004617 };
4618
4619 // Determines the location of the code pointer.
4620 enum class CodePtrLocation {
4621 // Recursive call, use local PC-relative call instruction.
4622 kCallSelf,
4623
Vladimir Marko86c87522020-05-11 16:55:55 +01004624 // Use native pointer from the Artmethod*.
4625 // Used for @CriticalNative to avoid going through the compiled stub. This call goes through
4626 // a special resolution stub if the class is not initialized or no native code is registered.
4627 kCallCriticalNative,
4628
Vladimir Marko58155012015-08-19 12:49:41 +00004629 // Use code pointer from the ArtMethod*.
4630 // Used when we don't know the target code. This is also the last-resort-kind used when
4631 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4632 kCallArtMethod,
4633 };
4634
4635 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004636 MethodLoadKind method_load_kind;
4637 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004638 // The method load data holds
4639 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4640 // Note that there are multiple string init methods, each having its own offset.
4641 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004642 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004643 };
4644
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004645 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004646 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004647 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004648 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004649 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004650 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004651 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004652 InvokeType invoke_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004653 MethodReference resolved_method_reference,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004654 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304655 : HInvoke(kInvokeStaticOrDirect,
4656 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004657 number_of_arguments,
Vladimir Marko86c87522020-05-11 16:55:55 +01004658 // There is potentially one extra argument for the HCurrentMethod input,
4659 // and one other if the clinit check is explicit. These can be removed later.
4660 (NeedsCurrentMethodInput(dispatch_info) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004661 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004662 return_type,
4663 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004664 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004665 resolved_method,
4666 invoke_type),
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004667 resolved_method_reference_(resolved_method_reference),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004668 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004669 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4670 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004672 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004673
Vladimir Marko86c87522020-05-11 16:55:55 +01004674 void SetDispatchInfo(DispatchInfo dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004675 bool had_current_method_input = HasCurrentMethodInput();
Vladimir Marko86c87522020-05-11 16:55:55 +01004676 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info);
Vladimir Markob554b5a2015-11-06 12:57:55 +00004677
4678 // Using the current method is the default and once we find a better
4679 // method load kind, we should not go back to using the current method.
4680 DCHECK(had_current_method_input || !needs_current_method_input);
4681
4682 if (had_current_method_input && !needs_current_method_input) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004683 DCHECK_EQ(InputAt(GetCurrentMethodIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4684 RemoveInputAt(GetCurrentMethodIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004685 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004686 dispatch_info_ = dispatch_info;
4687 }
4688
Aart Bik6daebeb2017-04-03 14:35:41 -07004689 DispatchInfo GetDispatchInfo() const {
4690 return dispatch_info_;
4691 }
4692
Vladimir Marko372f10e2016-05-17 16:30:10 +01004693 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004694 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004695 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4696 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4697 DCHECK(!input_records.empty());
4698 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4699 HInstruction* last_input = input_records.back().GetInstruction();
4700 // Note: `last_input` may be null during arguments setup.
4701 if (last_input != nullptr) {
4702 // `last_input` is the last input of a static invoke marked as having
4703 // an explicit clinit check. It must either be:
4704 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4705 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4706 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4707 }
4708 }
4709 return input_records;
4710 }
4711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004712 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Vladimir Marko86c87522020-05-11 16:55:55 +01004713 // We do not access the method via object reference, so we cannot do an implicit null check.
Calin Juravle77520bc2015-01-12 18:45:46 +00004714 // TODO: for intrinsics we can generate implicit null checks.
4715 return false;
4716 }
4717
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004718 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004719 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004720 }
4721
Vladimir Marko58155012015-08-19 12:49:41 +00004722 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4723 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4724 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004725 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004726 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004727 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004728 bool HasPcRelativeMethodLoadKind() const {
4729 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004730 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004731 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004732 }
Vladimir Marko58155012015-08-19 12:49:41 +00004733
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004734 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004735 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004736 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004737 }
4738
4739 uint64_t GetMethodAddress() const {
4740 DCHECK(HasMethodAddress());
4741 return dispatch_info_.method_load_data;
4742 }
4743
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004744 const DexFile& GetDexFileForPcRelativeDexCache() const;
4745
Vladimir Markoa1de9182016-02-25 11:37:38 +00004746 ClinitCheckRequirement GetClinitCheckRequirement() const {
4747 return GetPackedField<ClinitCheckRequirementField>();
4748 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004749
Roland Levillain4c0eb422015-04-24 16:43:49 +01004750 // Is this instruction a call to a static method?
4751 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004752 return GetInvokeType() == kStatic;
4753 }
4754
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004755 const MethodReference GetResolvedMethodReference() const {
4756 return resolved_method_reference_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004757 }
4758
Vladimir Marko86c87522020-05-11 16:55:55 +01004759 // Does this method load kind need the current method as an input?
4760 static bool NeedsCurrentMethodInput(DispatchInfo dispatch_info) {
4761 return dispatch_info.method_load_kind == MethodLoadKind::kRecursive ||
4762 dispatch_info.method_load_kind == MethodLoadKind::kRuntimeCall ||
4763 dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative;
4764 }
4765
4766 // Get the index of the current method input.
4767 size_t GetCurrentMethodIndex() const {
4768 DCHECK(HasCurrentMethodInput());
4769 return GetCurrentMethodIndexUnchecked();
4770 }
4771 size_t GetCurrentMethodIndexUnchecked() const {
4772 return GetNumberOfArguments();
4773 }
4774
4775 // Check if the method has a current method input.
4776 bool HasCurrentMethodInput() const {
4777 if (NeedsCurrentMethodInput(GetDispatchInfo())) {
4778 DCHECK(InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4779 InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4780 return true;
4781 } else {
4782 DCHECK(InputCount() == GetCurrentMethodIndexUnchecked() ||
4783 InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4784 !InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4785 return false;
4786 }
4787 }
4788
4789 // Get the index of the special input.
4790 size_t GetSpecialInputIndex() const {
4791 DCHECK(HasSpecialInput());
4792 return GetSpecialInputIndexUnchecked();
4793 }
4794 size_t GetSpecialInputIndexUnchecked() const {
4795 return GetNumberOfArguments() + (HasCurrentMethodInput() ? 1u : 0u);
4796 }
4797
4798 // Check if the method has a special input.
4799 bool HasSpecialInput() const {
4800 size_t other_inputs =
4801 GetSpecialInputIndexUnchecked() + (IsStaticWithExplicitClinitCheck() ? 1u : 0u);
4802 size_t input_count = InputCount();
4803 DCHECK_LE(input_count - other_inputs, 1u) << other_inputs << " " << input_count;
4804 return other_inputs != input_count;
4805 }
4806
4807 void AddSpecialInput(HInstruction* input) {
4808 // We allow only one special input.
4809 DCHECK(!HasSpecialInput());
4810 InsertInputAt(GetSpecialInputIndexUnchecked(), input);
4811 }
4812
Vladimir Markofbb184a2015-11-13 14:47:00 +00004813 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4814 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4815 // instruction; only relevant for static calls with explicit clinit check.
4816 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004817 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004818 size_t last_input_index = inputs_.size() - 1u;
4819 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004820 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004821 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004822 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004823 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004824 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004825 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004826 }
4827
4828 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004829 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004830 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004831 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004832 }
4833
4834 // Is this a call to a static method whose declaring class has an
4835 // implicit intialization check requirement?
4836 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004837 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004838 }
4839
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004840 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004841
Artem Serovcced8ba2017-07-19 18:18:09 +01004842 protected:
4843 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4844
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004845 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004846 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004847 static constexpr size_t kFieldClinitCheckRequirementSize =
4848 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4849 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4850 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4851 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4852 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004853 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4854 kFieldClinitCheckRequirement,
4855 kFieldClinitCheckRequirementSize>;
4856
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004857 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004858 const MethodReference resolved_method_reference_;
Vladimir Marko58155012015-08-19 12:49:41 +00004859 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004860};
Vladimir Markof64242a2015-12-01 14:58:23 +00004861std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Marko86c87522020-05-11 16:55:55 +01004862std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::CodePtrLocation rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004863std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004864
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004865class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004866 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004867 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004868 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004869 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004870 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004871 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004872 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004873 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304874 : HInvoke(kInvokeVirtual,
4875 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004876 number_of_arguments,
4877 0u,
4878 return_type,
4879 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004880 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004881 resolved_method,
4882 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304883 vtable_index_(vtable_index) {
4884 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004886 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004888 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004889 switch (GetIntrinsic()) {
4890 case Intrinsics::kThreadCurrentThread:
4891 case Intrinsics::kStringBufferAppend:
4892 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004893 case Intrinsics::kStringBuilderAppendObject:
4894 case Intrinsics::kStringBuilderAppendString:
4895 case Intrinsics::kStringBuilderAppendCharSequence:
4896 case Intrinsics::kStringBuilderAppendCharArray:
4897 case Intrinsics::kStringBuilderAppendBoolean:
4898 case Intrinsics::kStringBuilderAppendChar:
4899 case Intrinsics::kStringBuilderAppendInt:
4900 case Intrinsics::kStringBuilderAppendLong:
4901 case Intrinsics::kStringBuilderAppendFloat:
4902 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004903 case Intrinsics::kStringBuilderToString:
4904 return false;
4905 default:
4906 return HInvoke::CanBeNull();
4907 }
4908 }
4909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004910 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004911 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004912 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004913 }
4914
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004915 uint32_t GetVTableIndex() const { return vtable_index_; }
4916
4917 DECLARE_INSTRUCTION(InvokeVirtual);
4918
Artem Serovcced8ba2017-07-19 18:18:09 +01004919 protected:
4920 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4921
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004922 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004923 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004924 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004925};
4926
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004927class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004928 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004929 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004930 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004931 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004932 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004933 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004934 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004935 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304936 : HInvoke(kInvokeInterface,
4937 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004938 number_of_arguments,
4939 0u,
4940 return_type,
4941 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004942 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004943 resolved_method,
4944 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304945 imt_index_(imt_index) {
4946 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004947
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004948 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004950 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004951 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004952 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004953 }
4954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004955 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004956 // The assembly stub currently needs it.
4957 return true;
4958 }
4959
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004960 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004961
4962 DECLARE_INSTRUCTION(InvokeInterface);
4963
Artem Serovcced8ba2017-07-19 18:18:09 +01004964 protected:
4965 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4966
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004967 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004968 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004969 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004970};
4971
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004972class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004973 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004974 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304975 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004976 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004977 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004978
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004979 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004981 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004982 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004983 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004984 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004985 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004986 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004987 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004988 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4989 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004990 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004991 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4992 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004993
Roland Levillain88cb1752014-10-20 16:36:47 +01004994 DECLARE_INSTRUCTION(Neg);
4995
Artem Serovcced8ba2017-07-19 18:18:09 +01004996 protected:
4997 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004998};
4999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005000class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005001 public:
Vladimir Markob5461632018-10-15 14:24:21 +01005002 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305003 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005004 SetRawInputAt(0, cls);
5005 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01005006 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005007 }
5008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005009 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005010
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005011 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005012 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005013
Mingyao Yang0c365e62015-03-31 15:09:29 -07005014 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005015 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07005016
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005017 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005018
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005019 HLoadClass* GetLoadClass() const {
5020 DCHECK(InputAt(0)->IsLoadClass());
5021 return InputAt(0)->AsLoadClass();
5022 }
5023
5024 HInstruction* GetLength() const {
5025 return InputAt(1);
5026 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00005027
Vladimir Markob5461632018-10-15 14:24:21 +01005028 size_t GetComponentSizeShift() {
5029 return GetPackedField<ComponentSizeShiftField>();
5030 }
5031
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005032 DECLARE_INSTRUCTION(NewArray);
5033
Artem Serovcced8ba2017-07-19 18:18:09 +01005034 protected:
5035 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01005036
5037 private:
5038 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
5039 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
5040 static constexpr size_t kNumberOfNewArrayPackedBits =
5041 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
5042 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5043 using ComponentSizeShiftField =
5044 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005045};
5046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005047class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005048 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005049 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005050 HInstruction* left,
5051 HInstruction* right,
5052 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305053 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5054 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005056 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005057
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005058 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005060 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005061 return GetBlock()->GetGraph()->GetIntConstant(
5062 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005063 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005064 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005065 return GetBlock()->GetGraph()->GetLongConstant(
5066 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005067 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005068 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005069 return GetBlock()->GetGraph()->GetFloatConstant(
5070 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5071 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005072 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005073 return GetBlock()->GetGraph()->GetDoubleConstant(
5074 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5075 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005076
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005077 DECLARE_INSTRUCTION(Add);
5078
Artem Serovcced8ba2017-07-19 18:18:09 +01005079 protected:
5080 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005081};
5082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005083class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005084 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005085 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005086 HInstruction* left,
5087 HInstruction* right,
5088 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305089 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5090 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005091
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005092 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005093
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005094 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005095 return GetBlock()->GetGraph()->GetIntConstant(
5096 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005097 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005098 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005099 return GetBlock()->GetGraph()->GetLongConstant(
5100 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005101 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005102 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005103 return GetBlock()->GetGraph()->GetFloatConstant(
5104 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5105 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005106 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005107 return GetBlock()->GetGraph()->GetDoubleConstant(
5108 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5109 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005110
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005111 DECLARE_INSTRUCTION(Sub);
5112
Artem Serovcced8ba2017-07-19 18:18:09 +01005113 protected:
5114 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005115};
5116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005117class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005118 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005119 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005120 HInstruction* left,
5121 HInstruction* right,
5122 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305123 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5124 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005126 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005127
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005128 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005130 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005131 return GetBlock()->GetGraph()->GetIntConstant(
5132 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005133 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005134 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005135 return GetBlock()->GetGraph()->GetLongConstant(
5136 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005137 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005138 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005139 return GetBlock()->GetGraph()->GetFloatConstant(
5140 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5141 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005142 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005143 return GetBlock()->GetGraph()->GetDoubleConstant(
5144 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5145 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005146
5147 DECLARE_INSTRUCTION(Mul);
5148
Artem Serovcced8ba2017-07-19 18:18:09 +01005149 protected:
5150 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005151};
5152
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005153class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005154 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005155 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005156 HInstruction* left,
5157 HInstruction* right,
5158 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305159 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5160 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005161
Roland Levillain9867bc72015-08-05 10:21:34 +01005162 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005163 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005164 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005165 // Our graph structure ensures we never have 0 for `y` during
5166 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005167 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005168 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005169 return (y == -1) ? -x : x / y;
5170 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005171
Roland Levillain31dd3d62016-02-16 12:21:02 +00005172 template <typename T>
5173 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005174 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005175 return x / y;
5176 }
5177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005178 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005179 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005180 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005181 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005182 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005183 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005184 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5185 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005186 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005187 return GetBlock()->GetGraph()->GetFloatConstant(
5188 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5189 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005190 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005191 return GetBlock()->GetGraph()->GetDoubleConstant(
5192 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005193 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005194
5195 DECLARE_INSTRUCTION(Div);
5196
Artem Serovcced8ba2017-07-19 18:18:09 +01005197 protected:
5198 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005199};
5200
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005201class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005202 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005203 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005204 HInstruction* left,
5205 HInstruction* right,
5206 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305207 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5208 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005209
Roland Levillain9867bc72015-08-05 10:21:34 +01005210 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005211 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005212 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005213 // Our graph structure ensures we never have 0 for `y` during
5214 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005215 DCHECK_NE(y, 0);
5216 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5217 return (y == -1) ? 0 : x % y;
5218 }
5219
Roland Levillain31dd3d62016-02-16 12:21:02 +00005220 template <typename T>
5221 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005222 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005223 return std::fmod(x, y);
5224 }
5225
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005226 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005227 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005228 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005229 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005230 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005231 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005232 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005233 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005234 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005235 return GetBlock()->GetGraph()->GetFloatConstant(
5236 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5237 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005238 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005239 return GetBlock()->GetGraph()->GetDoubleConstant(
5240 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5241 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005242
5243 DECLARE_INSTRUCTION(Rem);
5244
Artem Serovcced8ba2017-07-19 18:18:09 +01005245 protected:
5246 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005247};
5248
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005249class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005250 public:
5251 HMin(DataType::Type result_type,
5252 HInstruction* left,
5253 HInstruction* right,
5254 uint32_t dex_pc)
5255 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005257 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005258
5259 // Evaluation for integral values.
5260 template <typename T> static T ComputeIntegral(T x, T y) {
5261 return (x <= y) ? x : y;
5262 }
5263
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005264 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005265 return GetBlock()->GetGraph()->GetIntConstant(
5266 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5267 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005268 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005269 return GetBlock()->GetGraph()->GetLongConstant(
5270 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5271 }
5272 // TODO: Evaluation for floating-point values.
5273 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005274 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005275 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005276 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005277
5278 DECLARE_INSTRUCTION(Min);
5279
5280 protected:
5281 DEFAULT_COPY_CONSTRUCTOR(Min);
5282};
5283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005285 public:
5286 HMax(DataType::Type result_type,
5287 HInstruction* left,
5288 HInstruction* right,
5289 uint32_t dex_pc)
5290 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005292 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005293
5294 // Evaluation for integral values.
5295 template <typename T> static T ComputeIntegral(T x, T y) {
5296 return (x >= y) ? x : y;
5297 }
5298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005299 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005300 return GetBlock()->GetGraph()->GetIntConstant(
5301 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5302 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005303 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005304 return GetBlock()->GetGraph()->GetLongConstant(
5305 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5306 }
5307 // TODO: Evaluation for floating-point values.
5308 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005309 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005310 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005311 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005312
5313 DECLARE_INSTRUCTION(Max);
5314
5315 protected:
5316 DEFAULT_COPY_CONSTRUCTOR(Max);
5317};
5318
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005319class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005320 public:
5321 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5322 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5323
5324 // Evaluation for integral values.
5325 template <typename T> static T ComputeIntegral(T x) {
5326 return x < 0 ? -x : x;
5327 }
5328
5329 // Evaluation for floating-point values.
5330 // Note, as a "quality of implementation", rather than pure "spec compliance",
5331 // we require that Math.abs() clears the sign bit (but changes nothing else)
5332 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5333 // http://b/30758343
5334 template <typename T, typename S> static T ComputeFP(T x) {
5335 S bits = bit_cast<S, T>(x);
5336 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5337 }
5338
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005339 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005340 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5341 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005342 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005343 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5344 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005345 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005346 return GetBlock()->GetGraph()->GetFloatConstant(
5347 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5348 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005349 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005350 return GetBlock()->GetGraph()->GetDoubleConstant(
5351 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5352 }
5353
5354 DECLARE_INSTRUCTION(Abs);
5355
5356 protected:
5357 DEFAULT_COPY_CONSTRUCTOR(Abs);
5358};
5359
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005360class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005361 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005362 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005363 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5364 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005365 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005366 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005367 SetRawInputAt(0, value);
5368 }
5369
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005370 bool IsClonable() const override { return true; }
5371 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005372
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005373 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005374 return true;
5375 }
5376
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005377 bool NeedsEnvironment() const override { return true; }
5378 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005379
Calin Juravled0d48522014-11-04 16:40:20 +00005380 DECLARE_INSTRUCTION(DivZeroCheck);
5381
Artem Serovcced8ba2017-07-19 18:18:09 +01005382 protected:
5383 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005384};
5385
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005386class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005387 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005388 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005389 HInstruction* value,
5390 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005391 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305392 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005393 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5394 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005395 }
5396
Roland Levillain5b5b9312016-03-22 14:57:31 +00005397 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005398 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005399 return value << (distance & max_shift_distance);
5400 }
5401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005402 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005403 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005404 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005405 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005406 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005407 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005408 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005409 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005410 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005411 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005412 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5413 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005414 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005415 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005416 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005417 LOG(FATAL) << DebugName() << " is not defined for float values";
5418 UNREACHABLE();
5419 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005420 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005421 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005422 LOG(FATAL) << DebugName() << " is not defined for double values";
5423 UNREACHABLE();
5424 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005425
5426 DECLARE_INSTRUCTION(Shl);
5427
Artem Serovcced8ba2017-07-19 18:18:09 +01005428 protected:
5429 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005430};
5431
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005432class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005433 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005434 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005435 HInstruction* value,
5436 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005437 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305438 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005439 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5440 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005441 }
5442
Roland Levillain5b5b9312016-03-22 14:57:31 +00005443 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005444 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005445 return value >> (distance & max_shift_distance);
5446 }
5447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005448 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005449 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005450 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005451 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005452 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005453 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005454 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005455 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005456 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005457 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005458 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5459 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005460 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005461 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005463 LOG(FATAL) << DebugName() << " is not defined for float values";
5464 UNREACHABLE();
5465 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005466 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005467 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005468 LOG(FATAL) << DebugName() << " is not defined for double values";
5469 UNREACHABLE();
5470 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005471
5472 DECLARE_INSTRUCTION(Shr);
5473
Artem Serovcced8ba2017-07-19 18:18:09 +01005474 protected:
5475 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005476};
5477
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005478class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005479 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005481 HInstruction* value,
5482 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005483 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305484 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005485 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5486 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005487 }
5488
Roland Levillain5b5b9312016-03-22 14:57:31 +00005489 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005490 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005491 typedef typename std::make_unsigned<T>::type V;
5492 V ux = static_cast<V>(value);
5493 return static_cast<T>(ux >> (distance & max_shift_distance));
5494 }
5495
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005496 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005497 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005498 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005499 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005500 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005501 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005502 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005503 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005504 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005505 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005506 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5507 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005508 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005509 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005510 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005511 LOG(FATAL) << DebugName() << " is not defined for float values";
5512 UNREACHABLE();
5513 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005514 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005515 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005516 LOG(FATAL) << DebugName() << " is not defined for double values";
5517 UNREACHABLE();
5518 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005519
5520 DECLARE_INSTRUCTION(UShr);
5521
Artem Serovcced8ba2017-07-19 18:18:09 +01005522 protected:
5523 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005524};
5525
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005526class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005527 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005528 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005529 HInstruction* left,
5530 HInstruction* right,
5531 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305532 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5533 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005534
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005535 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005536
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005537 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005538
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005539 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005540 return GetBlock()->GetGraph()->GetIntConstant(
5541 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005542 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005543 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005544 return GetBlock()->GetGraph()->GetLongConstant(
5545 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005546 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005547 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005548 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005549 LOG(FATAL) << DebugName() << " is not defined for float values";
5550 UNREACHABLE();
5551 }
5552 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005553 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005554 LOG(FATAL) << DebugName() << " is not defined for double values";
5555 UNREACHABLE();
5556 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005557
5558 DECLARE_INSTRUCTION(And);
5559
Artem Serovcced8ba2017-07-19 18:18:09 +01005560 protected:
5561 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005562};
5563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005564class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005565 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005566 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005567 HInstruction* left,
5568 HInstruction* right,
5569 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305570 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5571 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005572
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005573 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005574
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005575 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005576
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005577 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005578 return GetBlock()->GetGraph()->GetIntConstant(
5579 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005580 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005581 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005582 return GetBlock()->GetGraph()->GetLongConstant(
5583 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005584 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005585 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005586 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005587 LOG(FATAL) << DebugName() << " is not defined for float values";
5588 UNREACHABLE();
5589 }
5590 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005591 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005592 LOG(FATAL) << DebugName() << " is not defined for double values";
5593 UNREACHABLE();
5594 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005595
5596 DECLARE_INSTRUCTION(Or);
5597
Artem Serovcced8ba2017-07-19 18:18:09 +01005598 protected:
5599 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005600};
5601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005602class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005603 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005604 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005605 HInstruction* left,
5606 HInstruction* right,
5607 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305608 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5609 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005610
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005611 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005612
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005613 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005614
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005615 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005616 return GetBlock()->GetGraph()->GetIntConstant(
5617 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005618 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005619 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005620 return GetBlock()->GetGraph()->GetLongConstant(
5621 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005622 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005623 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005624 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005625 LOG(FATAL) << DebugName() << " is not defined for float values";
5626 UNREACHABLE();
5627 }
5628 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005629 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005630 LOG(FATAL) << DebugName() << " is not defined for double values";
5631 UNREACHABLE();
5632 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005633
5634 DECLARE_INSTRUCTION(Xor);
5635
Artem Serovcced8ba2017-07-19 18:18:09 +01005636 protected:
5637 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005638};
5639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005640class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005641 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005642 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305643 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005644 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005645
Roland Levillain5b5b9312016-03-22 14:57:31 +00005646 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005647 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005648 typedef typename std::make_unsigned<T>::type V;
5649 V ux = static_cast<V>(value);
5650 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005651 return static_cast<T>(ux);
5652 } else {
5653 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005654 return static_cast<T>(ux >> (distance & max_shift_value)) |
5655 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005656 }
5657 }
5658
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005659 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005660 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005661 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005662 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005663 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005664 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005665 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005666 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005667 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005668 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005669 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5670 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005671 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005672 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005673 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005674 LOG(FATAL) << DebugName() << " is not defined for float values";
5675 UNREACHABLE();
5676 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005677 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005678 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005679 LOG(FATAL) << DebugName() << " is not defined for double values";
5680 UNREACHABLE();
5681 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005682
5683 DECLARE_INSTRUCTION(Ror);
5684
Artem Serovcced8ba2017-07-19 18:18:09 +01005685 protected:
5686 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005687};
5688
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005689// The value of a parameter in this method. Its location depends on
5690// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005691class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005692 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005693 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005694 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005695 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005696 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005697 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305698 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005699 dex_file_(dex_file),
5700 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005701 index_(index) {
5702 SetPackedFlag<kFlagIsThis>(is_this);
5703 SetPackedFlag<kFlagCanBeNull>(!is_this);
5704 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005705
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005706 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005707 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005708 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005709 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005710
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005711 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005713
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005714 DECLARE_INSTRUCTION(ParameterValue);
5715
Artem Serovcced8ba2017-07-19 18:18:09 +01005716 protected:
5717 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5718
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005719 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005720 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005721 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005722 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5723 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5724 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5725 "Too many packed fields.");
5726
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005727 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005728 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005729 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005730 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005731 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005732};
5733
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005734class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005735 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005736 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305737 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5738 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005739
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005740 bool CanBeMoved() const override { return true; }
5741 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005742 return true;
5743 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005744
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005745 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005747 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005748 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005749 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005750 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005751 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005752 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005753 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005754 LOG(FATAL) << DebugName() << " is not defined for float values";
5755 UNREACHABLE();
5756 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005757 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005758 LOG(FATAL) << DebugName() << " is not defined for double values";
5759 UNREACHABLE();
5760 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005761
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005762 DECLARE_INSTRUCTION(Not);
5763
Artem Serovcced8ba2017-07-19 18:18:09 +01005764 protected:
5765 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005766};
5767
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005768class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005769 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005770 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305771 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5772 }
David Brazdil66d126e2015-04-03 16:02:44 +01005773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005774 bool CanBeMoved() const override { return true; }
5775 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005776 return true;
5777 }
5778
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005779 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005780 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005781 return !x;
5782 }
5783
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005784 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005785 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005786 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005787 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005788 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005789 UNREACHABLE();
5790 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005791 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005792 LOG(FATAL) << DebugName() << " is not defined for float values";
5793 UNREACHABLE();
5794 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005795 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005796 LOG(FATAL) << DebugName() << " is not defined for double values";
5797 UNREACHABLE();
5798 }
David Brazdil66d126e2015-04-03 16:02:44 +01005799
5800 DECLARE_INSTRUCTION(BooleanNot);
5801
Artem Serovcced8ba2017-07-19 18:18:09 +01005802 protected:
5803 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005804};
5805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005806class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005807 public:
5808 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005809 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305810 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005811 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005812 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005813 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005814 }
5815
5816 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005817 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5818 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005820 bool IsClonable() const override { return true; }
5821 bool CanBeMoved() const override { return true; }
5822 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005823 return true;
5824 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005825 // Return whether the conversion is implicit. This includes conversion to the same type.
5826 bool IsImplicitConversion() const {
5827 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5828 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005829
Mark Mendelle82549b2015-05-06 10:55:34 -04005830 // Try to statically evaluate the conversion and return a HConstant
5831 // containing the result. If the input cannot be converted, return nullptr.
5832 HConstant* TryStaticEvaluation() const;
5833
Roland Levillaindff1f282014-11-05 14:15:05 +00005834 DECLARE_INSTRUCTION(TypeConversion);
5835
Artem Serovcced8ba2017-07-19 18:18:09 +01005836 protected:
5837 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005838};
5839
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005840static constexpr uint32_t kNoRegNumber = -1;
5841
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005842class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005843 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005844 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005845 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5846 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005847 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005848 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005849 SetRawInputAt(0, value);
5850 }
5851
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 {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005855 return true;
5856 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005857
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005858 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005860 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005862 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005863
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005864 DECLARE_INSTRUCTION(NullCheck);
5865
Artem Serovcced8ba2017-07-19 18:18:09 +01005866 protected:
5867 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005868};
5869
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005870// Embeds an ArtField and all the information required by the compiler. We cache
5871// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005872class FieldInfo : public ValueObject {
5873 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005874 FieldInfo(ArtField* field,
5875 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005876 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005877 bool is_volatile,
5878 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005879 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005880 const DexFile& dex_file)
5881 : field_(field),
5882 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005883 field_type_(field_type),
5884 is_volatile_(is_volatile),
5885 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005886 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005887 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005888
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005889 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005890 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005891 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005892 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005893 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005894 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005895 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005896
5897 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005898 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005899 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005900 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005901 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005902 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005903 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005904 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005905};
5906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005907class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005908 public:
5909 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005910 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005911 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005912 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005913 bool is_volatile,
5914 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005915 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005916 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005917 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305918 : HExpression(kInstanceFieldGet,
5919 field_type,
5920 SideEffects::FieldReadOfType(field_type, is_volatile),
5921 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005922 field_info_(field,
5923 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005924 field_type,
5925 is_volatile,
5926 field_idx,
5927 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005928 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005929 SetRawInputAt(0, value);
5930 }
5931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005932 bool IsClonable() const override { return true; }
5933 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005934
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005935 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005936 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005937 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005938 }
5939
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005940 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005941 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005942 }
5943
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005944 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005945 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5946 }
5947
Calin Juravle52c48962014-12-16 17:02:57 +00005948 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005949 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005950 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005951 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005952
Vladimir Marko61b92282017-10-11 13:23:17 +01005953 void SetType(DataType::Type new_type) {
5954 DCHECK(DataType::IsIntegralType(GetType()));
5955 DCHECK(DataType::IsIntegralType(new_type));
5956 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5957 SetPackedField<TypeField>(new_type);
5958 }
5959
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005960 DECLARE_INSTRUCTION(InstanceFieldGet);
5961
Artem Serovcced8ba2017-07-19 18:18:09 +01005962 protected:
5963 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5964
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005965 private:
5966 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005967};
5968
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005969class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005970 public:
5971 HInstanceFieldSet(HInstruction* object,
5972 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005973 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005975 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005976 bool is_volatile,
5977 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005978 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005979 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005980 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005981 : HExpression(kInstanceFieldSet,
5982 SideEffects::FieldWriteOfType(field_type, is_volatile),
5983 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005984 field_info_(field,
5985 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005986 field_type,
5987 is_volatile,
5988 field_idx,
5989 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005990 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005991 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005992 SetRawInputAt(0, object);
5993 SetRawInputAt(1, value);
5994 }
5995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005996 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005997
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005998 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005999 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00006000 }
6001
Calin Juravle52c48962014-12-16 17:02:57 +00006002 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006003 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006005 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006006 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006007 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6008 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006009
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006010 DECLARE_INSTRUCTION(InstanceFieldSet);
6011
Artem Serovcced8ba2017-07-19 18:18:09 +01006012 protected:
6013 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
6014
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006015 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006016 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6017 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
6018 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
6019 "Too many packed fields.");
6020
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006021 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006022};
6023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006024class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006025 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006026 HArrayGet(HInstruction* array,
6027 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006028 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006029 uint32_t dex_pc)
6030 : HArrayGet(array,
6031 index,
6032 type,
6033 SideEffects::ArrayReadOfType(type),
6034 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08006035 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306036 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006037
6038 HArrayGet(HInstruction* array,
6039 HInstruction* index,
6040 DataType::Type type,
6041 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006042 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006043 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306044 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006045 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006046 SetRawInputAt(0, array);
6047 SetRawInputAt(1, index);
6048 }
6049
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006050 bool IsClonable() const override { return true; }
6051 bool CanBeMoved() const override { return true; }
6052 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006053 return true;
6054 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006055 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006056 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006057 // Currently, unless the array is the result of NewArray, the array access is always
6058 // preceded by some form of null NullCheck necessary for the bounds check, usually
6059 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6060 // dynamic BCE. There are cases when these could be removed to produce better code.
6061 // If we ever add optimizations to do so we should allow an implicit check here
6062 // (as long as the address falls in the first page).
6063 //
6064 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6065 // a = cond ? new int[1] : null;
6066 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006067 return false;
6068 }
6069
David Brazdil4833f5a2015-12-16 10:37:39 +00006070 bool IsEquivalentOf(HArrayGet* other) const {
6071 bool result = (GetDexPc() == other->GetDexPc());
6072 if (kIsDebugBuild && result) {
6073 DCHECK_EQ(GetBlock(), other->GetBlock());
6074 DCHECK_EQ(GetArray(), other->GetArray());
6075 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006076 if (DataType::IsIntOrLongType(GetType())) {
6077 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006078 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006079 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6080 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006081 }
6082 }
6083 return result;
6084 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006085
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006086 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6087
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006088 HInstruction* GetArray() const { return InputAt(0); }
6089 HInstruction* GetIndex() const { return InputAt(1); }
6090
Vladimir Marko61b92282017-10-11 13:23:17 +01006091 void SetType(DataType::Type new_type) {
6092 DCHECK(DataType::IsIntegralType(GetType()));
6093 DCHECK(DataType::IsIntegralType(new_type));
6094 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6095 SetPackedField<TypeField>(new_type);
6096 }
6097
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006098 DECLARE_INSTRUCTION(ArrayGet);
6099
Artem Serovcced8ba2017-07-19 18:18:09 +01006100 protected:
6101 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6102
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006103 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006104 // We treat a String as an array, creating the HArrayGet from String.charAt()
6105 // intrinsic in the instruction simplifier. We can always determine whether
6106 // a particular HArrayGet is actually a String.charAt() by looking at the type
6107 // of the input but that requires holding the mutator lock, so we prefer to use
6108 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006109 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006110 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6111 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6112 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006113};
6114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006115class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006116 public:
6117 HArraySet(HInstruction* array,
6118 HInstruction* index,
6119 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006120 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006121 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006122 : HArraySet(array,
6123 index,
6124 value,
6125 expected_component_type,
6126 // Make a best guess for side effects now, may be refined during SSA building.
6127 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306128 dex_pc) {
6129 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006130
6131 HArraySet(HInstruction* array,
6132 HInstruction* index,
6133 HInstruction* value,
6134 DataType::Type expected_component_type,
6135 SideEffects side_effects,
6136 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006137 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006138 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006139 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006140 SetPackedFlag<kFlagValueCanBeNull>(true);
6141 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006142 SetRawInputAt(0, array);
6143 SetRawInputAt(1, index);
6144 SetRawInputAt(2, value);
6145 }
6146
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006147 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006149 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006150 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006151 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006152 }
6153
Mingyao Yang81014cb2015-06-02 03:16:27 -07006154 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006155 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006156
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006157 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006158 // TODO: Same as for ArrayGet.
6159 return false;
6160 }
6161
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006162 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006163 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006164 }
6165
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006166 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006167 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006168 }
6169
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006170 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006171 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006172 }
6173
Vladimir Markoa1de9182016-02-25 11:37:38 +00006174 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6175 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6176 bool StaticTypeOfArrayIsObjectArray() const {
6177 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6178 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006179
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006180 HInstruction* GetArray() const { return InputAt(0); }
6181 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006182 HInstruction* GetValue() const { return InputAt(2); }
6183
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006184 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006185 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6186 }
6187
6188 static DataType::Type GetComponentType(DataType::Type value_type,
6189 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006190 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006191 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006192 // be correct, we also check what is the value type. If it is a floating
6193 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006194 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006195 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006196 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006197 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006199 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006200 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006201 }
6202
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006203 static SideEffects ComputeSideEffects(DataType::Type type) {
6204 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006205 }
6206
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006207 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6208 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6209 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006210 }
6211
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006212 DECLARE_INSTRUCTION(ArraySet);
6213
Artem Serovcced8ba2017-07-19 18:18:09 +01006214 protected:
6215 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6216
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006217 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006218 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6219 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006221 static constexpr size_t kFlagNeedsTypeCheck =
6222 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6223 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006224 // Cached information for the reference_type_info_ so that codegen
6225 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006226 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6227 static constexpr size_t kNumberOfArraySetPackedBits =
6228 kFlagStaticTypeOfArrayIsObjectArray + 1;
6229 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6230 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006231 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006232};
6233
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006234class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006235 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006236 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306237 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006238 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006239 // Note that arrays do not change length, so the instruction does not
6240 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006241 SetRawInputAt(0, array);
6242 }
6243
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006244 bool IsClonable() const override { return true; }
6245 bool CanBeMoved() const override { return true; }
6246 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006247 return true;
6248 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006249 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006250 return obj == InputAt(0);
6251 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006252
Vladimir Markodce016e2016-04-28 13:10:02 +01006253 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6254
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006255 DECLARE_INSTRUCTION(ArrayLength);
6256
Artem Serovcced8ba2017-07-19 18:18:09 +01006257 protected:
6258 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006260 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006261 // We treat a String as an array, creating the HArrayLength from String.length()
6262 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6263 // determine whether a particular HArrayLength is actually a String.length() by
6264 // looking at the type of the input but that requires holding the mutator lock, so
6265 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006266 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006267 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6268 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6269 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006270};
6271
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006272class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006273 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006274 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006275 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6276 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006277 HBoundsCheck(HInstruction* index,
6278 HInstruction* length,
6279 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006280 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006281 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006282 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006283 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006284 SetRawInputAt(0, index);
6285 SetRawInputAt(1, length);
6286 }
6287
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006288 bool IsClonable() const override { return true; }
6289 bool CanBeMoved() const override { return true; }
6290 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006291 return true;
6292 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006293
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006294 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006296 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006297
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006298 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006299
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006300 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006301
6302 DECLARE_INSTRUCTION(BoundsCheck);
6303
Artem Serovcced8ba2017-07-19 18:18:09 +01006304 protected:
6305 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6306
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006307 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006308 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006309 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6310 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6311 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006312};
6313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006314class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006315 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006316 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006317 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306318 slow_path_(nullptr) {
6319 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006321 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006323 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006324 return true;
6325 }
6326
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006327 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6328 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006329
6330 DECLARE_INSTRUCTION(SuspendCheck);
6331
Artem Serovcced8ba2017-07-19 18:18:09 +01006332 protected:
6333 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6334
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006335 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006336 // Only used for code generation, in order to share the same slow path between back edges
6337 // of a same loop.
6338 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006339};
6340
David Srbecky0cf44932015-12-09 14:09:59 +00006341// Pseudo-instruction which provides the native debugger with mapping information.
6342// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006343class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006344 public:
6345 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006346 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306347 }
David Srbecky0cf44932015-12-09 14:09:59 +00006348
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006349 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006350 return true;
6351 }
6352
6353 DECLARE_INSTRUCTION(NativeDebugInfo);
6354
Artem Serovcced8ba2017-07-19 18:18:09 +01006355 protected:
6356 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006357};
6358
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006359/**
6360 * Instruction to load a Class object.
6361 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006362class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006363 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006364 // Determines how to load the Class.
6365 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006366 // We cannot load this class. See HSharpening::SharpenLoadClass.
6367 kInvalid = -1,
6368
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006369 // Use the Class* from the method's own ArtMethod*.
6370 kReferrersClass,
6371
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006372 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006373 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006374 kBootImageLinkTimePcRelative,
6375
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006376 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006377 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006378 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006379
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006380 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006381 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006382 kBssEntry,
6383
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006384 // Use a known boot image Class* address, embedded in the code by the codegen.
6385 // Used for boot image classes referenced by apps in JIT-compiled code.
6386 kJitBootImageAddress,
6387
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006388 // Load from the root table associated with the JIT compiled method.
6389 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006390
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006391 // Load using a simple runtime call. This is the fall-back load kind when
6392 // the codegen is unable to use another appropriate kind.
6393 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006394
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006395 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006396 };
6397
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006398 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006399 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006400 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006401 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006402 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006403 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006404 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006405 : HInstruction(kLoadClass,
6406 DataType::Type::kReference,
6407 SideEffectsForArchRuntimeCalls(),
6408 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006409 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006410 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006411 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006412 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006413 // Referrers class should not need access check. We never inline unverified
6414 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006415 DCHECK(!is_referrers_class || !needs_access_check);
6416
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006417 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006418 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006419 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006420 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006421 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006422 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006423 }
6424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006425 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006426
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006427 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006428
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006429 LoadKind GetLoadKind() const {
6430 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006431 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006432
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006433 bool HasPcRelativeLoadKind() const {
6434 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6435 GetLoadKind() == LoadKind::kBootImageRelRo ||
6436 GetLoadKind() == LoadKind::kBssEntry;
6437 }
6438
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006439 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006440
Yi Kong39402542019-03-24 02:47:16 -07006441 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006442
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006443 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006444
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006445 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006446
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006447 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006448 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006449 }
6450
Calin Juravle0ba218d2015-05-19 18:46:01 +01006451 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006452 // The entrypoint the code generator is going to call does not do
6453 // clinit of the class.
6454 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006455 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006456 }
6457
6458 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006459 return NeedsAccessCheck() ||
6460 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006461 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006462 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006463 }
6464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006465 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006466 return NeedsAccessCheck() ||
6467 MustGenerateClinitCheck() ||
6468 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006469 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006470 GetLoadKind() == LoadKind::kBssEntry) &&
6471 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006472 }
6473
Calin Juravleacf735c2015-02-12 15:25:22 +00006474 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006475 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6476 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006477 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006478 } else {
6479 return ReferenceTypeInfo::CreateInvalid();
6480 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006481 }
6482
Vladimir Marko175e7862018-03-27 09:03:13 +00006483 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6484 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6485 DCHECK(klass_ != nullptr);
6486 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006487 }
6488
Andreas Gampea5b09a62016-11-17 15:21:22 -08006489 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006490 const DexFile& GetDexFile() const { return dex_file_; }
6491
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006492 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006493 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006494 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006495
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006496 static SideEffects SideEffectsForArchRuntimeCalls() {
6497 return SideEffects::CanTriggerGC();
6498 }
6499
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006500 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006501 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006502 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006503 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006504
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006505 bool MustResolveTypeOnSlowPath() const {
6506 // Check that this instruction has a slow path.
6507 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6508 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6509 return GetLoadKind() == LoadKind::kBssEntry;
6510 }
6511
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006512 void MarkInBootImage() {
6513 SetPackedFlag<kFlagIsInBootImage>(true);
6514 }
6515
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006516 void AddSpecialInput(HInstruction* special_input);
6517
6518 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006519 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006520 return ArrayRef<HUserRecord<HInstruction*>>(
6521 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6522 }
6523
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006524 Handle<mirror::Class> GetClass() const {
6525 return klass_;
6526 }
6527
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006528 DECLARE_INSTRUCTION(LoadClass);
6529
Artem Serovcced8ba2017-07-19 18:18:09 +01006530 protected:
6531 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6532
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006533 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006534 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006535 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006536 // Whether this instruction must generate the initialization check.
6537 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006538 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006539 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6540 static constexpr size_t kFieldLoadKindSize =
6541 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006542 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6543 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006544 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006545 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6546
6547 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006548 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006549 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006550 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006551 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006552 }
6553
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006554 void SetLoadKindInternal(LoadKind load_kind);
6555
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006556 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006557 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006558 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006559 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006560
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006561 // A type index and dex file where the class can be accessed. The dex file can be:
6562 // - The compiling method's dex file if the class is defined there too.
6563 // - The compiling method's dex file if the class is referenced there.
6564 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006565 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006566 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006567 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006568
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006569 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006570};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006571std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6572
6573// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006574inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6575 // The load kind should be determined before inserting the instruction to the graph.
6576 DCHECK(GetBlock() == nullptr);
6577 DCHECK(GetEnvironment() == nullptr);
6578 SetPackedField<LoadKindField>(load_kind);
6579 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6580 special_input_ = HUserRecord<HInstruction*>(nullptr);
6581 }
6582 if (!NeedsEnvironment()) {
6583 SetSideEffects(SideEffects::None());
6584 }
6585}
6586
6587// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006588inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006589 // The special input is used for PC-relative loads on some architectures,
6590 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006591 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006592 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006593 GetLoadKind() == LoadKind::kBssEntry ||
6594 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006595 DCHECK(special_input_.GetInstruction() == nullptr);
6596 special_input_ = HUserRecord<HInstruction*>(special_input);
6597 special_input->AddUseAt(this, 0);
6598}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006599
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006600class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006601 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006602 // Determines how to load the String.
6603 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006604 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006605 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006606 kBootImageLinkTimePcRelative,
6607
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006608 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006609 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006610 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006611
Vladimir Markoaad75c62016-10-03 08:46:48 +00006612 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006613 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006614 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006615
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006616 // Use a known boot image String* address, embedded in the code by the codegen.
6617 // Used for boot image strings referenced by apps in JIT-compiled code.
6618 kJitBootImageAddress,
6619
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006620 // Load from the root table associated with the JIT compiled method.
6621 kJitTableAddress,
6622
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006623 // Load using a simple runtime call. This is the fall-back load kind when
6624 // the codegen is unable to use another appropriate kind.
6625 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006626
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006627 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006628 };
6629
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006630 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006631 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006632 const DexFile& dex_file,
6633 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006634 : HInstruction(kLoadString,
6635 DataType::Type::kReference,
6636 SideEffectsForArchRuntimeCalls(),
6637 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006638 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006639 string_index_(string_index),
6640 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006641 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006642 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006643
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006644 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006645
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006646 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006647
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006648 LoadKind GetLoadKind() const {
6649 return GetPackedField<LoadKindField>();
6650 }
6651
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006652 bool HasPcRelativeLoadKind() const {
6653 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6654 GetLoadKind() == LoadKind::kBootImageRelRo ||
6655 GetLoadKind() == LoadKind::kBssEntry;
6656 }
6657
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006658 const DexFile& GetDexFile() const {
6659 return dex_file_;
6660 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006661
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006662 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006663 return string_index_;
6664 }
6665
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006666 Handle<mirror::String> GetString() const {
6667 return string_;
6668 }
6669
6670 void SetString(Handle<mirror::String> str) {
6671 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006672 }
6673
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006674 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006676 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006677
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006678 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006679
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006680 // Will call the runtime if we need to load the string through
6681 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006682 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006683 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006684 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006685 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006686 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006687 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006688 return false;
6689 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006690 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006691 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006692
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006693 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006694 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006695 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006696
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006697 bool CanBeNull() const override { return false; }
6698 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006699
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006700 static SideEffects SideEffectsForArchRuntimeCalls() {
6701 return SideEffects::CanTriggerGC();
6702 }
6703
Vladimir Marko372f10e2016-05-17 16:30:10 +01006704 void AddSpecialInput(HInstruction* special_input);
6705
6706 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006707 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006708 return ArrayRef<HUserRecord<HInstruction*>>(
6709 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006710 }
6711
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006712 DECLARE_INSTRUCTION(LoadString);
6713
Artem Serovcced8ba2017-07-19 18:18:09 +01006714 protected:
6715 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6716
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006717 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006718 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006719 static constexpr size_t kFieldLoadKindSize =
6720 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6721 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006722 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006723 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006724
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006725 void SetLoadKindInternal(LoadKind load_kind);
6726
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006727 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006728 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006729 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006730 HUserRecord<HInstruction*> special_input_;
6731
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006732 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006733 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006734
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006735 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006736};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006737std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6738
6739// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006740inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6741 // The load kind should be determined before inserting the instruction to the graph.
6742 DCHECK(GetBlock() == nullptr);
6743 DCHECK(GetEnvironment() == nullptr);
6744 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6745 SetPackedField<LoadKindField>(load_kind);
6746 if (load_kind != LoadKind::kRuntimeCall) {
6747 special_input_ = HUserRecord<HInstruction*>(nullptr);
6748 }
6749 if (!NeedsEnvironment()) {
6750 SetSideEffects(SideEffects::None());
6751 }
6752}
6753
6754// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006755inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006756 // The special input is used for PC-relative loads on some architectures,
6757 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006758 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006759 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006760 GetLoadKind() == LoadKind::kBssEntry ||
6761 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006762 // HLoadString::GetInputRecords() returns an empty array at this point,
6763 // so use the GetInputRecords() from the base class to set the input record.
6764 DCHECK(special_input_.GetInstruction() == nullptr);
6765 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006766 special_input->AddUseAt(this, 0);
6767}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006769class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006770 public:
6771 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006772 uint16_t method_handle_idx,
6773 const DexFile& dex_file,
6774 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006775 : HInstruction(kLoadMethodHandle,
6776 DataType::Type::kReference,
6777 SideEffectsForArchRuntimeCalls(),
6778 dex_pc),
6779 special_input_(HUserRecord<HInstruction*>(current_method)),
6780 method_handle_idx_(method_handle_idx),
6781 dex_file_(dex_file) {
6782 }
6783
6784 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006785 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006786 return ArrayRef<HUserRecord<HInstruction*>>(
6787 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6788 }
6789
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006790 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006791
6792 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6793
6794 const DexFile& GetDexFile() const { return dex_file_; }
6795
6796 static SideEffects SideEffectsForArchRuntimeCalls() {
6797 return SideEffects::CanTriggerGC();
6798 }
6799
6800 DECLARE_INSTRUCTION(LoadMethodHandle);
6801
6802 protected:
6803 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6804
6805 private:
6806 // The special input is the HCurrentMethod for kRuntimeCall.
6807 HUserRecord<HInstruction*> special_input_;
6808
6809 const uint16_t method_handle_idx_;
6810 const DexFile& dex_file_;
6811};
6812
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006813class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006814 public:
6815 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006816 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006817 const DexFile& dex_file,
6818 uint32_t dex_pc)
6819 : HInstruction(kLoadMethodType,
6820 DataType::Type::kReference,
6821 SideEffectsForArchRuntimeCalls(),
6822 dex_pc),
6823 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006824 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006825 dex_file_(dex_file) {
6826 }
6827
6828 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006829 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006830 return ArrayRef<HUserRecord<HInstruction*>>(
6831 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6832 }
6833
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006834 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006835
Orion Hodson06d10a72018-05-14 08:53:38 +01006836 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006837
6838 const DexFile& GetDexFile() const { return dex_file_; }
6839
6840 static SideEffects SideEffectsForArchRuntimeCalls() {
6841 return SideEffects::CanTriggerGC();
6842 }
6843
6844 DECLARE_INSTRUCTION(LoadMethodType);
6845
6846 protected:
6847 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6848
6849 private:
6850 // The special input is the HCurrentMethod for kRuntimeCall.
6851 HUserRecord<HInstruction*> special_input_;
6852
Orion Hodson06d10a72018-05-14 08:53:38 +01006853 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006854 const DexFile& dex_file_;
6855};
6856
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006857/**
6858 * Performs an initialization check on its Class object input.
6859 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006860class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006861 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006862 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006863 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306864 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006865 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006866 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006867 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006868 SetRawInputAt(0, constant);
6869 }
Artem Serova6e26142018-06-19 14:55:17 +01006870 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006871 bool CanBeMoved() const override { return true; }
6872 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006873 return true;
6874 }
6875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006876 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006877 // May call runtime to initialize the class.
6878 return true;
6879 }
6880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006881 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006882
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006883 HLoadClass* GetLoadClass() const {
6884 DCHECK(InputAt(0)->IsLoadClass());
6885 return InputAt(0)->AsLoadClass();
6886 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006887
6888 DECLARE_INSTRUCTION(ClinitCheck);
6889
Artem Serovcced8ba2017-07-19 18:18:09 +01006890
6891 protected:
6892 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006893};
6894
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006895class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006896 public:
6897 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006898 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006899 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006900 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006901 bool is_volatile,
6902 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006903 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006904 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006905 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306906 : HExpression(kStaticFieldGet,
6907 field_type,
6908 SideEffects::FieldReadOfType(field_type, is_volatile),
6909 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006910 field_info_(field,
6911 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006912 field_type,
6913 is_volatile,
6914 field_idx,
6915 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006916 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006917 SetRawInputAt(0, cls);
6918 }
6919
Calin Juravle52c48962014-12-16 17:02:57 +00006920
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006921 bool IsClonable() const override { return true; }
6922 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006923
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006924 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006925 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006926 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006927 }
6928
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006929 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006930 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6931 }
6932
Calin Juravle52c48962014-12-16 17:02:57 +00006933 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006934 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006935 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006936 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006937
Vladimir Marko61b92282017-10-11 13:23:17 +01006938 void SetType(DataType::Type new_type) {
6939 DCHECK(DataType::IsIntegralType(GetType()));
6940 DCHECK(DataType::IsIntegralType(new_type));
6941 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6942 SetPackedField<TypeField>(new_type);
6943 }
6944
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006945 DECLARE_INSTRUCTION(StaticFieldGet);
6946
Artem Serovcced8ba2017-07-19 18:18:09 +01006947 protected:
6948 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6949
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006950 private:
6951 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006952};
6953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006954class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006955 public:
6956 HStaticFieldSet(HInstruction* cls,
6957 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006958 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006959 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006960 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006961 bool is_volatile,
6962 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006963 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006964 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006965 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006966 : HExpression(kStaticFieldSet,
6967 SideEffects::FieldWriteOfType(field_type, is_volatile),
6968 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006969 field_info_(field,
6970 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006971 field_type,
6972 is_volatile,
6973 field_idx,
6974 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006975 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006976 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006977 SetRawInputAt(0, cls);
6978 SetRawInputAt(1, value);
6979 }
6980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006981 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006982 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006983 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006984 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006985 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006986
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006987 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006988 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6989 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006990
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006991 DECLARE_INSTRUCTION(StaticFieldSet);
6992
Artem Serovcced8ba2017-07-19 18:18:09 +01006993 protected:
6994 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6995
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006996 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006997 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6998 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6999 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
7000 "Too many packed fields.");
7001
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007002 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007003};
7004
Vladimir Marko552a1342017-10-31 10:56:47 +00007005class HStringBuilderAppend final : public HVariableInputSizeInstruction {
7006 public:
7007 HStringBuilderAppend(HIntConstant* format,
7008 uint32_t number_of_arguments,
7009 ArenaAllocator* allocator,
7010 uint32_t dex_pc)
7011 : HVariableInputSizeInstruction(
7012 kStringBuilderAppend,
7013 DataType::Type::kReference,
7014 // The runtime call may read memory from inputs. It never writes outside
7015 // of the newly allocated result object (or newly allocated helper objects).
7016 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
7017 dex_pc,
7018 allocator,
7019 number_of_arguments + /* format */ 1u,
7020 kArenaAllocInvokeInputs) {
7021 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
7022 SetRawInputAt(FormatIndex(), format);
7023 }
7024
7025 void SetArgumentAt(size_t index, HInstruction* argument) {
7026 DCHECK_LE(index, GetNumberOfArguments());
7027 SetRawInputAt(index, argument);
7028 }
7029
7030 // Return the number of arguments, excluding the format.
7031 size_t GetNumberOfArguments() const {
7032 DCHECK_GE(InputCount(), 1u);
7033 return InputCount() - 1u;
7034 }
7035
7036 size_t FormatIndex() const {
7037 return GetNumberOfArguments();
7038 }
7039
7040 HIntConstant* GetFormat() {
7041 return InputAt(FormatIndex())->AsIntConstant();
7042 }
7043
7044 bool NeedsEnvironment() const override { return true; }
7045
7046 bool CanThrow() const override { return true; }
7047
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007048 bool CanBeNull() const override { return false; }
7049
Vladimir Marko552a1342017-10-31 10:56:47 +00007050 DECLARE_INSTRUCTION(StringBuilderAppend);
7051
7052 protected:
7053 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7054};
7055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007056class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007057 public:
7058 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007059 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007060 uint32_t field_index,
7061 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307062 : HExpression(kUnresolvedInstanceFieldGet,
7063 field_type,
7064 SideEffects::AllExceptGCDependency(),
7065 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007066 field_index_(field_index) {
7067 SetRawInputAt(0, obj);
7068 }
7069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007070 bool IsClonable() const override { return true; }
7071 bool NeedsEnvironment() const override { return true; }
7072 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007074 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007075 uint32_t GetFieldIndex() const { return field_index_; }
7076
7077 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7078
Artem Serovcced8ba2017-07-19 18:18:09 +01007079 protected:
7080 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7081
Calin Juravlee460d1d2015-09-29 04:52:17 +01007082 private:
7083 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007084};
7085
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007086class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007087 public:
7088 HUnresolvedInstanceFieldSet(HInstruction* obj,
7089 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007090 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007091 uint32_t field_index,
7092 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007093 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007094 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007095 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007096 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007097 SetRawInputAt(0, obj);
7098 SetRawInputAt(1, value);
7099 }
7100
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007101 bool IsClonable() const override { return true; }
7102 bool NeedsEnvironment() const override { return true; }
7103 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007104
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007105 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007106 uint32_t GetFieldIndex() const { return field_index_; }
7107
7108 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7109
Artem Serovcced8ba2017-07-19 18:18:09 +01007110 protected:
7111 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7112
Calin Juravlee460d1d2015-09-29 04:52:17 +01007113 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007114 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7115 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007116 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007117 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7118 kFieldFieldType + kFieldFieldTypeSize;
7119 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7120 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007121 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007122
Calin Juravlee460d1d2015-09-29 04:52:17 +01007123 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007124};
7125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007126class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007127 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007128 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007129 uint32_t field_index,
7130 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307131 : HExpression(kUnresolvedStaticFieldGet,
7132 field_type,
7133 SideEffects::AllExceptGCDependency(),
7134 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007135 field_index_(field_index) {
7136 }
7137
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007138 bool IsClonable() const override { return true; }
7139 bool NeedsEnvironment() const override { return true; }
7140 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007142 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007143 uint32_t GetFieldIndex() const { return field_index_; }
7144
7145 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7146
Artem Serovcced8ba2017-07-19 18:18:09 +01007147 protected:
7148 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7149
Calin Juravlee460d1d2015-09-29 04:52:17 +01007150 private:
7151 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007152};
7153
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007154class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007155 public:
7156 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007157 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007158 uint32_t field_index,
7159 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007160 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007161 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007162 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007163 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007164 SetRawInputAt(0, value);
7165 }
7166
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007167 bool IsClonable() const override { return true; }
7168 bool NeedsEnvironment() const override { return true; }
7169 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007171 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007172 uint32_t GetFieldIndex() const { return field_index_; }
7173
7174 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7175
Artem Serovcced8ba2017-07-19 18:18:09 +01007176 protected:
7177 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7178
Calin Juravlee460d1d2015-09-29 04:52:17 +01007179 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007180 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7181 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007182 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007183 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7184 kFieldFieldType + kFieldFieldTypeSize;
7185 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7186 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007187 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007188
Calin Juravlee460d1d2015-09-29 04:52:17 +01007189 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007190};
7191
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007192// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007193class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007194 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007195 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307196 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7197 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007198
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007199 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007200
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007201 DECLARE_INSTRUCTION(LoadException);
7202
Artem Serovcced8ba2017-07-19 18:18:09 +01007203 protected:
7204 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007205};
7206
David Brazdilcb1c0552015-08-04 16:22:25 +01007207// Implicit part of move-exception which clears thread-local exception storage.
7208// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007209class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007210 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007211 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007212 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307213 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007214
7215 DECLARE_INSTRUCTION(ClearException);
7216
Artem Serovcced8ba2017-07-19 18:18:09 +01007217 protected:
7218 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007219};
7220
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007221class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007222 public:
7223 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007224 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007225 SetRawInputAt(0, exception);
7226 }
7227
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007228 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007230 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007231
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007232 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007233
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007234 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007235
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007236 DECLARE_INSTRUCTION(Throw);
7237
Artem Serovcced8ba2017-07-19 18:18:09 +01007238 protected:
7239 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007240};
7241
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007242/**
7243 * Implementation strategies for the code generator of a HInstanceOf
7244 * or `HCheckCast`.
7245 */
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007246enum class TypeCheckKind { // private marker to avoid generate-operator-out.py from processing.
Calin Juravle98893e12015-10-02 21:05:03 +01007247 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007248 kExactCheck, // Can do a single class compare.
7249 kClassHierarchyCheck, // Can just walk the super class chain.
7250 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7251 kInterfaceCheck, // No optimization yet when checking against an interface.
7252 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007253 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007254 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007255 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007256};
7257
Roland Levillain86503782016-02-11 19:07:30 +00007258std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7259
Vladimir Marko175e7862018-03-27 09:03:13 +00007260// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7261// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7262class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007263 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007264 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007265 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007266 HInstruction* object,
7267 HInstruction* target_class_or_null,
7268 TypeCheckKind check_kind,
7269 Handle<mirror::Class> klass,
7270 uint32_t dex_pc,
7271 ArenaAllocator* allocator,
7272 HIntConstant* bitstring_path_to_root,
7273 HIntConstant* bitstring_mask,
7274 SideEffects side_effects)
7275 : HVariableInputSizeInstruction(
7276 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007277 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007278 side_effects,
7279 dex_pc,
7280 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007281 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007282 kArenaAllocTypeCheckInputs),
7283 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007284 SetPackedField<TypeCheckKindField>(check_kind);
7285 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007286 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007287 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007288 SetRawInputAt(1, target_class_or_null);
7289 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7290 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7291 if (check_kind == TypeCheckKind::kBitstringCheck) {
7292 DCHECK(target_class_or_null->IsNullConstant());
7293 SetRawInputAt(2, bitstring_path_to_root);
7294 SetRawInputAt(3, bitstring_mask);
7295 } else {
7296 DCHECK(target_class_or_null->IsLoadClass());
7297 }
Vladimir Marko87584542017-12-12 17:47:52 +00007298 }
7299
7300 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007301 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007302 HInstruction* load_class = InputAt(1);
7303 DCHECK(load_class->IsLoadClass());
7304 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007305 }
7306
Vladimir Marko175e7862018-03-27 09:03:13 +00007307 uint32_t GetBitstringPathToRoot() const {
7308 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7309 HInstruction* path_to_root = InputAt(2);
7310 DCHECK(path_to_root->IsIntConstant());
7311 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7312 }
7313
7314 uint32_t GetBitstringMask() const {
7315 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7316 HInstruction* mask = InputAt(3);
7317 DCHECK(mask->IsIntConstant());
7318 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7319 }
7320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007321 bool IsClonable() const override { return true; }
7322 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007323
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007324 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007325 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7326 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007327 }
7328
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007329 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7330 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7331 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7332 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7333
Vladimir Marko175e7862018-03-27 09:03:13 +00007334 ReferenceTypeInfo GetTargetClassRTI() {
7335 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7336 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007337 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007338 } else {
7339 return ReferenceTypeInfo::CreateInvalid();
7340 }
7341 }
7342
7343 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7344 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7345 DCHECK(klass_ != nullptr);
7346 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7347 }
7348
7349 Handle<mirror::Class> GetClass() const {
7350 return klass_;
7351 }
7352
7353 protected:
7354 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7355
7356 private:
7357 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7358 static constexpr size_t kFieldTypeCheckKindSize =
7359 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7360 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7361 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7362 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7363 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7364 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7365
7366 Handle<mirror::Class> klass_;
7367};
7368
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007369class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007370 public:
7371 HInstanceOf(HInstruction* object,
7372 HInstruction* target_class_or_null,
7373 TypeCheckKind check_kind,
7374 Handle<mirror::Class> klass,
7375 uint32_t dex_pc,
7376 ArenaAllocator* allocator,
7377 HIntConstant* bitstring_path_to_root,
7378 HIntConstant* bitstring_mask)
7379 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007380 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007381 object,
7382 target_class_or_null,
7383 check_kind,
7384 klass,
7385 dex_pc,
7386 allocator,
7387 bitstring_path_to_root,
7388 bitstring_mask,
7389 SideEffectsForArchRuntimeCalls(check_kind)) {}
7390
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007391 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007392
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007393 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007394 return CanCallRuntime(GetTypeCheckKind());
7395 }
7396
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007397 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007398 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007399 return check_kind != TypeCheckKind::kExactCheck;
7400 }
7401
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007402 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007403 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007404 }
7405
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007406 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007407
Artem Serovcced8ba2017-07-19 18:18:09 +01007408 protected:
7409 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007410};
7411
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007412class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007413 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007414 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307415 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007416 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7417 SetPackedFlag<kFlagUpperCanBeNull>(true);
7418 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007419 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007420 SetRawInputAt(0, input);
7421 }
7422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007423 bool InstructionDataEquals(const HInstruction* other) const override;
7424 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007425
David Brazdilf5552582015-12-27 13:36:12 +00007426 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007427 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007428 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007429 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007430
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007431 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007432 DCHECK(GetUpperCanBeNull() || !can_be_null);
7433 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007434 }
7435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007436 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007437
Calin Juravleb1498f62015-02-16 13:13:29 +00007438 DECLARE_INSTRUCTION(BoundType);
7439
Artem Serovcced8ba2017-07-19 18:18:09 +01007440 protected:
7441 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7442
Calin Juravleb1498f62015-02-16 13:13:29 +00007443 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007444 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7445 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007446 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007447 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7448 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7449 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7450
Calin Juravleb1498f62015-02-16 13:13:29 +00007451 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007452 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7453 // It is used to bound the type in cases like:
7454 // if (x instanceof ClassX) {
7455 // // uper_bound_ will be ClassX
7456 // }
David Brazdilf5552582015-12-27 13:36:12 +00007457 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007458};
7459
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007460class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007461 public:
7462 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007463 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007464 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007465 Handle<mirror::Class> klass,
7466 uint32_t dex_pc,
7467 ArenaAllocator* allocator,
7468 HIntConstant* bitstring_path_to_root,
7469 HIntConstant* bitstring_mask)
7470 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007471 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007472 object,
7473 target_class_or_null,
7474 check_kind,
7475 klass,
7476 dex_pc,
7477 allocator,
7478 bitstring_path_to_root,
7479 bitstring_mask,
7480 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007481
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007482 bool IsClonable() const override { return true; }
7483 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007484 // Instruction may throw a CheckCastError.
7485 return true;
7486 }
7487
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007488 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007489
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007490 DECLARE_INSTRUCTION(CheckCast);
7491
Artem Serovcced8ba2017-07-19 18:18:09 +01007492 protected:
7493 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007494};
7495
Andreas Gampe26de38b2016-07-27 17:53:11 -07007496/**
7497 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7498 * @details We define the combined barrier types that are actually required
7499 * by the Java Memory Model, rather than using exactly the terminology from
7500 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7501 * primitives. Note that the JSR-133 cookbook generally does not deal with
7502 * store atomicity issues, and the recipes there are not always entirely sufficient.
7503 * The current recipe is as follows:
7504 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7505 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7506 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7507 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7508 * class has final fields.
7509 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7510 * store-to-memory instructions. Only generated together with non-temporal stores.
7511 */
7512enum MemBarrierKind {
7513 kAnyStore,
7514 kLoadAny,
7515 kStoreStore,
7516 kAnyAny,
7517 kNTStoreStore,
7518 kLastBarrierKind = kNTStoreStore
7519};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007520std::ostream& operator<<(std::ostream& os, MemBarrierKind kind);
Andreas Gampe26de38b2016-07-27 17:53:11 -07007521
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007522class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007523 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007524 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007525 : HExpression(kMemoryBarrier,
7526 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7527 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007528 SetPackedField<BarrierKindField>(barrier_kind);
7529 }
Calin Juravle27df7582015-04-17 19:12:31 +01007530
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007531 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007532
Vladimir Markoa1de9182016-02-25 11:37:38 +00007533 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007534
7535 DECLARE_INSTRUCTION(MemoryBarrier);
7536
Artem Serovcced8ba2017-07-19 18:18:09 +01007537 protected:
7538 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7539
Calin Juravle27df7582015-04-17 19:12:31 +01007540 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007541 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7542 static constexpr size_t kFieldBarrierKindSize =
7543 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7544 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7545 kFieldBarrierKind + kFieldBarrierKindSize;
7546 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7547 "Too many packed fields.");
7548 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007549};
7550
Igor Murashkind01745e2017-04-05 16:40:31 -07007551// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7552// the specified object(s), with respect to any subsequent store that might "publish"
7553// (i.e. make visible) the specified object to another thread.
7554//
7555// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7556// for all final fields (that were set) at the end of the invoked constructor.
7557//
7558// The constructor fence models the freeze actions for the final fields of an object
7559// being constructed (semantically at the end of the constructor). Constructor fences
7560// have a per-object affinity; two separate objects being constructed get two separate
7561// constructor fences.
7562//
7563// (Note: that if calling a super-constructor or forwarding to another constructor,
7564// the freezes would happen at the end of *that* constructor being invoked).
7565//
7566// The memory model guarantees that when the object being constructed is "published" after
7567// constructor completion (i.e. escapes the current thread via a store), then any final field
7568// writes must be observable on other threads (once they observe that publication).
7569//
7570// Further, anything written before the freeze, and read by dereferencing through the final field,
7571// must also be visible (so final object field could itself have an object with non-final fields;
7572// yet the freeze must also extend to them).
7573//
7574// Constructor example:
7575//
7576// class HasFinal {
7577// final int field; Optimizing IR for <init>()V:
7578// HasFinal() {
7579// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7580// // freeze(this.field); HConstructorFence(this)
7581// } HReturn
7582// }
7583//
7584// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7585// already-initialized classes; in that case the allocation must act as a "default-initializer"
7586// of the object which effectively writes the class pointer "final field".
7587//
7588// For example, we can model default-initialiation as roughly the equivalent of the following:
7589//
7590// class Object {
7591// private final Class header;
7592// }
7593//
7594// Java code: Optimizing IR:
7595//
7596// T new_instance<T>() {
7597// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7598// obj.header = T.class; // header write is done by above call.
7599// // freeze(obj.header) HConstructorFence(obj)
7600// return (T)obj;
7601// }
7602//
7603// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007604// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007605// * QuasiAtomic::ThreadFenceForConstructor
7606//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007607class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007608 // A fence has variable inputs because the inputs can be removed
7609 // after prepare_for_register_allocation phase.
7610 // (TODO: In the future a fence could freeze multiple objects
7611 // after merging two fences together.)
7612 public:
7613 // `fence_object` is the reference that needs to be protected for correct publication.
7614 //
7615 // It makes sense in the following situations:
7616 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7617 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7618 //
7619 // After construction the `fence_object` becomes the 0th input.
7620 // This is not an input in a real sense, but just a convenient place to stash the information
7621 // about the associated object.
7622 HConstructorFence(HInstruction* fence_object,
7623 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007624 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007625 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7626 // constraints described in the class header. We claim that these SideEffects constraints
7627 // enforce a superset of the real constraints.
7628 //
7629 // The ordering described above is conservatively modeled with SideEffects as follows:
7630 //
7631 // * To prevent reordering of the publication stores:
7632 // ----> "Reads of objects" is the initial SideEffect.
7633 // * For every primitive final field store in the constructor:
7634 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7635 // * If there are any stores to reference final fields in the constructor:
7636 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7637 // that are reachable from `fence_object` also need to be prevented for reordering
7638 // (and we do not want to do alias analysis to figure out what those stores are).
7639 //
7640 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7641 // even more conservative approach than the one described above, and prevents all of the
7642 // above reordering without analyzing any of the instructions in the constructor.
7643 //
7644 // If in a later phase we discover that there are no writes to reference final fields,
7645 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307646 : HVariableInputSizeInstruction(kConstructorFence,
7647 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007648 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007649 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007650 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007651 kArenaAllocConstructorFenceInputs) {
7652 DCHECK(fence_object != nullptr);
7653 SetRawInputAt(0, fence_object);
7654 }
7655
7656 // The object associated with this constructor fence.
7657 //
7658 // (Note: This will be null after the prepare_for_register_allocation phase,
7659 // as all constructor fence inputs are removed there).
7660 HInstruction* GetFenceObject() const {
7661 return InputAt(0);
7662 }
7663
7664 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7665 // - Delete `fence_use` from `this`'s use list.
7666 // - Delete `this` from `fence_use`'s inputs list.
7667 // - If the `fence_use` is dead, remove it from the graph.
7668 //
7669 // A fence is considered dead once it no longer has any uses
7670 // and all of the inputs are dead.
7671 //
7672 // This must *not* be called during/after prepare_for_register_allocation,
7673 // because that removes all the inputs to the fences but the fence is actually
7674 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007675 //
7676 // Returns how many HConstructorFence instructions were removed from graph.
7677 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007678
Igor Murashkindd018df2017-08-09 10:38:31 -07007679 // Combine all inputs of `this` and `other` instruction and remove
7680 // `other` from the graph.
7681 //
7682 // Inputs are unique after the merge.
7683 //
7684 // Requirement: `this` must not be the same as `other.
7685 void Merge(HConstructorFence* other);
7686
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007687 // Check if this constructor fence is protecting
7688 // an HNewInstance or HNewArray that is also the immediate
7689 // predecessor of `this`.
7690 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007691 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7692 // to be one of the inputs of `this`.
7693 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007694 // Returns the associated HNewArray or HNewInstance,
7695 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007696 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007697
Igor Murashkind01745e2017-04-05 16:40:31 -07007698 DECLARE_INSTRUCTION(ConstructorFence);
7699
Artem Serovcced8ba2017-07-19 18:18:09 +01007700 protected:
7701 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007702};
7703
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007704class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007705 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007706 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007707 kEnter,
7708 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007709 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007710 };
7711
7712 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007713 : HExpression(kMonitorOperation,
7714 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7715 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007716 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007717 SetRawInputAt(0, object);
7718 }
7719
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007720 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007721 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007722
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007723 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007724 // Verifier guarantees that monitor-exit cannot throw.
7725 // This is important because it allows the HGraphBuilder to remove
7726 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7727 return IsEnter();
7728 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007729
Vladimir Markoa1de9182016-02-25 11:37:38 +00007730 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7731 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007732
7733 DECLARE_INSTRUCTION(MonitorOperation);
7734
Artem Serovcced8ba2017-07-19 18:18:09 +01007735 protected:
7736 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7737
Calin Juravle52c48962014-12-16 17:02:57 +00007738 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007739 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7740 static constexpr size_t kFieldOperationKindSize =
7741 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7742 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7743 kFieldOperationKind + kFieldOperationKindSize;
7744 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7745 "Too many packed fields.");
7746 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007747};
7748
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007749class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007750 public:
7751 HSelect(HInstruction* condition,
7752 HInstruction* true_value,
7753 HInstruction* false_value,
7754 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307755 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007756 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7757
7758 // First input must be `true_value` or `false_value` to allow codegens to
7759 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7760 // that architectures which implement HSelect as a conditional move also
7761 // will not need to invert the condition.
7762 SetRawInputAt(0, false_value);
7763 SetRawInputAt(1, true_value);
7764 SetRawInputAt(2, condition);
7765 }
7766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007767 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007768 HInstruction* GetFalseValue() const { return InputAt(0); }
7769 HInstruction* GetTrueValue() const { return InputAt(1); }
7770 HInstruction* GetCondition() const { return InputAt(2); }
7771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007772 bool CanBeMoved() const override { return true; }
7773 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007774 return true;
7775 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007777 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007778 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7779 }
7780
7781 DECLARE_INSTRUCTION(Select);
7782
Artem Serovcced8ba2017-07-19 18:18:09 +01007783 protected:
7784 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007785};
7786
Vladimir Markof9f64412015-09-02 14:05:49 +01007787class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007788 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007789 MoveOperands(Location source,
7790 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007791 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007792 HInstruction* instruction)
7793 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007794
7795 Location GetSource() const { return source_; }
7796 Location GetDestination() const { return destination_; }
7797
7798 void SetSource(Location value) { source_ = value; }
7799 void SetDestination(Location value) { destination_ = value; }
7800
7801 // The parallel move resolver marks moves as "in-progress" by clearing the
7802 // destination (but not the source).
7803 Location MarkPending() {
7804 DCHECK(!IsPending());
7805 Location dest = destination_;
7806 destination_ = Location::NoLocation();
7807 return dest;
7808 }
7809
7810 void ClearPending(Location dest) {
7811 DCHECK(IsPending());
7812 destination_ = dest;
7813 }
7814
7815 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007816 DCHECK(source_.IsValid() || destination_.IsInvalid());
7817 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007818 }
7819
7820 // True if this blocks a move from the given location.
7821 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007822 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007823 }
7824
7825 // A move is redundant if it's been eliminated, if its source and
7826 // destination are the same, or if its destination is unneeded.
7827 bool IsRedundant() const {
7828 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7829 }
7830
7831 // We clear both operands to indicate move that's been eliminated.
7832 void Eliminate() {
7833 source_ = destination_ = Location::NoLocation();
7834 }
7835
7836 bool IsEliminated() const {
7837 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7838 return source_.IsInvalid();
7839 }
7840
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007841 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007842
Nicolas Geoffray90218252015-04-15 11:56:51 +01007843 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007844 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007845 }
7846
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007847 HInstruction* GetInstruction() const { return instruction_; }
7848
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007849 private:
7850 Location source_;
7851 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007852 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007853 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007854 // The instruction this move is assocatied with. Null when this move is
7855 // for moving an input in the expected locations of user (including a phi user).
7856 // This is only used in debug mode, to ensure we do not connect interval siblings
7857 // in the same parallel move.
7858 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007859};
7860
Roland Levillainc9285912015-12-18 10:38:42 +00007861std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7862
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007863static constexpr size_t kDefaultNumberOfMoves = 4;
7864
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007865class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007866 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007867 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007868 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007869 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007870 moves_.reserve(kDefaultNumberOfMoves);
7871 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007872
Nicolas Geoffray90218252015-04-15 11:56:51 +01007873 void AddMove(Location source,
7874 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007875 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007876 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007877 DCHECK(source.IsValid());
7878 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007879 if (kIsDebugBuild) {
7880 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007881 for (const MoveOperands& move : moves_) {
7882 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007883 // Special case the situation where the move is for the spill slot
7884 // of the instruction.
7885 if ((GetPrevious() == instruction)
7886 || ((GetPrevious() == nullptr)
7887 && instruction->IsPhi()
7888 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007889 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007890 << "Doing parallel moves for the same instruction.";
7891 } else {
7892 DCHECK(false) << "Doing parallel moves for the same instruction.";
7893 }
7894 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007895 }
7896 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007897 for (const MoveOperands& move : moves_) {
7898 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007899 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007900 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007901 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007902 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007903 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007904 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007905 }
7906
Vladimir Marko225b6462015-09-28 12:17:40 +01007907 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007908 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007909 }
7910
Vladimir Marko225b6462015-09-28 12:17:40 +01007911 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007912
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007913 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007914
Artem Serovcced8ba2017-07-19 18:18:09 +01007915 protected:
7916 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7917
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007918 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007919 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007920};
7921
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007922// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7923// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7924// never used across anything that can trigger GC.
7925// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7926// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007927class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007928 public:
7929 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307930 : HExpression(kIntermediateAddress,
7931 DataType::Type::kInt32,
7932 SideEffects::DependsOnGC(),
7933 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007934 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7935 DataType::Size(DataType::Type::kReference))
7936 << "kPrimInt and kPrimNot have different sizes.";
7937 SetRawInputAt(0, base_address);
7938 SetRawInputAt(1, offset);
7939 }
7940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007941 bool IsClonable() const override { return true; }
7942 bool CanBeMoved() const override { return true; }
7943 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007944 return true;
7945 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007946 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007947
7948 HInstruction* GetBaseAddress() const { return InputAt(0); }
7949 HInstruction* GetOffset() const { return InputAt(1); }
7950
7951 DECLARE_INSTRUCTION(IntermediateAddress);
7952
Artem Serovcced8ba2017-07-19 18:18:09 +01007953 protected:
7954 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007955};
7956
7957
Mark Mendell0616ae02015-04-17 12:49:27 -04007958} // namespace art
7959
Aart Bikf8f5a162017-02-06 15:35:29 -08007960#include "nodes_vector.h"
7961
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007962#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7963#include "nodes_shared.h"
7964#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307965#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007966#include "nodes_x86.h"
7967#endif
7968
Vladimir Marko0a516052019-10-14 13:00:44 +00007969namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007970
Igor Murashkin6ef45672017-08-08 13:59:55 -07007971class OptimizingCompilerStats;
7972
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007973class HGraphVisitor : public ValueObject {
7974 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007975 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7976 : stats_(stats),
7977 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007978 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007979
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007980 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007981 virtual void VisitBasicBlock(HBasicBlock* block);
7982
Roland Levillain633021e2014-10-01 14:12:25 +01007983 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007984 void VisitInsertionOrder();
7985
Roland Levillain633021e2014-10-01 14:12:25 +01007986 // Visit the graph following dominator tree reverse post-order.
7987 void VisitReversePostOrder();
7988
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007989 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007990
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007991 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007992#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007993 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7994
7995 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7996
7997#undef DECLARE_VISIT_INSTRUCTION
7998
Igor Murashkin6ef45672017-08-08 13:59:55 -07007999 protected:
8000 OptimizingCompilerStats* stats_;
8001
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008002 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07008003 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008004
8005 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
8006};
8007
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008008class HGraphDelegateVisitor : public HGraphVisitor {
8009 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008010 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8011 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008012 virtual ~HGraphDelegateVisitor() {}
8013
8014 // Visit functions that delegate to to super class.
8015#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008016 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008017
8018 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8019
8020#undef DECLARE_VISIT_INSTRUCTION
8021
8022 private:
8023 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
8024};
8025
Artem Serovcced8ba2017-07-19 18:18:09 +01008026// Create a clone of the instruction, insert it into the graph; replace the old one with a new
8027// and remove the old instruction.
8028HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
8029
8030// Create a clone for each clonable instructions/phis and replace the original with the clone.
8031//
8032// Used for testing individual instruction cloner.
8033class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
8034 public:
8035 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01008036 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01008037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008038 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01008039 if (instruction->IsClonable()) {
8040 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01008041 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01008042 }
8043 }
8044
Artem Serov7f4aff62017-06-21 17:02:18 +01008045 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008046
8047 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008048 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008049
8050 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8051};
8052
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008053// Iterator over the blocks that art part of the loop. Includes blocks part
8054// of an inner loop. The order in which the blocks are iterated is on their
8055// block id.
8056class HBlocksInLoopIterator : public ValueObject {
8057 public:
8058 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8059 : blocks_in_loop_(info.GetBlocks()),
8060 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8061 index_(0) {
8062 if (!blocks_in_loop_.IsBitSet(index_)) {
8063 Advance();
8064 }
8065 }
8066
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008067 bool Done() const { return index_ == blocks_.size(); }
8068 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008069 void Advance() {
8070 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008071 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008072 if (blocks_in_loop_.IsBitSet(index_)) {
8073 break;
8074 }
8075 }
8076 }
8077
8078 private:
8079 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008080 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008081 size_t index_;
8082
8083 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8084};
8085
Mingyao Yang3584bce2015-05-19 16:01:59 -07008086// Iterator over the blocks that art part of the loop. Includes blocks part
8087// of an inner loop. The order in which the blocks are iterated is reverse
8088// post order.
8089class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8090 public:
8091 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8092 : blocks_in_loop_(info.GetBlocks()),
8093 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8094 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008095 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008096 Advance();
8097 }
8098 }
8099
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008100 bool Done() const { return index_ == blocks_.size(); }
8101 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008102 void Advance() {
8103 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008104 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8105 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008106 break;
8107 }
8108 }
8109 }
8110
8111 private:
8112 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008113 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008114 size_t index_;
8115
8116 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8117};
8118
Aart Bikf3e61ee2017-04-12 17:09:20 -07008119// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008120inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008121 if (constant->IsIntConstant()) {
8122 return constant->AsIntConstant()->GetValue();
8123 } else if (constant->IsLongConstant()) {
8124 return constant->AsLongConstant()->GetValue();
8125 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008126 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008127 return 0;
8128 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008129}
8130
Aart Bikf3e61ee2017-04-12 17:09:20 -07008131// Returns true iff instruction is an integral constant (and sets value on success).
8132inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8133 if (instruction->IsIntConstant()) {
8134 *value = instruction->AsIntConstant()->GetValue();
8135 return true;
8136 } else if (instruction->IsLongConstant()) {
8137 *value = instruction->AsLongConstant()->GetValue();
8138 return true;
8139 } else if (instruction->IsNullConstant()) {
8140 *value = 0;
8141 return true;
8142 }
8143 return false;
8144}
8145
Aart Bik0148de42017-09-05 09:25:01 -07008146// Returns true iff instruction is the given integral constant.
8147inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8148 int64_t val = 0;
8149 return IsInt64AndGet(instruction, &val) && val == value;
8150}
8151
8152// Returns true iff instruction is a zero bit pattern.
8153inline bool IsZeroBitPattern(HInstruction* instruction) {
8154 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8155}
8156
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008157// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008158#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008159 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8160 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8161#undef INSTRUCTION_TYPE_CHECK
8162
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008163// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008164#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8165 std::is_base_of<BaseType, H##type>::value,
8166#define INSTRUCTION_TYPE_CHECK(type, super) \
8167 inline bool HInstruction::Is##type() const { \
8168 DCHECK_LT(GetKind(), kLastInstructionKind); \
8169 using BaseType = H##type; \
8170 static constexpr bool results[] = { \
8171 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8172 }; \
8173 return results[static_cast<size_t>(GetKind())]; \
8174 }
8175
8176 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8177#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008178#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008179
8180#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008181 inline const H##type* HInstruction::As##type() const { \
8182 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8183 } \
8184 inline H##type* HInstruction::As##type() { \
8185 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8186 }
8187
Vladimir Markoa90dd512018-05-04 15:04:45 +01008188 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8189#undef INSTRUCTION_TYPE_CAST
8190
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008191
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008192// Create space in `blocks` for adding `number_of_new_blocks` entries
8193// starting at location `at`. Blocks after `at` are moved accordingly.
8194inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8195 size_t number_of_new_blocks,
8196 size_t after) {
8197 DCHECK_LT(after, blocks->size());
8198 size_t old_size = blocks->size();
8199 size_t new_size = old_size + number_of_new_blocks;
8200 blocks->resize(new_size);
8201 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8202}
8203
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008204/*
8205 * Hunt "under the hood" of array lengths (leading to array references),
8206 * null checks (also leading to array references), and new arrays
8207 * (leading to the actual length). This makes it more likely related
8208 * instructions become actually comparable.
8209 */
8210inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8211 while (instruction->IsArrayLength() ||
8212 instruction->IsNullCheck() ||
8213 instruction->IsNewArray()) {
8214 instruction = instruction->IsNewArray()
8215 ? instruction->AsNewArray()->GetLength()
8216 : instruction->InputAt(0);
8217 }
8218 return instruction;
8219}
8220
Artem Serovb47b9782019-12-04 21:02:09 +00008221inline bool IsAddOrSub(const HInstruction* instruction) {
8222 return instruction->IsAdd() || instruction->IsSub();
8223}
8224
Artem Serov21c7e6f2017-07-27 16:04:42 +01008225void RemoveEnvironmentUses(HInstruction* instruction);
8226bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8227void ResetEnvironmentInputRecords(HInstruction* instruction);
8228
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01008229// Detects an instruction that is >= 0. As long as the value is carried by
8230// a single instruction, arithmetic wrap-around cannot occur.
8231bool IsGEZero(HInstruction* instruction);
8232
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008233} // namespace art
8234
8235#endif // ART_COMPILER_OPTIMIZING_NODES_H_