blob: 55e579baf4678544d10b5d1656601581001e0c4c [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();
2246 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002247 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002248 uses_.push_front(*new_node);
2249 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002250 }
2251
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002252 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002253 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002254 // Note: env_fixup_end remains valid across push_front().
2255 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2256 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002257 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002258 env_uses_.push_front(*new_node);
2259 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002260 }
2261
David Brazdil1abb4192015-02-17 18:33:36 +00002262 void RemoveAsUserOfInput(size_t input) {
2263 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002264 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2265 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2266 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002267 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002268
Vladimir Marko372f10e2016-05-17 16:30:10 +01002269 void RemoveAsUserOfAllInputs() {
2270 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2271 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2272 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2273 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2274 }
2275 }
2276
David Brazdil1abb4192015-02-17 18:33:36 +00002277 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2278 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002279
Vladimir Marko46817b82016-03-29 12:21:58 +01002280 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2281 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2282 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002283 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002284 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002285 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002286
Aart Bikcc42be02016-10-20 16:14:16 -07002287 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002288 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002289 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002290 !CanThrow() &&
2291 !IsSuspendCheck() &&
2292 !IsControlFlow() &&
2293 !IsNativeDebugInfo() &&
2294 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002295 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002296 !IsMemoryBarrier() &&
2297 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002298 }
2299
Aart Bikcc42be02016-10-20 16:14:16 -07002300 bool IsDeadAndRemovable() const {
2301 return IsRemovable() && !HasUses();
2302 }
2303
Roland Levillain6c82d402014-10-13 16:10:27 +01002304 // Does this instruction strictly dominate `other_instruction`?
2305 // Returns false if this instruction and `other_instruction` are the same.
2306 // Aborts if this instruction and `other_instruction` are both phis.
2307 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002308
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002309 int GetId() const { return id_; }
2310 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002311
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002312 int GetSsaIndex() const { return ssa_index_; }
2313 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2314 bool HasSsaIndex() const { return ssa_index_ != -1; }
2315
2316 bool HasEnvironment() const { return environment_ != nullptr; }
2317 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002318 // Set the `environment_` field. Raw because this method does not
2319 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002320 void SetRawEnvironment(HEnvironment* environment) {
2321 DCHECK(environment_ == nullptr);
2322 DCHECK_EQ(environment->GetHolder(), this);
2323 environment_ = environment;
2324 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002325
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002326 void InsertRawEnvironment(HEnvironment* environment) {
2327 DCHECK(environment_ != nullptr);
2328 DCHECK_EQ(environment->GetHolder(), this);
2329 DCHECK(environment->GetParent() == nullptr);
2330 environment->parent_ = environment_;
2331 environment_ = environment;
2332 }
2333
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002334 void RemoveEnvironment();
2335
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002336 // Set the environment of this instruction, copying it from `environment`. While
2337 // copying, the uses lists are being updated.
2338 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002339 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002340 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002341 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002342 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002343 if (environment->GetParent() != nullptr) {
2344 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2345 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002346 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002347
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002348 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2349 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002350 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002351 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002352 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002353 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002354 if (environment->GetParent() != nullptr) {
2355 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2356 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002357 }
2358
Nicolas Geoffray39468442014-09-02 15:17:15 +01002359 // Returns the number of entries in the environment. Typically, that is the
2360 // number of dex registers in a method. It could be more in case of inlining.
2361 size_t EnvironmentSize() const;
2362
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002363 LocationSummary* GetLocations() const { return locations_; }
2364 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002366 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002367 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002368 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002369 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002370
Alexandre Rames188d4312015-04-09 18:30:21 +01002371 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2372 // uses of this instruction by `other` are *not* updated.
2373 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2374 ReplaceWith(other);
2375 other->ReplaceInput(this, use_index);
2376 }
2377
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002378 // Move `this` instruction before `cursor`
2379 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002380
Vladimir Markofb337ea2015-11-25 15:25:10 +00002381 // Move `this` before its first user and out of any loops. If there is no
2382 // out-of-loop user that dominates all other users, move the instruction
2383 // to the end of the out-of-loop common dominator of the user's blocks.
2384 //
2385 // This can be used only on non-throwing instructions with no side effects that
2386 // have at least one use but no environment uses.
2387 void MoveBeforeFirstUserAndOutOfLoops();
2388
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002389#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002390 bool Is##type() const;
2391
2392 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2393#undef INSTRUCTION_TYPE_CHECK
2394
2395#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002396 const H##type* As##type() const; \
2397 H##type* As##type();
2398
Vladimir Markoa90dd512018-05-04 15:04:45 +01002399 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2400#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002401
Artem Serovcced8ba2017-07-19 18:18:09 +01002402 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2403 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2404 // the instruction then the behaviour of this function is undefined.
2405 //
2406 // Note: It is semantically valid to create a clone of the instruction only until
2407 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2408 // copied.
2409 //
2410 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2411 // 'explicit HInstruction(const HInstruction& other)' for details.
2412 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2413 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2414 DebugName() << " " << GetId();
2415 UNREACHABLE();
2416 }
2417
2418 // Return whether instruction can be cloned (copied).
2419 virtual bool IsClonable() const { return false; }
2420
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002421 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002422 // TODO: this method is used by LICM and GVN with possibly different
2423 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002424 virtual bool CanBeMoved() const { return false; }
2425
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002426 // Returns whether any data encoded in the two instructions is equal.
2427 // This method does not look at the inputs. Both instructions must be
2428 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002429 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002430 return false;
2431 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002432
2433 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002434 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002435 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002436 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002437
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302438 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002439
2440 virtual size_t ComputeHashCode() const {
2441 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002442 for (const HInstruction* input : GetInputs()) {
2443 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002444 }
2445 return result;
2446 }
2447
2448 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002449 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002450 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002451
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002452 size_t GetLifetimePosition() const { return lifetime_position_; }
2453 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2454 LiveInterval* GetLiveInterval() const { return live_interval_; }
2455 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2456 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2457
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002458 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2459
2460 // Returns whether the code generation of the instruction will require to have access
2461 // to the current method. Such instructions are:
2462 // (1): Instructions that require an environment, as calling the runtime requires
2463 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002464 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2465 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002466 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002467 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002468 }
2469
Vladimir Markodc151b22015-10-15 18:02:30 +01002470 // Returns whether the code generation of the instruction will require to have access
2471 // to the dex cache of the current method's declaring class via the current method.
2472 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002473
Mark Mendellc4701932015-04-10 13:18:51 -04002474 // Does this instruction have any use in an environment before
2475 // control flow hits 'other'?
2476 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2477
2478 // Remove all references to environment uses of this instruction.
2479 // The caller must ensure that this is safe to do.
2480 void RemoveEnvironmentUsers();
2481
Vladimir Markoa1de9182016-02-25 11:37:38 +00002482 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2483 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002484
David Brazdil1abb4192015-02-17 18:33:36 +00002485 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002486 // If set, the machine code for this instruction is assumed to be generated by
2487 // its users. Used by liveness analysis to compute use positions accordingly.
2488 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2489 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302490 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2491 static constexpr size_t kFieldInstructionKindSize =
2492 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002493 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302494 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002495 static constexpr size_t kFieldTypeSize =
2496 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2497 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002498 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2499
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302500 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2501 "Too many generic packed fields");
2502
Vladimir Markobd785672018-05-03 17:09:09 +01002503 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2504
Vladimir Marko372f10e2016-05-17 16:30:10 +01002505 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2506 return GetInputRecords()[i];
2507 }
2508
2509 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2510 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2511 input_records[index] = input;
2512 }
David Brazdil1abb4192015-02-17 18:33:36 +00002513
Vladimir Markoa1de9182016-02-25 11:37:38 +00002514 uint32_t GetPackedFields() const {
2515 return packed_fields_;
2516 }
2517
2518 template <size_t flag>
2519 bool GetPackedFlag() const {
2520 return (packed_fields_ & (1u << flag)) != 0u;
2521 }
2522
2523 template <size_t flag>
2524 void SetPackedFlag(bool value = true) {
2525 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2526 }
2527
2528 template <typename BitFieldType>
2529 typename BitFieldType::value_type GetPackedField() const {
2530 return BitFieldType::Decode(packed_fields_);
2531 }
2532
2533 template <typename BitFieldType>
2534 void SetPackedField(typename BitFieldType::value_type value) {
2535 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2536 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2537 }
2538
Artem Serovcced8ba2017-07-19 18:18:09 +01002539 // Copy construction for the instruction (used for Clone function).
2540 //
2541 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2542 // prepare_for_register_allocator are not copied (set to default values).
2543 //
2544 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2545 // fine for most of them. However for some of the instructions a custom copy constructor must be
2546 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2547 // for copying them).
2548 explicit HInstruction(const HInstruction& other)
2549 : previous_(nullptr),
2550 next_(nullptr),
2551 block_(nullptr),
2552 dex_pc_(other.dex_pc_),
2553 id_(-1),
2554 ssa_index_(-1),
2555 packed_fields_(other.packed_fields_),
2556 environment_(nullptr),
2557 locations_(nullptr),
2558 live_interval_(nullptr),
2559 lifetime_position_(kNoLifetime),
2560 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302561 reference_type_handle_(other.reference_type_handle_) {
2562 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002563
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002564 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302565 using InstructionKindField =
2566 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2567
Vladimir Marko46817b82016-03-29 12:21:58 +01002568 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2569 auto before_use_node = uses_.before_begin();
2570 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2571 HInstruction* user = use_node->GetUser();
2572 size_t input_index = use_node->GetIndex();
2573 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2574 before_use_node = use_node;
2575 }
2576 }
2577
2578 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2579 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2580 if (next != uses_.end()) {
2581 HInstruction* next_user = next->GetUser();
2582 size_t next_index = next->GetIndex();
2583 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2584 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2585 }
2586 }
2587
2588 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2589 auto before_env_use_node = env_uses_.before_begin();
2590 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2591 HEnvironment* user = env_use_node->GetUser();
2592 size_t input_index = env_use_node->GetIndex();
2593 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2594 before_env_use_node = env_use_node;
2595 }
2596 }
2597
2598 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2599 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2600 if (next != env_uses_.end()) {
2601 HEnvironment* next_user = next->GetUser();
2602 size_t next_index = next->GetIndex();
2603 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2604 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2605 }
2606 }
David Brazdil1abb4192015-02-17 18:33:36 +00002607
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002608 HInstruction* previous_;
2609 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002610 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002611 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002612
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002613 // An instruction gets an id when it is added to the graph.
2614 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002615 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002616 int id_;
2617
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002618 // When doing liveness analysis, instructions that have uses get an SSA index.
2619 int ssa_index_;
2620
Vladimir Markoa1de9182016-02-25 11:37:38 +00002621 // Packed fields.
2622 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002623
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002624 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002625 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002626
2627 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002628 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002629
Nicolas Geoffray39468442014-09-02 15:17:15 +01002630 // The environment associated with this instruction. Not null if the instruction
2631 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002632 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002633
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002634 // Set by the code generator.
2635 LocationSummary* locations_;
2636
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002637 // Set by the liveness analysis.
2638 LiveInterval* live_interval_;
2639
2640 // Set by the liveness analysis, this is the position in a linear
2641 // order of blocks where this instruction's live interval start.
2642 size_t lifetime_position_;
2643
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002644 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002645
Vladimir Markoa1de9182016-02-25 11:37:38 +00002646 // The reference handle part of the reference type info.
2647 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002648 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002649 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002650
David Brazdil1abb4192015-02-17 18:33:36 +00002651 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002652 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002653 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002654 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002655 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002656};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002657std::ostream& operator<<(std::ostream& os, HInstruction::InstructionKind rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002658
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002659// Iterates over the instructions, while preserving the next instruction
2660// in case the current instruction gets removed from the list by the user
2661// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002662class HInstructionIterator : public ValueObject {
2663 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002664 explicit HInstructionIterator(const HInstructionList& instructions)
2665 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002666 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002667 }
2668
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002669 bool Done() const { return instruction_ == nullptr; }
2670 HInstruction* Current() const { return instruction_; }
2671 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002672 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002673 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002674 }
2675
2676 private:
2677 HInstruction* instruction_;
2678 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002679
2680 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002681};
2682
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002683// Iterates over the instructions without saving the next instruction,
2684// therefore handling changes in the graph potentially made by the user
2685// of this iterator.
2686class HInstructionIteratorHandleChanges : public ValueObject {
2687 public:
2688 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2689 : instruction_(instructions.first_instruction_) {
2690 }
2691
2692 bool Done() const { return instruction_ == nullptr; }
2693 HInstruction* Current() const { return instruction_; }
2694 void Advance() {
2695 instruction_ = instruction_->GetNext();
2696 }
2697
2698 private:
2699 HInstruction* instruction_;
2700
2701 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2702};
2703
2704
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002705class HBackwardInstructionIterator : public ValueObject {
2706 public:
2707 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2708 : instruction_(instructions.last_instruction_) {
2709 next_ = Done() ? nullptr : instruction_->GetPrevious();
2710 }
2711
2712 bool Done() const { return instruction_ == nullptr; }
2713 HInstruction* Current() const { return instruction_; }
2714 void Advance() {
2715 instruction_ = next_;
2716 next_ = Done() ? nullptr : instruction_->GetPrevious();
2717 }
2718
2719 private:
2720 HInstruction* instruction_;
2721 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002722
2723 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002724};
2725
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002726class HVariableInputSizeInstruction : public HInstruction {
2727 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002728 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002729 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002730 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2731 }
2732
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002733 void AddInput(HInstruction* input);
2734 void InsertInputAt(size_t index, HInstruction* input);
2735 void RemoveInputAt(size_t index);
2736
Igor Murashkind01745e2017-04-05 16:40:31 -07002737 // Removes all the inputs.
2738 // Also removes this instructions from each input's use list
2739 // (for non-environment uses only).
2740 void RemoveAllInputs();
2741
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002742 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302743 HVariableInputSizeInstruction(InstructionKind inst_kind,
2744 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002745 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002746 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002747 size_t number_of_inputs,
2748 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302749 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002750 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002751 HVariableInputSizeInstruction(InstructionKind inst_kind,
2752 DataType::Type type,
2753 SideEffects side_effects,
2754 uint32_t dex_pc,
2755 ArenaAllocator* allocator,
2756 size_t number_of_inputs,
2757 ArenaAllocKind kind)
2758 : HInstruction(inst_kind, type, side_effects, dex_pc),
2759 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002760
Artem Serovcced8ba2017-07-19 18:18:09 +01002761 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002762
Artem Serovcced8ba2017-07-19 18:18:09 +01002763 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002764};
2765
Vladimir Markof9f64412015-09-02 14:05:49 +01002766template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002767class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002768 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002769 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302770 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002771 HExpression<N>(InstructionKind kind,
2772 DataType::Type type,
2773 SideEffects side_effects,
2774 uint32_t dex_pc)
2775 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2776 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002777
Vladimir Marko372f10e2016-05-17 16:30:10 +01002778 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002779 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002780 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002781 }
2782
Artem Serovcced8ba2017-07-19 18:18:09 +01002783 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002784 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002785
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002786 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002787 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002788
2789 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002790};
2791
Vladimir Markobd785672018-05-03 17:09:09 +01002792// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002793template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002794class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002795 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002796 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797
Vladimir Markobd785672018-05-03 17:09:09 +01002798 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002799
Vladimir Marko372f10e2016-05-17 16:30:10 +01002800 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002801 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002802 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002803 }
2804
Artem Serovcced8ba2017-07-19 18:18:09 +01002805 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002806 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002807
Vladimir Markof9f64412015-09-02 14:05:49 +01002808 private:
2809 friend class SsaBuilder;
2810};
2811
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002812// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2813// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002814class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002817 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302818 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002820 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002821
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002822 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002823
Artem Serovcced8ba2017-07-19 18:18:09 +01002824 protected:
2825 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002826};
2827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002828// Represents dex's RETURN opcodes. A HReturn is a control flow
2829// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002830class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002832 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002833 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002834 SetRawInputAt(0, value);
2835 }
2836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002837 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002838
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002839 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002840
Artem Serovcced8ba2017-07-19 18:18:09 +01002841 protected:
2842 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002843};
2844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002845class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002846 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002847 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002848 uint32_t reg_number,
2849 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002850 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002851 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002852 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302853 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002854 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002855 SideEffects::None(),
2856 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002857 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002858 number_of_inputs,
2859 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002860 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002861 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002862 // Phis are constructed live and marked dead if conflicting or unused.
2863 // Individual steps of SsaBuilder should assume that if a phi has been
2864 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2865 SetPackedFlag<kFlagIsLive>(true);
2866 SetPackedFlag<kFlagCanBeNull>(true);
2867 }
2868
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002869 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002870
David Brazdildee58d62016-04-07 09:54:26 +00002871 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 static DataType::Type ToPhiType(DataType::Type type) {
2873 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002874 }
2875
2876 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002879 // Make sure that only valid type changes occur. The following are allowed:
2880 // (1) int -> float/ref (primitive type propagation),
2881 // (2) long -> double (primitive type propagation).
2882 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2884 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2885 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002886 SetPackedField<TypeField>(new_type);
2887 }
2888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002889 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002890 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2891
2892 uint32_t GetRegNumber() const { return reg_number_; }
2893
2894 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2895 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2896 bool IsDead() const { return !IsLive(); }
2897 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2898
Vladimir Markoe9004912016-06-16 16:50:52 +01002899 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002900 return other != nullptr
2901 && other->IsPhi()
2902 && other->AsPhi()->GetBlock() == GetBlock()
2903 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2904 }
2905
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002906 bool HasEquivalentPhi() const {
2907 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2908 return true;
2909 }
2910 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2911 return true;
2912 }
2913 return false;
2914 }
2915
David Brazdildee58d62016-04-07 09:54:26 +00002916 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2917 // An equivalent phi is a phi having the same dex register and type.
2918 // It assumes that phis with the same dex register are adjacent.
2919 HPhi* GetNextEquivalentPhiWithSameType() {
2920 HInstruction* next = GetNext();
2921 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2922 if (next->GetType() == GetType()) {
2923 return next->AsPhi();
2924 }
2925 next = next->GetNext();
2926 }
2927 return nullptr;
2928 }
2929
2930 DECLARE_INSTRUCTION(Phi);
2931
Artem Serovcced8ba2017-07-19 18:18:09 +01002932 protected:
2933 DEFAULT_COPY_CONSTRUCTOR(Phi);
2934
David Brazdildee58d62016-04-07 09:54:26 +00002935 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002936 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002937 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2938 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2939 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002940
David Brazdildee58d62016-04-07 09:54:26 +00002941 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002942};
2943
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002944// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002945// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002946// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002947class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002948 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302949 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002950 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302951 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002953 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002954
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002955 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002956
Artem Serovcced8ba2017-07-19 18:18:09 +01002957 protected:
2958 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002959};
2960
2961// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002962class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002963 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302964 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002965 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302966 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002968 bool IsClonable() const override { return true; }
2969 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002970
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002971 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002972 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002973 }
2974
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002975 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002976
Artem Serovcced8ba2017-07-19 18:18:09 +01002977 protected:
2978 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002979};
2980
Roland Levillain9867bc72015-08-05 10:21:34 +01002981class HConstant : public HExpression<0> {
2982 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302983 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2984 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2985 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002987 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002988
Roland Levillain1a653882016-03-18 18:05:57 +00002989 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002991 // Is this constant 0 in the arithmetic sense?
2992 virtual bool IsArithmeticZero() const { return false; }
2993 // Is this constant a 0-bit pattern?
2994 virtual bool IsZeroBitPattern() const { return false; }
2995 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002996 virtual bool IsOne() const { return false; }
2997
David Brazdil77a48ae2015-09-15 12:34:04 +00002998 virtual uint64_t GetValueAsUint64() const = 0;
2999
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003000 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003001
Artem Serovcced8ba2017-07-19 18:18:09 +01003002 protected:
3003 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003004};
3005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003006class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003007 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003008 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01003009 return true;
3010 }
3011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003012 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003014 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003015
Roland Levillain1a653882016-03-18 18:05:57 +00003016 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07003017 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00003018
Roland Levillain9867bc72015-08-05 10:21:34 +01003019 DECLARE_INSTRUCTION(NullConstant);
3020
Artem Serovcced8ba2017-07-19 18:18:09 +01003021 protected:
3022 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
3023
Roland Levillain9867bc72015-08-05 10:21:34 +01003024 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003025 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303026 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
3027 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003028
3029 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003030};
3031
3032// Constants of the type int. Those can be from Dex instructions, or
3033// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003034class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003035 public:
3036 int32_t GetValue() const { return value_; }
3037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003038 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01003039 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
3040 }
David Brazdil77a48ae2015-09-15 12:34:04 +00003041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003042 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003043 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003044 return other->AsIntConstant()->value_ == value_;
3045 }
3046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003047 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003049 bool IsMinusOne() const override { return GetValue() == -1; }
3050 bool IsArithmeticZero() const override { return GetValue() == 0; }
3051 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3052 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003053
Roland Levillain1a653882016-03-18 18:05:57 +00003054 // Integer constants are used to encode Boolean values as well,
3055 // where 1 means true and 0 means false.
3056 bool IsTrue() const { return GetValue() == 1; }
3057 bool IsFalse() const { return GetValue() == 0; }
3058
Roland Levillain9867bc72015-08-05 10:21:34 +01003059 DECLARE_INSTRUCTION(IntConstant);
3060
Artem Serovcced8ba2017-07-19 18:18:09 +01003061 protected:
3062 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3063
Roland Levillain9867bc72015-08-05 10:21:34 +01003064 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003065 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303066 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3067 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003068 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303069 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3070 value_(value ? 1 : 0) {
3071 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003072
3073 const int32_t value_;
3074
3075 friend class HGraph;
3076 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3077 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003078};
3079
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003080class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003081 public:
3082 int64_t GetValue() const { return value_; }
3083
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003084 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003085
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003086 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003087 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003088 return other->AsLongConstant()->value_ == value_;
3089 }
3090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003091 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003093 bool IsMinusOne() const override { return GetValue() == -1; }
3094 bool IsArithmeticZero() const override { return GetValue() == 0; }
3095 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3096 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003097
3098 DECLARE_INSTRUCTION(LongConstant);
3099
Artem Serovcced8ba2017-07-19 18:18:09 +01003100 protected:
3101 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3102
Roland Levillain9867bc72015-08-05 10:21:34 +01003103 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003104 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303105 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3106 value_(value) {
3107 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003108
3109 const int64_t value_;
3110
3111 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003112};
Dave Allison20dfc792014-06-16 20:44:29 -07003113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003114class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 public:
3116 float GetValue() const { return value_; }
3117
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003118 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003119 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3120 }
3121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003122 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003123 DCHECK(other->IsFloatConstant()) << other->DebugName();
3124 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3125 }
3126
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003127 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003128
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003129 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003130 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3131 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003132 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003133 return std::fpclassify(value_) == FP_ZERO;
3134 }
3135 bool IsArithmeticPositiveZero() const {
3136 return IsArithmeticZero() && !std::signbit(value_);
3137 }
3138 bool IsArithmeticNegativeZero() const {
3139 return IsArithmeticZero() && std::signbit(value_);
3140 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003141 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003142 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003143 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003144 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003145 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3146 }
3147 bool IsNaN() const {
3148 return std::isnan(value_);
3149 }
3150
3151 DECLARE_INSTRUCTION(FloatConstant);
3152
Artem Serovcced8ba2017-07-19 18:18:09 +01003153 protected:
3154 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3155
Roland Levillain31dd3d62016-02-16 12:21:02 +00003156 private:
3157 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303158 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3159 value_(value) {
3160 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003161 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303162 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3163 value_(bit_cast<float, int32_t>(value)) {
3164 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003165
3166 const float value_;
3167
3168 // Only the SsaBuilder and HGraph can create floating-point constants.
3169 friend class SsaBuilder;
3170 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003171};
3172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003173class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003174 public:
3175 double GetValue() const { return value_; }
3176
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003177 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003179 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003180 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3181 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3182 }
3183
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003184 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003185
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003186 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003187 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3188 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003189 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003190 return std::fpclassify(value_) == FP_ZERO;
3191 }
3192 bool IsArithmeticPositiveZero() const {
3193 return IsArithmeticZero() && !std::signbit(value_);
3194 }
3195 bool IsArithmeticNegativeZero() const {
3196 return IsArithmeticZero() && std::signbit(value_);
3197 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003198 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003199 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003200 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003201 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003202 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3203 }
3204 bool IsNaN() const {
3205 return std::isnan(value_);
3206 }
3207
3208 DECLARE_INSTRUCTION(DoubleConstant);
3209
Artem Serovcced8ba2017-07-19 18:18:09 +01003210 protected:
3211 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3212
Roland Levillain31dd3d62016-02-16 12:21:02 +00003213 private:
3214 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303215 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3216 value_(value) {
3217 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003218 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303219 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3220 value_(bit_cast<double, int64_t>(value)) {
3221 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003222
3223 const double value_;
3224
3225 // Only the SsaBuilder and HGraph can create floating-point constants.
3226 friend class SsaBuilder;
3227 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003228};
3229
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003230// Conditional branch. A block ending with an HIf instruction must have
3231// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003232class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003233 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003234 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003235 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003236 SetRawInputAt(0, input);
3237 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003239 bool IsClonable() const override { return true; }
3240 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003241
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003242 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003243 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003244 }
3245
3246 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003247 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003248 }
3249
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003250 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003251
Artem Serovcced8ba2017-07-19 18:18:09 +01003252 protected:
3253 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003254};
3255
David Brazdilfc6a86a2015-06-26 10:33:45 +00003256
3257// Abstract instruction which marks the beginning and/or end of a try block and
3258// links it to the respective exception handlers. Behaves the same as a Goto in
3259// non-exceptional control flow.
3260// Normal-flow successor is stored at index zero, exception handlers under
3261// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003262class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003263 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003264 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003265 kEntry,
3266 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003267 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003268 };
3269
Artem Serovd1aa7d02018-06-22 11:35:46 +01003270 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3271 // across the catch block entering edges as GC might happen during throwing an exception.
3272 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3273 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003274 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003275 : HExpression(kTryBoundary,
3276 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3277 : SideEffects::None(),
3278 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003279 SetPackedField<BoundaryKindField>(kind);
3280 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003282 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003283
3284 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003285 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003286
David Brazdild26a4112015-11-10 11:07:31 +00003287 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3288 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3289 }
3290
David Brazdilfc6a86a2015-06-26 10:33:45 +00003291 // Returns whether `handler` is among its exception handlers (non-zero index
3292 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003293 bool HasExceptionHandler(const HBasicBlock& handler) const {
3294 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003295 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003296 }
3297
3298 // If not present already, adds `handler` to its block's list of exception
3299 // handlers.
3300 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003301 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003302 GetBlock()->AddSuccessor(handler);
3303 }
3304 }
3305
Vladimir Markoa1de9182016-02-25 11:37:38 +00003306 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3307 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003308
David Brazdilffee3d32015-07-06 11:48:53 +01003309 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3310
David Brazdilfc6a86a2015-06-26 10:33:45 +00003311 DECLARE_INSTRUCTION(TryBoundary);
3312
Artem Serovcced8ba2017-07-19 18:18:09 +01003313 protected:
3314 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3315
David Brazdilfc6a86a2015-06-26 10:33:45 +00003316 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003317 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3318 static constexpr size_t kFieldBoundaryKindSize =
3319 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3320 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3321 kFieldBoundaryKind + kFieldBoundaryKindSize;
3322 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3323 "Too many packed fields.");
3324 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003325};
3326
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003327// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003328class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003329 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003330 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3331 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003332 HDeoptimize(ArenaAllocator* allocator,
3333 HInstruction* cond,
3334 DeoptimizationKind kind,
3335 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003336 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303337 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003338 SideEffects::All(),
3339 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003340 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003341 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003342 kArenaAllocMisc) {
3343 SetPackedFlag<kFieldCanBeMoved>(false);
3344 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003345 SetRawInputAt(0, cond);
3346 }
3347
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003348 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003349
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003350 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3351 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3352 // instead of `guard`.
3353 // We set CanTriggerGC to prevent any intermediate address to be live
3354 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003355 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003356 HInstruction* cond,
3357 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003358 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003359 uint32_t dex_pc)
3360 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303361 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003362 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003363 SideEffects::CanTriggerGC(),
3364 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003365 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003366 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003367 kArenaAllocMisc) {
3368 SetPackedFlag<kFieldCanBeMoved>(true);
3369 SetPackedField<DeoptimizeKindField>(kind);
3370 SetRawInputAt(0, cond);
3371 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003372 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003373
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003374 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003376 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003377 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3378 }
3379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003380 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003381
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003382 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003383
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003384 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003385
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003386 bool GuardsAnInput() const {
3387 return InputCount() == 2;
3388 }
3389
3390 HInstruction* GuardedInput() const {
3391 DCHECK(GuardsAnInput());
3392 return InputAt(1);
3393 }
3394
3395 void RemoveGuard() {
3396 RemoveInputAt(1);
3397 }
3398
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003399 DECLARE_INSTRUCTION(Deoptimize);
3400
Artem Serovcced8ba2017-07-19 18:18:09 +01003401 protected:
3402 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3403
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003404 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003405 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3406 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3407 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003408 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003409 static constexpr size_t kNumberOfDeoptimizePackedBits =
3410 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3411 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3412 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003413 using DeoptimizeKindField =
3414 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003415};
3416
Mingyao Yang063fc772016-08-02 11:02:54 -07003417// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3418// The compiled code checks this flag value in a guard before devirtualized call and
3419// if it's true, starts to do deoptimization.
3420// It has a 4-byte slot on stack.
3421// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003422class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003423 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003424 // CHA guards are only optimized in a separate pass and it has no side effects
3425 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003426 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303427 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003428 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303429 SideEffects::None(),
3430 dex_pc,
3431 allocator,
3432 0,
3433 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003434 }
3435
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003436 // We do all CHA guard elimination/motion in a single pass, after which there is no
3437 // further guard elimination/motion since a guard might have been used for justification
3438 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3439 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003440 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003441
3442 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3443
Artem Serovcced8ba2017-07-19 18:18:09 +01003444 protected:
3445 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003446};
3447
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003448// Represents the ArtMethod that was passed as a first argument to
3449// the method. It is used by instructions that depend on it, like
3450// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003451class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003452 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003453 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303454 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3455 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003456
3457 DECLARE_INSTRUCTION(CurrentMethod);
3458
Artem Serovcced8ba2017-07-19 18:18:09 +01003459 protected:
3460 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003461};
3462
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003463// Fetches an ArtMethod from the virtual table or the interface method table
3464// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003465class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003466 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003467 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003468 kVTable,
3469 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003470 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003471 };
3472 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003473 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003474 TableKind kind,
3475 size_t index,
3476 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303477 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003478 index_(index) {
3479 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003480 SetRawInputAt(0, cls);
3481 }
3482
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003483 bool IsClonable() const override { return true; }
3484 bool CanBeMoved() const override { return true; }
3485 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003486 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003487 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003488 }
3489
Vladimir Markoa1de9182016-02-25 11:37:38 +00003490 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003491 size_t GetIndex() const { return index_; }
3492
3493 DECLARE_INSTRUCTION(ClassTableGet);
3494
Artem Serovcced8ba2017-07-19 18:18:09 +01003495 protected:
3496 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3497
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003498 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003499 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003500 static constexpr size_t kFieldTableKindSize =
3501 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3502 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3503 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3504 "Too many packed fields.");
3505 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3506
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003507 // The index of the ArtMethod in the table.
3508 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003509};
3510
Mark Mendellfe57faa2015-09-18 09:26:15 -04003511// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3512// have one successor for each entry in the switch table, and the final successor
3513// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003514class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003515 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003516 HPackedSwitch(int32_t start_value,
3517 uint32_t num_entries,
3518 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003519 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003520 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003521 start_value_(start_value),
3522 num_entries_(num_entries) {
3523 SetRawInputAt(0, input);
3524 }
3525
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003526 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003527
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003528 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003529
3530 int32_t GetStartValue() const { return start_value_; }
3531
Vladimir Marko211c2112015-09-24 16:52:33 +01003532 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003533
3534 HBasicBlock* GetDefaultBlock() const {
3535 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003536 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003537 }
3538 DECLARE_INSTRUCTION(PackedSwitch);
3539
Artem Serovcced8ba2017-07-19 18:18:09 +01003540 protected:
3541 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3542
Mark Mendellfe57faa2015-09-18 09:26:15 -04003543 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003544 const int32_t start_value_;
3545 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003546};
3547
Roland Levillain88cb1752014-10-20 16:36:47 +01003548class HUnaryOperation : public HExpression<1> {
3549 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303550 HUnaryOperation(InstructionKind kind,
3551 DataType::Type result_type,
3552 HInstruction* input,
3553 uint32_t dex_pc = kNoDexPc)
3554 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003555 SetRawInputAt(0, input);
3556 }
3557
Artem Serovcced8ba2017-07-19 18:18:09 +01003558 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003559 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003560
Roland Levillain88cb1752014-10-20 16:36:47 +01003561 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003562 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003564 bool CanBeMoved() const override { return true; }
3565 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003566 return true;
3567 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003568
Roland Levillain31dd3d62016-02-16 12:21:02 +00003569 // Try to statically evaluate `this` and return a HConstant
3570 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003571 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003572 HConstant* TryStaticEvaluation() const;
3573
3574 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003575 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3576 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003577 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3578 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003579
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003580 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003581
Artem Serovcced8ba2017-07-19 18:18:09 +01003582 protected:
3583 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003584};
3585
Dave Allison20dfc792014-06-16 20:44:29 -07003586class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003587 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303588 HBinaryOperation(InstructionKind kind,
3589 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003590 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003591 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003592 SideEffects side_effects = SideEffects::None(),
3593 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303594 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003595 SetRawInputAt(0, left);
3596 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003597 }
3598
Artem Serovcced8ba2017-07-19 18:18:09 +01003599 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003600 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003601
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003602 HInstruction* GetLeft() const { return InputAt(0); }
3603 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003604 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003605
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003606 virtual bool IsCommutative() const { return false; }
3607
3608 // Put constant on the right.
3609 // Returns whether order is changed.
3610 bool OrderInputsWithConstantOnTheRight() {
3611 HInstruction* left = InputAt(0);
3612 HInstruction* right = InputAt(1);
3613 if (left->IsConstant() && !right->IsConstant()) {
3614 ReplaceInput(right, 0);
3615 ReplaceInput(left, 1);
3616 return true;
3617 }
3618 return false;
3619 }
3620
3621 // Order inputs by instruction id, but favor constant on the right side.
3622 // This helps GVN for commutative ops.
3623 void OrderInputs() {
3624 DCHECK(IsCommutative());
3625 HInstruction* left = InputAt(0);
3626 HInstruction* right = InputAt(1);
3627 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3628 return;
3629 }
3630 if (OrderInputsWithConstantOnTheRight()) {
3631 return;
3632 }
3633 // Order according to instruction id.
3634 if (left->GetId() > right->GetId()) {
3635 ReplaceInput(right, 0);
3636 ReplaceInput(left, 1);
3637 }
3638 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003640 bool CanBeMoved() const override { return true; }
3641 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003642 return true;
3643 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003644
Roland Levillain31dd3d62016-02-16 12:21:02 +00003645 // Try to statically evaluate `this` and return a HConstant
3646 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003647 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003648 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003649
3650 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003651 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3652 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003653 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3654 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003656 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3657 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003658 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3659 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003660 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3661 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003662 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003663 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3664 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003665
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003666 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003667 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003668 HConstant* GetConstantRight() const;
3669
3670 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003671 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003672 HInstruction* GetLeastConstantLeft() const;
3673
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003674 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003675
Artem Serovcced8ba2017-07-19 18:18:09 +01003676 protected:
3677 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003678};
3679
Mark Mendellc4701932015-04-10 13:18:51 -04003680// The comparison bias applies for floating point operations and indicates how NaN
3681// comparisons are treated:
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003682enum class ComparisonBias { // private marker to avoid generate-operator-out.py from processing.
Mark Mendellc4701932015-04-10 13:18:51 -04003683 kNoBias, // bias is not applicable (i.e. for long operation)
3684 kGtBias, // return 1 for NaN comparisons
3685 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003686 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003687};
3688
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003689std::ostream& operator<<(std::ostream& os, ComparisonBias rhs);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003690
Dave Allison20dfc792014-06-16 20:44:29 -07003691class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003692 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303693 HCondition(InstructionKind kind,
3694 HInstruction* first,
3695 HInstruction* second,
3696 uint32_t dex_pc = kNoDexPc)
3697 : HBinaryOperation(kind,
3698 DataType::Type::kBool,
3699 first,
3700 second,
3701 SideEffects::None(),
3702 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003703 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3704 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003705
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003706 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003707 // `instruction`, and disregard moves in between.
3708 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003709
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003710 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003711
3712 virtual IfCondition GetCondition() const = 0;
3713
Mark Mendellc4701932015-04-10 13:18:51 -04003714 virtual IfCondition GetOppositeCondition() const = 0;
3715
Vladimir Markoa1de9182016-02-25 11:37:38 +00003716 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003717 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3718
Vladimir Markoa1de9182016-02-25 11:37:38 +00003719 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3720 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003721
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003722 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003723 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003724 }
3725
Roland Levillain4fa13f62015-07-06 18:11:54 +01003726 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003727 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003728 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003729 if (if_cond == kCondNE) {
3730 return true;
3731 } else if (if_cond == kCondEQ) {
3732 return false;
3733 }
3734 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003735 }
3736
3737 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003738 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003739 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003740 if (if_cond == kCondEQ) {
3741 return true;
3742 } else if (if_cond == kCondNE) {
3743 return false;
3744 }
3745 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003746 }
3747
Roland Levillain31dd3d62016-02-16 12:21:02 +00003748 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003749 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003750 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003751 static constexpr size_t kFieldComparisonBiasSize =
3752 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3753 static constexpr size_t kNumberOfConditionPackedBits =
3754 kFieldComparisonBias + kFieldComparisonBiasSize;
3755 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3756 using ComparisonBiasField =
3757 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3758
Roland Levillain31dd3d62016-02-16 12:21:02 +00003759 template <typename T>
3760 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3761
3762 template <typename T>
3763 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003764 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003765 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3766 // Handle the bias.
3767 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3768 }
3769
3770 // Return an integer constant containing the result of a condition evaluated at compile time.
3771 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3772 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3773 }
3774
Artem Serovcced8ba2017-07-19 18:18:09 +01003775 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003776};
3777
3778// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003779class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003780 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003781 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303782 : HCondition(kEqual, first, second, dex_pc) {
3783 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003785 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003786
Vladimir Marko9e23df52015-11-10 17:14:35 +00003787 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003788 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003789 return MakeConstantCondition(true, GetDexPc());
3790 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003792 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3793 }
3794 // In the following Evaluate methods, a HCompare instruction has
3795 // been merged into this HEqual instruction; evaluate it as
3796 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003797 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003798 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3799 GetDexPc());
3800 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003801 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003802 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3803 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003804 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003805 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003806 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003807
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003808 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003809
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003810 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003811 return kCondEQ;
3812 }
3813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003814 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003815 return kCondNE;
3816 }
3817
Artem Serovcced8ba2017-07-19 18:18:09 +01003818 protected:
3819 DEFAULT_COPY_CONSTRUCTOR(Equal);
3820
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003821 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003822 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003823};
3824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003825class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003826 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303827 HNotEqual(HInstruction* first, HInstruction* second,
3828 uint32_t dex_pc = kNoDexPc)
3829 : HCondition(kNotEqual, first, second, dex_pc) {
3830 }
Dave Allison20dfc792014-06-16 20:44:29 -07003831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003832 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003833
Vladimir Marko9e23df52015-11-10 17:14:35 +00003834 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003835 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003836 return MakeConstantCondition(false, GetDexPc());
3837 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003838 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003839 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3840 }
3841 // In the following Evaluate methods, a HCompare instruction has
3842 // been merged into this HNotEqual instruction; evaluate it as
3843 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003844 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003845 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3846 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003847 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003848 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3849 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003850 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003851 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003852 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003853
Dave Allison20dfc792014-06-16 20:44:29 -07003854 DECLARE_INSTRUCTION(NotEqual);
3855
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003856 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003857 return kCondNE;
3858 }
3859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003861 return kCondEQ;
3862 }
3863
Artem Serovcced8ba2017-07-19 18:18:09 +01003864 protected:
3865 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3866
Dave Allison20dfc792014-06-16 20:44:29 -07003867 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003868 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003869};
3870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003871class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003872 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303873 HLessThan(HInstruction* first, HInstruction* second,
3874 uint32_t dex_pc = kNoDexPc)
3875 : HCondition(kLessThan, first, second, dex_pc) {
3876 }
Dave Allison20dfc792014-06-16 20:44:29 -07003877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003878 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003879 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003880 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003881 // In the following Evaluate methods, a HCompare instruction has
3882 // been merged into this HLessThan instruction; evaluate it as
3883 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003885 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3886 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003887 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003888 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3889 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003890 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003891 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003892 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003893
Dave Allison20dfc792014-06-16 20:44:29 -07003894 DECLARE_INSTRUCTION(LessThan);
3895
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003896 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003897 return kCondLT;
3898 }
3899
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003900 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003901 return kCondGE;
3902 }
3903
Artem Serovcced8ba2017-07-19 18:18:09 +01003904 protected:
3905 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3906
Dave Allison20dfc792014-06-16 20:44:29 -07003907 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003908 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003909};
3910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003911class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003912 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303913 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3914 uint32_t dex_pc = kNoDexPc)
3915 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3916 }
Dave Allison20dfc792014-06-16 20:44:29 -07003917
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003918 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003919 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003920 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003921 // In the following Evaluate methods, a HCompare instruction has
3922 // been merged into this HLessThanOrEqual instruction; evaluate it as
3923 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003924 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003925 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3926 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003927 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003928 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3929 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003930 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003931 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003932 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003933
Dave Allison20dfc792014-06-16 20:44:29 -07003934 DECLARE_INSTRUCTION(LessThanOrEqual);
3935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003936 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003937 return kCondLE;
3938 }
3939
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003940 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003941 return kCondGT;
3942 }
3943
Artem Serovcced8ba2017-07-19 18:18:09 +01003944 protected:
3945 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3946
Dave Allison20dfc792014-06-16 20:44:29 -07003947 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003948 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003949};
3950
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003951class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003952 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003953 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303954 : HCondition(kGreaterThan, first, second, dex_pc) {
3955 }
Dave Allison20dfc792014-06-16 20:44:29 -07003956
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003957 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003958 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003959 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003960 // In the following Evaluate methods, a HCompare instruction has
3961 // been merged into this HGreaterThan instruction; evaluate it as
3962 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003963 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003964 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3965 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003966 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003967 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3968 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003969 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003970 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003971 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003972
Dave Allison20dfc792014-06-16 20:44:29 -07003973 DECLARE_INSTRUCTION(GreaterThan);
3974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003975 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003976 return kCondGT;
3977 }
3978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003979 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003980 return kCondLE;
3981 }
3982
Artem Serovcced8ba2017-07-19 18:18:09 +01003983 protected:
3984 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3985
Dave Allison20dfc792014-06-16 20:44:29 -07003986 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003987 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003988};
3989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003990class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003991 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003992 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303993 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3994 }
Dave Allison20dfc792014-06-16 20:44:29 -07003995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003996 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003997 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003998 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003999 // In the following Evaluate methods, a HCompare instruction has
4000 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
4001 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004002 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004003 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4004 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004005 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004006 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4007 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004008 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004009 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004010 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004011
Dave Allison20dfc792014-06-16 20:44:29 -07004012 DECLARE_INSTRUCTION(GreaterThanOrEqual);
4013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004014 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004015 return kCondGE;
4016 }
4017
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004018 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004019 return kCondLT;
4020 }
4021
Artem Serovcced8ba2017-07-19 18:18:09 +01004022 protected:
4023 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
4024
Dave Allison20dfc792014-06-16 20:44:29 -07004025 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004026 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004027};
4028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004029class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004030 public:
4031 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304032 : HCondition(kBelow, first, second, dex_pc) {
4033 }
Aart Bike9f37602015-10-09 11:15:55 -07004034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004035 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004036 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004037 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004038 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004039 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4040 }
4041 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004042 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004043 LOG(FATAL) << DebugName() << " is not defined for float values";
4044 UNREACHABLE();
4045 }
4046 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004047 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004048 LOG(FATAL) << DebugName() << " is not defined for double values";
4049 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004050 }
4051
4052 DECLARE_INSTRUCTION(Below);
4053
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004054 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004055 return kCondB;
4056 }
4057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004058 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004059 return kCondAE;
4060 }
4061
Artem Serovcced8ba2017-07-19 18:18:09 +01004062 protected:
4063 DEFAULT_COPY_CONSTRUCTOR(Below);
4064
Aart Bike9f37602015-10-09 11:15:55 -07004065 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004066 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004067 return MakeUnsigned(x) < MakeUnsigned(y);
4068 }
Aart Bike9f37602015-10-09 11:15:55 -07004069};
4070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004071class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004072 public:
4073 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304074 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4075 }
Aart Bike9f37602015-10-09 11:15:55 -07004076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004077 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004078 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004079 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004080 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004081 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4082 }
4083 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004084 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004085 LOG(FATAL) << DebugName() << " is not defined for float values";
4086 UNREACHABLE();
4087 }
4088 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004089 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004090 LOG(FATAL) << DebugName() << " is not defined for double values";
4091 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004092 }
4093
4094 DECLARE_INSTRUCTION(BelowOrEqual);
4095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004096 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004097 return kCondBE;
4098 }
4099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004100 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004101 return kCondA;
4102 }
4103
Artem Serovcced8ba2017-07-19 18:18:09 +01004104 protected:
4105 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4106
Aart Bike9f37602015-10-09 11:15:55 -07004107 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004108 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004109 return MakeUnsigned(x) <= MakeUnsigned(y);
4110 }
Aart Bike9f37602015-10-09 11:15:55 -07004111};
4112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004113class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004114 public:
4115 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304116 : HCondition(kAbove, first, second, dex_pc) {
4117 }
Aart Bike9f37602015-10-09 11:15:55 -07004118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004119 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004120 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004121 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004122 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004123 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4124 }
4125 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004126 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004127 LOG(FATAL) << DebugName() << " is not defined for float values";
4128 UNREACHABLE();
4129 }
4130 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004131 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004132 LOG(FATAL) << DebugName() << " is not defined for double values";
4133 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004134 }
4135
4136 DECLARE_INSTRUCTION(Above);
4137
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004138 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004139 return kCondA;
4140 }
4141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004142 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004143 return kCondBE;
4144 }
4145
Artem Serovcced8ba2017-07-19 18:18:09 +01004146 protected:
4147 DEFAULT_COPY_CONSTRUCTOR(Above);
4148
Aart Bike9f37602015-10-09 11:15:55 -07004149 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004150 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004151 return MakeUnsigned(x) > MakeUnsigned(y);
4152 }
Aart Bike9f37602015-10-09 11:15:55 -07004153};
4154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004155class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004156 public:
4157 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304158 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4159 }
Aart Bike9f37602015-10-09 11:15:55 -07004160
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004161 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004162 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004163 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004164 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004165 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4166 }
4167 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004168 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004169 LOG(FATAL) << DebugName() << " is not defined for float values";
4170 UNREACHABLE();
4171 }
4172 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004173 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004174 LOG(FATAL) << DebugName() << " is not defined for double values";
4175 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004176 }
4177
4178 DECLARE_INSTRUCTION(AboveOrEqual);
4179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004180 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004181 return kCondAE;
4182 }
4183
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004184 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004185 return kCondB;
4186 }
4187
Artem Serovcced8ba2017-07-19 18:18:09 +01004188 protected:
4189 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4190
Aart Bike9f37602015-10-09 11:15:55 -07004191 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004192 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004193 return MakeUnsigned(x) >= MakeUnsigned(y);
4194 }
Aart Bike9f37602015-10-09 11:15:55 -07004195};
Dave Allison20dfc792014-06-16 20:44:29 -07004196
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004197// Instruction to check how two inputs compare to each other.
4198// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004199class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004200 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004201 // Note that `comparison_type` is the type of comparison performed
4202 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004203 // HCompare instruction (which is always DataType::Type::kInt).
4204 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004205 HInstruction* first,
4206 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004207 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004208 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304209 : HBinaryOperation(kCompare,
4210 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004211 first,
4212 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004213 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004214 dex_pc) {
4215 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004216 }
4217
Roland Levillain9867bc72015-08-05 10:21:34 +01004218 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004219 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4220
4221 template <typename T>
4222 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004223 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004224 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4225 // Handle the bias.
4226 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4227 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004229 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004230 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4231 // reach this code path when processing a freshly built HIR
4232 // graph. However HCompare integer instructions can be synthesized
4233 // by the instruction simplifier to implement IntegerCompare and
4234 // IntegerSignum intrinsics, so we have to handle this case.
4235 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004236 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004237 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004238 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4239 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004240 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004241 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4242 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004243 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004244 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004245 }
4246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004247 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004248 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004249 }
4250
Vladimir Markoa1de9182016-02-25 11:37:38 +00004251 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004252
Roland Levillain31dd3d62016-02-16 12:21:02 +00004253 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004254 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004255 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004256 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004257 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004258 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004260 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004261 // Comparisons do not require a runtime call in any back end.
4262 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004263 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004264
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004265 DECLARE_INSTRUCTION(Compare);
4266
Roland Levillain31dd3d62016-02-16 12:21:02 +00004267 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004268 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004269 static constexpr size_t kFieldComparisonBiasSize =
4270 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4271 static constexpr size_t kNumberOfComparePackedBits =
4272 kFieldComparisonBias + kFieldComparisonBiasSize;
4273 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4274 using ComparisonBiasField =
4275 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4276
Roland Levillain31dd3d62016-02-16 12:21:02 +00004277 // Return an integer constant containing the result of a comparison evaluated at compile time.
4278 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4279 DCHECK(value == -1 || value == 0 || value == 1) << value;
4280 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4281 }
4282
Artem Serovcced8ba2017-07-19 18:18:09 +01004283 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004284};
4285
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004286class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004287 public:
4288 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004289 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004290 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004291 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004292 bool finalizable,
4293 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304294 : HExpression(kNewInstance,
4295 DataType::Type::kReference,
4296 SideEffects::CanTriggerGC(),
4297 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004298 type_index_(type_index),
4299 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004300 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004301 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004302 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004303 }
4304
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004305 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004306
Andreas Gampea5b09a62016-11-17 15:21:22 -08004307 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004308 const DexFile& GetDexFile() const { return dex_file_; }
4309
4310 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004311 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004312
Mingyao Yang062157f2016-03-02 10:15:36 -08004313 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004314 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004315
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004316 bool NeedsChecks() const {
4317 return entrypoint_ == kQuickAllocObjectWithChecks;
4318 }
David Brazdil6de19382016-01-08 17:37:10 +00004319
Vladimir Markoa1de9182016-02-25 11:37:38 +00004320 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004322 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004323
4324 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4325
4326 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4327 entrypoint_ = entrypoint;
4328 }
4329
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004330 HLoadClass* GetLoadClass() const {
4331 HInstruction* input = InputAt(0);
4332 if (input->IsClinitCheck()) {
4333 input = input->InputAt(0);
4334 }
4335 DCHECK(input->IsLoadClass());
4336 return input->AsLoadClass();
4337 }
4338
David Brazdil6de19382016-01-08 17:37:10 +00004339 bool IsStringAlloc() const;
4340
4341 DECLARE_INSTRUCTION(NewInstance);
4342
Artem Serovcced8ba2017-07-19 18:18:09 +01004343 protected:
4344 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4345
David Brazdil6de19382016-01-08 17:37:10 +00004346 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004347 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004348 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4349 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4350 "Too many packed fields.");
4351
Andreas Gampea5b09a62016-11-17 15:21:22 -08004352 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004353 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004354 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004355};
4356
Agi Csaki05f20562015-08-19 14:58:14 -07004357enum IntrinsicNeedsEnvironmentOrCache {
4358 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4359 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004360};
4361
Aart Bik5d75afe2015-12-14 11:57:01 -08004362enum IntrinsicSideEffects {
4363 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4364 kReadSideEffects, // Intrinsic may read heap memory.
4365 kWriteSideEffects, // Intrinsic may write heap memory.
4366 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4367};
4368
4369enum IntrinsicExceptions {
4370 kNoThrow, // Intrinsic does not throw any exceptions.
4371 kCanThrow // Intrinsic may throw exceptions.
4372};
4373
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004374class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004375 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004376 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004377
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004378 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004379 SetRawInputAt(index, argument);
4380 }
4381
Roland Levillain3e3d7332015-04-28 11:00:54 +01004382 // Return the number of arguments. This number can be lower than
4383 // the number of inputs returned by InputCount(), as some invoke
4384 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4385 // inputs at the end of their list of inputs.
4386 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4387
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004388 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4389
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004390 InvokeType GetInvokeType() const {
4391 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004392 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004393
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004394 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004395 return intrinsic_;
4396 }
4397
Aart Bik5d75afe2015-12-14 11:57:01 -08004398 void SetIntrinsic(Intrinsics intrinsic,
4399 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4400 IntrinsicSideEffects side_effects,
4401 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004402
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004403 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004404 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004405 }
4406
Aart Bikff7d89c2016-11-07 08:49:28 -08004407 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4408
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004409 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004410
Aart Bika8b8e9b2018-01-09 11:01:02 -08004411 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004413 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004414
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004415 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004417 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004418 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4419 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004420
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004421 uint32_t* GetIntrinsicOptimizations() {
4422 return &intrinsic_optimizations_;
4423 }
4424
4425 const uint32_t* GetIntrinsicOptimizations() const {
4426 return &intrinsic_optimizations_;
4427 }
4428
4429 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4430
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004431 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004432 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004433
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004434 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004435
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004436 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004437 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4438 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004439 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004440 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004441 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4442 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004443 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004444 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004445
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304446 HInvoke(InstructionKind kind,
4447 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004448 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004449 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004450 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004451 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004452 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004453 ArtMethod* resolved_method,
4454 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004455 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304456 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004457 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004458 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4459 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004460 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004461 number_of_arguments + number_of_other_inputs,
4462 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004463 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004464 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004465 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004466 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004467 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004468 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004469 // Check mutator lock, constructors lack annotalysis support.
4470 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4471 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004472 }
4473
Artem Serovcced8ba2017-07-19 18:18:09 +01004474 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4475
Roland Levillain3e3d7332015-04-28 11:00:54 +01004476 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004477 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004478 const uint32_t dex_method_index_;
4479 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004480
4481 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4482 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004483};
4484
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004485class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004486 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004487 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004488 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004489 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004490 uint32_t dex_pc,
4491 uint32_t dex_method_index,
4492 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304493 : HInvoke(kInvokeUnresolved,
4494 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004495 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004496 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004497 return_type,
4498 dex_pc,
4499 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004500 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004501 invoke_type) {
4502 }
4503
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004504 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004505
Calin Juravle175dc732015-08-25 15:42:32 +01004506 DECLARE_INSTRUCTION(InvokeUnresolved);
4507
Artem Serovcced8ba2017-07-19 18:18:09 +01004508 protected:
4509 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004510};
4511
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004512class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004513 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004514 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004515 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004516 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004517 uint32_t dex_pc,
Andra Danciua0130e82020-07-23 12:34:56 +00004518 uint32_t dex_method_index,
Andra Danciub931b8c2020-07-27 13:16:37 +00004519 // resolved_method is the ArtMethod object corresponding to the polymorphic
4520 // method (e.g. VarHandle.get), resolved using the class linker. It is needed
4521 // to pass intrinsic information to the HInvokePolymorphic node.
Andra Danciua0130e82020-07-23 12:34:56 +00004522 ArtMethod* resolved_method)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304523 : HInvoke(kInvokePolymorphic,
4524 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004525 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004526 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004527 return_type,
4528 dex_pc,
4529 dex_method_index,
Andra Danciua0130e82020-07-23 12:34:56 +00004530 resolved_method,
Orion Hodson20910192020-07-28 10:08:11 +01004531 kPolymorphic) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304532 }
Orion Hodsonac141392017-01-13 11:53:47 +00004533
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004534 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004535
Orion Hodsonac141392017-01-13 11:53:47 +00004536 DECLARE_INSTRUCTION(InvokePolymorphic);
4537
Artem Serovcced8ba2017-07-19 18:18:09 +01004538 protected:
4539 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004540};
4541
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004542class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004543 public:
4544 HInvokeCustom(ArenaAllocator* allocator,
4545 uint32_t number_of_arguments,
4546 uint32_t call_site_index,
4547 DataType::Type return_type,
4548 uint32_t dex_pc)
4549 : HInvoke(kInvokeCustom,
4550 allocator,
4551 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004552 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004553 return_type,
4554 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004555 /* dex_method_index= */ dex::kDexNoIndex,
4556 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004557 kStatic),
4558 call_site_index_(call_site_index) {
4559 }
4560
4561 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004563 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004564
4565 DECLARE_INSTRUCTION(InvokeCustom);
4566
4567 protected:
4568 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4569
4570 private:
4571 uint32_t call_site_index_;
4572};
4573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004574class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004575 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004576 // Requirements of this method call regarding the class
4577 // initialization (clinit) check of its declaring class.
Vladimir Marko9974e3c2020-06-10 16:27:06 +01004578 enum class ClinitCheckRequirement { // private marker to avoid generate-operator-out.py from processing.
Roland Levillain4c0eb422015-04-24 16:43:49 +01004579 kNone, // Class already initialized.
4580 kExplicit, // Static call having explicit clinit check as last input.
4581 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004582 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004583 };
4584
Vladimir Marko58155012015-08-19 12:49:41 +00004585 // Determines how to load the target ArtMethod*.
4586 enum class MethodLoadKind {
4587 // Use a String init ArtMethod* loaded from Thread entrypoints.
4588 kStringInit,
4589
4590 // Use the method's own ArtMethod* loaded by the register allocator.
4591 kRecursive,
4592
Vladimir Marko65979462017-05-19 17:25:12 +01004593 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4594 // Used for boot image methods referenced by boot image code.
4595 kBootImageLinkTimePcRelative,
4596
Vladimir Markob066d432018-01-03 13:14:37 +00004597 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4598 // Used for app->boot calls with relocatable image.
4599 kBootImageRelRo,
4600
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004601 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004602 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004603 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004604
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004605 // Use ArtMethod* at a known address, embed the direct address in the code.
4606 // Used for for JIT-compiled calls.
4607 kJitDirectAddress,
4608
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004609 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4610 // used when other kinds are unimplemented on a particular architecture.
4611 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004612 };
4613
4614 // Determines the location of the code pointer.
4615 enum class CodePtrLocation {
4616 // Recursive call, use local PC-relative call instruction.
4617 kCallSelf,
4618
Vladimir Marko86c87522020-05-11 16:55:55 +01004619 // Use native pointer from the Artmethod*.
4620 // Used for @CriticalNative to avoid going through the compiled stub. This call goes through
4621 // a special resolution stub if the class is not initialized or no native code is registered.
4622 kCallCriticalNative,
4623
Vladimir Marko58155012015-08-19 12:49:41 +00004624 // Use code pointer from the ArtMethod*.
4625 // Used when we don't know the target code. This is also the last-resort-kind used when
4626 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4627 kCallArtMethod,
4628 };
4629
4630 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004631 MethodLoadKind method_load_kind;
4632 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004633 // The method load data holds
4634 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4635 // Note that there are multiple string init methods, each having its own offset.
4636 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004637 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004638 };
4639
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004640 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004641 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004642 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004643 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004644 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004645 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004646 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004647 InvokeType invoke_type,
4648 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004649 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304650 : HInvoke(kInvokeStaticOrDirect,
4651 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004652 number_of_arguments,
Vladimir Marko86c87522020-05-11 16:55:55 +01004653 // There is potentially one extra argument for the HCurrentMethod input,
4654 // and one other if the clinit check is explicit. These can be removed later.
4655 (NeedsCurrentMethodInput(dispatch_info) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004656 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004657 return_type,
4658 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004659 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004660 resolved_method,
4661 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004662 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004663 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004664 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4665 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004666
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004667 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004668
Vladimir Marko86c87522020-05-11 16:55:55 +01004669 void SetDispatchInfo(DispatchInfo dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004670 bool had_current_method_input = HasCurrentMethodInput();
Vladimir Marko86c87522020-05-11 16:55:55 +01004671 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info);
Vladimir Markob554b5a2015-11-06 12:57:55 +00004672
4673 // Using the current method is the default and once we find a better
4674 // method load kind, we should not go back to using the current method.
4675 DCHECK(had_current_method_input || !needs_current_method_input);
4676
4677 if (had_current_method_input && !needs_current_method_input) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004678 DCHECK_EQ(InputAt(GetCurrentMethodIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4679 RemoveInputAt(GetCurrentMethodIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004680 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004681 dispatch_info_ = dispatch_info;
4682 }
4683
Aart Bik6daebeb2017-04-03 14:35:41 -07004684 DispatchInfo GetDispatchInfo() const {
4685 return dispatch_info_;
4686 }
4687
Vladimir Marko372f10e2016-05-17 16:30:10 +01004688 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004689 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004690 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4691 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4692 DCHECK(!input_records.empty());
4693 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4694 HInstruction* last_input = input_records.back().GetInstruction();
4695 // Note: `last_input` may be null during arguments setup.
4696 if (last_input != nullptr) {
4697 // `last_input` is the last input of a static invoke marked as having
4698 // an explicit clinit check. It must either be:
4699 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4700 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4701 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4702 }
4703 }
4704 return input_records;
4705 }
4706
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004707 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Vladimir Marko86c87522020-05-11 16:55:55 +01004708 // We do not access the method via object reference, so we cannot do an implicit null check.
Calin Juravle77520bc2015-01-12 18:45:46 +00004709 // TODO: for intrinsics we can generate implicit null checks.
4710 return false;
4711 }
4712
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004713 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004714 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004715 }
4716
Vladimir Marko58155012015-08-19 12:49:41 +00004717 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4718 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4719 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004720 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004721 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004722 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004723 bool HasPcRelativeMethodLoadKind() const {
4724 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004725 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004726 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004727 }
Vladimir Marko58155012015-08-19 12:49:41 +00004728
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004729 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004730 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004731 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004732 }
4733
4734 uint64_t GetMethodAddress() const {
4735 DCHECK(HasMethodAddress());
4736 return dispatch_info_.method_load_data;
4737 }
4738
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004739 const DexFile& GetDexFileForPcRelativeDexCache() const;
4740
Vladimir Markoa1de9182016-02-25 11:37:38 +00004741 ClinitCheckRequirement GetClinitCheckRequirement() const {
4742 return GetPackedField<ClinitCheckRequirementField>();
4743 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004744
Roland Levillain4c0eb422015-04-24 16:43:49 +01004745 // Is this instruction a call to a static method?
4746 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004747 return GetInvokeType() == kStatic;
4748 }
4749
4750 MethodReference GetTargetMethod() const {
4751 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004752 }
4753
Vladimir Marko86c87522020-05-11 16:55:55 +01004754 // Does this method load kind need the current method as an input?
4755 static bool NeedsCurrentMethodInput(DispatchInfo dispatch_info) {
4756 return dispatch_info.method_load_kind == MethodLoadKind::kRecursive ||
4757 dispatch_info.method_load_kind == MethodLoadKind::kRuntimeCall ||
4758 dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative;
4759 }
4760
4761 // Get the index of the current method input.
4762 size_t GetCurrentMethodIndex() const {
4763 DCHECK(HasCurrentMethodInput());
4764 return GetCurrentMethodIndexUnchecked();
4765 }
4766 size_t GetCurrentMethodIndexUnchecked() const {
4767 return GetNumberOfArguments();
4768 }
4769
4770 // Check if the method has a current method input.
4771 bool HasCurrentMethodInput() const {
4772 if (NeedsCurrentMethodInput(GetDispatchInfo())) {
4773 DCHECK(InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4774 InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4775 return true;
4776 } else {
4777 DCHECK(InputCount() == GetCurrentMethodIndexUnchecked() ||
4778 InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4779 !InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4780 return false;
4781 }
4782 }
4783
4784 // Get the index of the special input.
4785 size_t GetSpecialInputIndex() const {
4786 DCHECK(HasSpecialInput());
4787 return GetSpecialInputIndexUnchecked();
4788 }
4789 size_t GetSpecialInputIndexUnchecked() const {
4790 return GetNumberOfArguments() + (HasCurrentMethodInput() ? 1u : 0u);
4791 }
4792
4793 // Check if the method has a special input.
4794 bool HasSpecialInput() const {
4795 size_t other_inputs =
4796 GetSpecialInputIndexUnchecked() + (IsStaticWithExplicitClinitCheck() ? 1u : 0u);
4797 size_t input_count = InputCount();
4798 DCHECK_LE(input_count - other_inputs, 1u) << other_inputs << " " << input_count;
4799 return other_inputs != input_count;
4800 }
4801
4802 void AddSpecialInput(HInstruction* input) {
4803 // We allow only one special input.
4804 DCHECK(!HasSpecialInput());
4805 InsertInputAt(GetSpecialInputIndexUnchecked(), input);
4806 }
4807
Vladimir Markofbb184a2015-11-13 14:47:00 +00004808 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4809 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4810 // instruction; only relevant for static calls with explicit clinit check.
4811 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004812 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004813 size_t last_input_index = inputs_.size() - 1u;
4814 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004815 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004816 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004817 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004818 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004819 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004820 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004821 }
4822
4823 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004824 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004825 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004826 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004827 }
4828
4829 // Is this a call to a static method whose declaring class has an
4830 // implicit intialization check requirement?
4831 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004832 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004833 }
4834
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004835 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004836
Artem Serovcced8ba2017-07-19 18:18:09 +01004837 protected:
4838 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4839
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004840 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004841 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004842 static constexpr size_t kFieldClinitCheckRequirementSize =
4843 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4844 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4845 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4846 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4847 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004848 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4849 kFieldClinitCheckRequirement,
4850 kFieldClinitCheckRequirementSize>;
4851
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004852 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004853 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004854 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004855};
Vladimir Markof64242a2015-12-01 14:58:23 +00004856std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Marko86c87522020-05-11 16:55:55 +01004857std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::CodePtrLocation rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004858std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004860class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004861 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004862 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004863 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004864 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004865 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004866 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004867 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004868 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304869 : HInvoke(kInvokeVirtual,
4870 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004871 number_of_arguments,
4872 0u,
4873 return_type,
4874 dex_pc,
4875 dex_method_index,
4876 resolved_method,
4877 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304878 vtable_index_(vtable_index) {
4879 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004881 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004882
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004883 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004884 switch (GetIntrinsic()) {
4885 case Intrinsics::kThreadCurrentThread:
4886 case Intrinsics::kStringBufferAppend:
4887 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004888 case Intrinsics::kStringBuilderAppendObject:
4889 case Intrinsics::kStringBuilderAppendString:
4890 case Intrinsics::kStringBuilderAppendCharSequence:
4891 case Intrinsics::kStringBuilderAppendCharArray:
4892 case Intrinsics::kStringBuilderAppendBoolean:
4893 case Intrinsics::kStringBuilderAppendChar:
4894 case Intrinsics::kStringBuilderAppendInt:
4895 case Intrinsics::kStringBuilderAppendLong:
4896 case Intrinsics::kStringBuilderAppendFloat:
4897 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004898 case Intrinsics::kStringBuilderToString:
4899 return false;
4900 default:
4901 return HInvoke::CanBeNull();
4902 }
4903 }
4904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004905 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004906 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004907 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004908 }
4909
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004910 uint32_t GetVTableIndex() const { return vtable_index_; }
4911
4912 DECLARE_INSTRUCTION(InvokeVirtual);
4913
Artem Serovcced8ba2017-07-19 18:18:09 +01004914 protected:
4915 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4916
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004917 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004918 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004919 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004920};
4921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004922class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004923 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004924 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004925 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004926 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004927 uint32_t dex_pc,
4928 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004929 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004930 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304931 : HInvoke(kInvokeInterface,
4932 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004933 number_of_arguments,
4934 0u,
4935 return_type,
4936 dex_pc,
4937 dex_method_index,
4938 resolved_method,
4939 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304940 imt_index_(imt_index) {
4941 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004942
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004943 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004945 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004946 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004947 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004948 }
4949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004950 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004951 // The assembly stub currently needs it.
4952 return true;
4953 }
4954
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004955 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004956
4957 DECLARE_INSTRUCTION(InvokeInterface);
4958
Artem Serovcced8ba2017-07-19 18:18:09 +01004959 protected:
4960 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4961
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004962 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004963 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004964 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004965};
4966
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004967class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004968 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004969 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304970 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004971 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004972 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004973
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004974 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004976 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004977 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004978 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004979 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004981 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004982 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004983 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4984 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004985 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004986 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4987 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004988
Roland Levillain88cb1752014-10-20 16:36:47 +01004989 DECLARE_INSTRUCTION(Neg);
4990
Artem Serovcced8ba2017-07-19 18:18:09 +01004991 protected:
4992 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004993};
4994
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004995class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004996 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004997 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304998 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004999 SetRawInputAt(0, cls);
5000 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01005001 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005002 }
5003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005004 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005005
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005006 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005007 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005008
Mingyao Yang0c365e62015-03-31 15:09:29 -07005009 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005010 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07005011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005012 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005013
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005014 HLoadClass* GetLoadClass() const {
5015 DCHECK(InputAt(0)->IsLoadClass());
5016 return InputAt(0)->AsLoadClass();
5017 }
5018
5019 HInstruction* GetLength() const {
5020 return InputAt(1);
5021 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00005022
Vladimir Markob5461632018-10-15 14:24:21 +01005023 size_t GetComponentSizeShift() {
5024 return GetPackedField<ComponentSizeShiftField>();
5025 }
5026
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005027 DECLARE_INSTRUCTION(NewArray);
5028
Artem Serovcced8ba2017-07-19 18:18:09 +01005029 protected:
5030 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01005031
5032 private:
5033 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
5034 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
5035 static constexpr size_t kNumberOfNewArrayPackedBits =
5036 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
5037 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5038 using ComponentSizeShiftField =
5039 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005040};
5041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005042class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005043 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005044 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005045 HInstruction* left,
5046 HInstruction* right,
5047 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305048 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5049 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005051 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005052
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005053 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005055 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005056 return GetBlock()->GetGraph()->GetIntConstant(
5057 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005058 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005059 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005060 return GetBlock()->GetGraph()->GetLongConstant(
5061 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005062 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005063 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005064 return GetBlock()->GetGraph()->GetFloatConstant(
5065 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5066 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005067 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005068 return GetBlock()->GetGraph()->GetDoubleConstant(
5069 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5070 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005071
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005072 DECLARE_INSTRUCTION(Add);
5073
Artem Serovcced8ba2017-07-19 18:18:09 +01005074 protected:
5075 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005076};
5077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005078class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005079 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005080 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005081 HInstruction* left,
5082 HInstruction* right,
5083 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305084 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5085 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005086
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005087 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005088
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005089 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005090 return GetBlock()->GetGraph()->GetIntConstant(
5091 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005092 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005093 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005094 return GetBlock()->GetGraph()->GetLongConstant(
5095 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005096 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005097 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005098 return GetBlock()->GetGraph()->GetFloatConstant(
5099 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5100 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005101 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005102 return GetBlock()->GetGraph()->GetDoubleConstant(
5103 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5104 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005105
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005106 DECLARE_INSTRUCTION(Sub);
5107
Artem Serovcced8ba2017-07-19 18:18:09 +01005108 protected:
5109 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005110};
5111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005112class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005113 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005114 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005115 HInstruction* left,
5116 HInstruction* right,
5117 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305118 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5119 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005121 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005122
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005123 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005125 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005126 return GetBlock()->GetGraph()->GetIntConstant(
5127 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005128 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005129 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005130 return GetBlock()->GetGraph()->GetLongConstant(
5131 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005132 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005133 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005134 return GetBlock()->GetGraph()->GetFloatConstant(
5135 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5136 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005137 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005138 return GetBlock()->GetGraph()->GetDoubleConstant(
5139 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5140 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005141
5142 DECLARE_INSTRUCTION(Mul);
5143
Artem Serovcced8ba2017-07-19 18:18:09 +01005144 protected:
5145 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005146};
5147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005148class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005149 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005150 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005151 HInstruction* left,
5152 HInstruction* right,
5153 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305154 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5155 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005156
Roland Levillain9867bc72015-08-05 10:21:34 +01005157 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005158 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005159 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005160 // Our graph structure ensures we never have 0 for `y` during
5161 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005162 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005163 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005164 return (y == -1) ? -x : x / y;
5165 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005166
Roland Levillain31dd3d62016-02-16 12:21:02 +00005167 template <typename T>
5168 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005169 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005170 return x / y;
5171 }
5172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005173 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005174 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005175 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005176 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005177 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005178 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005179 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5180 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005181 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005182 return GetBlock()->GetGraph()->GetFloatConstant(
5183 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5184 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005185 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005186 return GetBlock()->GetGraph()->GetDoubleConstant(
5187 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005188 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005189
5190 DECLARE_INSTRUCTION(Div);
5191
Artem Serovcced8ba2017-07-19 18:18:09 +01005192 protected:
5193 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005194};
5195
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005196class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005197 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005198 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005199 HInstruction* left,
5200 HInstruction* right,
5201 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305202 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5203 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005204
Roland Levillain9867bc72015-08-05 10:21:34 +01005205 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005206 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005207 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005208 // Our graph structure ensures we never have 0 for `y` during
5209 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005210 DCHECK_NE(y, 0);
5211 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5212 return (y == -1) ? 0 : x % y;
5213 }
5214
Roland Levillain31dd3d62016-02-16 12:21:02 +00005215 template <typename T>
5216 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005217 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005218 return std::fmod(x, y);
5219 }
5220
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005221 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005222 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005223 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005224 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005225 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005226 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005227 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005228 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005229 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005230 return GetBlock()->GetGraph()->GetFloatConstant(
5231 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5232 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005233 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005234 return GetBlock()->GetGraph()->GetDoubleConstant(
5235 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5236 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005237
5238 DECLARE_INSTRUCTION(Rem);
5239
Artem Serovcced8ba2017-07-19 18:18:09 +01005240 protected:
5241 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005242};
5243
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005244class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005245 public:
5246 HMin(DataType::Type result_type,
5247 HInstruction* left,
5248 HInstruction* right,
5249 uint32_t dex_pc)
5250 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5251
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005252 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005253
5254 // Evaluation for integral values.
5255 template <typename T> static T ComputeIntegral(T x, T y) {
5256 return (x <= y) ? x : y;
5257 }
5258
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005259 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005260 return GetBlock()->GetGraph()->GetIntConstant(
5261 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5262 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005263 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005264 return GetBlock()->GetGraph()->GetLongConstant(
5265 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5266 }
5267 // TODO: Evaluation for floating-point values.
5268 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005269 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005270 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005271 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005272
5273 DECLARE_INSTRUCTION(Min);
5274
5275 protected:
5276 DEFAULT_COPY_CONSTRUCTOR(Min);
5277};
5278
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005279class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005280 public:
5281 HMax(DataType::Type result_type,
5282 HInstruction* left,
5283 HInstruction* right,
5284 uint32_t dex_pc)
5285 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5286
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005287 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005288
5289 // Evaluation for integral values.
5290 template <typename T> static T ComputeIntegral(T x, T y) {
5291 return (x >= y) ? x : y;
5292 }
5293
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005294 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005295 return GetBlock()->GetGraph()->GetIntConstant(
5296 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5297 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005298 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005299 return GetBlock()->GetGraph()->GetLongConstant(
5300 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5301 }
5302 // TODO: Evaluation for floating-point values.
5303 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005304 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005305 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005306 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005307
5308 DECLARE_INSTRUCTION(Max);
5309
5310 protected:
5311 DEFAULT_COPY_CONSTRUCTOR(Max);
5312};
5313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005314class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005315 public:
5316 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5317 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5318
5319 // Evaluation for integral values.
5320 template <typename T> static T ComputeIntegral(T x) {
5321 return x < 0 ? -x : x;
5322 }
5323
5324 // Evaluation for floating-point values.
5325 // Note, as a "quality of implementation", rather than pure "spec compliance",
5326 // we require that Math.abs() clears the sign bit (but changes nothing else)
5327 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5328 // http://b/30758343
5329 template <typename T, typename S> static T ComputeFP(T x) {
5330 S bits = bit_cast<S, T>(x);
5331 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5332 }
5333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005334 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005335 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5336 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005337 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005338 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5339 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005340 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005341 return GetBlock()->GetGraph()->GetFloatConstant(
5342 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5343 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005344 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005345 return GetBlock()->GetGraph()->GetDoubleConstant(
5346 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5347 }
5348
5349 DECLARE_INSTRUCTION(Abs);
5350
5351 protected:
5352 DEFAULT_COPY_CONSTRUCTOR(Abs);
5353};
5354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005355class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005356 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005357 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005358 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5359 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005360 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005361 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005362 SetRawInputAt(0, value);
5363 }
5364
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005365 bool IsClonable() const override { return true; }
5366 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005367
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005368 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005369 return true;
5370 }
5371
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005372 bool NeedsEnvironment() const override { return true; }
5373 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005374
Calin Juravled0d48522014-11-04 16:40:20 +00005375 DECLARE_INSTRUCTION(DivZeroCheck);
5376
Artem Serovcced8ba2017-07-19 18:18:09 +01005377 protected:
5378 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005379};
5380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005381class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005382 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005383 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005384 HInstruction* value,
5385 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005386 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305387 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005388 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5389 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005390 }
5391
Roland Levillain5b5b9312016-03-22 14:57:31 +00005392 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005393 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005394 return value << (distance & max_shift_distance);
5395 }
5396
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005397 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005398 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005399 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005400 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005401 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005402 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005403 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005404 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005405 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005406 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005407 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5408 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005409 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005410 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005411 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005412 LOG(FATAL) << DebugName() << " is not defined for float values";
5413 UNREACHABLE();
5414 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005415 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005416 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005417 LOG(FATAL) << DebugName() << " is not defined for double values";
5418 UNREACHABLE();
5419 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005420
5421 DECLARE_INSTRUCTION(Shl);
5422
Artem Serovcced8ba2017-07-19 18:18:09 +01005423 protected:
5424 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005425};
5426
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005427class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005428 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005429 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005430 HInstruction* value,
5431 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005432 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305433 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005434 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5435 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005436 }
5437
Roland Levillain5b5b9312016-03-22 14:57:31 +00005438 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005439 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005440 return value >> (distance & max_shift_distance);
5441 }
5442
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005443 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005444 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005445 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005446 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005447 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005448 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005449 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005450 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005451 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005452 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005453 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5454 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005455 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005456 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005457 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005458 LOG(FATAL) << DebugName() << " is not defined for float values";
5459 UNREACHABLE();
5460 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005461 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005463 LOG(FATAL) << DebugName() << " is not defined for double values";
5464 UNREACHABLE();
5465 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005466
5467 DECLARE_INSTRUCTION(Shr);
5468
Artem Serovcced8ba2017-07-19 18:18:09 +01005469 protected:
5470 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005471};
5472
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005473class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005474 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005475 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005476 HInstruction* value,
5477 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005478 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305479 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5481 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005482 }
5483
Roland Levillain5b5b9312016-03-22 14:57:31 +00005484 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005485 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005486 typedef typename std::make_unsigned<T>::type V;
5487 V ux = static_cast<V>(value);
5488 return static_cast<T>(ux >> (distance & max_shift_distance));
5489 }
5490
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005491 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005492 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005493 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005494 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005495 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005496 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005497 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005498 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005499 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005500 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005501 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5502 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005503 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005504 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005505 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005506 LOG(FATAL) << DebugName() << " is not defined for float values";
5507 UNREACHABLE();
5508 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005509 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005510 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005511 LOG(FATAL) << DebugName() << " is not defined for double values";
5512 UNREACHABLE();
5513 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005514
5515 DECLARE_INSTRUCTION(UShr);
5516
Artem Serovcced8ba2017-07-19 18:18:09 +01005517 protected:
5518 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005519};
5520
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005521class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005522 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005523 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005524 HInstruction* left,
5525 HInstruction* right,
5526 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305527 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5528 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005529
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005530 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005531
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005532 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005533
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005534 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005535 return GetBlock()->GetGraph()->GetIntConstant(
5536 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005537 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005538 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005539 return GetBlock()->GetGraph()->GetLongConstant(
5540 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005541 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005542 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005543 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005544 LOG(FATAL) << DebugName() << " is not defined for float values";
5545 UNREACHABLE();
5546 }
5547 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005548 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005549 LOG(FATAL) << DebugName() << " is not defined for double values";
5550 UNREACHABLE();
5551 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005552
5553 DECLARE_INSTRUCTION(And);
5554
Artem Serovcced8ba2017-07-19 18:18:09 +01005555 protected:
5556 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005557};
5558
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005559class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005560 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005561 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005562 HInstruction* left,
5563 HInstruction* right,
5564 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305565 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5566 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005567
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005568 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005569
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005570 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005571
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005572 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005573 return GetBlock()->GetGraph()->GetIntConstant(
5574 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005575 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005576 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005577 return GetBlock()->GetGraph()->GetLongConstant(
5578 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005579 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005580 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005581 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005582 LOG(FATAL) << DebugName() << " is not defined for float values";
5583 UNREACHABLE();
5584 }
5585 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005586 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005587 LOG(FATAL) << DebugName() << " is not defined for double values";
5588 UNREACHABLE();
5589 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005590
5591 DECLARE_INSTRUCTION(Or);
5592
Artem Serovcced8ba2017-07-19 18:18:09 +01005593 protected:
5594 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005595};
5596
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005597class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005598 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005599 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005600 HInstruction* left,
5601 HInstruction* right,
5602 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305603 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5604 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005605
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005606 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005607
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005608 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005609
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005610 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005611 return GetBlock()->GetGraph()->GetIntConstant(
5612 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005613 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005614 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005615 return GetBlock()->GetGraph()->GetLongConstant(
5616 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005617 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005618 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005619 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005620 LOG(FATAL) << DebugName() << " is not defined for float values";
5621 UNREACHABLE();
5622 }
5623 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005624 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005625 LOG(FATAL) << DebugName() << " is not defined for double values";
5626 UNREACHABLE();
5627 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005628
5629 DECLARE_INSTRUCTION(Xor);
5630
Artem Serovcced8ba2017-07-19 18:18:09 +01005631 protected:
5632 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005633};
5634
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005635class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005636 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005637 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305638 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005639 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005640
Roland Levillain5b5b9312016-03-22 14:57:31 +00005641 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005642 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005643 typedef typename std::make_unsigned<T>::type V;
5644 V ux = static_cast<V>(value);
5645 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005646 return static_cast<T>(ux);
5647 } else {
5648 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005649 return static_cast<T>(ux >> (distance & max_shift_value)) |
5650 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005651 }
5652 }
5653
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005654 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005655 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005656 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005657 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005658 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005659 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005660 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005661 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005662 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005663 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005664 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5665 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005666 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005667 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005668 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005669 LOG(FATAL) << DebugName() << " is not defined for float values";
5670 UNREACHABLE();
5671 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005672 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005673 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005674 LOG(FATAL) << DebugName() << " is not defined for double values";
5675 UNREACHABLE();
5676 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005677
5678 DECLARE_INSTRUCTION(Ror);
5679
Artem Serovcced8ba2017-07-19 18:18:09 +01005680 protected:
5681 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005682};
5683
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005684// The value of a parameter in this method. Its location depends on
5685// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005686class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005687 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005688 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005689 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005690 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005691 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005692 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305693 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005694 dex_file_(dex_file),
5695 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005696 index_(index) {
5697 SetPackedFlag<kFlagIsThis>(is_this);
5698 SetPackedFlag<kFlagCanBeNull>(!is_this);
5699 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005700
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005701 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005702 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005703 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005704 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005705
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005706 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005707 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005708
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005709 DECLARE_INSTRUCTION(ParameterValue);
5710
Artem Serovcced8ba2017-07-19 18:18:09 +01005711 protected:
5712 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5713
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005714 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005715 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005716 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005717 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5718 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5719 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5720 "Too many packed fields.");
5721
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005722 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005723 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005724 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005725 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005726 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005727};
5728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005729class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005730 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005731 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305732 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5733 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005734
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005735 bool CanBeMoved() const override { return true; }
5736 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005737 return true;
5738 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005739
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005740 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005741
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005742 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005743 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005744 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005745 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005746 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005747 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005748 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005749 LOG(FATAL) << DebugName() << " is not defined for float values";
5750 UNREACHABLE();
5751 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005752 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005753 LOG(FATAL) << DebugName() << " is not defined for double values";
5754 UNREACHABLE();
5755 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005756
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005757 DECLARE_INSTRUCTION(Not);
5758
Artem Serovcced8ba2017-07-19 18:18:09 +01005759 protected:
5760 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005761};
5762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005763class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005764 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005765 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305766 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5767 }
David Brazdil66d126e2015-04-03 16:02:44 +01005768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005769 bool CanBeMoved() const override { return true; }
5770 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005771 return true;
5772 }
5773
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005774 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005775 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005776 return !x;
5777 }
5778
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005779 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005780 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005781 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005782 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005783 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005784 UNREACHABLE();
5785 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005786 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005787 LOG(FATAL) << DebugName() << " is not defined for float values";
5788 UNREACHABLE();
5789 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005790 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005791 LOG(FATAL) << DebugName() << " is not defined for double values";
5792 UNREACHABLE();
5793 }
David Brazdil66d126e2015-04-03 16:02:44 +01005794
5795 DECLARE_INSTRUCTION(BooleanNot);
5796
Artem Serovcced8ba2017-07-19 18:18:09 +01005797 protected:
5798 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005799};
5800
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005801class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005802 public:
5803 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005804 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305805 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005806 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005807 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005808 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005809 }
5810
5811 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005812 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5813 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005815 bool IsClonable() const override { return true; }
5816 bool CanBeMoved() const override { return true; }
5817 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005818 return true;
5819 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005820 // Return whether the conversion is implicit. This includes conversion to the same type.
5821 bool IsImplicitConversion() const {
5822 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5823 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005824
Mark Mendelle82549b2015-05-06 10:55:34 -04005825 // Try to statically evaluate the conversion and return a HConstant
5826 // containing the result. If the input cannot be converted, return nullptr.
5827 HConstant* TryStaticEvaluation() const;
5828
Roland Levillaindff1f282014-11-05 14:15:05 +00005829 DECLARE_INSTRUCTION(TypeConversion);
5830
Artem Serovcced8ba2017-07-19 18:18:09 +01005831 protected:
5832 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005833};
5834
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005835static constexpr uint32_t kNoRegNumber = -1;
5836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005837class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005838 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005839 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005840 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5841 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005842 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005843 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005844 SetRawInputAt(0, value);
5845 }
5846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005847 bool IsClonable() const override { return true; }
5848 bool CanBeMoved() const override { return true; }
5849 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005850 return true;
5851 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005852
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005853 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005854
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005855 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005856
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005857 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005858
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005859 DECLARE_INSTRUCTION(NullCheck);
5860
Artem Serovcced8ba2017-07-19 18:18:09 +01005861 protected:
5862 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005863};
5864
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005865// Embeds an ArtField and all the information required by the compiler. We cache
5866// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005867class FieldInfo : public ValueObject {
5868 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005869 FieldInfo(ArtField* field,
5870 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005871 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005872 bool is_volatile,
5873 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005874 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005875 const DexFile& dex_file)
5876 : field_(field),
5877 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005878 field_type_(field_type),
5879 is_volatile_(is_volatile),
5880 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005881 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005882 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005883
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005884 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005885 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005887 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005888 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005889 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005890 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005891
5892 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005893 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005894 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005896 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005897 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005898 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005899 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005900};
5901
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005902class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005903 public:
5904 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005905 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005906 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005907 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005908 bool is_volatile,
5909 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005910 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005911 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005912 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305913 : HExpression(kInstanceFieldGet,
5914 field_type,
5915 SideEffects::FieldReadOfType(field_type, is_volatile),
5916 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005917 field_info_(field,
5918 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005919 field_type,
5920 is_volatile,
5921 field_idx,
5922 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005923 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005924 SetRawInputAt(0, value);
5925 }
5926
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005927 bool IsClonable() const override { return true; }
5928 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005930 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005931 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005932 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005933 }
5934
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005935 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005936 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005937 }
5938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005939 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005940 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5941 }
5942
Calin Juravle52c48962014-12-16 17:02:57 +00005943 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005944 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005945 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005946 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005947
Vladimir Marko61b92282017-10-11 13:23:17 +01005948 void SetType(DataType::Type new_type) {
5949 DCHECK(DataType::IsIntegralType(GetType()));
5950 DCHECK(DataType::IsIntegralType(new_type));
5951 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5952 SetPackedField<TypeField>(new_type);
5953 }
5954
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005955 DECLARE_INSTRUCTION(InstanceFieldGet);
5956
Artem Serovcced8ba2017-07-19 18:18:09 +01005957 protected:
5958 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5959
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005960 private:
5961 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005962};
5963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005964class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005965 public:
5966 HInstanceFieldSet(HInstruction* object,
5967 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005968 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005969 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005970 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005971 bool is_volatile,
5972 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005973 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005974 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005975 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005976 : HExpression(kInstanceFieldSet,
5977 SideEffects::FieldWriteOfType(field_type, is_volatile),
5978 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005979 field_info_(field,
5980 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005981 field_type,
5982 is_volatile,
5983 field_idx,
5984 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005985 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005986 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005987 SetRawInputAt(0, object);
5988 SetRawInputAt(1, value);
5989 }
5990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005991 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005992
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005993 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005994 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005995 }
5996
Calin Juravle52c48962014-12-16 17:02:57 +00005997 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005998 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005999 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006000 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006001 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006002 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6003 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006004
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006005 DECLARE_INSTRUCTION(InstanceFieldSet);
6006
Artem Serovcced8ba2017-07-19 18:18:09 +01006007 protected:
6008 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
6009
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006010 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006011 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6012 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
6013 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
6014 "Too many packed fields.");
6015
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006016 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006017};
6018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006019class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006020 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006021 HArrayGet(HInstruction* array,
6022 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006023 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006024 uint32_t dex_pc)
6025 : HArrayGet(array,
6026 index,
6027 type,
6028 SideEffects::ArrayReadOfType(type),
6029 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08006030 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306031 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006032
6033 HArrayGet(HInstruction* array,
6034 HInstruction* index,
6035 DataType::Type type,
6036 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006037 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006038 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306039 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006040 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006041 SetRawInputAt(0, array);
6042 SetRawInputAt(1, index);
6043 }
6044
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006045 bool IsClonable() const override { return true; }
6046 bool CanBeMoved() const override { return true; }
6047 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006048 return true;
6049 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006050 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006051 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006052 // Currently, unless the array is the result of NewArray, the array access is always
6053 // preceded by some form of null NullCheck necessary for the bounds check, usually
6054 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6055 // dynamic BCE. There are cases when these could be removed to produce better code.
6056 // If we ever add optimizations to do so we should allow an implicit check here
6057 // (as long as the address falls in the first page).
6058 //
6059 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6060 // a = cond ? new int[1] : null;
6061 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006062 return false;
6063 }
6064
David Brazdil4833f5a2015-12-16 10:37:39 +00006065 bool IsEquivalentOf(HArrayGet* other) const {
6066 bool result = (GetDexPc() == other->GetDexPc());
6067 if (kIsDebugBuild && result) {
6068 DCHECK_EQ(GetBlock(), other->GetBlock());
6069 DCHECK_EQ(GetArray(), other->GetArray());
6070 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006071 if (DataType::IsIntOrLongType(GetType())) {
6072 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006073 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006074 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6075 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006076 }
6077 }
6078 return result;
6079 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006080
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006081 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6082
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006083 HInstruction* GetArray() const { return InputAt(0); }
6084 HInstruction* GetIndex() const { return InputAt(1); }
6085
Vladimir Marko61b92282017-10-11 13:23:17 +01006086 void SetType(DataType::Type new_type) {
6087 DCHECK(DataType::IsIntegralType(GetType()));
6088 DCHECK(DataType::IsIntegralType(new_type));
6089 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6090 SetPackedField<TypeField>(new_type);
6091 }
6092
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006093 DECLARE_INSTRUCTION(ArrayGet);
6094
Artem Serovcced8ba2017-07-19 18:18:09 +01006095 protected:
6096 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6097
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006098 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006099 // We treat a String as an array, creating the HArrayGet from String.charAt()
6100 // intrinsic in the instruction simplifier. We can always determine whether
6101 // a particular HArrayGet is actually a String.charAt() by looking at the type
6102 // of the input but that requires holding the mutator lock, so we prefer to use
6103 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006104 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006105 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6106 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6107 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006108};
6109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006110class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006111 public:
6112 HArraySet(HInstruction* array,
6113 HInstruction* index,
6114 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006115 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006116 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006117 : HArraySet(array,
6118 index,
6119 value,
6120 expected_component_type,
6121 // Make a best guess for side effects now, may be refined during SSA building.
6122 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306123 dex_pc) {
6124 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006125
6126 HArraySet(HInstruction* array,
6127 HInstruction* index,
6128 HInstruction* value,
6129 DataType::Type expected_component_type,
6130 SideEffects side_effects,
6131 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006132 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006133 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006134 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006135 SetPackedFlag<kFlagValueCanBeNull>(true);
6136 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006137 SetRawInputAt(0, array);
6138 SetRawInputAt(1, index);
6139 SetRawInputAt(2, value);
6140 }
6141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006142 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006143
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006144 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006145 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006146 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006147 }
6148
Mingyao Yang81014cb2015-06-02 03:16:27 -07006149 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006150 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006152 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006153 // TODO: Same as for ArrayGet.
6154 return false;
6155 }
6156
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006157 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006158 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006159 }
6160
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006161 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006162 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006163 }
6164
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006165 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006166 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006167 }
6168
Vladimir Markoa1de9182016-02-25 11:37:38 +00006169 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6170 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6171 bool StaticTypeOfArrayIsObjectArray() const {
6172 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6173 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006174
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006175 HInstruction* GetArray() const { return InputAt(0); }
6176 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006177 HInstruction* GetValue() const { return InputAt(2); }
6178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006179 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006180 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6181 }
6182
6183 static DataType::Type GetComponentType(DataType::Type value_type,
6184 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006185 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006186 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006187 // be correct, we also check what is the value type. If it is a floating
6188 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006189 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006190 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006191 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006192 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006194 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006195 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006196 }
6197
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006198 static SideEffects ComputeSideEffects(DataType::Type type) {
6199 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006200 }
6201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006202 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6203 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6204 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006205 }
6206
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006207 DECLARE_INSTRUCTION(ArraySet);
6208
Artem Serovcced8ba2017-07-19 18:18:09 +01006209 protected:
6210 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6211
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006212 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006213 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6214 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006215 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006216 static constexpr size_t kFlagNeedsTypeCheck =
6217 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6218 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006219 // Cached information for the reference_type_info_ so that codegen
6220 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006221 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6222 static constexpr size_t kNumberOfArraySetPackedBits =
6223 kFlagStaticTypeOfArrayIsObjectArray + 1;
6224 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6225 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006226 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006227};
6228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006229class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006230 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006231 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306232 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006233 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006234 // Note that arrays do not change length, so the instruction does not
6235 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006236 SetRawInputAt(0, array);
6237 }
6238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006239 bool IsClonable() const override { return true; }
6240 bool CanBeMoved() const override { return true; }
6241 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006242 return true;
6243 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006244 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006245 return obj == InputAt(0);
6246 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006247
Vladimir Markodce016e2016-04-28 13:10:02 +01006248 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6249
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006250 DECLARE_INSTRUCTION(ArrayLength);
6251
Artem Serovcced8ba2017-07-19 18:18:09 +01006252 protected:
6253 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6254
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006255 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006256 // We treat a String as an array, creating the HArrayLength from String.length()
6257 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6258 // determine whether a particular HArrayLength is actually a String.length() by
6259 // looking at the type of the input but that requires holding the mutator lock, so
6260 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006261 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006262 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6263 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6264 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006265};
6266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006267class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006268 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006269 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006270 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6271 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006272 HBoundsCheck(HInstruction* index,
6273 HInstruction* length,
6274 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006275 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006276 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006277 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006278 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006279 SetRawInputAt(0, index);
6280 SetRawInputAt(1, length);
6281 }
6282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006283 bool IsClonable() const override { return true; }
6284 bool CanBeMoved() const override { return true; }
6285 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006286 return true;
6287 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006288
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006289 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006291 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006292
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006293 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006294
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006295 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006296
6297 DECLARE_INSTRUCTION(BoundsCheck);
6298
Artem Serovcced8ba2017-07-19 18:18:09 +01006299 protected:
6300 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6301
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006302 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006303 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006304 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6305 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6306 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006307};
6308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006309class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006310 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006311 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006312 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306313 slow_path_(nullptr) {
6314 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006316 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006317
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006318 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006319 return true;
6320 }
6321
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006322 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6323 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006324
6325 DECLARE_INSTRUCTION(SuspendCheck);
6326
Artem Serovcced8ba2017-07-19 18:18:09 +01006327 protected:
6328 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6329
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006330 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006331 // Only used for code generation, in order to share the same slow path between back edges
6332 // of a same loop.
6333 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006334};
6335
David Srbecky0cf44932015-12-09 14:09:59 +00006336// Pseudo-instruction which provides the native debugger with mapping information.
6337// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006338class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006339 public:
6340 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006341 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306342 }
David Srbecky0cf44932015-12-09 14:09:59 +00006343
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006344 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006345 return true;
6346 }
6347
6348 DECLARE_INSTRUCTION(NativeDebugInfo);
6349
Artem Serovcced8ba2017-07-19 18:18:09 +01006350 protected:
6351 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006352};
6353
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006354/**
6355 * Instruction to load a Class object.
6356 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006357class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006358 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006359 // Determines how to load the Class.
6360 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006361 // We cannot load this class. See HSharpening::SharpenLoadClass.
6362 kInvalid = -1,
6363
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006364 // Use the Class* from the method's own ArtMethod*.
6365 kReferrersClass,
6366
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006367 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006368 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006369 kBootImageLinkTimePcRelative,
6370
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006371 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006372 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006373 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006374
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006375 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006376 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006377 kBssEntry,
6378
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006379 // Use a known boot image Class* address, embedded in the code by the codegen.
6380 // Used for boot image classes referenced by apps in JIT-compiled code.
6381 kJitBootImageAddress,
6382
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006383 // Load from the root table associated with the JIT compiled method.
6384 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006385
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006386 // Load using a simple runtime call. This is the fall-back load kind when
6387 // the codegen is unable to use another appropriate kind.
6388 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006389
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006390 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006391 };
6392
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006393 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006394 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006395 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006396 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006397 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006398 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006399 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006400 : HInstruction(kLoadClass,
6401 DataType::Type::kReference,
6402 SideEffectsForArchRuntimeCalls(),
6403 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006404 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006405 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006406 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006407 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006408 // Referrers class should not need access check. We never inline unverified
6409 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 DCHECK(!is_referrers_class || !needs_access_check);
6411
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006412 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006413 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006414 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006415 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006416 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006417 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006418 }
6419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006420 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006421
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006422 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006423
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006424 LoadKind GetLoadKind() const {
6425 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006426 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006427
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006428 bool HasPcRelativeLoadKind() const {
6429 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6430 GetLoadKind() == LoadKind::kBootImageRelRo ||
6431 GetLoadKind() == LoadKind::kBssEntry;
6432 }
6433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006434 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006435
Yi Kong39402542019-03-24 02:47:16 -07006436 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006437
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006438 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006439
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006440 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006441
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006442 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006443 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006444 }
6445
Calin Juravle0ba218d2015-05-19 18:46:01 +01006446 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006447 // The entrypoint the code generator is going to call does not do
6448 // clinit of the class.
6449 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006450 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006451 }
6452
6453 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006454 return NeedsAccessCheck() ||
6455 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006456 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006457 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006458 }
6459
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006460 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006461 return NeedsAccessCheck() ||
6462 MustGenerateClinitCheck() ||
6463 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006464 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006465 GetLoadKind() == LoadKind::kBssEntry) &&
6466 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006467 }
6468
Calin Juravleacf735c2015-02-12 15:25:22 +00006469 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006470 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6471 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006472 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006473 } else {
6474 return ReferenceTypeInfo::CreateInvalid();
6475 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006476 }
6477
Vladimir Marko175e7862018-03-27 09:03:13 +00006478 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6479 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6480 DCHECK(klass_ != nullptr);
6481 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006482 }
6483
Andreas Gampea5b09a62016-11-17 15:21:22 -08006484 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006485 const DexFile& GetDexFile() const { return dex_file_; }
6486
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006487 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006488 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006489 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006490
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006491 static SideEffects SideEffectsForArchRuntimeCalls() {
6492 return SideEffects::CanTriggerGC();
6493 }
6494
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006495 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006496 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006497 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006498 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006499
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006500 bool MustResolveTypeOnSlowPath() const {
6501 // Check that this instruction has a slow path.
6502 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6503 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6504 return GetLoadKind() == LoadKind::kBssEntry;
6505 }
6506
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006507 void MarkInBootImage() {
6508 SetPackedFlag<kFlagIsInBootImage>(true);
6509 }
6510
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006511 void AddSpecialInput(HInstruction* special_input);
6512
6513 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006514 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006515 return ArrayRef<HUserRecord<HInstruction*>>(
6516 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6517 }
6518
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006519 Handle<mirror::Class> GetClass() const {
6520 return klass_;
6521 }
6522
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006523 DECLARE_INSTRUCTION(LoadClass);
6524
Artem Serovcced8ba2017-07-19 18:18:09 +01006525 protected:
6526 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6527
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006528 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006529 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006530 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006531 // Whether this instruction must generate the initialization check.
6532 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006533 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006534 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6535 static constexpr size_t kFieldLoadKindSize =
6536 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006537 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6538 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006539 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006540 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6541
6542 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006543 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006544 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006545 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006546 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006547 }
6548
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006549 void SetLoadKindInternal(LoadKind load_kind);
6550
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006551 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006552 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006553 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006554 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006555
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006556 // A type index and dex file where the class can be accessed. The dex file can be:
6557 // - The compiling method's dex file if the class is defined there too.
6558 // - The compiling method's dex file if the class is referenced there.
6559 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006560 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006561 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006562 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006563
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006564 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006565};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006566std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6567
6568// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006569inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6570 // The load kind should be determined before inserting the instruction to the graph.
6571 DCHECK(GetBlock() == nullptr);
6572 DCHECK(GetEnvironment() == nullptr);
6573 SetPackedField<LoadKindField>(load_kind);
6574 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6575 special_input_ = HUserRecord<HInstruction*>(nullptr);
6576 }
6577 if (!NeedsEnvironment()) {
6578 SetSideEffects(SideEffects::None());
6579 }
6580}
6581
6582// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006583inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006584 // The special input is used for PC-relative loads on some architectures,
6585 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006586 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006587 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006588 GetLoadKind() == LoadKind::kBssEntry ||
6589 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006590 DCHECK(special_input_.GetInstruction() == nullptr);
6591 special_input_ = HUserRecord<HInstruction*>(special_input);
6592 special_input->AddUseAt(this, 0);
6593}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006594
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006595class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006596 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006597 // Determines how to load the String.
6598 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006599 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006600 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006601 kBootImageLinkTimePcRelative,
6602
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006603 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006604 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006605 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006606
Vladimir Markoaad75c62016-10-03 08:46:48 +00006607 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006608 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006609 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006610
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006611 // Use a known boot image String* address, embedded in the code by the codegen.
6612 // Used for boot image strings referenced by apps in JIT-compiled code.
6613 kJitBootImageAddress,
6614
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006615 // Load from the root table associated with the JIT compiled method.
6616 kJitTableAddress,
6617
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006618 // Load using a simple runtime call. This is the fall-back load kind when
6619 // the codegen is unable to use another appropriate kind.
6620 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006621
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006622 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006623 };
6624
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006625 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006626 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006627 const DexFile& dex_file,
6628 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006629 : HInstruction(kLoadString,
6630 DataType::Type::kReference,
6631 SideEffectsForArchRuntimeCalls(),
6632 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006633 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006634 string_index_(string_index),
6635 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006636 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006637 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006638
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006639 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006640
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006641 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006642
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006643 LoadKind GetLoadKind() const {
6644 return GetPackedField<LoadKindField>();
6645 }
6646
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006647 bool HasPcRelativeLoadKind() const {
6648 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6649 GetLoadKind() == LoadKind::kBootImageRelRo ||
6650 GetLoadKind() == LoadKind::kBssEntry;
6651 }
6652
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006653 const DexFile& GetDexFile() const {
6654 return dex_file_;
6655 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006656
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006657 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006658 return string_index_;
6659 }
6660
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006661 Handle<mirror::String> GetString() const {
6662 return string_;
6663 }
6664
6665 void SetString(Handle<mirror::String> str) {
6666 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006667 }
6668
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006669 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006670
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006671 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006672
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006673 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006674
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006675 // Will call the runtime if we need to load the string through
6676 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006677 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006678 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006679 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006680 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006681 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006682 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006683 return false;
6684 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006685 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006686 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006687
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006688 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006689 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006690 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006691
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006692 bool CanBeNull() const override { return false; }
6693 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006694
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006695 static SideEffects SideEffectsForArchRuntimeCalls() {
6696 return SideEffects::CanTriggerGC();
6697 }
6698
Vladimir Marko372f10e2016-05-17 16:30:10 +01006699 void AddSpecialInput(HInstruction* special_input);
6700
6701 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006702 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006703 return ArrayRef<HUserRecord<HInstruction*>>(
6704 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006705 }
6706
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006707 DECLARE_INSTRUCTION(LoadString);
6708
Artem Serovcced8ba2017-07-19 18:18:09 +01006709 protected:
6710 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6711
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006712 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006713 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006714 static constexpr size_t kFieldLoadKindSize =
6715 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6716 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006717 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006718 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006719
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006720 void SetLoadKindInternal(LoadKind load_kind);
6721
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006722 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006723 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006724 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006725 HUserRecord<HInstruction*> special_input_;
6726
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006727 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006728 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006729
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006730 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006731};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006732std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6733
6734// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006735inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6736 // The load kind should be determined before inserting the instruction to the graph.
6737 DCHECK(GetBlock() == nullptr);
6738 DCHECK(GetEnvironment() == nullptr);
6739 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6740 SetPackedField<LoadKindField>(load_kind);
6741 if (load_kind != LoadKind::kRuntimeCall) {
6742 special_input_ = HUserRecord<HInstruction*>(nullptr);
6743 }
6744 if (!NeedsEnvironment()) {
6745 SetSideEffects(SideEffects::None());
6746 }
6747}
6748
6749// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006750inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006751 // The special input is used for PC-relative loads on some architectures,
6752 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006753 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006754 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006755 GetLoadKind() == LoadKind::kBssEntry ||
6756 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006757 // HLoadString::GetInputRecords() returns an empty array at this point,
6758 // so use the GetInputRecords() from the base class to set the input record.
6759 DCHECK(special_input_.GetInstruction() == nullptr);
6760 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006761 special_input->AddUseAt(this, 0);
6762}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006763
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006764class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006765 public:
6766 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006767 uint16_t method_handle_idx,
6768 const DexFile& dex_file,
6769 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006770 : HInstruction(kLoadMethodHandle,
6771 DataType::Type::kReference,
6772 SideEffectsForArchRuntimeCalls(),
6773 dex_pc),
6774 special_input_(HUserRecord<HInstruction*>(current_method)),
6775 method_handle_idx_(method_handle_idx),
6776 dex_file_(dex_file) {
6777 }
6778
6779 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006780 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006781 return ArrayRef<HUserRecord<HInstruction*>>(
6782 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6783 }
6784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006785 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006786
6787 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6788
6789 const DexFile& GetDexFile() const { return dex_file_; }
6790
6791 static SideEffects SideEffectsForArchRuntimeCalls() {
6792 return SideEffects::CanTriggerGC();
6793 }
6794
6795 DECLARE_INSTRUCTION(LoadMethodHandle);
6796
6797 protected:
6798 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6799
6800 private:
6801 // The special input is the HCurrentMethod for kRuntimeCall.
6802 HUserRecord<HInstruction*> special_input_;
6803
6804 const uint16_t method_handle_idx_;
6805 const DexFile& dex_file_;
6806};
6807
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006808class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006809 public:
6810 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006811 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006812 const DexFile& dex_file,
6813 uint32_t dex_pc)
6814 : HInstruction(kLoadMethodType,
6815 DataType::Type::kReference,
6816 SideEffectsForArchRuntimeCalls(),
6817 dex_pc),
6818 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006819 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006820 dex_file_(dex_file) {
6821 }
6822
6823 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006824 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006825 return ArrayRef<HUserRecord<HInstruction*>>(
6826 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6827 }
6828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006829 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006830
Orion Hodson06d10a72018-05-14 08:53:38 +01006831 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006832
6833 const DexFile& GetDexFile() const { return dex_file_; }
6834
6835 static SideEffects SideEffectsForArchRuntimeCalls() {
6836 return SideEffects::CanTriggerGC();
6837 }
6838
6839 DECLARE_INSTRUCTION(LoadMethodType);
6840
6841 protected:
6842 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6843
6844 private:
6845 // The special input is the HCurrentMethod for kRuntimeCall.
6846 HUserRecord<HInstruction*> special_input_;
6847
Orion Hodson06d10a72018-05-14 08:53:38 +01006848 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006849 const DexFile& dex_file_;
6850};
6851
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006852/**
6853 * Performs an initialization check on its Class object input.
6854 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006855class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006856 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006857 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006858 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306859 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006860 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006861 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006862 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006863 SetRawInputAt(0, constant);
6864 }
Artem Serova6e26142018-06-19 14:55:17 +01006865 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006866 bool CanBeMoved() const override { return true; }
6867 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006868 return true;
6869 }
6870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006871 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006872 // May call runtime to initialize the class.
6873 return true;
6874 }
6875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006876 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006877
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006878 HLoadClass* GetLoadClass() const {
6879 DCHECK(InputAt(0)->IsLoadClass());
6880 return InputAt(0)->AsLoadClass();
6881 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006882
6883 DECLARE_INSTRUCTION(ClinitCheck);
6884
Artem Serovcced8ba2017-07-19 18:18:09 +01006885
6886 protected:
6887 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006888};
6889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006890class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006891 public:
6892 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006893 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006894 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006895 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006896 bool is_volatile,
6897 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006898 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006899 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006900 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306901 : HExpression(kStaticFieldGet,
6902 field_type,
6903 SideEffects::FieldReadOfType(field_type, is_volatile),
6904 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006905 field_info_(field,
6906 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006907 field_type,
6908 is_volatile,
6909 field_idx,
6910 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006911 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006912 SetRawInputAt(0, cls);
6913 }
6914
Calin Juravle52c48962014-12-16 17:02:57 +00006915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006916 bool IsClonable() const override { return true; }
6917 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006918
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006919 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006920 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006921 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006922 }
6923
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006924 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006925 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6926 }
6927
Calin Juravle52c48962014-12-16 17:02:57 +00006928 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006929 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006930 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006931 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006932
Vladimir Marko61b92282017-10-11 13:23:17 +01006933 void SetType(DataType::Type new_type) {
6934 DCHECK(DataType::IsIntegralType(GetType()));
6935 DCHECK(DataType::IsIntegralType(new_type));
6936 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6937 SetPackedField<TypeField>(new_type);
6938 }
6939
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006940 DECLARE_INSTRUCTION(StaticFieldGet);
6941
Artem Serovcced8ba2017-07-19 18:18:09 +01006942 protected:
6943 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6944
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006945 private:
6946 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006947};
6948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006949class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006950 public:
6951 HStaticFieldSet(HInstruction* cls,
6952 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006953 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006954 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006955 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006956 bool is_volatile,
6957 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006958 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006959 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006960 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006961 : HExpression(kStaticFieldSet,
6962 SideEffects::FieldWriteOfType(field_type, is_volatile),
6963 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006964 field_info_(field,
6965 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006966 field_type,
6967 is_volatile,
6968 field_idx,
6969 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006970 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006971 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006972 SetRawInputAt(0, cls);
6973 SetRawInputAt(1, value);
6974 }
6975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006976 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006977 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006978 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006979 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006980 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006981
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006982 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006983 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6984 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006985
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006986 DECLARE_INSTRUCTION(StaticFieldSet);
6987
Artem Serovcced8ba2017-07-19 18:18:09 +01006988 protected:
6989 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6990
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006991 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006992 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6993 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6994 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6995 "Too many packed fields.");
6996
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006997 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006998};
6999
Vladimir Marko552a1342017-10-31 10:56:47 +00007000class HStringBuilderAppend final : public HVariableInputSizeInstruction {
7001 public:
7002 HStringBuilderAppend(HIntConstant* format,
7003 uint32_t number_of_arguments,
7004 ArenaAllocator* allocator,
7005 uint32_t dex_pc)
7006 : HVariableInputSizeInstruction(
7007 kStringBuilderAppend,
7008 DataType::Type::kReference,
7009 // The runtime call may read memory from inputs. It never writes outside
7010 // of the newly allocated result object (or newly allocated helper objects).
7011 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
7012 dex_pc,
7013 allocator,
7014 number_of_arguments + /* format */ 1u,
7015 kArenaAllocInvokeInputs) {
7016 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
7017 SetRawInputAt(FormatIndex(), format);
7018 }
7019
7020 void SetArgumentAt(size_t index, HInstruction* argument) {
7021 DCHECK_LE(index, GetNumberOfArguments());
7022 SetRawInputAt(index, argument);
7023 }
7024
7025 // Return the number of arguments, excluding the format.
7026 size_t GetNumberOfArguments() const {
7027 DCHECK_GE(InputCount(), 1u);
7028 return InputCount() - 1u;
7029 }
7030
7031 size_t FormatIndex() const {
7032 return GetNumberOfArguments();
7033 }
7034
7035 HIntConstant* GetFormat() {
7036 return InputAt(FormatIndex())->AsIntConstant();
7037 }
7038
7039 bool NeedsEnvironment() const override { return true; }
7040
7041 bool CanThrow() const override { return true; }
7042
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007043 bool CanBeNull() const override { return false; }
7044
Vladimir Marko552a1342017-10-31 10:56:47 +00007045 DECLARE_INSTRUCTION(StringBuilderAppend);
7046
7047 protected:
7048 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7049};
7050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007051class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007052 public:
7053 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007054 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007055 uint32_t field_index,
7056 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307057 : HExpression(kUnresolvedInstanceFieldGet,
7058 field_type,
7059 SideEffects::AllExceptGCDependency(),
7060 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007061 field_index_(field_index) {
7062 SetRawInputAt(0, obj);
7063 }
7064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007065 bool IsClonable() const override { return true; }
7066 bool NeedsEnvironment() const override { return true; }
7067 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007069 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007070 uint32_t GetFieldIndex() const { return field_index_; }
7071
7072 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7073
Artem Serovcced8ba2017-07-19 18:18:09 +01007074 protected:
7075 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7076
Calin Juravlee460d1d2015-09-29 04:52:17 +01007077 private:
7078 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007079};
7080
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007081class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007082 public:
7083 HUnresolvedInstanceFieldSet(HInstruction* obj,
7084 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007085 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007086 uint32_t field_index,
7087 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007088 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007089 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007090 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007091 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007092 SetRawInputAt(0, obj);
7093 SetRawInputAt(1, value);
7094 }
7095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007096 bool IsClonable() const override { return true; }
7097 bool NeedsEnvironment() const override { return true; }
7098 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007100 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007101 uint32_t GetFieldIndex() const { return field_index_; }
7102
7103 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7104
Artem Serovcced8ba2017-07-19 18:18:09 +01007105 protected:
7106 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7107
Calin Juravlee460d1d2015-09-29 04:52:17 +01007108 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007109 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7110 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007111 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007112 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7113 kFieldFieldType + kFieldFieldTypeSize;
7114 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7115 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007116 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007117
Calin Juravlee460d1d2015-09-29 04:52:17 +01007118 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007119};
7120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007121class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007122 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007123 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007124 uint32_t field_index,
7125 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307126 : HExpression(kUnresolvedStaticFieldGet,
7127 field_type,
7128 SideEffects::AllExceptGCDependency(),
7129 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007130 field_index_(field_index) {
7131 }
7132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007133 bool IsClonable() const override { return true; }
7134 bool NeedsEnvironment() const override { return true; }
7135 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007137 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007138 uint32_t GetFieldIndex() const { return field_index_; }
7139
7140 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7141
Artem Serovcced8ba2017-07-19 18:18:09 +01007142 protected:
7143 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7144
Calin Juravlee460d1d2015-09-29 04:52:17 +01007145 private:
7146 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007147};
7148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007149class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007150 public:
7151 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007152 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007153 uint32_t field_index,
7154 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007155 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007156 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007157 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007158 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007159 SetRawInputAt(0, value);
7160 }
7161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007162 bool IsClonable() const override { return true; }
7163 bool NeedsEnvironment() const override { return true; }
7164 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007166 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007167 uint32_t GetFieldIndex() const { return field_index_; }
7168
7169 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7170
Artem Serovcced8ba2017-07-19 18:18:09 +01007171 protected:
7172 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7173
Calin Juravlee460d1d2015-09-29 04:52:17 +01007174 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007175 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7176 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007177 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007178 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7179 kFieldFieldType + kFieldFieldTypeSize;
7180 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7181 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007182 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007183
Calin Juravlee460d1d2015-09-29 04:52:17 +01007184 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007185};
7186
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007187// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007188class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007189 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007190 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307191 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7192 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007193
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007194 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007195
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007196 DECLARE_INSTRUCTION(LoadException);
7197
Artem Serovcced8ba2017-07-19 18:18:09 +01007198 protected:
7199 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007200};
7201
David Brazdilcb1c0552015-08-04 16:22:25 +01007202// Implicit part of move-exception which clears thread-local exception storage.
7203// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007204class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007206 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007207 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307208 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007209
7210 DECLARE_INSTRUCTION(ClearException);
7211
Artem Serovcced8ba2017-07-19 18:18:09 +01007212 protected:
7213 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007214};
7215
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007216class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007217 public:
7218 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007219 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007220 SetRawInputAt(0, exception);
7221 }
7222
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007223 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007224
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007225 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007227 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007229 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007230
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007231 DECLARE_INSTRUCTION(Throw);
7232
Artem Serovcced8ba2017-07-19 18:18:09 +01007233 protected:
7234 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007235};
7236
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007237/**
7238 * Implementation strategies for the code generator of a HInstanceOf
7239 * or `HCheckCast`.
7240 */
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007241enum class TypeCheckKind { // private marker to avoid generate-operator-out.py from processing.
Calin Juravle98893e12015-10-02 21:05:03 +01007242 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007243 kExactCheck, // Can do a single class compare.
7244 kClassHierarchyCheck, // Can just walk the super class chain.
7245 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7246 kInterfaceCheck, // No optimization yet when checking against an interface.
7247 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007248 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007249 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007250 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007251};
7252
Roland Levillain86503782016-02-11 19:07:30 +00007253std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7254
Vladimir Marko175e7862018-03-27 09:03:13 +00007255// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7256// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7257class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007258 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007259 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007260 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007261 HInstruction* object,
7262 HInstruction* target_class_or_null,
7263 TypeCheckKind check_kind,
7264 Handle<mirror::Class> klass,
7265 uint32_t dex_pc,
7266 ArenaAllocator* allocator,
7267 HIntConstant* bitstring_path_to_root,
7268 HIntConstant* bitstring_mask,
7269 SideEffects side_effects)
7270 : HVariableInputSizeInstruction(
7271 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007272 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007273 side_effects,
7274 dex_pc,
7275 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007276 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007277 kArenaAllocTypeCheckInputs),
7278 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007279 SetPackedField<TypeCheckKindField>(check_kind);
7280 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007281 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007282 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007283 SetRawInputAt(1, target_class_or_null);
7284 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7285 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7286 if (check_kind == TypeCheckKind::kBitstringCheck) {
7287 DCHECK(target_class_or_null->IsNullConstant());
7288 SetRawInputAt(2, bitstring_path_to_root);
7289 SetRawInputAt(3, bitstring_mask);
7290 } else {
7291 DCHECK(target_class_or_null->IsLoadClass());
7292 }
Vladimir Marko87584542017-12-12 17:47:52 +00007293 }
7294
7295 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007296 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007297 HInstruction* load_class = InputAt(1);
7298 DCHECK(load_class->IsLoadClass());
7299 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007300 }
7301
Vladimir Marko175e7862018-03-27 09:03:13 +00007302 uint32_t GetBitstringPathToRoot() const {
7303 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7304 HInstruction* path_to_root = InputAt(2);
7305 DCHECK(path_to_root->IsIntConstant());
7306 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7307 }
7308
7309 uint32_t GetBitstringMask() const {
7310 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7311 HInstruction* mask = InputAt(3);
7312 DCHECK(mask->IsIntConstant());
7313 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7314 }
7315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007316 bool IsClonable() const override { return true; }
7317 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007318
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007319 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007320 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7321 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007322 }
7323
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007324 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7325 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7326 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7327 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7328
Vladimir Marko175e7862018-03-27 09:03:13 +00007329 ReferenceTypeInfo GetTargetClassRTI() {
7330 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7331 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007332 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007333 } else {
7334 return ReferenceTypeInfo::CreateInvalid();
7335 }
7336 }
7337
7338 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7339 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7340 DCHECK(klass_ != nullptr);
7341 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7342 }
7343
7344 Handle<mirror::Class> GetClass() const {
7345 return klass_;
7346 }
7347
7348 protected:
7349 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7350
7351 private:
7352 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7353 static constexpr size_t kFieldTypeCheckKindSize =
7354 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7355 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7356 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7357 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7358 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7359 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7360
7361 Handle<mirror::Class> klass_;
7362};
7363
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007364class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007365 public:
7366 HInstanceOf(HInstruction* object,
7367 HInstruction* target_class_or_null,
7368 TypeCheckKind check_kind,
7369 Handle<mirror::Class> klass,
7370 uint32_t dex_pc,
7371 ArenaAllocator* allocator,
7372 HIntConstant* bitstring_path_to_root,
7373 HIntConstant* bitstring_mask)
7374 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007375 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007376 object,
7377 target_class_or_null,
7378 check_kind,
7379 klass,
7380 dex_pc,
7381 allocator,
7382 bitstring_path_to_root,
7383 bitstring_mask,
7384 SideEffectsForArchRuntimeCalls(check_kind)) {}
7385
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007386 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007388 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007389 return CanCallRuntime(GetTypeCheckKind());
7390 }
7391
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007392 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007393 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007394 return check_kind != TypeCheckKind::kExactCheck;
7395 }
7396
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007397 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007398 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007399 }
7400
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007401 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007402
Artem Serovcced8ba2017-07-19 18:18:09 +01007403 protected:
7404 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007405};
7406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007407class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007408 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007409 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307410 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007411 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7412 SetPackedFlag<kFlagUpperCanBeNull>(true);
7413 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007414 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007415 SetRawInputAt(0, input);
7416 }
7417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007418 bool InstructionDataEquals(const HInstruction* other) const override;
7419 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007420
David Brazdilf5552582015-12-27 13:36:12 +00007421 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007422 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007423 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007424 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007425
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007426 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007427 DCHECK(GetUpperCanBeNull() || !can_be_null);
7428 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007429 }
7430
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007431 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007432
Calin Juravleb1498f62015-02-16 13:13:29 +00007433 DECLARE_INSTRUCTION(BoundType);
7434
Artem Serovcced8ba2017-07-19 18:18:09 +01007435 protected:
7436 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7437
Calin Juravleb1498f62015-02-16 13:13:29 +00007438 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007439 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7440 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007441 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007442 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7443 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7444 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7445
Calin Juravleb1498f62015-02-16 13:13:29 +00007446 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007447 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7448 // It is used to bound the type in cases like:
7449 // if (x instanceof ClassX) {
7450 // // uper_bound_ will be ClassX
7451 // }
David Brazdilf5552582015-12-27 13:36:12 +00007452 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007453};
7454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007455class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007456 public:
7457 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007458 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007459 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007460 Handle<mirror::Class> klass,
7461 uint32_t dex_pc,
7462 ArenaAllocator* allocator,
7463 HIntConstant* bitstring_path_to_root,
7464 HIntConstant* bitstring_mask)
7465 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007466 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007467 object,
7468 target_class_or_null,
7469 check_kind,
7470 klass,
7471 dex_pc,
7472 allocator,
7473 bitstring_path_to_root,
7474 bitstring_mask,
7475 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007476
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007477 bool IsClonable() const override { return true; }
7478 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007479 // Instruction may throw a CheckCastError.
7480 return true;
7481 }
7482
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007483 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007484
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007485 DECLARE_INSTRUCTION(CheckCast);
7486
Artem Serovcced8ba2017-07-19 18:18:09 +01007487 protected:
7488 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007489};
7490
Andreas Gampe26de38b2016-07-27 17:53:11 -07007491/**
7492 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7493 * @details We define the combined barrier types that are actually required
7494 * by the Java Memory Model, rather than using exactly the terminology from
7495 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7496 * primitives. Note that the JSR-133 cookbook generally does not deal with
7497 * store atomicity issues, and the recipes there are not always entirely sufficient.
7498 * The current recipe is as follows:
7499 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7500 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7501 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7502 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7503 * class has final fields.
7504 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7505 * store-to-memory instructions. Only generated together with non-temporal stores.
7506 */
7507enum MemBarrierKind {
7508 kAnyStore,
7509 kLoadAny,
7510 kStoreStore,
7511 kAnyAny,
7512 kNTStoreStore,
7513 kLastBarrierKind = kNTStoreStore
7514};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007515std::ostream& operator<<(std::ostream& os, MemBarrierKind kind);
Andreas Gampe26de38b2016-07-27 17:53:11 -07007516
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007517class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007518 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007519 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007520 : HExpression(kMemoryBarrier,
7521 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7522 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007523 SetPackedField<BarrierKindField>(barrier_kind);
7524 }
Calin Juravle27df7582015-04-17 19:12:31 +01007525
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007526 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007527
Vladimir Markoa1de9182016-02-25 11:37:38 +00007528 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007529
7530 DECLARE_INSTRUCTION(MemoryBarrier);
7531
Artem Serovcced8ba2017-07-19 18:18:09 +01007532 protected:
7533 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7534
Calin Juravle27df7582015-04-17 19:12:31 +01007535 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007536 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7537 static constexpr size_t kFieldBarrierKindSize =
7538 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7539 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7540 kFieldBarrierKind + kFieldBarrierKindSize;
7541 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7542 "Too many packed fields.");
7543 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007544};
7545
Igor Murashkind01745e2017-04-05 16:40:31 -07007546// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7547// the specified object(s), with respect to any subsequent store that might "publish"
7548// (i.e. make visible) the specified object to another thread.
7549//
7550// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7551// for all final fields (that were set) at the end of the invoked constructor.
7552//
7553// The constructor fence models the freeze actions for the final fields of an object
7554// being constructed (semantically at the end of the constructor). Constructor fences
7555// have a per-object affinity; two separate objects being constructed get two separate
7556// constructor fences.
7557//
7558// (Note: that if calling a super-constructor or forwarding to another constructor,
7559// the freezes would happen at the end of *that* constructor being invoked).
7560//
7561// The memory model guarantees that when the object being constructed is "published" after
7562// constructor completion (i.e. escapes the current thread via a store), then any final field
7563// writes must be observable on other threads (once they observe that publication).
7564//
7565// Further, anything written before the freeze, and read by dereferencing through the final field,
7566// must also be visible (so final object field could itself have an object with non-final fields;
7567// yet the freeze must also extend to them).
7568//
7569// Constructor example:
7570//
7571// class HasFinal {
7572// final int field; Optimizing IR for <init>()V:
7573// HasFinal() {
7574// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7575// // freeze(this.field); HConstructorFence(this)
7576// } HReturn
7577// }
7578//
7579// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7580// already-initialized classes; in that case the allocation must act as a "default-initializer"
7581// of the object which effectively writes the class pointer "final field".
7582//
7583// For example, we can model default-initialiation as roughly the equivalent of the following:
7584//
7585// class Object {
7586// private final Class header;
7587// }
7588//
7589// Java code: Optimizing IR:
7590//
7591// T new_instance<T>() {
7592// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7593// obj.header = T.class; // header write is done by above call.
7594// // freeze(obj.header) HConstructorFence(obj)
7595// return (T)obj;
7596// }
7597//
7598// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007599// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007600// * QuasiAtomic::ThreadFenceForConstructor
7601//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007602class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007603 // A fence has variable inputs because the inputs can be removed
7604 // after prepare_for_register_allocation phase.
7605 // (TODO: In the future a fence could freeze multiple objects
7606 // after merging two fences together.)
7607 public:
7608 // `fence_object` is the reference that needs to be protected for correct publication.
7609 //
7610 // It makes sense in the following situations:
7611 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7612 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7613 //
7614 // After construction the `fence_object` becomes the 0th input.
7615 // This is not an input in a real sense, but just a convenient place to stash the information
7616 // about the associated object.
7617 HConstructorFence(HInstruction* fence_object,
7618 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007619 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007620 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7621 // constraints described in the class header. We claim that these SideEffects constraints
7622 // enforce a superset of the real constraints.
7623 //
7624 // The ordering described above is conservatively modeled with SideEffects as follows:
7625 //
7626 // * To prevent reordering of the publication stores:
7627 // ----> "Reads of objects" is the initial SideEffect.
7628 // * For every primitive final field store in the constructor:
7629 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7630 // * If there are any stores to reference final fields in the constructor:
7631 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7632 // that are reachable from `fence_object` also need to be prevented for reordering
7633 // (and we do not want to do alias analysis to figure out what those stores are).
7634 //
7635 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7636 // even more conservative approach than the one described above, and prevents all of the
7637 // above reordering without analyzing any of the instructions in the constructor.
7638 //
7639 // If in a later phase we discover that there are no writes to reference final fields,
7640 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307641 : HVariableInputSizeInstruction(kConstructorFence,
7642 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007643 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007644 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007645 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007646 kArenaAllocConstructorFenceInputs) {
7647 DCHECK(fence_object != nullptr);
7648 SetRawInputAt(0, fence_object);
7649 }
7650
7651 // The object associated with this constructor fence.
7652 //
7653 // (Note: This will be null after the prepare_for_register_allocation phase,
7654 // as all constructor fence inputs are removed there).
7655 HInstruction* GetFenceObject() const {
7656 return InputAt(0);
7657 }
7658
7659 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7660 // - Delete `fence_use` from `this`'s use list.
7661 // - Delete `this` from `fence_use`'s inputs list.
7662 // - If the `fence_use` is dead, remove it from the graph.
7663 //
7664 // A fence is considered dead once it no longer has any uses
7665 // and all of the inputs are dead.
7666 //
7667 // This must *not* be called during/after prepare_for_register_allocation,
7668 // because that removes all the inputs to the fences but the fence is actually
7669 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007670 //
7671 // Returns how many HConstructorFence instructions were removed from graph.
7672 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007673
Igor Murashkindd018df2017-08-09 10:38:31 -07007674 // Combine all inputs of `this` and `other` instruction and remove
7675 // `other` from the graph.
7676 //
7677 // Inputs are unique after the merge.
7678 //
7679 // Requirement: `this` must not be the same as `other.
7680 void Merge(HConstructorFence* other);
7681
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007682 // Check if this constructor fence is protecting
7683 // an HNewInstance or HNewArray that is also the immediate
7684 // predecessor of `this`.
7685 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007686 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7687 // to be one of the inputs of `this`.
7688 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007689 // Returns the associated HNewArray or HNewInstance,
7690 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007691 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007692
Igor Murashkind01745e2017-04-05 16:40:31 -07007693 DECLARE_INSTRUCTION(ConstructorFence);
7694
Artem Serovcced8ba2017-07-19 18:18:09 +01007695 protected:
7696 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007697};
7698
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007699class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007700 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007701 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007702 kEnter,
7703 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007704 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007705 };
7706
7707 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007708 : HExpression(kMonitorOperation,
7709 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7710 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007711 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007712 SetRawInputAt(0, object);
7713 }
7714
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007715 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007716 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007717
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007718 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007719 // Verifier guarantees that monitor-exit cannot throw.
7720 // This is important because it allows the HGraphBuilder to remove
7721 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7722 return IsEnter();
7723 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007724
Vladimir Markoa1de9182016-02-25 11:37:38 +00007725 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7726 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007727
7728 DECLARE_INSTRUCTION(MonitorOperation);
7729
Artem Serovcced8ba2017-07-19 18:18:09 +01007730 protected:
7731 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7732
Calin Juravle52c48962014-12-16 17:02:57 +00007733 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007734 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7735 static constexpr size_t kFieldOperationKindSize =
7736 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7737 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7738 kFieldOperationKind + kFieldOperationKindSize;
7739 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7740 "Too many packed fields.");
7741 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007742};
7743
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007744class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007745 public:
7746 HSelect(HInstruction* condition,
7747 HInstruction* true_value,
7748 HInstruction* false_value,
7749 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307750 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007751 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7752
7753 // First input must be `true_value` or `false_value` to allow codegens to
7754 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7755 // that architectures which implement HSelect as a conditional move also
7756 // will not need to invert the condition.
7757 SetRawInputAt(0, false_value);
7758 SetRawInputAt(1, true_value);
7759 SetRawInputAt(2, condition);
7760 }
7761
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007762 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007763 HInstruction* GetFalseValue() const { return InputAt(0); }
7764 HInstruction* GetTrueValue() const { return InputAt(1); }
7765 HInstruction* GetCondition() const { return InputAt(2); }
7766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007767 bool CanBeMoved() const override { return true; }
7768 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007769 return true;
7770 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007772 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007773 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7774 }
7775
7776 DECLARE_INSTRUCTION(Select);
7777
Artem Serovcced8ba2017-07-19 18:18:09 +01007778 protected:
7779 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007780};
7781
Vladimir Markof9f64412015-09-02 14:05:49 +01007782class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007783 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007784 MoveOperands(Location source,
7785 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007786 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007787 HInstruction* instruction)
7788 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007789
7790 Location GetSource() const { return source_; }
7791 Location GetDestination() const { return destination_; }
7792
7793 void SetSource(Location value) { source_ = value; }
7794 void SetDestination(Location value) { destination_ = value; }
7795
7796 // The parallel move resolver marks moves as "in-progress" by clearing the
7797 // destination (but not the source).
7798 Location MarkPending() {
7799 DCHECK(!IsPending());
7800 Location dest = destination_;
7801 destination_ = Location::NoLocation();
7802 return dest;
7803 }
7804
7805 void ClearPending(Location dest) {
7806 DCHECK(IsPending());
7807 destination_ = dest;
7808 }
7809
7810 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007811 DCHECK(source_.IsValid() || destination_.IsInvalid());
7812 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007813 }
7814
7815 // True if this blocks a move from the given location.
7816 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007817 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007818 }
7819
7820 // A move is redundant if it's been eliminated, if its source and
7821 // destination are the same, or if its destination is unneeded.
7822 bool IsRedundant() const {
7823 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7824 }
7825
7826 // We clear both operands to indicate move that's been eliminated.
7827 void Eliminate() {
7828 source_ = destination_ = Location::NoLocation();
7829 }
7830
7831 bool IsEliminated() const {
7832 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7833 return source_.IsInvalid();
7834 }
7835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007836 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007837
Nicolas Geoffray90218252015-04-15 11:56:51 +01007838 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007839 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007840 }
7841
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007842 HInstruction* GetInstruction() const { return instruction_; }
7843
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007844 private:
7845 Location source_;
7846 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007847 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007848 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007849 // The instruction this move is assocatied with. Null when this move is
7850 // for moving an input in the expected locations of user (including a phi user).
7851 // This is only used in debug mode, to ensure we do not connect interval siblings
7852 // in the same parallel move.
7853 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007854};
7855
Roland Levillainc9285912015-12-18 10:38:42 +00007856std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7857
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007858static constexpr size_t kDefaultNumberOfMoves = 4;
7859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007860class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007861 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007862 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007863 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007864 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007865 moves_.reserve(kDefaultNumberOfMoves);
7866 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007867
Nicolas Geoffray90218252015-04-15 11:56:51 +01007868 void AddMove(Location source,
7869 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007870 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007871 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007872 DCHECK(source.IsValid());
7873 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007874 if (kIsDebugBuild) {
7875 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007876 for (const MoveOperands& move : moves_) {
7877 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007878 // Special case the situation where the move is for the spill slot
7879 // of the instruction.
7880 if ((GetPrevious() == instruction)
7881 || ((GetPrevious() == nullptr)
7882 && instruction->IsPhi()
7883 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007884 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007885 << "Doing parallel moves for the same instruction.";
7886 } else {
7887 DCHECK(false) << "Doing parallel moves for the same instruction.";
7888 }
7889 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007890 }
7891 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007892 for (const MoveOperands& move : moves_) {
7893 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007894 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007895 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007896 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007897 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007898 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007899 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007900 }
7901
Vladimir Marko225b6462015-09-28 12:17:40 +01007902 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007903 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007904 }
7905
Vladimir Marko225b6462015-09-28 12:17:40 +01007906 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007907
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007908 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007909
Artem Serovcced8ba2017-07-19 18:18:09 +01007910 protected:
7911 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7912
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007913 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007914 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007915};
7916
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007917// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7918// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7919// never used across anything that can trigger GC.
7920// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7921// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007922class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007923 public:
7924 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307925 : HExpression(kIntermediateAddress,
7926 DataType::Type::kInt32,
7927 SideEffects::DependsOnGC(),
7928 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007929 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7930 DataType::Size(DataType::Type::kReference))
7931 << "kPrimInt and kPrimNot have different sizes.";
7932 SetRawInputAt(0, base_address);
7933 SetRawInputAt(1, offset);
7934 }
7935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007936 bool IsClonable() const override { return true; }
7937 bool CanBeMoved() const override { return true; }
7938 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007939 return true;
7940 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007941 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007942
7943 HInstruction* GetBaseAddress() const { return InputAt(0); }
7944 HInstruction* GetOffset() const { return InputAt(1); }
7945
7946 DECLARE_INSTRUCTION(IntermediateAddress);
7947
Artem Serovcced8ba2017-07-19 18:18:09 +01007948 protected:
7949 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007950};
7951
7952
Mark Mendell0616ae02015-04-17 12:49:27 -04007953} // namespace art
7954
Aart Bikf8f5a162017-02-06 15:35:29 -08007955#include "nodes_vector.h"
7956
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007957#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7958#include "nodes_shared.h"
7959#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307960#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007961#include "nodes_x86.h"
7962#endif
7963
Vladimir Marko0a516052019-10-14 13:00:44 +00007964namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007965
Igor Murashkin6ef45672017-08-08 13:59:55 -07007966class OptimizingCompilerStats;
7967
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007968class HGraphVisitor : public ValueObject {
7969 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007970 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7971 : stats_(stats),
7972 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007973 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007974
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007975 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007976 virtual void VisitBasicBlock(HBasicBlock* block);
7977
Roland Levillain633021e2014-10-01 14:12:25 +01007978 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007979 void VisitInsertionOrder();
7980
Roland Levillain633021e2014-10-01 14:12:25 +01007981 // Visit the graph following dominator tree reverse post-order.
7982 void VisitReversePostOrder();
7983
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007984 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007985
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007986 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007987#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007988 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7989
7990 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7991
7992#undef DECLARE_VISIT_INSTRUCTION
7993
Igor Murashkin6ef45672017-08-08 13:59:55 -07007994 protected:
7995 OptimizingCompilerStats* stats_;
7996
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007997 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007998 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007999
8000 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
8001};
8002
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008003class HGraphDelegateVisitor : public HGraphVisitor {
8004 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008005 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8006 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008007 virtual ~HGraphDelegateVisitor() {}
8008
8009 // Visit functions that delegate to to super class.
8010#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008011 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008012
8013 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8014
8015#undef DECLARE_VISIT_INSTRUCTION
8016
8017 private:
8018 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
8019};
8020
Artem Serovcced8ba2017-07-19 18:18:09 +01008021// Create a clone of the instruction, insert it into the graph; replace the old one with a new
8022// and remove the old instruction.
8023HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
8024
8025// Create a clone for each clonable instructions/phis and replace the original with the clone.
8026//
8027// Used for testing individual instruction cloner.
8028class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
8029 public:
8030 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01008031 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01008032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008033 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01008034 if (instruction->IsClonable()) {
8035 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01008036 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01008037 }
8038 }
8039
Artem Serov7f4aff62017-06-21 17:02:18 +01008040 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008041
8042 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008043 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008044
8045 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8046};
8047
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008048// Iterator over the blocks that art part of the loop. Includes blocks part
8049// of an inner loop. The order in which the blocks are iterated is on their
8050// block id.
8051class HBlocksInLoopIterator : public ValueObject {
8052 public:
8053 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8054 : blocks_in_loop_(info.GetBlocks()),
8055 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8056 index_(0) {
8057 if (!blocks_in_loop_.IsBitSet(index_)) {
8058 Advance();
8059 }
8060 }
8061
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008062 bool Done() const { return index_ == blocks_.size(); }
8063 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008064 void Advance() {
8065 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008066 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008067 if (blocks_in_loop_.IsBitSet(index_)) {
8068 break;
8069 }
8070 }
8071 }
8072
8073 private:
8074 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008075 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008076 size_t index_;
8077
8078 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8079};
8080
Mingyao Yang3584bce2015-05-19 16:01:59 -07008081// Iterator over the blocks that art part of the loop. Includes blocks part
8082// of an inner loop. The order in which the blocks are iterated is reverse
8083// post order.
8084class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8085 public:
8086 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8087 : blocks_in_loop_(info.GetBlocks()),
8088 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8089 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008090 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008091 Advance();
8092 }
8093 }
8094
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008095 bool Done() const { return index_ == blocks_.size(); }
8096 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008097 void Advance() {
8098 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008099 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8100 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008101 break;
8102 }
8103 }
8104 }
8105
8106 private:
8107 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008108 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008109 size_t index_;
8110
8111 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8112};
8113
Aart Bikf3e61ee2017-04-12 17:09:20 -07008114// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008115inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008116 if (constant->IsIntConstant()) {
8117 return constant->AsIntConstant()->GetValue();
8118 } else if (constant->IsLongConstant()) {
8119 return constant->AsLongConstant()->GetValue();
8120 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008121 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008122 return 0;
8123 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008124}
8125
Aart Bikf3e61ee2017-04-12 17:09:20 -07008126// Returns true iff instruction is an integral constant (and sets value on success).
8127inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8128 if (instruction->IsIntConstant()) {
8129 *value = instruction->AsIntConstant()->GetValue();
8130 return true;
8131 } else if (instruction->IsLongConstant()) {
8132 *value = instruction->AsLongConstant()->GetValue();
8133 return true;
8134 } else if (instruction->IsNullConstant()) {
8135 *value = 0;
8136 return true;
8137 }
8138 return false;
8139}
8140
Aart Bik0148de42017-09-05 09:25:01 -07008141// Returns true iff instruction is the given integral constant.
8142inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8143 int64_t val = 0;
8144 return IsInt64AndGet(instruction, &val) && val == value;
8145}
8146
8147// Returns true iff instruction is a zero bit pattern.
8148inline bool IsZeroBitPattern(HInstruction* instruction) {
8149 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8150}
8151
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008152// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008153#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008154 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8155 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8156#undef INSTRUCTION_TYPE_CHECK
8157
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008158// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008159#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8160 std::is_base_of<BaseType, H##type>::value,
8161#define INSTRUCTION_TYPE_CHECK(type, super) \
8162 inline bool HInstruction::Is##type() const { \
8163 DCHECK_LT(GetKind(), kLastInstructionKind); \
8164 using BaseType = H##type; \
8165 static constexpr bool results[] = { \
8166 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8167 }; \
8168 return results[static_cast<size_t>(GetKind())]; \
8169 }
8170
8171 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8172#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008173#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008174
8175#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008176 inline const H##type* HInstruction::As##type() const { \
8177 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8178 } \
8179 inline H##type* HInstruction::As##type() { \
8180 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8181 }
8182
Vladimir Markoa90dd512018-05-04 15:04:45 +01008183 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8184#undef INSTRUCTION_TYPE_CAST
8185
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008186
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008187// Create space in `blocks` for adding `number_of_new_blocks` entries
8188// starting at location `at`. Blocks after `at` are moved accordingly.
8189inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8190 size_t number_of_new_blocks,
8191 size_t after) {
8192 DCHECK_LT(after, blocks->size());
8193 size_t old_size = blocks->size();
8194 size_t new_size = old_size + number_of_new_blocks;
8195 blocks->resize(new_size);
8196 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8197}
8198
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008199/*
8200 * Hunt "under the hood" of array lengths (leading to array references),
8201 * null checks (also leading to array references), and new arrays
8202 * (leading to the actual length). This makes it more likely related
8203 * instructions become actually comparable.
8204 */
8205inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8206 while (instruction->IsArrayLength() ||
8207 instruction->IsNullCheck() ||
8208 instruction->IsNewArray()) {
8209 instruction = instruction->IsNewArray()
8210 ? instruction->AsNewArray()->GetLength()
8211 : instruction->InputAt(0);
8212 }
8213 return instruction;
8214}
8215
Artem Serovb47b9782019-12-04 21:02:09 +00008216inline bool IsAddOrSub(const HInstruction* instruction) {
8217 return instruction->IsAdd() || instruction->IsSub();
8218}
8219
Artem Serov21c7e6f2017-07-27 16:04:42 +01008220void RemoveEnvironmentUses(HInstruction* instruction);
8221bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8222void ResetEnvironmentInputRecords(HInstruction* instruction);
8223
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01008224// Detects an instruction that is >= 0. As long as the value is carried by
8225// a single instruction, arithmetic wrap-around cannot occur.
8226bool IsGEZero(HInstruction* instruction);
8227
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008228} // namespace art
8229
8230#endif // ART_COMPILER_OPTIMIZING_NODES_H_