blob: 99d80d77c5ef52fa4afb5ec96a476cd71ddf7248 [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 Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010031#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file.h"
34#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080035#include "dex/invoke_type.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000036#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000037#include "handle.h"
38#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010039#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010040#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000041#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000042#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010043#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
Vladimir Markoca6fff82017-10-03 14:49:14 +010048class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000049class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070051class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010052class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000056class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000059class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000060class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000062class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070063class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010064class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010065class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010066class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010067class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000068class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010069class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000070class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000071
Mathieu Chartier736b5602015-09-02 14:54:11 -070072namespace mirror {
73class DexCache;
74} // namespace mirror
75
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076static const int kDefaultNumberOfBlocks = 8;
77static const int kDefaultNumberOfSuccessors = 2;
78static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010079static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010080static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Roland Levillain5b5b9312016-03-22 14:57:31 +000083// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
84static constexpr int32_t kMaxIntShiftDistance = 0x1f;
85// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
86static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000087
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010088static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070089static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010091static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
92
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060093static constexpr uint32_t kNoDexPc = -1;
94
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010095inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
96 // For the purposes of the compiler, the dex files must actually be the same object
97 // if we want to safely treat them as the same. This is especially important for JIT
98 // as custom class loaders can open the same underlying file (or memory) multiple
99 // times and provide different class resolution but no two class loaders should ever
100 // use the same DexFile object - doing so is an unsupported hack that can lead to
101 // all sorts of weird failures.
102 return &lhs == &rhs;
103}
104
Dave Allison20dfc792014-06-16 20:44:29 -0700105enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700106 // All types.
107 kCondEQ, // ==
108 kCondNE, // !=
109 // Signed integers and floating-point numbers.
110 kCondLT, // <
111 kCondLE, // <=
112 kCondGT, // >
113 kCondGE, // >=
114 // Unsigned integers.
115 kCondB, // <
116 kCondBE, // <=
117 kCondA, // >
118 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100119 // First and last aliases.
120 kCondFirst = kCondEQ,
121 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700122};
123
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000124enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000125 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000126 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000127 kAnalysisFailThrowCatchLoop,
128 kAnalysisFailAmbiguousArrayOp,
129 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000130};
131
Andreas Gampe9186ced2016-12-12 14:28:21 -0800132template <typename T>
133static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
134 return static_cast<typename std::make_unsigned<T>::type>(x);
135}
136
Vladimir Markof9f64412015-09-02 14:05:49 +0100137class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 public:
139 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
140
141 void AddInstruction(HInstruction* instruction);
142 void RemoveInstruction(HInstruction* instruction);
143
David Brazdilc3d743f2015-04-22 13:40:50 +0100144 // Insert `instruction` before/after an existing instruction `cursor`.
145 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
146 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
147
Roland Levillain6b469232014-09-25 10:10:38 +0100148 // Return true if this list contains `instruction`.
149 bool Contains(HInstruction* instruction) const;
150
Roland Levillainccc07a92014-09-16 14:48:16 +0100151 // Return true if `instruction1` is found before `instruction2` in
152 // this instruction list and false otherwise. Abort if none
153 // of these instructions is found.
154 bool FoundBefore(const HInstruction* instruction1,
155 const HInstruction* instruction2) const;
156
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000157 bool IsEmpty() const { return first_instruction_ == nullptr; }
158 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
159
160 // Update the block of all instructions to be `block`.
161 void SetBlockOfInstructions(HBasicBlock* block) const;
162
163 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000164 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000165 void Add(const HInstructionList& instruction_list);
166
David Brazdil2d7352b2015-04-20 14:52:42 +0100167 // Return the number of instructions in the list. This is an expensive operation.
168 size_t CountSize() const;
169
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100170 private:
171 HInstruction* first_instruction_;
172 HInstruction* last_instruction_;
173
174 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 friend class HGraph;
176 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100177 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000178 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100179 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180
181 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
182};
183
David Brazdil4833f5a2015-12-16 10:37:39 +0000184class ReferenceTypeInfo : ValueObject {
185 public:
186 typedef Handle<mirror::Class> TypeHandle;
187
Vladimir Markoa1de9182016-02-25 11:37:38 +0000188 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
189
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100191 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
192 }
193
Vladimir Markoa1de9182016-02-25 11:37:38 +0000194 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000195 return ReferenceTypeInfo(type_handle, is_exact);
196 }
197
198 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
199
Vladimir Markof39745e2016-01-26 12:16:55 +0000200 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return handle.GetReference() != nullptr;
202 }
203
Vladimir Marko456307a2016-04-19 14:12:13 +0000204 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000205 return IsValidHandle(type_handle_);
206 }
207
208 bool IsExact() const { return is_exact_; }
209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 DCHECK(IsValid());
212 return GetTypeHandle()->IsObjectClass();
213 }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return GetTypeHandle()->IsStringClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return GetTypeHandle()->IsInterface();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsArrayClass();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsPrimitiveArray();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
243 }
244
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 DCHECK(IsValid());
247 if (!IsExact()) return false;
248 if (!IsArrayClass()) return false;
249 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
250 }
251
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000253 DCHECK(IsValid());
254 if (!IsExact()) return false;
255 if (!IsArrayClass()) return false;
256 if (!rti.IsArrayClass()) return false;
257 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
258 rti.GetTypeHandle()->GetComponentType());
259 }
260
261 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
262
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000264 DCHECK(IsValid());
265 DCHECK(rti.IsValid());
266 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
267 }
268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 DCHECK(IsValid());
271 DCHECK(rti.IsValid());
272 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
273 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
274 }
275
276 // Returns true if the type information provide the same amount of details.
277 // Note that it does not mean that the instructions have the same actual type
278 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000280 if (!IsValid() && !rti.IsValid()) {
281 // Invalid types are equal.
282 return true;
283 }
284 if (!IsValid() || !rti.IsValid()) {
285 // One is valid, the other not.
286 return false;
287 }
288 return IsExact() == rti.IsExact()
289 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
290 }
291
292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000293 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
294 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
295 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000296
297 // The class of the object.
298 TypeHandle type_handle_;
299 // Whether or not the type is exact or a superclass of the actual type.
300 // Whether or not we have any information about this type.
301 bool is_exact_;
302};
303
304std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
305
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100307class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100309 HGraph(ArenaAllocator* allocator,
310 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 const DexFile& dex_file,
312 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100314 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000316 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100318 : allocator_(allocator),
319 arena_stack_(arena_stack),
320 blocks_(allocator->Adapter(kArenaAllocBlockList)),
321 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
322 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700323 entry_block_(nullptr),
324 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100325 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100326 number_of_vregs_(0),
327 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000328 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400329 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000330 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700331 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800332 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000333 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000334 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100336 dex_file_(dex_file),
337 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100338 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100339 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800340 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
344 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
345 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100348 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000349 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700350 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100351 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 blocks_.reserve(kDefaultNumberOfBlocks);
353 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354
David Brazdilbadd8262016-02-02 16:28:56 +0000355 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700356 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000357
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 ArenaAllocator* GetAllocator() const { return allocator_; }
359 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100360 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
361
David Brazdil69ba7b72015-06-23 18:27:30 +0100362 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000363 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 HBasicBlock* GetEntryBlock() const { return entry_block_; }
366 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100367 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000368
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000369 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
370 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000371
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100373
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100374 void ComputeDominanceInformation();
375 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000376 void ClearLoopInformation();
377 void FindBackEdges(ArenaBitVector* visited);
378 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100379 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100380 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000381
David Brazdil4833f5a2015-12-16 10:37:39 +0000382 // Analyze all natural loops in this graph. Returns a code specifying that it
383 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000385 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386
David Brazdilffee3d32015-07-06 11:48:53 +0100387 // Iterate over blocks to compute try block membership. Needs reverse post
388 // order and loop information.
389 void ComputeTryBlockInformation();
390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000392 // Returns the instruction to replace the invoke expression or null if the
393 // invoke is for a void method. Note that the caller is responsible for replacing
394 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000395 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000397 // Update the loop and try membership of `block`, which was spawned from `reference`.
398 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
399 // should be the new back edge.
400 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
401 HBasicBlock* reference,
402 bool replace_if_back_edge);
403
Mingyao Yang3584bce2015-05-19 16:01:59 -0700404 // Need to add a couple of blocks to test if the loop body is entered and
405 // put deoptimization instructions, etc.
406 void TransformLoopHeaderForBCE(HBasicBlock* header);
407
Aart Bikf8f5a162017-02-06 15:35:29 -0800408 // Adds a new loop directly after the loop with the given header and exit.
409 // Returns the new preheader.
410 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
411 HBasicBlock* body,
412 HBasicBlock* exit);
413
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000414 // Removes `block` from the graph. Assumes `block` has been disconnected from
415 // other blocks and has no instructions or phis.
416 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000417
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418 // Splits the edge between `block` and `successor` while preserving the
419 // indices in the predecessor/successor lists. If there are multiple edges
420 // between the blocks, the lowest indices are used.
421 // Returns the new block which is empty and has the same dex pc as `successor`.
422 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100425 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000426
427 // Transform a loop into a format with a single preheader.
428 //
429 // Each phi in the header should be split: original one in the header should only hold
430 // inputs reachable from the back edges and a single input from the preheader. The newly created
431 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
432 //
433 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
434 // that no longer have this property.
435 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
436
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100437 void SimplifyLoop(HBasicBlock* header);
438
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100440 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000441 return current_instruction_id_++;
442 }
443
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000444 int32_t GetCurrentInstructionId() const {
445 return current_instruction_id_;
446 }
447
448 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100449 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000450 current_instruction_id_ = id;
451 }
452
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100453 uint16_t GetMaximumNumberOfOutVRegs() const {
454 return maximum_number_of_out_vregs_;
455 }
456
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000457 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
458 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100459 }
460
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100461 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
462 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
463 }
464
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000465 void UpdateTemporariesVRegSlots(size_t slots) {
466 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100467 }
468
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000469 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100470 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100472 }
473
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100474 void SetNumberOfVRegs(uint16_t number_of_vregs) {
475 number_of_vregs_ = number_of_vregs;
476 }
477
478 uint16_t GetNumberOfVRegs() const {
479 return number_of_vregs_;
480 }
481
482 void SetNumberOfInVRegs(uint16_t value) {
483 number_of_in_vregs_ = value;
484 }
485
David Brazdildee58d62016-04-07 09:54:26 +0000486 uint16_t GetNumberOfInVRegs() const {
487 return number_of_in_vregs_;
488 }
489
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100490 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100491 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100492 return number_of_vregs_ - number_of_in_vregs_;
493 }
494
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100495 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100496 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100498
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100499 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
500 DCHECK(GetReversePostOrder()[0] == entry_block_);
501 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
502 }
503
504 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
505 return ReverseRange(GetReversePostOrder());
506 }
507
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100508 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100509 return linear_order_;
510 }
511
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100512 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
513 return ReverseRange(GetLinearOrder());
514 }
515
Mark Mendell1152c922015-04-24 17:06:35 -0400516 bool HasBoundsChecks() const {
517 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800518 }
519
Mark Mendell1152c922015-04-24 17:06:35 -0400520 void SetHasBoundsChecks(bool value) {
521 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800522 }
523
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000524 bool IsDebuggable() const { return debuggable_; }
525
David Brazdil8d5b8b22015-03-24 10:51:52 +0000526 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 // already, it is created and inserted into the graph. This method is only for
528 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100529 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000530
531 // TODO: This is problematic for the consistency of reference type propagation
532 // because it can be created anytime after the pass and thus it will be left
533 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600534 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000535
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600536 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
537 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000538 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
540 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000541 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600542 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
543 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000544 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600545 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
546 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000547 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000548
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100549 HCurrentMethod* GetCurrentMethod();
550
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100551 const DexFile& GetDexFile() const {
552 return dex_file_;
553 }
554
555 uint32_t GetMethodIdx() const {
556 return method_idx_;
557 }
558
Igor Murashkind01745e2017-04-05 16:40:31 -0700559 // Get the method name (without the signature), e.g. "<init>"
560 const char* GetMethodName() const;
561
562 // Get the pretty method name (class + name + optionally signature).
563 std::string PrettyMethod(bool with_signature = true) const;
564
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100565 InvokeType GetInvokeType() const {
566 return invoke_type_;
567 }
568
Mark Mendellc4701932015-04-10 13:18:51 -0400569 InstructionSet GetInstructionSet() const {
570 return instruction_set_;
571 }
572
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000573 bool IsCompilingOsr() const { return osr_; }
574
Mingyao Yang063fc772016-08-02 11:02:54 -0700575 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
576 return cha_single_implementation_list_;
577 }
578
579 void AddCHASingleImplementationDependency(ArtMethod* method) {
580 cha_single_implementation_list_.insert(method);
581 }
582
583 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800584 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700585 }
586
David Brazdil77a48ae2015-09-15 12:34:04 +0000587 bool HasTryCatch() const { return has_try_catch_; }
588 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100589
Aart Bikb13c65b2017-03-21 20:14:07 -0700590 bool HasSIMD() const { return has_simd_; }
591 void SetHasSIMD(bool value) { has_simd_ = value; }
592
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800593 bool HasLoops() const { return has_loops_; }
594 void SetHasLoops(bool value) { has_loops_ = value; }
595
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000596 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
597 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
598
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100599 ArtMethod* GetArtMethod() const { return art_method_; }
600 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
601
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100602 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500603 // The instruction has been inserted into the graph, either as a constant, or
604 // before cursor.
605 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
606
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000607 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
608
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800609 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
610 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
611 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
612
David Brazdil2d7352b2015-04-20 14:52:42 +0100613 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000614 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100615 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 template <class InstructionType, typename ValueType>
618 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600619 ArenaSafeMap<ValueType, InstructionType*>* cache,
620 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 // Try to find an existing constant of the given value.
622 InstructionType* constant = nullptr;
623 auto cached_constant = cache->find(value);
624 if (cached_constant != cache->end()) {
625 constant = cached_constant->second;
626 }
627
628 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100629 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000630 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100631 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 cache->Overwrite(value, constant);
633 InsertConstant(constant);
634 }
635 return constant;
636 }
637
David Brazdil8d5b8b22015-03-24 10:51:52 +0000638 void InsertConstant(HConstant* instruction);
639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 // Cache a float constant into the graph. This method should only be
641 // called by the SsaBuilder when creating "equivalent" instructions.
642 void CacheFloatConstant(HFloatConstant* constant);
643
644 // See CacheFloatConstant comment.
645 void CacheDoubleConstant(HDoubleConstant* constant);
646
Vladimir Markoca6fff82017-10-03 14:49:14 +0100647 ArenaAllocator* const allocator_;
648 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000649
650 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000655
Aart Bik281c6812016-08-26 11:31:48 -0700656 // List of blocks to perform a linear order tree traversal. Unlike the reverse
657 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100659
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000660 HBasicBlock* entry_block_;
661 HBasicBlock* exit_block_;
662
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100663 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100664 uint16_t maximum_number_of_out_vregs_;
665
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100666 // The number of virtual registers in this method. Contains the parameters.
667 uint16_t number_of_vregs_;
668
669 // The number of virtual registers used by parameters of this method.
670 uint16_t number_of_in_vregs_;
671
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000672 // Number of vreg size slots that the temporaries use (used in baseline compiler).
673 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100674
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800675 // Flag whether there are bounds checks in the graph. We can skip
676 // BCE if it's false. It's only best effort to keep it up to date in
677 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400678 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800679
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800680 // Flag whether there are try/catch blocks in the graph. We will skip
681 // try/catch-related passes if it's false. It's only best effort to keep
682 // it up to date in the presence of code elimination so there might be
683 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000684 bool has_try_catch_;
685
Aart Bikb13c65b2017-03-21 20:14:07 -0700686 // Flag whether SIMD instructions appear in the graph. If true, the
687 // code generators may have to be more careful spilling the wider
688 // contents of SIMD registers.
689 bool has_simd_;
690
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800691 // Flag whether there are any loops in the graph. We can skip loop
692 // optimization if it's false. It's only best effort to keep it up
693 // to date in the presence of code elimination so there might be false
694 // positives.
695 bool has_loops_;
696
697 // Flag whether there are any irreducible loops in the graph. It's only
698 // best effort to keep it up to date in the presence of code elimination
699 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000700 bool has_irreducible_loops_;
701
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000702 // Indicates whether the graph should be compiled in a way that
703 // ensures full debuggability. If false, we can apply more
704 // aggressive optimizations that may limit the level of debugging.
705 const bool debuggable_;
706
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000707 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000708 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100710 // The dex file from which the method is from.
711 const DexFile& dex_file_;
712
713 // The method index in the dex file.
714 const uint32_t method_idx_;
715
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100716 // If inlined, this encodes how the callee is being invoked.
717 const InvokeType invoke_type_;
718
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100719 // Whether the graph has been transformed to SSA form. Only used
720 // in debug mode to ensure we are not using properties only valid
721 // for non-SSA form (like the number of temporaries).
722 bool in_ssa_form_;
723
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800724 // Number of CHA guards in the graph. Used to short-circuit the
725 // CHA guard optimization pass when there is no CHA guard left.
726 uint32_t number_of_cha_guards_;
727
Mathieu Chartiere401d142015-04-22 13:56:20 -0700728 const InstructionSet instruction_set_;
729
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000730 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000731 HNullConstant* cached_null_constant_;
732 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000733 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000734 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000736
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100737 HCurrentMethod* cached_current_method_;
738
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100739 // The ArtMethod this graph is for. Note that for AOT, it may be null,
740 // for example for methods whose declaring class could not be resolved
741 // (such as when the superclass could not be found).
742 ArtMethod* art_method_;
743
David Brazdil4833f5a2015-12-16 10:37:39 +0000744 // Keep the RTI of inexact Object to avoid having to pass stack handle
745 // collection pointer to passes which may create NullConstant.
746 ReferenceTypeInfo inexact_object_rti_;
747
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000748 // Whether we are compiling this graph for on stack replacement: this will
749 // make all loops seen as irreducible and emit special stack maps to mark
750 // compiled code entries which the interpreter can directly jump to.
751 const bool osr_;
752
Mingyao Yang063fc772016-08-02 11:02:54 -0700753 // List of methods that are assumed to have single implementation.
754 ArenaSet<ArtMethod*> cha_single_implementation_list_;
755
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000756 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100757 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000758 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000759 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760 DISALLOW_COPY_AND_ASSIGN(HGraph);
761};
762
Vladimir Markof9f64412015-09-02 14:05:49 +0100763class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000764 public:
765 HLoopInformation(HBasicBlock* header, HGraph* graph)
766 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100767 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000768 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100769 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100770 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100771 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100772 blocks_(graph->GetAllocator(),
773 graph->GetBlocks().size(),
774 true,
775 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776 back_edges_.reserve(kDefaultNumberOfBackEdges);
777 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000779 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100780 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781
782 void Dump(std::ostream& os);
783
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100784 HBasicBlock* GetHeader() const {
785 return header_;
786 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000787
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000788 void SetHeader(HBasicBlock* block) {
789 header_ = block;
790 }
791
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100792 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
793 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
794 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
795
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000796 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100797 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 }
799
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100800 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100801 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 }
803
David Brazdil46e2a392015-03-16 17:31:52 +0000804 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100805 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100806 }
807
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000808 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100809 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000810 }
811
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100812 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100813
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100814 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100816 }
817
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100818 // Returns the lifetime position of the back edge that has the
819 // greatest lifetime position.
820 size_t GetLifetimeEnd() const;
821
822 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100823 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100824 }
825
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000826 // Finds blocks that are part of this loop.
827 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100828
Artem Serov7f4aff62017-06-21 17:02:18 +0100829 // Updates blocks population of the loop and all of its outer' ones recursively after the
830 // population of the inner loop is updated.
831 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
832
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 // Returns whether this loop information contains `block`.
834 // Note that this loop information *must* be populated before entering this function.
835 bool Contains(const HBasicBlock& block) const;
836
837 // Returns whether this loop information is an inner loop of `other`.
838 // Note that `other` *must* be populated before entering this function.
839 bool IsIn(const HLoopInformation& other) const;
840
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800841 // Returns true if instruction is not defined within this loop.
842 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700843
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100844 const ArenaBitVector& GetBlocks() const { return blocks_; }
845
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000847 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000849 void ClearAllBlocks() {
850 blocks_.ClearAllBits();
851 }
852
David Brazdil3f4a5222016-05-06 12:46:21 +0100853 bool HasBackEdgeNotDominatedByHeader() const;
854
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100855 bool IsPopulated() const {
856 return blocks_.GetHighestBitSet() != -1;
857 }
858
Anton Shaminf89381f2016-05-16 16:44:13 +0600859 bool DominatesAllBackEdges(HBasicBlock* block);
860
David Sehrc757dec2016-11-04 15:48:34 -0700861 bool HasExitEdge() const;
862
Artem Serov7f4aff62017-06-21 17:02:18 +0100863 // Resets back edge and blocks-in-loop data.
864 void ResetBasicBlockData() {
865 back_edges_.clear();
866 ClearAllBlocks();
867 }
868
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000869 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100870 // Internal recursive implementation of `Populate`.
871 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100872 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100873
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100875 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000876 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100877 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100878 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100879 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000880
881 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
882};
883
David Brazdilec16f792015-08-19 15:04:01 +0100884// Stores try/catch information for basic blocks.
885// Note that HGraph is constructed so that catch blocks cannot simultaneously
886// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100887class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100888 public:
889 // Try block information constructor.
890 explicit TryCatchInformation(const HTryBoundary& try_entry)
891 : try_entry_(&try_entry),
892 catch_dex_file_(nullptr),
893 catch_type_index_(DexFile::kDexNoIndex16) {
894 DCHECK(try_entry_ != nullptr);
895 }
896
897 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800898 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100899 : try_entry_(nullptr),
900 catch_dex_file_(&dex_file),
901 catch_type_index_(catch_type_index) {}
902
903 bool IsTryBlock() const { return try_entry_ != nullptr; }
904
905 const HTryBoundary& GetTryEntry() const {
906 DCHECK(IsTryBlock());
907 return *try_entry_;
908 }
909
910 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
911
912 bool IsCatchAllTypeIndex() const {
913 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800914 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100915 }
916
Andreas Gampea5b09a62016-11-17 15:21:22 -0800917 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100918 DCHECK(IsCatchBlock());
919 return catch_type_index_;
920 }
921
922 const DexFile& GetCatchDexFile() const {
923 DCHECK(IsCatchBlock());
924 return *catch_dex_file_;
925 }
926
927 private:
928 // One of possibly several TryBoundary instructions entering the block's try.
929 // Only set for try blocks.
930 const HTryBoundary* try_entry_;
931
932 // Exception type information. Only set for catch blocks.
933 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800934 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100935};
936
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100937static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100938static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940// A block in a method. Contains the list of instructions represented
941// as a double linked list. Each block knows its predecessors and
942// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100943
Vladimir Markof9f64412015-09-02 14:05:49 +0100944class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000945 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700946 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100948 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
949 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000950 loop_information_(nullptr),
951 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100952 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100953 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100954 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100955 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000956 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000957 try_catch_information_(nullptr) {
958 predecessors_.reserve(kDefaultNumberOfPredecessors);
959 successors_.reserve(kDefaultNumberOfSuccessors);
960 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
961 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962
Vladimir Marko60584552015-09-03 13:35:12 +0000963 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100964 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965 }
966
Vladimir Marko60584552015-09-03 13:35:12 +0000967 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100968 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969 }
970
David Brazdild26a4112015-11-10 11:07:31 +0000971 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
972 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
973
Vladimir Marko60584552015-09-03 13:35:12 +0000974 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
975 return ContainsElement(successors_, block, start_from);
976 }
977
978 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100979 return dominated_blocks_;
980 }
981
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100982 bool IsEntryBlock() const {
983 return graph_->GetEntryBlock() == this;
984 }
985
986 bool IsExitBlock() const {
987 return graph_->GetExitBlock() == this;
988 }
989
David Brazdil46e2a392015-03-16 17:31:52 +0000990 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200991 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700992 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000993 bool IsSingleTryBoundary() const;
994
995 // Returns true if this block emits nothing but a jump.
996 bool IsSingleJump() const {
997 HLoopInformation* loop_info = GetLoopInformation();
998 return (IsSingleGoto() || IsSingleTryBoundary())
999 // Back edges generate a suspend check.
1000 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1001 }
David Brazdil46e2a392015-03-16 17:31:52 +00001002
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001003 void AddBackEdge(HBasicBlock* back_edge) {
1004 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001005 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001006 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001007 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001008 loop_information_->AddBackEdge(back_edge);
1009 }
1010
Artem Serov7f4aff62017-06-21 17:02:18 +01001011 // Registers a back edge; if the block was not a loop header before the call associates a newly
1012 // created loop info with it.
1013 //
1014 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1015 // info for all blocks during back edges recalculation.
1016 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1017 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1018 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1019 }
1020 loop_information_->AddBackEdge(back_edge);
1021 }
1022
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001023 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001024 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001025
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001026 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001027 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001028 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001029
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001030 HBasicBlock* GetDominator() const { return dominator_; }
1031 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001032 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1033
1034 void RemoveDominatedBlock(HBasicBlock* block) {
1035 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001036 }
Vladimir Marko60584552015-09-03 13:35:12 +00001037
1038 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1039 ReplaceElement(dominated_blocks_, existing, new_block);
1040 }
1041
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001042 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001043
1044 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001045 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001046 }
1047
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1049 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001050 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001051 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001052 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1053 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001055 HInstruction* GetFirstInstructionDisregardMoves() const;
1056
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001057 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001058 successors_.push_back(block);
1059 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060 }
1061
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001062 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1063 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001064 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001065 new_block->predecessors_.push_back(this);
1066 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001067 }
1068
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001069 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1070 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001072 new_block->successors_.push_back(this);
1073 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001074 }
1075
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001076 // Insert `this` between `predecessor` and `successor. This method
1077 // preserves the indicies, and will update the first edge found between
1078 // `predecessor` and `successor`.
1079 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1080 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001081 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001082 successor->predecessors_[predecessor_index] = this;
1083 predecessor->successors_[successor_index] = this;
1084 successors_.push_back(successor);
1085 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001086 }
1087
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001088 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001089 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001090 }
1091
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001093 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 }
1095
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001096 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001097 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 }
1099
1100 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001101 predecessors_.push_back(block);
1102 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001103 }
1104
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001105 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001106 DCHECK_EQ(predecessors_.size(), 2u);
1107 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001108 }
1109
David Brazdil769c9e52015-04-27 13:54:09 +01001110 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001111 DCHECK_EQ(successors_.size(), 2u);
1112 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001113 }
1114
David Brazdilfc6a86a2015-06-26 10:33:45 +00001115 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001116 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001117 }
1118
David Brazdilfc6a86a2015-06-26 10:33:45 +00001119 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001120 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001121 }
1122
David Brazdilfc6a86a2015-06-26 10:33:45 +00001123 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001124 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001125 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001126 }
1127
1128 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001129 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001130 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001131 }
1132
1133 // Returns whether the first occurrence of `predecessor` in the list of
1134 // predecessors is at index `idx`.
1135 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001136 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001137 return GetPredecessorIndexOf(predecessor) == idx;
1138 }
1139
David Brazdild7558da2015-09-22 13:04:14 +01001140 // Create a new block between this block and its predecessors. The new block
1141 // is added to the graph, all predecessor edges are relinked to it and an edge
1142 // is created to `this`. Returns the new empty block. Reverse post order or
1143 // loop and try/catch information are not updated.
1144 HBasicBlock* CreateImmediateDominator();
1145
David Brazdilfc6a86a2015-06-26 10:33:45 +00001146 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001147 // created, latter block. Note that this method will add the block to the
1148 // graph, create a Goto at the end of the former block and will create an edge
1149 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001150 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001151 HBasicBlock* SplitBefore(HInstruction* cursor);
1152
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001153 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001154 // created block. Note that this method just updates raw block information,
1155 // like predecessors, successors, dominators, and instruction list. It does not
1156 // update the graph, reverse post order, loop information, nor make sure the
1157 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001158 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1159
1160 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1161 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001162
1163 // Merge `other` at the end of `this`. Successors and dominated blocks of
1164 // `other` are changed to be successors and dominated blocks of `this`. Note
1165 // that this method does not update the graph, reverse post order, loop
1166 // information, nor make sure the blocks are consistent (for example ending
1167 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001168 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001169
1170 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1171 // of `this` are moved to `other`.
1172 // Note that this method does not update the graph, reverse post order, loop
1173 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001174 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001175 void ReplaceWith(HBasicBlock* other);
1176
Aart Bik6b69e0a2017-01-11 10:20:43 -08001177 // Merges the instructions of `other` at the end of `this`.
1178 void MergeInstructionsWith(HBasicBlock* other);
1179
David Brazdil2d7352b2015-04-20 14:52:42 +01001180 // Merge `other` at the end of `this`. This method updates loops, reverse post
1181 // order, links to predecessors, successors, dominators and deletes the block
1182 // from the graph. The two blocks must be successive, i.e. `this` the only
1183 // predecessor of `other` and vice versa.
1184 void MergeWith(HBasicBlock* other);
1185
1186 // Disconnects `this` from all its predecessors, successors and dominator,
1187 // removes it from all loops it is included in and eventually from the graph.
1188 // The block must not dominate any other block. Predecessors and successors
1189 // are safely updated.
1190 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001193 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001194 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001195 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001196 // Replace phi `initial` with `replacement` within this block.
1197 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001198 // Replace instruction `initial` with `replacement` within this block.
1199 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1200 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001201 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001202 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001203 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1204 // instruction list. With 'ensure_safety' set to true, it verifies that the
1205 // instruction is not in use and removes it from the use lists of its inputs.
1206 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1207 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001208 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001209
1210 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001211 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001212 }
1213
Roland Levillain6b879dd2014-09-22 17:13:44 +01001214 bool IsLoopPreHeaderFirstPredecessor() const {
1215 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001216 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001217 }
1218
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001219 bool IsFirstPredecessorBackEdge() const {
1220 DCHECK(IsLoopHeader());
1221 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1222 }
1223
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001224 HLoopInformation* GetLoopInformation() const {
1225 return loop_information_;
1226 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001227
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001228 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001229 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 void SetInLoop(HLoopInformation* info) {
1232 if (IsLoopHeader()) {
1233 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001234 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001235 loop_information_ = info;
1236 } else if (loop_information_->Contains(*info->GetHeader())) {
1237 // Block is currently part of an outer loop. Make it part of this inner loop.
1238 // Note that a non loop header having a loop information means this loop information
1239 // has already been populated
1240 loop_information_ = info;
1241 } else {
1242 // Block is part of an inner loop. Do not update the loop information.
1243 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1244 // at this point, because this method is being called while populating `info`.
1245 }
1246 }
1247
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001248 // Raw update of the loop information.
1249 void SetLoopInformation(HLoopInformation* info) {
1250 loop_information_ = info;
1251 }
1252
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001253 bool IsInLoop() const { return loop_information_ != nullptr; }
1254
David Brazdilec16f792015-08-19 15:04:01 +01001255 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1256
1257 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1258 try_catch_information_ = try_catch_information;
1259 }
1260
1261 bool IsTryBlock() const {
1262 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1263 }
1264
1265 bool IsCatchBlock() const {
1266 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1267 }
David Brazdilffee3d32015-07-06 11:48:53 +01001268
1269 // Returns the try entry that this block's successors should have. They will
1270 // be in the same try, unless the block ends in a try boundary. In that case,
1271 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001272 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001273
David Brazdild7558da2015-09-22 13:04:14 +01001274 bool HasThrowingInstructions() const;
1275
David Brazdila4b8c212015-05-07 09:59:30 +01001276 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001277 bool Dominates(HBasicBlock* block) const;
1278
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001279 size_t GetLifetimeStart() const { return lifetime_start_; }
1280 size_t GetLifetimeEnd() const { return lifetime_end_; }
1281
1282 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1283 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1284
David Brazdil8d5b8b22015-03-24 10:51:52 +00001285 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001286 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001287 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001288 bool HasSinglePhi() const;
1289
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001290 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001291 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001292 ArenaVector<HBasicBlock*> predecessors_;
1293 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001294 HInstructionList instructions_;
1295 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001296 HLoopInformation* loop_information_;
1297 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001298 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001299 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001300 // The dex program counter of the first instruction of this block.
1301 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001302 size_t lifetime_start_;
1303 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001304 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001305
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001306 friend class HGraph;
1307 friend class HInstruction;
1308
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001309 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1310};
1311
David Brazdilb2bd1c52015-03-25 11:17:37 +00001312// Iterates over the LoopInformation of all loops which contain 'block'
1313// from the innermost to the outermost.
1314class HLoopInformationOutwardIterator : public ValueObject {
1315 public:
1316 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1317 : current_(block.GetLoopInformation()) {}
1318
1319 bool Done() const { return current_ == nullptr; }
1320
1321 void Advance() {
1322 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001323 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001324 }
1325
1326 HLoopInformation* Current() const {
1327 DCHECK(!Done());
1328 return current_;
1329 }
1330
1331 private:
1332 HLoopInformation* current_;
1333
1334 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1335};
1336
Alexandre Ramesef20f712015-06-09 10:29:30 +01001337#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001338 M(Above, Condition) \
1339 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001340 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001341 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001342 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001343 M(ArrayGet, Instruction) \
1344 M(ArrayLength, Instruction) \
1345 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001346 M(Below, Condition) \
1347 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001348 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001349 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001350 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001351 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001352 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001353 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001354 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001355 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001356 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001357 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001358 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001359 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001360 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001361 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001362 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001363 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001364 M(Exit, Instruction) \
1365 M(FloatConstant, Constant) \
1366 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(GreaterThan, Condition) \
1368 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001370 M(InstanceFieldGet, Instruction) \
1371 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001372 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001373 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001374 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001375 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001376 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001377 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001378 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001379 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001380 M(LessThan, Condition) \
1381 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001382 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001383 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001384 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001385 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001386 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001387 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001388 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001389 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001390 M(Neg, UnaryOperation) \
1391 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001392 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001393 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001394 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001395 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001397 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001398 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001400 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001401 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001402 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001403 M(Return, Instruction) \
1404 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001405 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001406 M(Shl, BinaryOperation) \
1407 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001408 M(StaticFieldGet, Instruction) \
1409 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001410 M(UnresolvedInstanceFieldGet, Instruction) \
1411 M(UnresolvedInstanceFieldSet, Instruction) \
1412 M(UnresolvedStaticFieldGet, Instruction) \
1413 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001414 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001415 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001416 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001417 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001418 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001419 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001420 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001421 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001422 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001423 M(VecExtractScalar, VecUnaryOperation) \
1424 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001425 M(VecCnv, VecUnaryOperation) \
1426 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001427 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001428 M(VecNot, VecUnaryOperation) \
1429 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001430 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001431 M(VecSub, VecBinaryOperation) \
1432 M(VecMul, VecBinaryOperation) \
1433 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001434 M(VecMin, VecBinaryOperation) \
1435 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001436 M(VecAnd, VecBinaryOperation) \
1437 M(VecAndNot, VecBinaryOperation) \
1438 M(VecOr, VecBinaryOperation) \
1439 M(VecXor, VecBinaryOperation) \
1440 M(VecShl, VecBinaryOperation) \
1441 M(VecShr, VecBinaryOperation) \
1442 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001443 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001444 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001445 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001446 M(VecLoad, VecMemoryOperation) \
1447 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001448
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001449/*
1450 * Instructions, shared across several (not all) architectures.
1451 */
1452#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1453#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1454#else
1455#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001456 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001457 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001458 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001459 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001460#endif
1461
Alexandre Ramesef20f712015-06-09 10:29:30 +01001462#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1463
1464#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1465
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001466#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001467#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001468#else
1469#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1470 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001471 M(MipsPackedSwitch, Instruction) \
1472 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001473#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001474
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001475#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1476
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001477#ifndef ART_ENABLE_CODEGEN_x86
1478#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1479#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001480#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1481 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001482 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001483 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001484 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001485#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001486
1487#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1488
1489#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1490 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001491 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001492 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1493 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001494 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001495 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001496 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1497 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1498
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001499#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1500 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001501 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001502 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001503 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001504 M(Invoke, Instruction) \
1505 M(VecOperation, Instruction) \
1506 M(VecUnaryOperation, VecOperation) \
1507 M(VecBinaryOperation, VecOperation) \
1508 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001509
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001510#define FOR_EACH_INSTRUCTION(M) \
1511 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1512 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1513
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001514#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001515FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1516#undef FORWARD_DECLARATION
1517
Vladimir Marko372f10e2016-05-17 16:30:10 +01001518#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001519 private: \
1520 H##type& operator=(const H##type&) = delete; \
1521 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001522 const char* DebugName() const OVERRIDE { return #type; } \
1523 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1524 return other->Is##type(); \
1525 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001526 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1527 DCHECK(IsClonable()); \
1528 return new (arena) H##type(*this->As##type()); \
1529 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001530 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001531
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001532#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001533 private: \
1534 H##type& operator=(const H##type&) = delete; \
1535 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001536 bool Is##type() const { return As##type() != nullptr; } \
1537 const H##type* As##type() const { return this; } \
1538 H##type* As##type() { return this; }
1539
Artem Serovcced8ba2017-07-19 18:18:09 +01001540#define DEFAULT_COPY_CONSTRUCTOR(type) \
1541 explicit H##type(const H##type& other) = default;
1542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001543template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001544class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1545 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001546 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001547 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001548 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001549 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001551 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001552 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001554 private:
David Brazdiled596192015-01-23 10:39:45 +00001555 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001556 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001557
1558 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001559 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001560
Vladimir Marko46817b82016-03-29 12:21:58 +01001561 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001562
1563 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1564};
1565
David Brazdiled596192015-01-23 10:39:45 +00001566template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001567using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001568
David Brazdil1abb4192015-02-17 18:33:36 +00001569// This class is used by HEnvironment and HInstruction classes to record the
1570// instructions they use and pointers to the corresponding HUseListNodes kept
1571// by the used instructions.
1572template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001573class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001574 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001575 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1576 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001577
Vladimir Marko46817b82016-03-29 12:21:58 +01001578 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1579 : HUserRecord(old_record.instruction_, before_use_node) {}
1580 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1581 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001582 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001583 }
1584
1585 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001586 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1587 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001588
1589 private:
1590 // Instruction used by the user.
1591 HInstruction* instruction_;
1592
Vladimir Marko46817b82016-03-29 12:21:58 +01001593 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1594 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001595};
1596
Vladimir Markoe9004912016-06-16 16:50:52 +01001597// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1598// This is used for HInstruction::GetInputs() to return a container wrapper providing
1599// HInstruction* values even though the underlying container has HUserRecord<>s.
1600struct HInputExtractor {
1601 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1602 return record.GetInstruction();
1603 }
1604 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1605 return record.GetInstruction();
1606 }
1607};
1608
1609using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1610using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1611
Aart Bik854a02b2015-07-14 16:07:00 -07001612/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001614 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 * For write/read dependences on fields/arrays, the dependence analysis uses
1616 * type disambiguation (e.g. a float field write cannot modify the value of an
1617 * integer field read) and the access type (e.g. a reference array write cannot
1618 * modify the value of a reference field read [although it may modify the
1619 * reference fetch prior to reading the field, which is represented by its own
1620 * write/read dependence]). The analysis makes conservative points-to
1621 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1622 * the same, and any reference read depends on any reference read without
1623 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001624 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 * The internal representation uses 38-bit and is described in the table below.
1626 * The first line indicates the side effect, and for field/array accesses the
1627 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 * The two numbered lines below indicate the bit position in the bitfield (read
1630 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001631 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1633 * +-------------+---------+---------+--------------+---------+---------+
1634 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1635 * | 3 |333333322|222222221| 1 |111111110|000000000|
1636 * | 7 |654321098|765432109| 8 |765432109|876543210|
1637 *
1638 * Note that, to ease the implementation, 'changes' bits are least significant
1639 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001640 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641class SideEffects : public ValueObject {
1642 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001643 SideEffects() : flags_(0) {}
1644
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001645 static SideEffects None() {
1646 return SideEffects(0);
1647 }
1648
1649 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 return SideEffects(kAllChangeBits | kAllDependOnBits);
1651 }
1652
1653 static SideEffects AllChanges() {
1654 return SideEffects(kAllChangeBits);
1655 }
1656
1657 static SideEffects AllDependencies() {
1658 return SideEffects(kAllDependOnBits);
1659 }
1660
1661 static SideEffects AllExceptGCDependency() {
1662 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1663 }
1664
1665 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001666 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001667 }
1668
Aart Bik34c3ba92015-07-20 14:08:59 -07001669 static SideEffects AllWrites() {
1670 return SideEffects(kAllWrites);
1671 }
1672
1673 static SideEffects AllReads() {
1674 return SideEffects(kAllReads);
1675 }
1676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001677 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001678 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001679 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001680 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001681 }
1682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001684 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001685 }
1686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001688 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001689 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001690 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001691 }
1692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001693 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001694 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001695 }
1696
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001697 static SideEffects CanTriggerGC() {
1698 return SideEffects(1ULL << kCanTriggerGCBit);
1699 }
1700
1701 static SideEffects DependsOnGC() {
1702 return SideEffects(1ULL << kDependsOnGCBit);
1703 }
1704
Aart Bik854a02b2015-07-14 16:07:00 -07001705 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001706 SideEffects Union(SideEffects other) const {
1707 return SideEffects(flags_ | other.flags_);
1708 }
1709
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001710 SideEffects Exclusion(SideEffects other) const {
1711 return SideEffects(flags_ & ~other.flags_);
1712 }
1713
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001714 void Add(SideEffects other) {
1715 flags_ |= other.flags_;
1716 }
1717
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001718 bool Includes(SideEffects other) const {
1719 return (other.flags_ & flags_) == other.flags_;
1720 }
1721
1722 bool HasSideEffects() const {
1723 return (flags_ & kAllChangeBits);
1724 }
1725
1726 bool HasDependencies() const {
1727 return (flags_ & kAllDependOnBits);
1728 }
1729
1730 // Returns true if there are no side effects or dependencies.
1731 bool DoesNothing() const {
1732 return flags_ == 0;
1733 }
1734
Aart Bik854a02b2015-07-14 16:07:00 -07001735 // Returns true if something is written.
1736 bool DoesAnyWrite() const {
1737 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001738 }
1739
Aart Bik854a02b2015-07-14 16:07:00 -07001740 // Returns true if something is read.
1741 bool DoesAnyRead() const {
1742 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001743 }
1744
Aart Bik854a02b2015-07-14 16:07:00 -07001745 // Returns true if potentially everything is written and read
1746 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001747 bool DoesAllReadWrite() const {
1748 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1749 }
1750
Aart Bik854a02b2015-07-14 16:07:00 -07001751 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001752 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001753 }
1754
Roland Levillain0d5a2812015-11-13 10:07:31 +00001755 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001756 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001757 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1758 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001759 }
1760
1761 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001762 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001763 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001764 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001765 for (int s = kLastBit; s >= 0; s--) {
1766 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1767 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1768 // This is a bit for the GC side effect.
1769 if (current_bit_is_set) {
1770 flags += "GC";
1771 }
Aart Bik854a02b2015-07-14 16:07:00 -07001772 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001773 } else {
1774 // This is a bit for the array/field analysis.
1775 // The underscore character stands for the 'can trigger GC' bit.
1776 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1777 if (current_bit_is_set) {
1778 flags += kDebug[s];
1779 }
1780 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1781 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1782 flags += "|";
1783 }
1784 }
Aart Bik854a02b2015-07-14 16:07:00 -07001785 }
1786 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001787 }
1788
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001789 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 private:
1792 static constexpr int kFieldArrayAnalysisBits = 9;
1793
1794 static constexpr int kFieldWriteOffset = 0;
1795 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1796 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1797 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1798
1799 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1800
1801 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1802 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1803 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1804 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1805
1806 static constexpr int kLastBit = kDependsOnGCBit;
1807 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1808
1809 // Aliases.
1810
1811 static_assert(kChangeBits == kDependOnBits,
1812 "the 'change' bits should match the 'depend on' bits.");
1813
1814 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1815 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1816 static constexpr uint64_t kAllWrites =
1817 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1818 static constexpr uint64_t kAllReads =
1819 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001820
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001821 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001823 int shift;
1824 switch (type) {
1825 case DataType::Type::kReference: shift = 0; break;
1826 case DataType::Type::kBool: shift = 1; break;
1827 case DataType::Type::kInt8: shift = 2; break;
1828 case DataType::Type::kUint16: shift = 3; break;
1829 case DataType::Type::kInt16: shift = 4; break;
1830 case DataType::Type::kInt32: shift = 5; break;
1831 case DataType::Type::kInt64: shift = 6; break;
1832 case DataType::Type::kFloat32: shift = 7; break;
1833 case DataType::Type::kFloat64: shift = 8; break;
1834 default:
1835 LOG(FATAL) << "Unexpected data type " << type;
1836 UNREACHABLE();
1837 }
Aart Bik854a02b2015-07-14 16:07:00 -07001838 DCHECK_LE(kFieldWriteOffset, shift);
1839 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001840 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001841 }
1842
1843 // Private constructor on direct flags value.
1844 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1845
1846 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001847};
1848
David Brazdiled596192015-01-23 10:39:45 +00001849// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001850class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001851 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001852 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001853 size_t number_of_vregs,
1854 ArtMethod* method,
1855 uint32_t dex_pc,
1856 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001857 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1858 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001859 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001860 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001861 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001862 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001863 }
1864
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001865 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1866 const HEnvironment& to_copy,
1867 HInstruction* holder)
1868 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001869 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001870 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001871 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001872 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001873
Vladimir Markoec32f642017-06-02 10:51:55 +01001874 void AllocateLocations() {
1875 DCHECK(locations_.empty());
1876 locations_.resize(vregs_.size());
1877 }
1878
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001879 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001880 if (parent_ != nullptr) {
1881 parent_->SetAndCopyParentChain(allocator, parent);
1882 } else {
1883 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1884 parent_->CopyFrom(parent);
1885 if (parent->GetParent() != nullptr) {
1886 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1887 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001888 }
David Brazdiled596192015-01-23 10:39:45 +00001889 }
1890
Vladimir Marko69d310e2017-10-09 14:12:23 +01001891 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001892 void CopyFrom(HEnvironment* environment);
1893
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001894 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1895 // input to the loop phi instead. This is for inserting instructions that
1896 // require an environment (like HDeoptimization) in the loop pre-header.
1897 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001898
1899 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001900 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001901 }
1902
1903 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001904 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001905 }
1906
David Brazdil1abb4192015-02-17 18:33:36 +00001907 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001908
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001909 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001910
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001911 HEnvironment* GetParent() const { return parent_; }
1912
1913 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001914 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001915 }
1916
1917 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001918 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001919 }
1920
1921 uint32_t GetDexPc() const {
1922 return dex_pc_;
1923 }
1924
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001925 ArtMethod* GetMethod() const {
1926 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001927 }
1928
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001929 HInstruction* GetHolder() const {
1930 return holder_;
1931 }
1932
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001933
1934 bool IsFromInlinedInvoke() const {
1935 return GetParent() != nullptr;
1936 }
1937
David Brazdiled596192015-01-23 10:39:45 +00001938 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001939 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1940 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001941 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001942 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001943 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001944
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001945 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001946 HInstruction* const holder_;
1947
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001948 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001949
1950 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1951};
1952
Vladimir Markof9f64412015-09-02 14:05:49 +01001953class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001954 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301955#define DECLARE_KIND(type, super) k##type,
1956 enum InstructionKind {
1957 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1958 kLastInstructionKind
1959 };
1960#undef DECLARE_KIND
1961
1962 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001963 : previous_(nullptr),
1964 next_(nullptr),
1965 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001966 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001967 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001968 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001969 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001970 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001971 locations_(nullptr),
1972 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001973 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001974 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001975 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301976 SetPackedField<InstructionKindField>(kind);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001977 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1978 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001979
Dave Allison20dfc792014-06-16 20:44:29 -07001980 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001981
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001982
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001983 HInstruction* GetNext() const { return next_; }
1984 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985
Calin Juravle77520bc2015-01-12 18:45:46 +00001986 HInstruction* GetNextDisregardingMoves() const;
1987 HInstruction* GetPreviousDisregardingMoves() const;
1988
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001989 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001990 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001991 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001992 bool IsInBlock() const { return block_ != nullptr; }
1993 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001994 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1995 bool IsIrreducibleLoopHeaderPhi() const {
1996 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1997 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001998
Vladimir Marko372f10e2016-05-17 16:30:10 +01001999 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2000
2001 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2002 // One virtual method is enough, just const_cast<> and then re-add the const.
2003 return ArrayRef<const HUserRecord<HInstruction*>>(
2004 const_cast<HInstruction*>(this)->GetInputRecords());
2005 }
2006
Vladimir Markoe9004912016-06-16 16:50:52 +01002007 HInputsRef GetInputs() {
2008 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002009 }
2010
Vladimir Markoe9004912016-06-16 16:50:52 +01002011 HConstInputsRef GetInputs() const {
2012 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002013 }
2014
2015 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002016 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017
Aart Bik2767f4b2016-10-28 15:03:53 -07002018 bool HasInput(HInstruction* input) const {
2019 for (const HInstruction* i : GetInputs()) {
2020 if (i == input) {
2021 return true;
2022 }
2023 }
2024 return false;
2025 }
2026
Vladimir Marko372f10e2016-05-17 16:30:10 +01002027 void SetRawInputAt(size_t index, HInstruction* input) {
2028 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2029 }
2030
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002031 virtual void Accept(HGraphVisitor* visitor) = 0;
2032 virtual const char* DebugName() const = 0;
2033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002035
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002036 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002037
2038 uint32_t GetDexPc() const { return dex_pc_; }
2039
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002040 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002041
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002042 // Can the instruction throw?
2043 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2044 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002045 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002046
2047 // Does the instruction always throw an exception unconditionally?
2048 virtual bool AlwaysThrows() const { return false; }
2049
David Brazdilec16f792015-08-19 15:04:01 +01002050 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002051
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002052 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002053 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002054
Calin Juravle10e244f2015-01-26 18:54:32 +00002055 // Does not apply for all instructions, but having this at top level greatly
2056 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002057 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002058 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002060 return true;
2061 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002062
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002063 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002064 return false;
2065 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002066
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002067 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002068 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002069 }
2070
Calin Juravle2e768302015-07-28 14:41:11 +00002071 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002072
Calin Juravle61d544b2015-02-23 16:46:57 +00002073 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002075 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002076 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002077 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002078
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002079 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002080 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002081 // Note: fixup_end remains valid across push_front().
2082 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2083 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002084 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002085 uses_.push_front(*new_node);
2086 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002087 }
2088
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002089 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002090 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002091 // Note: env_fixup_end remains valid across push_front().
2092 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2093 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002094 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002095 env_uses_.push_front(*new_node);
2096 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002097 }
2098
David Brazdil1abb4192015-02-17 18:33:36 +00002099 void RemoveAsUserOfInput(size_t input) {
2100 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002101 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2102 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2103 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002104 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002105
Vladimir Marko372f10e2016-05-17 16:30:10 +01002106 void RemoveAsUserOfAllInputs() {
2107 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2108 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2109 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2110 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2111 }
2112 }
2113
David Brazdil1abb4192015-02-17 18:33:36 +00002114 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2115 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002116
Vladimir Marko46817b82016-03-29 12:21:58 +01002117 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2118 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2119 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002120 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002121 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002122 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123
Aart Bikcc42be02016-10-20 16:14:16 -07002124 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002125 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002126 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002127 !CanThrow() &&
2128 !IsSuspendCheck() &&
2129 !IsControlFlow() &&
2130 !IsNativeDebugInfo() &&
2131 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002132 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002133 !IsMemoryBarrier() &&
2134 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002135 }
2136
Aart Bikcc42be02016-10-20 16:14:16 -07002137 bool IsDeadAndRemovable() const {
2138 return IsRemovable() && !HasUses();
2139 }
2140
Roland Levillain6c82d402014-10-13 16:10:27 +01002141 // Does this instruction strictly dominate `other_instruction`?
2142 // Returns false if this instruction and `other_instruction` are the same.
2143 // Aborts if this instruction and `other_instruction` are both phis.
2144 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002145
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002146 int GetId() const { return id_; }
2147 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002148
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002149 int GetSsaIndex() const { return ssa_index_; }
2150 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2151 bool HasSsaIndex() const { return ssa_index_ != -1; }
2152
2153 bool HasEnvironment() const { return environment_ != nullptr; }
2154 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002155 // Set the `environment_` field. Raw because this method does not
2156 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002157 void SetRawEnvironment(HEnvironment* environment) {
2158 DCHECK(environment_ == nullptr);
2159 DCHECK_EQ(environment->GetHolder(), this);
2160 environment_ = environment;
2161 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002162
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002163 void InsertRawEnvironment(HEnvironment* environment) {
2164 DCHECK(environment_ != nullptr);
2165 DCHECK_EQ(environment->GetHolder(), this);
2166 DCHECK(environment->GetParent() == nullptr);
2167 environment->parent_ = environment_;
2168 environment_ = environment;
2169 }
2170
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002171 void RemoveEnvironment();
2172
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002173 // Set the environment of this instruction, copying it from `environment`. While
2174 // copying, the uses lists are being updated.
2175 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002176 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002177 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002178 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002179 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002180 if (environment->GetParent() != nullptr) {
2181 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2182 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002183 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002184
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002185 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2186 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002187 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002188 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002189 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002190 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002191 if (environment->GetParent() != nullptr) {
2192 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2193 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002194 }
2195
Nicolas Geoffray39468442014-09-02 15:17:15 +01002196 // Returns the number of entries in the environment. Typically, that is the
2197 // number of dex registers in a method. It could be more in case of inlining.
2198 size_t EnvironmentSize() const;
2199
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002200 LocationSummary* GetLocations() const { return locations_; }
2201 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002202
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002204 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002205 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002206
Alexandre Rames188d4312015-04-09 18:30:21 +01002207 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2208 // uses of this instruction by `other` are *not* updated.
2209 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2210 ReplaceWith(other);
2211 other->ReplaceInput(this, use_index);
2212 }
2213
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002214 // Move `this` instruction before `cursor`
2215 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002216
Vladimir Markofb337ea2015-11-25 15:25:10 +00002217 // Move `this` before its first user and out of any loops. If there is no
2218 // out-of-loop user that dominates all other users, move the instruction
2219 // to the end of the out-of-loop common dominator of the user's blocks.
2220 //
2221 // This can be used only on non-throwing instructions with no side effects that
2222 // have at least one use but no environment uses.
2223 void MoveBeforeFirstUserAndOutOfLoops();
2224
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002225#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002226 bool Is##type() const; \
2227 const H##type* As##type() const; \
2228 H##type* As##type();
2229
2230 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2231#undef INSTRUCTION_TYPE_CHECK
2232
2233#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002234 bool Is##type() const { return (As##type() != nullptr); } \
2235 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002236 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002237 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002238#undef INSTRUCTION_TYPE_CHECK
2239
Artem Serovcced8ba2017-07-19 18:18:09 +01002240 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2241 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2242 // the instruction then the behaviour of this function is undefined.
2243 //
2244 // Note: It is semantically valid to create a clone of the instruction only until
2245 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2246 // copied.
2247 //
2248 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2249 // 'explicit HInstruction(const HInstruction& other)' for details.
2250 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2251 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2252 DebugName() << " " << GetId();
2253 UNREACHABLE();
2254 }
2255
2256 // Return whether instruction can be cloned (copied).
2257 virtual bool IsClonable() const { return false; }
2258
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002259 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002260 // TODO: this method is used by LICM and GVN with possibly different
2261 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002262 virtual bool CanBeMoved() const { return false; }
2263
2264 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002265 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002266 return false;
2267 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002268
2269 // Returns whether any data encoded in the two instructions is equal.
2270 // This method does not look at the inputs. Both instructions must be
2271 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002272 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002273 return false;
2274 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002275
2276 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002277 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002278 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002279 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002280
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002281 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2282 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2283 // the virtual function because the __attribute__((__pure__)) doesn't really
2284 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302285 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002286
2287 virtual size_t ComputeHashCode() const {
2288 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002289 for (const HInstruction* input : GetInputs()) {
2290 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002291 }
2292 return result;
2293 }
2294
2295 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002296 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002297 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002298
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002299 size_t GetLifetimePosition() const { return lifetime_position_; }
2300 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2301 LiveInterval* GetLiveInterval() const { return live_interval_; }
2302 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2303 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2304
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002305 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2306
2307 // Returns whether the code generation of the instruction will require to have access
2308 // to the current method. Such instructions are:
2309 // (1): Instructions that require an environment, as calling the runtime requires
2310 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002311 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2312 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002313 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002314 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002315 }
2316
Vladimir Markodc151b22015-10-15 18:02:30 +01002317 // Returns whether the code generation of the instruction will require to have access
2318 // to the dex cache of the current method's declaring class via the current method.
2319 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002320
Mark Mendellc4701932015-04-10 13:18:51 -04002321 // Does this instruction have any use in an environment before
2322 // control flow hits 'other'?
2323 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2324
2325 // Remove all references to environment uses of this instruction.
2326 // The caller must ensure that this is safe to do.
2327 void RemoveEnvironmentUsers();
2328
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2330 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002331
David Brazdil1abb4192015-02-17 18:33:36 +00002332 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002333 // If set, the machine code for this instruction is assumed to be generated by
2334 // its users. Used by liveness analysis to compute use positions accordingly.
2335 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2336 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302337 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2338 static constexpr size_t kFieldInstructionKindSize =
2339 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
2340 static constexpr size_t kNumberOfGenericPackedBits =
2341 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002342 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2343
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302344 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2345 "Too many generic packed fields");
2346
Vladimir Marko372f10e2016-05-17 16:30:10 +01002347 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2348 return GetInputRecords()[i];
2349 }
2350
2351 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2352 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2353 input_records[index] = input;
2354 }
David Brazdil1abb4192015-02-17 18:33:36 +00002355
Vladimir Markoa1de9182016-02-25 11:37:38 +00002356 uint32_t GetPackedFields() const {
2357 return packed_fields_;
2358 }
2359
2360 template <size_t flag>
2361 bool GetPackedFlag() const {
2362 return (packed_fields_ & (1u << flag)) != 0u;
2363 }
2364
2365 template <size_t flag>
2366 void SetPackedFlag(bool value = true) {
2367 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2368 }
2369
2370 template <typename BitFieldType>
2371 typename BitFieldType::value_type GetPackedField() const {
2372 return BitFieldType::Decode(packed_fields_);
2373 }
2374
2375 template <typename BitFieldType>
2376 void SetPackedField(typename BitFieldType::value_type value) {
2377 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2378 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2379 }
2380
Artem Serovcced8ba2017-07-19 18:18:09 +01002381 // Copy construction for the instruction (used for Clone function).
2382 //
2383 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2384 // prepare_for_register_allocator are not copied (set to default values).
2385 //
2386 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2387 // fine for most of them. However for some of the instructions a custom copy constructor must be
2388 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2389 // for copying them).
2390 explicit HInstruction(const HInstruction& other)
2391 : previous_(nullptr),
2392 next_(nullptr),
2393 block_(nullptr),
2394 dex_pc_(other.dex_pc_),
2395 id_(-1),
2396 ssa_index_(-1),
2397 packed_fields_(other.packed_fields_),
2398 environment_(nullptr),
2399 locations_(nullptr),
2400 live_interval_(nullptr),
2401 lifetime_position_(kNoLifetime),
2402 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302403 reference_type_handle_(other.reference_type_handle_) {
2404 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002405
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002406 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302407 using InstructionKindField =
2408 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2409
Vladimir Marko46817b82016-03-29 12:21:58 +01002410 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2411 auto before_use_node = uses_.before_begin();
2412 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2413 HInstruction* user = use_node->GetUser();
2414 size_t input_index = use_node->GetIndex();
2415 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2416 before_use_node = use_node;
2417 }
2418 }
2419
2420 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2421 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2422 if (next != uses_.end()) {
2423 HInstruction* next_user = next->GetUser();
2424 size_t next_index = next->GetIndex();
2425 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2426 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2427 }
2428 }
2429
2430 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2431 auto before_env_use_node = env_uses_.before_begin();
2432 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2433 HEnvironment* user = env_use_node->GetUser();
2434 size_t input_index = env_use_node->GetIndex();
2435 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2436 before_env_use_node = env_use_node;
2437 }
2438 }
2439
2440 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2441 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2442 if (next != env_uses_.end()) {
2443 HEnvironment* next_user = next->GetUser();
2444 size_t next_index = next->GetIndex();
2445 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2446 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2447 }
2448 }
David Brazdil1abb4192015-02-17 18:33:36 +00002449
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002450 HInstruction* previous_;
2451 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002452 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002453 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002455 // An instruction gets an id when it is added to the graph.
2456 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002457 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002458 int id_;
2459
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002460 // When doing liveness analysis, instructions that have uses get an SSA index.
2461 int ssa_index_;
2462
Vladimir Markoa1de9182016-02-25 11:37:38 +00002463 // Packed fields.
2464 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002465
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002466 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002467 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002468
2469 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002470 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002471
Nicolas Geoffray39468442014-09-02 15:17:15 +01002472 // The environment associated with this instruction. Not null if the instruction
2473 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002474 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002475
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002476 // Set by the code generator.
2477 LocationSummary* locations_;
2478
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002479 // Set by the liveness analysis.
2480 LiveInterval* live_interval_;
2481
2482 // Set by the liveness analysis, this is the position in a linear
2483 // order of blocks where this instruction's live interval start.
2484 size_t lifetime_position_;
2485
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002486 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002487
Vladimir Markoa1de9182016-02-25 11:37:38 +00002488 // The reference handle part of the reference type info.
2489 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002490 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002491 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002492
David Brazdil1abb4192015-02-17 18:33:36 +00002493 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002494 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002495 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002496 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002497 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002499std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002500
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002501// Iterates over the instructions, while preserving the next instruction
2502// in case the current instruction gets removed from the list by the user
2503// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002504class HInstructionIterator : public ValueObject {
2505 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002506 explicit HInstructionIterator(const HInstructionList& instructions)
2507 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002508 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002509 }
2510
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002511 bool Done() const { return instruction_ == nullptr; }
2512 HInstruction* Current() const { return instruction_; }
2513 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002514 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002515 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002516 }
2517
2518 private:
2519 HInstruction* instruction_;
2520 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002521
2522 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002523};
2524
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002525// Iterates over the instructions without saving the next instruction,
2526// therefore handling changes in the graph potentially made by the user
2527// of this iterator.
2528class HInstructionIteratorHandleChanges : public ValueObject {
2529 public:
2530 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2531 : instruction_(instructions.first_instruction_) {
2532 }
2533
2534 bool Done() const { return instruction_ == nullptr; }
2535 HInstruction* Current() const { return instruction_; }
2536 void Advance() {
2537 instruction_ = instruction_->GetNext();
2538 }
2539
2540 private:
2541 HInstruction* instruction_;
2542
2543 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2544};
2545
2546
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002547class HBackwardInstructionIterator : public ValueObject {
2548 public:
2549 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2550 : instruction_(instructions.last_instruction_) {
2551 next_ = Done() ? nullptr : instruction_->GetPrevious();
2552 }
2553
2554 bool Done() const { return instruction_ == nullptr; }
2555 HInstruction* Current() const { return instruction_; }
2556 void Advance() {
2557 instruction_ = next_;
2558 next_ = Done() ? nullptr : instruction_->GetPrevious();
2559 }
2560
2561 private:
2562 HInstruction* instruction_;
2563 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002564
2565 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002566};
2567
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002568class HVariableInputSizeInstruction : public HInstruction {
2569 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002570 using HInstruction::GetInputRecords; // Keep the const version visible.
2571 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2572 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2573 }
2574
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002575 void AddInput(HInstruction* input);
2576 void InsertInputAt(size_t index, HInstruction* input);
2577 void RemoveInputAt(size_t index);
2578
Igor Murashkind01745e2017-04-05 16:40:31 -07002579 // Removes all the inputs.
2580 // Also removes this instructions from each input's use list
2581 // (for non-environment uses only).
2582 void RemoveAllInputs();
2583
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002584 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302585 HVariableInputSizeInstruction(InstructionKind inst_kind,
2586 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002587 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002588 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002589 size_t number_of_inputs,
2590 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302591 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002592 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002593
Artem Serovcced8ba2017-07-19 18:18:09 +01002594 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002595
Artem Serovcced8ba2017-07-19 18:18:09 +01002596 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002597};
2598
Vladimir Markof9f64412015-09-02 14:05:49 +01002599template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002600class HTemplateInstruction: public HInstruction {
2601 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302602 HTemplateInstruction<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2603 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002604 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002605
Vladimir Marko372f10e2016-05-17 16:30:10 +01002606 using HInstruction::GetInputRecords; // Keep the const version visible.
2607 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2608 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002609 }
2610
Artem Serovcced8ba2017-07-19 18:18:09 +01002611 protected:
2612 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2613
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002614 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002615 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002616
2617 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002618};
2619
Vladimir Markof9f64412015-09-02 14:05:49 +01002620// HTemplateInstruction specialization for N=0.
2621template<>
2622class HTemplateInstruction<0>: public HInstruction {
2623 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302624 explicit HTemplateInstruction<0>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2625 : HInstruction(kind, side_effects, dex_pc) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002626
Vladimir Markof9f64412015-09-02 14:05:49 +01002627 virtual ~HTemplateInstruction() {}
2628
Vladimir Marko372f10e2016-05-17 16:30:10 +01002629 using HInstruction::GetInputRecords; // Keep the const version visible.
2630 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2631 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002632 }
2633
Artem Serovcced8ba2017-07-19 18:18:09 +01002634 protected:
2635 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2636
Vladimir Markof9f64412015-09-02 14:05:49 +01002637 private:
2638 friend class SsaBuilder;
2639};
2640
Dave Allison20dfc792014-06-16 20:44:29 -07002641template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002642class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002643 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302644 using HInstruction::InstructionKind;
2645 HExpression<N>(InstructionKind kind,
2646 DataType::Type type,
2647 SideEffects side_effects,
2648 uint32_t dex_pc)
2649 : HTemplateInstruction<N>(kind, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002650 this->template SetPackedField<TypeField>(type);
2651 }
Dave Allison20dfc792014-06-16 20:44:29 -07002652 virtual ~HExpression() {}
2653
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002655 return TypeField::Decode(this->GetPackedFields());
2656 }
Dave Allison20dfc792014-06-16 20:44:29 -07002657
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002658 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002659 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2660 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002662 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2663 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2664 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002666 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002667};
2668
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002669// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2670// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002671class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002672 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002673 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302674 : HTemplateInstruction(kReturnVoid, SideEffects::None(), dex_pc) {
2675 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002676
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002677 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002678
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002679 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002680
Artem Serovcced8ba2017-07-19 18:18:09 +01002681 protected:
2682 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002683};
2684
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002685// Represents dex's RETURN opcodes. A HReturn is a control flow
2686// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002687class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002688 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002689 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302690 : HTemplateInstruction(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002691 SetRawInputAt(0, value);
2692 }
2693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002694 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002695
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002696 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002697
Artem Serovcced8ba2017-07-19 18:18:09 +01002698 protected:
2699 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002700};
2701
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002702class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002703 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002704 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002705 uint32_t reg_number,
2706 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002707 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002708 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002709 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302710 kPhi,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002711 SideEffects::None(),
2712 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002713 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002714 number_of_inputs,
2715 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002716 reg_number_(reg_number) {
2717 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002719 // Phis are constructed live and marked dead if conflicting or unused.
2720 // Individual steps of SsaBuilder should assume that if a phi has been
2721 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2722 SetPackedFlag<kFlagIsLive>(true);
2723 SetPackedFlag<kFlagCanBeNull>(true);
2724 }
2725
Artem Serovcced8ba2017-07-19 18:18:09 +01002726 bool IsClonable() const OVERRIDE { return true; }
2727
David Brazdildee58d62016-04-07 09:54:26 +00002728 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002729 static DataType::Type ToPhiType(DataType::Type type) {
2730 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002731 }
2732
2733 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002735 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2736 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002737 // Make sure that only valid type changes occur. The following are allowed:
2738 // (1) int -> float/ref (primitive type propagation),
2739 // (2) long -> double (primitive type propagation).
2740 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2742 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2743 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002744 SetPackedField<TypeField>(new_type);
2745 }
2746
2747 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2748 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2749
2750 uint32_t GetRegNumber() const { return reg_number_; }
2751
2752 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2753 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2754 bool IsDead() const { return !IsLive(); }
2755 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2756
Vladimir Markoe9004912016-06-16 16:50:52 +01002757 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002758 return other != nullptr
2759 && other->IsPhi()
2760 && other->AsPhi()->GetBlock() == GetBlock()
2761 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2762 }
2763
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002764 bool HasEquivalentPhi() const {
2765 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2766 return true;
2767 }
2768 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2769 return true;
2770 }
2771 return false;
2772 }
2773
David Brazdildee58d62016-04-07 09:54:26 +00002774 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2775 // An equivalent phi is a phi having the same dex register and type.
2776 // It assumes that phis with the same dex register are adjacent.
2777 HPhi* GetNextEquivalentPhiWithSameType() {
2778 HInstruction* next = GetNext();
2779 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2780 if (next->GetType() == GetType()) {
2781 return next->AsPhi();
2782 }
2783 next = next->GetNext();
2784 }
2785 return nullptr;
2786 }
2787
2788 DECLARE_INSTRUCTION(Phi);
2789
Artem Serovcced8ba2017-07-19 18:18:09 +01002790 protected:
2791 DEFAULT_COPY_CONSTRUCTOR(Phi);
2792
David Brazdildee58d62016-04-07 09:54:26 +00002793 private:
2794 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2795 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002796 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002797 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2798 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2799 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2800 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002802
David Brazdildee58d62016-04-07 09:54:26 +00002803 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002804};
2805
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002806// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002807// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002808// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002809class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002810 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302811 explicit HExit(uint32_t dex_pc = kNoDexPc)
2812 : HTemplateInstruction(kExit, SideEffects::None(), dex_pc) {
2813 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002815 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002816
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002817 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002818
Artem Serovcced8ba2017-07-19 18:18:09 +01002819 protected:
2820 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002821};
2822
2823// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002824class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002825 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302826 explicit HGoto(uint32_t dex_pc = kNoDexPc)
2827 : HTemplateInstruction(kGoto, SideEffects::None(), dex_pc) {
2828 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002829
Artem Serovcced8ba2017-07-19 18:18:09 +01002830 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002831 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002832
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002833 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002834 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002835 }
2836
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002837 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002838
Artem Serovcced8ba2017-07-19 18:18:09 +01002839 protected:
2840 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002841};
2842
Roland Levillain9867bc72015-08-05 10:21:34 +01002843class HConstant : public HExpression<0> {
2844 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302845 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2846 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2847 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002848
2849 bool CanBeMoved() const OVERRIDE { return true; }
2850
Roland Levillain1a653882016-03-18 18:05:57 +00002851 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002852 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002853 // Is this constant 0 in the arithmetic sense?
2854 virtual bool IsArithmeticZero() const { return false; }
2855 // Is this constant a 0-bit pattern?
2856 virtual bool IsZeroBitPattern() const { return false; }
2857 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002858 virtual bool IsOne() const { return false; }
2859
David Brazdil77a48ae2015-09-15 12:34:04 +00002860 virtual uint64_t GetValueAsUint64() const = 0;
2861
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002862 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002863
Artem Serovcced8ba2017-07-19 18:18:09 +01002864 protected:
2865 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002866};
2867
Vladimir Markofcb503c2016-05-18 12:48:17 +01002868class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002869 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002870 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002871 return true;
2872 }
2873
David Brazdil77a48ae2015-09-15 12:34:04 +00002874 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2875
Roland Levillain9867bc72015-08-05 10:21:34 +01002876 size_t ComputeHashCode() const OVERRIDE { return 0; }
2877
Roland Levillain1a653882016-03-18 18:05:57 +00002878 // The null constant representation is a 0-bit pattern.
2879 virtual bool IsZeroBitPattern() const { return true; }
2880
Roland Levillain9867bc72015-08-05 10:21:34 +01002881 DECLARE_INSTRUCTION(NullConstant);
2882
Artem Serovcced8ba2017-07-19 18:18:09 +01002883 protected:
2884 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2885
Roland Levillain9867bc72015-08-05 10:21:34 +01002886 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302888 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2889 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002890
2891 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002892};
2893
2894// Constants of the type int. Those can be from Dex instructions, or
2895// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002896class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002897 public:
2898 int32_t GetValue() const { return value_; }
2899
David Brazdil9f389d42015-10-01 14:32:56 +01002900 uint64_t GetValueAsUint64() const OVERRIDE {
2901 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2902 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002903
Vladimir Marko372f10e2016-05-17 16:30:10 +01002904 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002905 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002906 return other->AsIntConstant()->value_ == value_;
2907 }
2908
2909 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2910
2911 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002912 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2913 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2915
Roland Levillain1a653882016-03-18 18:05:57 +00002916 // Integer constants are used to encode Boolean values as well,
2917 // where 1 means true and 0 means false.
2918 bool IsTrue() const { return GetValue() == 1; }
2919 bool IsFalse() const { return GetValue() == 0; }
2920
Roland Levillain9867bc72015-08-05 10:21:34 +01002921 DECLARE_INSTRUCTION(IntConstant);
2922
Artem Serovcced8ba2017-07-19 18:18:09 +01002923 protected:
2924 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2925
Roland Levillain9867bc72015-08-05 10:21:34 +01002926 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002927 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302928 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2929 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002930 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302931 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2932 value_(value ? 1 : 0) {
2933 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002934
2935 const int32_t value_;
2936
2937 friend class HGraph;
2938 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2939 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002940};
2941
Vladimir Markofcb503c2016-05-18 12:48:17 +01002942class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002943 public:
2944 int64_t GetValue() const { return value_; }
2945
David Brazdil77a48ae2015-09-15 12:34:04 +00002946 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2947
Vladimir Marko372f10e2016-05-17 16:30:10 +01002948 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002949 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002950 return other->AsLongConstant()->value_ == value_;
2951 }
2952
2953 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2954
2955 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002956 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2957 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002958 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2959
2960 DECLARE_INSTRUCTION(LongConstant);
2961
Artem Serovcced8ba2017-07-19 18:18:09 +01002962 protected:
2963 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2964
Roland Levillain9867bc72015-08-05 10:21:34 +01002965 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002966 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302967 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2968 value_(value) {
2969 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002970
2971 const int64_t value_;
2972
2973 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002974};
Dave Allison20dfc792014-06-16 20:44:29 -07002975
Vladimir Markofcb503c2016-05-18 12:48:17 +01002976class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002977 public:
2978 float GetValue() const { return value_; }
2979
2980 uint64_t GetValueAsUint64() const OVERRIDE {
2981 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2982 }
2983
Vladimir Marko372f10e2016-05-17 16:30:10 +01002984 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002985 DCHECK(other->IsFloatConstant()) << other->DebugName();
2986 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2987 }
2988
2989 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2990
2991 bool IsMinusOne() const OVERRIDE {
2992 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2993 }
Roland Levillain1a653882016-03-18 18:05:57 +00002994 bool IsArithmeticZero() const OVERRIDE {
2995 return std::fpclassify(value_) == FP_ZERO;
2996 }
2997 bool IsArithmeticPositiveZero() const {
2998 return IsArithmeticZero() && !std::signbit(value_);
2999 }
3000 bool IsArithmeticNegativeZero() const {
3001 return IsArithmeticZero() && std::signbit(value_);
3002 }
3003 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003004 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003005 }
3006 bool IsOne() const OVERRIDE {
3007 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3008 }
3009 bool IsNaN() const {
3010 return std::isnan(value_);
3011 }
3012
3013 DECLARE_INSTRUCTION(FloatConstant);
3014
Artem Serovcced8ba2017-07-19 18:18:09 +01003015 protected:
3016 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3017
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018 private:
3019 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303020 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3021 value_(value) {
3022 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303024 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3025 value_(bit_cast<float, int32_t>(value)) {
3026 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027
3028 const float value_;
3029
3030 // Only the SsaBuilder and HGraph can create floating-point constants.
3031 friend class SsaBuilder;
3032 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003033};
3034
Vladimir Markofcb503c2016-05-18 12:48:17 +01003035class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 public:
3037 double GetValue() const { return value_; }
3038
3039 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3040
Vladimir Marko372f10e2016-05-17 16:30:10 +01003041 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003042 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3043 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3044 }
3045
3046 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3047
3048 bool IsMinusOne() const OVERRIDE {
3049 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3050 }
Roland Levillain1a653882016-03-18 18:05:57 +00003051 bool IsArithmeticZero() const OVERRIDE {
3052 return std::fpclassify(value_) == FP_ZERO;
3053 }
3054 bool IsArithmeticPositiveZero() const {
3055 return IsArithmeticZero() && !std::signbit(value_);
3056 }
3057 bool IsArithmeticNegativeZero() const {
3058 return IsArithmeticZero() && std::signbit(value_);
3059 }
3060 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003061 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062 }
3063 bool IsOne() const OVERRIDE {
3064 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3065 }
3066 bool IsNaN() const {
3067 return std::isnan(value_);
3068 }
3069
3070 DECLARE_INSTRUCTION(DoubleConstant);
3071
Artem Serovcced8ba2017-07-19 18:18:09 +01003072 protected:
3073 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3074
Roland Levillain31dd3d62016-02-16 12:21:02 +00003075 private:
3076 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303077 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3078 value_(value) {
3079 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003080 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303081 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3082 value_(bit_cast<double, int64_t>(value)) {
3083 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003084
3085 const double value_;
3086
3087 // Only the SsaBuilder and HGraph can create floating-point constants.
3088 friend class SsaBuilder;
3089 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003090};
3091
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003092// Conditional branch. A block ending with an HIf instruction must have
3093// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003094class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003095 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003096 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303097 : HTemplateInstruction(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003098 SetRawInputAt(0, input);
3099 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003100
Artem Serovcced8ba2017-07-19 18:18:09 +01003101 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003102 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003103
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003104 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003105 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003106 }
3107
3108 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003109 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003110 }
3111
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003112 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003113
Artem Serovcced8ba2017-07-19 18:18:09 +01003114 protected:
3115 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003116};
3117
David Brazdilfc6a86a2015-06-26 10:33:45 +00003118
3119// Abstract instruction which marks the beginning and/or end of a try block and
3120// links it to the respective exception handlers. Behaves the same as a Goto in
3121// non-exceptional control flow.
3122// Normal-flow successor is stored at index zero, exception handlers under
3123// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003124class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003125 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003126 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003127 kEntry,
3128 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003129 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003130 };
3131
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003132 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303133 : HTemplateInstruction(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003134 SetPackedField<BoundaryKindField>(kind);
3135 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003136
3137 bool IsControlFlow() const OVERRIDE { return true; }
3138
3139 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003140 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003141
David Brazdild26a4112015-11-10 11:07:31 +00003142 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3143 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3144 }
3145
David Brazdilfc6a86a2015-06-26 10:33:45 +00003146 // Returns whether `handler` is among its exception handlers (non-zero index
3147 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003148 bool HasExceptionHandler(const HBasicBlock& handler) const {
3149 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003150 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003151 }
3152
3153 // If not present already, adds `handler` to its block's list of exception
3154 // handlers.
3155 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003156 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003157 GetBlock()->AddSuccessor(handler);
3158 }
3159 }
3160
Vladimir Markoa1de9182016-02-25 11:37:38 +00003161 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3162 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003163
David Brazdilffee3d32015-07-06 11:48:53 +01003164 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3165
David Brazdilfc6a86a2015-06-26 10:33:45 +00003166 DECLARE_INSTRUCTION(TryBoundary);
3167
Artem Serovcced8ba2017-07-19 18:18:09 +01003168 protected:
3169 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3170
David Brazdilfc6a86a2015-06-26 10:33:45 +00003171 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003172 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3173 static constexpr size_t kFieldBoundaryKindSize =
3174 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3175 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3176 kFieldBoundaryKind + kFieldBoundaryKindSize;
3177 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3178 "Too many packed fields.");
3179 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003180};
3181
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003182// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003183class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003184 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003185 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3186 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003187 HDeoptimize(ArenaAllocator* allocator,
3188 HInstruction* cond,
3189 DeoptimizationKind kind,
3190 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003191 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303192 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003193 SideEffects::All(),
3194 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003195 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003196 /* number_of_inputs */ 1,
3197 kArenaAllocMisc) {
3198 SetPackedFlag<kFieldCanBeMoved>(false);
3199 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003200 SetRawInputAt(0, cond);
3201 }
3202
Artem Serovcced8ba2017-07-19 18:18:09 +01003203 bool IsClonable() const OVERRIDE { return true; }
3204
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003205 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3206 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3207 // instead of `guard`.
3208 // We set CanTriggerGC to prevent any intermediate address to be live
3209 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003210 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003211 HInstruction* cond,
3212 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003213 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003214 uint32_t dex_pc)
3215 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303216 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003217 SideEffects::CanTriggerGC(),
3218 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003219 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003220 /* number_of_inputs */ 2,
3221 kArenaAllocMisc) {
3222 SetPackedFlag<kFieldCanBeMoved>(true);
3223 SetPackedField<DeoptimizeKindField>(kind);
3224 SetRawInputAt(0, cond);
3225 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003226 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003227
3228 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3229
3230 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3231 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3232 }
3233
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003234 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003235
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003236 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003237
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003238 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 DataType::Type GetType() const OVERRIDE {
3241 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003242 }
3243
3244 bool GuardsAnInput() const {
3245 return InputCount() == 2;
3246 }
3247
3248 HInstruction* GuardedInput() const {
3249 DCHECK(GuardsAnInput());
3250 return InputAt(1);
3251 }
3252
3253 void RemoveGuard() {
3254 RemoveInputAt(1);
3255 }
3256
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003257 DECLARE_INSTRUCTION(Deoptimize);
3258
Artem Serovcced8ba2017-07-19 18:18:09 +01003259 protected:
3260 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3261
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003262 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003263 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3264 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3265 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003266 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003267 static constexpr size_t kNumberOfDeoptimizePackedBits =
3268 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3269 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3270 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003271 using DeoptimizeKindField =
3272 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003273};
3274
Mingyao Yang063fc772016-08-02 11:02:54 -07003275// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3276// The compiled code checks this flag value in a guard before devirtualized call and
3277// if it's true, starts to do deoptimization.
3278// It has a 4-byte slot on stack.
3279// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003280class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003281 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003282 // CHA guards are only optimized in a separate pass and it has no side effects
3283 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003284 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303285 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
3286 SideEffects::None(),
3287 dex_pc,
3288 allocator,
3289 0,
3290 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003291 }
3292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003293 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003294
3295 // We do all CHA guard elimination/motion in a single pass, after which there is no
3296 // further guard elimination/motion since a guard might have been used for justification
3297 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3298 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003299 bool CanBeMoved() const OVERRIDE { return false; }
3300
3301 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3302
Artem Serovcced8ba2017-07-19 18:18:09 +01003303 protected:
3304 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003305};
3306
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003307// Represents the ArtMethod that was passed as a first argument to
3308// the method. It is used by instructions that depend on it, like
3309// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003310class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003311 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003312 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303313 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3314 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003315
3316 DECLARE_INSTRUCTION(CurrentMethod);
3317
Artem Serovcced8ba2017-07-19 18:18:09 +01003318 protected:
3319 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003320};
3321
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003322// Fetches an ArtMethod from the virtual table or the interface method table
3323// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003324class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003325 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003326 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003327 kVTable,
3328 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003329 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003330 };
3331 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003332 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003333 TableKind kind,
3334 size_t index,
3335 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303336 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003337 index_(index) {
3338 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003339 SetRawInputAt(0, cls);
3340 }
3341
Artem Serovcced8ba2017-07-19 18:18:09 +01003342 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003343 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003344 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003345 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003346 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003347 }
3348
Vladimir Markoa1de9182016-02-25 11:37:38 +00003349 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003350 size_t GetIndex() const { return index_; }
3351
3352 DECLARE_INSTRUCTION(ClassTableGet);
3353
Artem Serovcced8ba2017-07-19 18:18:09 +01003354 protected:
3355 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3356
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003357 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003358 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3359 static constexpr size_t kFieldTableKindSize =
3360 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3361 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3362 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3363 "Too many packed fields.");
3364 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3365
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003366 // The index of the ArtMethod in the table.
3367 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003368};
3369
Mark Mendellfe57faa2015-09-18 09:26:15 -04003370// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3371// have one successor for each entry in the switch table, and the final successor
3372// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003373class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003374 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003375 HPackedSwitch(int32_t start_value,
3376 uint32_t num_entries,
3377 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003378 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303379 : HTemplateInstruction(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003380 start_value_(start_value),
3381 num_entries_(num_entries) {
3382 SetRawInputAt(0, input);
3383 }
3384
Artem Serovcced8ba2017-07-19 18:18:09 +01003385 bool IsClonable() const OVERRIDE { return true; }
3386
Mark Mendellfe57faa2015-09-18 09:26:15 -04003387 bool IsControlFlow() const OVERRIDE { return true; }
3388
3389 int32_t GetStartValue() const { return start_value_; }
3390
Vladimir Marko211c2112015-09-24 16:52:33 +01003391 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003392
3393 HBasicBlock* GetDefaultBlock() const {
3394 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003395 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003396 }
3397 DECLARE_INSTRUCTION(PackedSwitch);
3398
Artem Serovcced8ba2017-07-19 18:18:09 +01003399 protected:
3400 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3401
Mark Mendellfe57faa2015-09-18 09:26:15 -04003402 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003403 const int32_t start_value_;
3404 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003405};
3406
Roland Levillain88cb1752014-10-20 16:36:47 +01003407class HUnaryOperation : public HExpression<1> {
3408 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303409 HUnaryOperation(InstructionKind kind,
3410 DataType::Type result_type,
3411 HInstruction* input,
3412 uint32_t dex_pc = kNoDexPc)
3413 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003414 SetRawInputAt(0, input);
3415 }
3416
Artem Serovcced8ba2017-07-19 18:18:09 +01003417 // All of the UnaryOperation instructions are clonable.
3418 bool IsClonable() const OVERRIDE { return true; }
3419
Roland Levillain88cb1752014-10-20 16:36:47 +01003420 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003421 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003422
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003423 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003424 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003425 return true;
3426 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003427
Roland Levillain31dd3d62016-02-16 12:21:02 +00003428 // Try to statically evaluate `this` and return a HConstant
3429 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003430 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003431 HConstant* TryStaticEvaluation() const;
3432
3433 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003434 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3435 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003436 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3437 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003438
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003439 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003440
Artem Serovcced8ba2017-07-19 18:18:09 +01003441 protected:
3442 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003443};
3444
Dave Allison20dfc792014-06-16 20:44:29 -07003445class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003446 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303447 HBinaryOperation(InstructionKind kind,
3448 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003449 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003450 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003451 SideEffects side_effects = SideEffects::None(),
3452 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303453 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003454 SetRawInputAt(0, left);
3455 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003456 }
3457
Artem Serovcced8ba2017-07-19 18:18:09 +01003458 // All of the BinaryOperation instructions are clonable.
3459 bool IsClonable() const OVERRIDE { return true; }
3460
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003461 HInstruction* GetLeft() const { return InputAt(0); }
3462 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003463 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003464
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003465 virtual bool IsCommutative() const { return false; }
3466
3467 // Put constant on the right.
3468 // Returns whether order is changed.
3469 bool OrderInputsWithConstantOnTheRight() {
3470 HInstruction* left = InputAt(0);
3471 HInstruction* right = InputAt(1);
3472 if (left->IsConstant() && !right->IsConstant()) {
3473 ReplaceInput(right, 0);
3474 ReplaceInput(left, 1);
3475 return true;
3476 }
3477 return false;
3478 }
3479
3480 // Order inputs by instruction id, but favor constant on the right side.
3481 // This helps GVN for commutative ops.
3482 void OrderInputs() {
3483 DCHECK(IsCommutative());
3484 HInstruction* left = InputAt(0);
3485 HInstruction* right = InputAt(1);
3486 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3487 return;
3488 }
3489 if (OrderInputsWithConstantOnTheRight()) {
3490 return;
3491 }
3492 // Order according to instruction id.
3493 if (left->GetId() > right->GetId()) {
3494 ReplaceInput(right, 0);
3495 ReplaceInput(left, 1);
3496 }
3497 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003498
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003499 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003500 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003501 return true;
3502 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003503
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 // Try to statically evaluate `this` and return a HConstant
3505 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003506 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003507 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003508
3509 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3511 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003512 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3513 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003514 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003515 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3516 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003517 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3518 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003519 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3520 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003521 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003522 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3523 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003524
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003525 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003526 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003527 HConstant* GetConstantRight() const;
3528
3529 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003530 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003531 HInstruction* GetLeastConstantLeft() const;
3532
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003533 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003534
Artem Serovcced8ba2017-07-19 18:18:09 +01003535 protected:
3536 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003537};
3538
Mark Mendellc4701932015-04-10 13:18:51 -04003539// The comparison bias applies for floating point operations and indicates how NaN
3540// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003541enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003542 kNoBias, // bias is not applicable (i.e. for long operation)
3543 kGtBias, // return 1 for NaN comparisons
3544 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003545 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003546};
3547
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3549
Dave Allison20dfc792014-06-16 20:44:29 -07003550class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003551 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303552 HCondition(InstructionKind kind,
3553 HInstruction* first,
3554 HInstruction* second,
3555 uint32_t dex_pc = kNoDexPc)
3556 : HBinaryOperation(kind,
3557 DataType::Type::kBool,
3558 first,
3559 second,
3560 SideEffects::None(),
3561 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003562 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3563 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003564
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003565 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003566 // `instruction`, and disregard moves in between.
3567 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003568
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003569 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003570
3571 virtual IfCondition GetCondition() const = 0;
3572
Mark Mendellc4701932015-04-10 13:18:51 -04003573 virtual IfCondition GetOppositeCondition() const = 0;
3574
Vladimir Markoa1de9182016-02-25 11:37:38 +00003575 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003576 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3577
Vladimir Markoa1de9182016-02-25 11:37:38 +00003578 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3579 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003580
Vladimir Marko372f10e2016-05-17 16:30:10 +01003581 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003582 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003583 }
3584
Roland Levillain4fa13f62015-07-06 18:11:54 +01003585 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003586 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003587 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003588 if (if_cond == kCondNE) {
3589 return true;
3590 } else if (if_cond == kCondEQ) {
3591 return false;
3592 }
3593 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003594 }
3595
3596 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003597 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003598 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003599 if (if_cond == kCondEQ) {
3600 return true;
3601 } else if (if_cond == kCondNE) {
3602 return false;
3603 }
3604 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003605 }
3606
Roland Levillain31dd3d62016-02-16 12:21:02 +00003607 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003608 // Needed if we merge a HCompare into a HCondition.
3609 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3610 static constexpr size_t kFieldComparisonBiasSize =
3611 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3612 static constexpr size_t kNumberOfConditionPackedBits =
3613 kFieldComparisonBias + kFieldComparisonBiasSize;
3614 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3615 using ComparisonBiasField =
3616 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3617
Roland Levillain31dd3d62016-02-16 12:21:02 +00003618 template <typename T>
3619 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3620
3621 template <typename T>
3622 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003623 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3625 // Handle the bias.
3626 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3627 }
3628
3629 // Return an integer constant containing the result of a condition evaluated at compile time.
3630 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3631 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3632 }
3633
Artem Serovcced8ba2017-07-19 18:18:09 +01003634 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003635};
3636
3637// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003638class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003639 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003640 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303641 : HCondition(kEqual, first, second, dex_pc) {
3642 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003643
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003644 bool IsCommutative() const OVERRIDE { return true; }
3645
Vladimir Marko9e23df52015-11-10 17:14:35 +00003646 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3647 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003648 return MakeConstantCondition(true, GetDexPc());
3649 }
3650 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3651 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3652 }
3653 // In the following Evaluate methods, a HCompare instruction has
3654 // been merged into this HEqual instruction; evaluate it as
3655 // `Compare(x, y) == 0`.
3656 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3657 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3658 GetDexPc());
3659 }
3660 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3661 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3662 }
3663 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3664 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003665 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003666
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003667 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003668
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003669 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003670 return kCondEQ;
3671 }
3672
Mark Mendellc4701932015-04-10 13:18:51 -04003673 IfCondition GetOppositeCondition() const OVERRIDE {
3674 return kCondNE;
3675 }
3676
Artem Serovcced8ba2017-07-19 18:18:09 +01003677 protected:
3678 DEFAULT_COPY_CONSTRUCTOR(Equal);
3679
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003680 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003681 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003682};
3683
Vladimir Markofcb503c2016-05-18 12:48:17 +01003684class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003685 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303686 HNotEqual(HInstruction* first, HInstruction* second,
3687 uint32_t dex_pc = kNoDexPc)
3688 : HCondition(kNotEqual, first, second, dex_pc) {
3689 }
Dave Allison20dfc792014-06-16 20:44:29 -07003690
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003691 bool IsCommutative() const OVERRIDE { return true; }
3692
Vladimir Marko9e23df52015-11-10 17:14:35 +00003693 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3694 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003695 return MakeConstantCondition(false, GetDexPc());
3696 }
3697 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3698 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3699 }
3700 // In the following Evaluate methods, a HCompare instruction has
3701 // been merged into this HNotEqual instruction; evaluate it as
3702 // `Compare(x, y) != 0`.
3703 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3704 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3705 }
3706 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3707 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3708 }
3709 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3710 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003711 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003712
Dave Allison20dfc792014-06-16 20:44:29 -07003713 DECLARE_INSTRUCTION(NotEqual);
3714
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003715 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003716 return kCondNE;
3717 }
3718
Mark Mendellc4701932015-04-10 13:18:51 -04003719 IfCondition GetOppositeCondition() const OVERRIDE {
3720 return kCondEQ;
3721 }
3722
Artem Serovcced8ba2017-07-19 18:18:09 +01003723 protected:
3724 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3725
Dave Allison20dfc792014-06-16 20:44:29 -07003726 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003727 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003728};
3729
Vladimir Markofcb503c2016-05-18 12:48:17 +01003730class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003731 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303732 HLessThan(HInstruction* first, HInstruction* second,
3733 uint32_t dex_pc = kNoDexPc)
3734 : HCondition(kLessThan, first, second, dex_pc) {
3735 }
Dave Allison20dfc792014-06-16 20:44:29 -07003736
Roland Levillain9867bc72015-08-05 10:21:34 +01003737 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003738 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003739 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 // In the following Evaluate methods, a HCompare instruction has
3741 // been merged into this HLessThan instruction; evaluate it as
3742 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003743 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003744 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3745 }
3746 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3747 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3748 }
3749 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3750 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003751 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003752
Dave Allison20dfc792014-06-16 20:44:29 -07003753 DECLARE_INSTRUCTION(LessThan);
3754
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003755 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003756 return kCondLT;
3757 }
3758
Mark Mendellc4701932015-04-10 13:18:51 -04003759 IfCondition GetOppositeCondition() const OVERRIDE {
3760 return kCondGE;
3761 }
3762
Artem Serovcced8ba2017-07-19 18:18:09 +01003763 protected:
3764 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3765
Dave Allison20dfc792014-06-16 20:44:29 -07003766 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003767 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003768};
3769
Vladimir Markofcb503c2016-05-18 12:48:17 +01003770class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003771 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303772 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3773 uint32_t dex_pc = kNoDexPc)
3774 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3775 }
Dave Allison20dfc792014-06-16 20:44:29 -07003776
Roland Levillain9867bc72015-08-05 10:21:34 +01003777 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003778 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003779 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003780 // In the following Evaluate methods, a HCompare instruction has
3781 // been merged into this HLessThanOrEqual instruction; evaluate it as
3782 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003783 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003784 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3785 }
3786 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3787 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3788 }
3789 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3790 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003791 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003792
Dave Allison20dfc792014-06-16 20:44:29 -07003793 DECLARE_INSTRUCTION(LessThanOrEqual);
3794
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003795 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003796 return kCondLE;
3797 }
3798
Mark Mendellc4701932015-04-10 13:18:51 -04003799 IfCondition GetOppositeCondition() const OVERRIDE {
3800 return kCondGT;
3801 }
3802
Artem Serovcced8ba2017-07-19 18:18:09 +01003803 protected:
3804 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3805
Dave Allison20dfc792014-06-16 20:44:29 -07003806 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003807 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003808};
3809
Vladimir Markofcb503c2016-05-18 12:48:17 +01003810class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003812 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303813 : HCondition(kGreaterThan, first, second, dex_pc) {
3814 }
Dave Allison20dfc792014-06-16 20:44:29 -07003815
Roland Levillain9867bc72015-08-05 10:21:34 +01003816 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003817 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003818 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003819 // In the following Evaluate methods, a HCompare instruction has
3820 // been merged into this HGreaterThan instruction; evaluate it as
3821 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003822 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003823 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3824 }
3825 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3826 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3827 }
3828 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3829 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003830 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003831
Dave Allison20dfc792014-06-16 20:44:29 -07003832 DECLARE_INSTRUCTION(GreaterThan);
3833
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003834 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003835 return kCondGT;
3836 }
3837
Mark Mendellc4701932015-04-10 13:18:51 -04003838 IfCondition GetOppositeCondition() const OVERRIDE {
3839 return kCondLE;
3840 }
3841
Artem Serovcced8ba2017-07-19 18:18:09 +01003842 protected:
3843 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3844
Dave Allison20dfc792014-06-16 20:44:29 -07003845 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003846 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003847};
3848
Vladimir Markofcb503c2016-05-18 12:48:17 +01003849class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003850 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003851 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303852 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3853 }
Dave Allison20dfc792014-06-16 20:44:29 -07003854
Roland Levillain9867bc72015-08-05 10:21:34 +01003855 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003856 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003857 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003858 // In the following Evaluate methods, a HCompare instruction has
3859 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3860 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003861 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003862 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3863 }
3864 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3865 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3866 }
3867 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3868 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003869 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003870
Dave Allison20dfc792014-06-16 20:44:29 -07003871 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003873 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003874 return kCondGE;
3875 }
3876
Mark Mendellc4701932015-04-10 13:18:51 -04003877 IfCondition GetOppositeCondition() const OVERRIDE {
3878 return kCondLT;
3879 }
3880
Artem Serovcced8ba2017-07-19 18:18:09 +01003881 protected:
3882 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3883
Dave Allison20dfc792014-06-16 20:44:29 -07003884 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003885 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003886};
3887
Vladimir Markofcb503c2016-05-18 12:48:17 +01003888class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003889 public:
3890 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303891 : HCondition(kBelow, first, second, dex_pc) {
3892 }
Aart Bike9f37602015-10-09 11:15:55 -07003893
3894 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003895 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003896 }
3897 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003898 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3899 }
3900 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3901 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3902 LOG(FATAL) << DebugName() << " is not defined for float values";
3903 UNREACHABLE();
3904 }
3905 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3906 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3907 LOG(FATAL) << DebugName() << " is not defined for double values";
3908 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003909 }
3910
3911 DECLARE_INSTRUCTION(Below);
3912
3913 IfCondition GetCondition() const OVERRIDE {
3914 return kCondB;
3915 }
3916
3917 IfCondition GetOppositeCondition() const OVERRIDE {
3918 return kCondAE;
3919 }
3920
Artem Serovcced8ba2017-07-19 18:18:09 +01003921 protected:
3922 DEFAULT_COPY_CONSTRUCTOR(Below);
3923
Aart Bike9f37602015-10-09 11:15:55 -07003924 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003925 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003926 return MakeUnsigned(x) < MakeUnsigned(y);
3927 }
Aart Bike9f37602015-10-09 11:15:55 -07003928};
3929
Vladimir Markofcb503c2016-05-18 12:48:17 +01003930class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003931 public:
3932 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303933 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3934 }
Aart Bike9f37602015-10-09 11:15:55 -07003935
3936 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003937 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003938 }
3939 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003940 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3941 }
3942 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3943 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3944 LOG(FATAL) << DebugName() << " is not defined for float values";
3945 UNREACHABLE();
3946 }
3947 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3948 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3949 LOG(FATAL) << DebugName() << " is not defined for double values";
3950 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003951 }
3952
3953 DECLARE_INSTRUCTION(BelowOrEqual);
3954
3955 IfCondition GetCondition() const OVERRIDE {
3956 return kCondBE;
3957 }
3958
3959 IfCondition GetOppositeCondition() const OVERRIDE {
3960 return kCondA;
3961 }
3962
Artem Serovcced8ba2017-07-19 18:18:09 +01003963 protected:
3964 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3965
Aart Bike9f37602015-10-09 11:15:55 -07003966 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003967 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003968 return MakeUnsigned(x) <= MakeUnsigned(y);
3969 }
Aart Bike9f37602015-10-09 11:15:55 -07003970};
3971
Vladimir Markofcb503c2016-05-18 12:48:17 +01003972class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003973 public:
3974 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303975 : HCondition(kAbove, first, second, dex_pc) {
3976 }
Aart Bike9f37602015-10-09 11:15:55 -07003977
3978 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003979 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003980 }
3981 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003982 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3983 }
3984 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3985 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3986 LOG(FATAL) << DebugName() << " is not defined for float values";
3987 UNREACHABLE();
3988 }
3989 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3990 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3991 LOG(FATAL) << DebugName() << " is not defined for double values";
3992 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003993 }
3994
3995 DECLARE_INSTRUCTION(Above);
3996
3997 IfCondition GetCondition() const OVERRIDE {
3998 return kCondA;
3999 }
4000
4001 IfCondition GetOppositeCondition() const OVERRIDE {
4002 return kCondBE;
4003 }
4004
Artem Serovcced8ba2017-07-19 18:18:09 +01004005 protected:
4006 DEFAULT_COPY_CONSTRUCTOR(Above);
4007
Aart Bike9f37602015-10-09 11:15:55 -07004008 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004009 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004010 return MakeUnsigned(x) > MakeUnsigned(y);
4011 }
Aart Bike9f37602015-10-09 11:15:55 -07004012};
4013
Vladimir Markofcb503c2016-05-18 12:48:17 +01004014class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004015 public:
4016 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304017 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4018 }
Aart Bike9f37602015-10-09 11:15:55 -07004019
4020 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004021 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004022 }
4023 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004024 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4025 }
4026 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4027 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4028 LOG(FATAL) << DebugName() << " is not defined for float values";
4029 UNREACHABLE();
4030 }
4031 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4032 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4033 LOG(FATAL) << DebugName() << " is not defined for double values";
4034 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004035 }
4036
4037 DECLARE_INSTRUCTION(AboveOrEqual);
4038
4039 IfCondition GetCondition() const OVERRIDE {
4040 return kCondAE;
4041 }
4042
4043 IfCondition GetOppositeCondition() const OVERRIDE {
4044 return kCondB;
4045 }
4046
Artem Serovcced8ba2017-07-19 18:18:09 +01004047 protected:
4048 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4049
Aart Bike9f37602015-10-09 11:15:55 -07004050 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004051 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004052 return MakeUnsigned(x) >= MakeUnsigned(y);
4053 }
Aart Bike9f37602015-10-09 11:15:55 -07004054};
Dave Allison20dfc792014-06-16 20:44:29 -07004055
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004056// Instruction to check how two inputs compare to each other.
4057// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004058class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004059 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004060 // Note that `comparison_type` is the type of comparison performed
4061 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004062 // HCompare instruction (which is always DataType::Type::kInt).
4063 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004064 HInstruction* first,
4065 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004066 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004067 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304068 : HBinaryOperation(kCompare,
4069 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 first,
4071 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004072 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004073 dex_pc) {
4074 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4076 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004077 }
4078
Roland Levillain9867bc72015-08-05 10:21:34 +01004079 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004080 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4081
4082 template <typename T>
4083 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004084 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004085 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4086 // Handle the bias.
4087 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4088 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004089
Roland Levillain9867bc72015-08-05 10:21:34 +01004090 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004091 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4092 // reach this code path when processing a freshly built HIR
4093 // graph. However HCompare integer instructions can be synthesized
4094 // by the instruction simplifier to implement IntegerCompare and
4095 // IntegerSignum intrinsics, so we have to handle this case.
4096 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004097 }
4098 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004099 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4100 }
4101 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4102 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4103 }
4104 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4105 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004106 }
4107
Vladimir Marko372f10e2016-05-17 16:30:10 +01004108 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004109 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004110 }
4111
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004113
Roland Levillain31dd3d62016-02-16 12:21:02 +00004114 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004115 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004116 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004117 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004118 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004119 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004122 // Comparisons do not require a runtime call in any back end.
4123 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004124 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004125
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004126 DECLARE_INSTRUCTION(Compare);
4127
Roland Levillain31dd3d62016-02-16 12:21:02 +00004128 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004129 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4130 static constexpr size_t kFieldComparisonBiasSize =
4131 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4132 static constexpr size_t kNumberOfComparePackedBits =
4133 kFieldComparisonBias + kFieldComparisonBiasSize;
4134 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4135 using ComparisonBiasField =
4136 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4137
Roland Levillain31dd3d62016-02-16 12:21:02 +00004138 // Return an integer constant containing the result of a comparison evaluated at compile time.
4139 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4140 DCHECK(value == -1 || value == 0 || value == 1) << value;
4141 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4142 }
4143
Artem Serovcced8ba2017-07-19 18:18:09 +01004144 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004145};
4146
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004147class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004148 public:
4149 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004150 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004151 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004152 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004153 bool finalizable,
4154 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304155 : HExpression(kNewInstance,
4156 DataType::Type::kReference,
4157 SideEffects::CanTriggerGC(),
4158 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004159 type_index_(type_index),
4160 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004161 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004162 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004163 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004164 }
4165
Artem Serovcced8ba2017-07-19 18:18:09 +01004166 bool IsClonable() const OVERRIDE { return true; }
4167
Andreas Gampea5b09a62016-11-17 15:21:22 -08004168 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004169 const DexFile& GetDexFile() const { return dex_file_; }
4170
4171 // Calls runtime so needs an environment.
4172 bool NeedsEnvironment() const OVERRIDE { return true; }
4173
Mingyao Yang062157f2016-03-02 10:15:36 -08004174 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4175 bool CanThrow() const OVERRIDE { return true; }
4176
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004177 bool NeedsChecks() const {
4178 return entrypoint_ == kQuickAllocObjectWithChecks;
4179 }
David Brazdil6de19382016-01-08 17:37:10 +00004180
Vladimir Markoa1de9182016-02-25 11:37:38 +00004181 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004182
4183 bool CanBeNull() const OVERRIDE { return false; }
4184
4185 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4186
4187 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4188 entrypoint_ = entrypoint;
4189 }
4190
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004191 HLoadClass* GetLoadClass() const {
4192 HInstruction* input = InputAt(0);
4193 if (input->IsClinitCheck()) {
4194 input = input->InputAt(0);
4195 }
4196 DCHECK(input->IsLoadClass());
4197 return input->AsLoadClass();
4198 }
4199
David Brazdil6de19382016-01-08 17:37:10 +00004200 bool IsStringAlloc() const;
4201
4202 DECLARE_INSTRUCTION(NewInstance);
4203
Artem Serovcced8ba2017-07-19 18:18:09 +01004204 protected:
4205 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4206
David Brazdil6de19382016-01-08 17:37:10 +00004207 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004208 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004209 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4210 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4211 "Too many packed fields.");
4212
Andreas Gampea5b09a62016-11-17 15:21:22 -08004213 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004214 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004215 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004216};
4217
Agi Csaki05f20562015-08-19 14:58:14 -07004218enum IntrinsicNeedsEnvironmentOrCache {
4219 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4220 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004221};
4222
Aart Bik5d75afe2015-12-14 11:57:01 -08004223enum IntrinsicSideEffects {
4224 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4225 kReadSideEffects, // Intrinsic may read heap memory.
4226 kWriteSideEffects, // Intrinsic may write heap memory.
4227 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4228};
4229
4230enum IntrinsicExceptions {
4231 kNoThrow, // Intrinsic does not throw any exceptions.
4232 kCanThrow // Intrinsic may throw exceptions.
4233};
4234
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004235class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004236 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004237 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004238
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004239 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004240 SetRawInputAt(index, argument);
4241 }
4242
Roland Levillain3e3d7332015-04-28 11:00:54 +01004243 // Return the number of arguments. This number can be lower than
4244 // the number of inputs returned by InputCount(), as some invoke
4245 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4246 // inputs at the end of their list of inputs.
4247 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4248
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004249 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004250
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004251 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4252
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004253 InvokeType GetInvokeType() const {
4254 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004255 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004256
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004257 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004258 return intrinsic_;
4259 }
4260
Aart Bik5d75afe2015-12-14 11:57:01 -08004261 void SetIntrinsic(Intrinsics intrinsic,
4262 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4263 IntrinsicSideEffects side_effects,
4264 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004265
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004266 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004267 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004268 }
4269
Aart Bikff7d89c2016-11-07 08:49:28 -08004270 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4271
Vladimir Markoa1de9182016-02-25 11:37:38 +00004272 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004273
Aart Bika8b8e9b2018-01-09 11:01:02 -08004274 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4275
4276 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4277
Aart Bik71bf7b42016-11-16 10:17:46 -08004278 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004279
Vladimir Marko372f10e2016-05-17 16:30:10 +01004280 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004281 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4282 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004283
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004284 uint32_t* GetIntrinsicOptimizations() {
4285 return &intrinsic_optimizations_;
4286 }
4287
4288 const uint32_t* GetIntrinsicOptimizations() const {
4289 return &intrinsic_optimizations_;
4290 }
4291
4292 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4293
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004294 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004295 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004296
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004297 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004298
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004299 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004300 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4301 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004302 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4303 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004304 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004305 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004306 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004307 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004308 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4309 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004310 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004311 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004312 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004313
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304314 HInvoke(InstructionKind kind,
4315 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004316 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004317 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004319 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004320 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004321 ArtMethod* resolved_method,
4322 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004323 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304324 kind,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004325 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4326 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004327 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004328 number_of_arguments + number_of_other_inputs,
4329 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004330 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004331 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004332 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004333 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004334 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004335 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004336 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004337 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004338 }
4339
Artem Serovcced8ba2017-07-19 18:18:09 +01004340 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4341
Roland Levillain3e3d7332015-04-28 11:00:54 +01004342 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004343 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004344 const uint32_t dex_method_index_;
4345 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004346
4347 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4348 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004349};
4350
Vladimir Markofcb503c2016-05-18 12:48:17 +01004351class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004352 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004353 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004354 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004355 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004356 uint32_t dex_pc,
4357 uint32_t dex_method_index,
4358 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304359 : HInvoke(kInvokeUnresolved,
4360 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004361 number_of_arguments,
4362 0u /* number_of_other_inputs */,
4363 return_type,
4364 dex_pc,
4365 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004366 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004367 invoke_type) {
4368 }
4369
Artem Serovcced8ba2017-07-19 18:18:09 +01004370 bool IsClonable() const OVERRIDE { return true; }
4371
Calin Juravle175dc732015-08-25 15:42:32 +01004372 DECLARE_INSTRUCTION(InvokeUnresolved);
4373
Artem Serovcced8ba2017-07-19 18:18:09 +01004374 protected:
4375 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004376};
4377
Orion Hodsonac141392017-01-13 11:53:47 +00004378class HInvokePolymorphic FINAL : public HInvoke {
4379 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004380 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004381 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004382 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004383 uint32_t dex_pc,
4384 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304385 : HInvoke(kInvokePolymorphic,
4386 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004387 number_of_arguments,
4388 0u /* number_of_other_inputs */,
4389 return_type,
4390 dex_pc,
4391 dex_method_index,
4392 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304393 kVirtual) {
4394 }
Orion Hodsonac141392017-01-13 11:53:47 +00004395
Artem Serovcced8ba2017-07-19 18:18:09 +01004396 bool IsClonable() const OVERRIDE { return true; }
4397
Orion Hodsonac141392017-01-13 11:53:47 +00004398 DECLARE_INSTRUCTION(InvokePolymorphic);
4399
Artem Serovcced8ba2017-07-19 18:18:09 +01004400 protected:
4401 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004402};
4403
Vladimir Markofcb503c2016-05-18 12:48:17 +01004404class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004405 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004406 // Requirements of this method call regarding the class
4407 // initialization (clinit) check of its declaring class.
4408 enum class ClinitCheckRequirement {
4409 kNone, // Class already initialized.
4410 kExplicit, // Static call having explicit clinit check as last input.
4411 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004412 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004413 };
4414
Vladimir Marko58155012015-08-19 12:49:41 +00004415 // Determines how to load the target ArtMethod*.
4416 enum class MethodLoadKind {
4417 // Use a String init ArtMethod* loaded from Thread entrypoints.
4418 kStringInit,
4419
4420 // Use the method's own ArtMethod* loaded by the register allocator.
4421 kRecursive,
4422
Vladimir Marko65979462017-05-19 17:25:12 +01004423 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4424 // Used for boot image methods referenced by boot image code.
4425 kBootImageLinkTimePcRelative,
4426
Vladimir Marko58155012015-08-19 12:49:41 +00004427 // Use ArtMethod* at a known address, embed the direct address in the code.
4428 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4429 kDirectAddress,
4430
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004431 // Load from an entry in the .bss section using a PC-relative load.
4432 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4433 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004434
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004435 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4436 // used when other kinds are unimplemented on a particular architecture.
4437 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004438 };
4439
4440 // Determines the location of the code pointer.
4441 enum class CodePtrLocation {
4442 // Recursive call, use local PC-relative call instruction.
4443 kCallSelf,
4444
Vladimir Marko58155012015-08-19 12:49:41 +00004445 // Use code pointer from the ArtMethod*.
4446 // Used when we don't know the target code. This is also the last-resort-kind used when
4447 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4448 kCallArtMethod,
4449 };
4450
4451 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004452 MethodLoadKind method_load_kind;
4453 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004454 // The method load data holds
4455 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4456 // Note that there are multiple string init methods, each having its own offset.
4457 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004458 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004459 };
4460
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004461 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004462 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004463 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004464 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004465 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004466 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004467 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004468 InvokeType invoke_type,
4469 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004470 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304471 : HInvoke(kInvokeStaticOrDirect,
4472 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004473 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004474 // There is potentially one extra argument for the HCurrentMethod node, and
4475 // potentially one other if the clinit check is explicit, and potentially
4476 // one other if the method is a string factory.
4477 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004478 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004479 return_type,
4480 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004481 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004482 resolved_method,
4483 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004484 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004485 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004486 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4487 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004488
Artem Serovcced8ba2017-07-19 18:18:09 +01004489 bool IsClonable() const OVERRIDE { return true; }
4490
Vladimir Markodc151b22015-10-15 18:02:30 +01004491 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004492 bool had_current_method_input = HasCurrentMethodInput();
4493 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4494
4495 // Using the current method is the default and once we find a better
4496 // method load kind, we should not go back to using the current method.
4497 DCHECK(had_current_method_input || !needs_current_method_input);
4498
4499 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004500 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4501 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004502 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004503 dispatch_info_ = dispatch_info;
4504 }
4505
Aart Bik6daebeb2017-04-03 14:35:41 -07004506 DispatchInfo GetDispatchInfo() const {
4507 return dispatch_info_;
4508 }
4509
Vladimir Markoc53c0792015-11-19 15:48:33 +00004510 void AddSpecialInput(HInstruction* input) {
4511 // We allow only one special input.
4512 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4513 DCHECK(InputCount() == GetSpecialInputIndex() ||
4514 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4515 InsertInputAt(GetSpecialInputIndex(), input);
4516 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004517
Vladimir Marko372f10e2016-05-17 16:30:10 +01004518 using HInstruction::GetInputRecords; // Keep the const version visible.
4519 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4520 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4521 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4522 DCHECK(!input_records.empty());
4523 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4524 HInstruction* last_input = input_records.back().GetInstruction();
4525 // Note: `last_input` may be null during arguments setup.
4526 if (last_input != nullptr) {
4527 // `last_input` is the last input of a static invoke marked as having
4528 // an explicit clinit check. It must either be:
4529 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4530 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4531 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4532 }
4533 }
4534 return input_records;
4535 }
4536
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004537 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004538 // We access the method via the dex cache so we can't do an implicit null check.
4539 // TODO: for intrinsics we can generate implicit null checks.
4540 return false;
4541 }
4542
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004543 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004544 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004545 }
4546
Vladimir Markoc53c0792015-11-19 15:48:33 +00004547 // Get the index of the special input, if any.
4548 //
David Brazdil6de19382016-01-08 17:37:10 +00004549 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4550 // method pointer; otherwise there may be one platform-specific special input,
4551 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004552 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004553 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004554
Vladimir Marko58155012015-08-19 12:49:41 +00004555 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4556 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4557 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004558 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004559 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004560 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004561 bool HasPcRelativeMethodLoadKind() const {
4562 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004563 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004564 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004565 bool HasCurrentMethodInput() const {
4566 // This function can be called only after the invoke has been fully initialized by the builder.
4567 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004568 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004569 return true;
4570 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004571 DCHECK(InputCount() == GetSpecialInputIndex() ||
4572 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004573 return false;
4574 }
4575 }
Vladimir Marko58155012015-08-19 12:49:41 +00004576
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004577 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004578 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004579 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004580 }
4581
4582 uint64_t GetMethodAddress() const {
4583 DCHECK(HasMethodAddress());
4584 return dispatch_info_.method_load_data;
4585 }
4586
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004587 const DexFile& GetDexFileForPcRelativeDexCache() const;
4588
Vladimir Markoa1de9182016-02-25 11:37:38 +00004589 ClinitCheckRequirement GetClinitCheckRequirement() const {
4590 return GetPackedField<ClinitCheckRequirementField>();
4591 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004592
Roland Levillain4c0eb422015-04-24 16:43:49 +01004593 // Is this instruction a call to a static method?
4594 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004595 return GetInvokeType() == kStatic;
4596 }
4597
4598 MethodReference GetTargetMethod() const {
4599 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004600 }
4601
Vladimir Markofbb184a2015-11-13 14:47:00 +00004602 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4603 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4604 // instruction; only relevant for static calls with explicit clinit check.
4605 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004606 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004607 size_t last_input_index = inputs_.size() - 1u;
4608 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004609 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004610 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004611 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004612 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004613 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004614 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004615 }
4616
4617 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004618 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004619 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004620 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004621 }
4622
4623 // Is this a call to a static method whose declaring class has an
4624 // implicit intialization check requirement?
4625 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004626 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004627 }
4628
Vladimir Markob554b5a2015-11-06 12:57:55 +00004629 // Does this method load kind need the current method as an input?
4630 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004631 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004632 }
4633
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004634 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004635
Artem Serovcced8ba2017-07-19 18:18:09 +01004636 protected:
4637 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4638
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004639 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004640 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004641 static constexpr size_t kFieldClinitCheckRequirementSize =
4642 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4643 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4644 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4645 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4646 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004647 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4648 kFieldClinitCheckRequirement,
4649 kFieldClinitCheckRequirementSize>;
4650
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004651 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004652 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004653 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004654};
Vladimir Markof64242a2015-12-01 14:58:23 +00004655std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004656std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004657
Vladimir Markofcb503c2016-05-18 12:48:17 +01004658class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004659 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004660 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004661 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004662 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004663 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004664 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004665 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004666 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304667 : HInvoke(kInvokeVirtual,
4668 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004669 number_of_arguments,
4670 0u,
4671 return_type,
4672 dex_pc,
4673 dex_method_index,
4674 resolved_method,
4675 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304676 vtable_index_(vtable_index) {
4677 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004678
Artem Serovcced8ba2017-07-19 18:18:09 +01004679 bool IsClonable() const OVERRIDE { return true; }
4680
Aart Bik71bf7b42016-11-16 10:17:46 -08004681 bool CanBeNull() const OVERRIDE {
4682 switch (GetIntrinsic()) {
4683 case Intrinsics::kThreadCurrentThread:
4684 case Intrinsics::kStringBufferAppend:
4685 case Intrinsics::kStringBufferToString:
4686 case Intrinsics::kStringBuilderAppend:
4687 case Intrinsics::kStringBuilderToString:
4688 return false;
4689 default:
4690 return HInvoke::CanBeNull();
4691 }
4692 }
4693
Calin Juravle641547a2015-04-21 22:08:51 +01004694 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004695 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004696 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004697 }
4698
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004699 uint32_t GetVTableIndex() const { return vtable_index_; }
4700
4701 DECLARE_INSTRUCTION(InvokeVirtual);
4702
Artem Serovcced8ba2017-07-19 18:18:09 +01004703 protected:
4704 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4705
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004706 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004707 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004708 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004709};
4710
Vladimir Markofcb503c2016-05-18 12:48:17 +01004711class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004712 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004713 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004714 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004715 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004716 uint32_t dex_pc,
4717 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004718 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004719 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304720 : HInvoke(kInvokeInterface,
4721 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004722 number_of_arguments,
4723 0u,
4724 return_type,
4725 dex_pc,
4726 dex_method_index,
4727 resolved_method,
4728 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304729 imt_index_(imt_index) {
4730 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004731
Artem Serovcced8ba2017-07-19 18:18:09 +01004732 bool IsClonable() const OVERRIDE { return true; }
4733
Calin Juravle641547a2015-04-21 22:08:51 +01004734 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004735 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004736 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004737 }
4738
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004739 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4740 // The assembly stub currently needs it.
4741 return true;
4742 }
4743
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004744 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004745
4746 DECLARE_INSTRUCTION(InvokeInterface);
4747
Artem Serovcced8ba2017-07-19 18:18:09 +01004748 protected:
4749 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4750
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004751 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004752 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004753 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004754};
4755
Vladimir Markofcb503c2016-05-18 12:48:17 +01004756class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004757 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004758 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304759 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004760 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004761 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004762
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004763 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004764
4765 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004766 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004767 }
4768 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004769 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004770 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004771 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4772 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4773 }
4774 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4775 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4776 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004777
Roland Levillain88cb1752014-10-20 16:36:47 +01004778 DECLARE_INSTRUCTION(Neg);
4779
Artem Serovcced8ba2017-07-19 18:18:09 +01004780 protected:
4781 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004782};
4783
Vladimir Markofcb503c2016-05-18 12:48:17 +01004784class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004785 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004786 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304787 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004788 SetRawInputAt(0, cls);
4789 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004790 }
4791
Artem Serovcced8ba2017-07-19 18:18:09 +01004792 bool IsClonable() const OVERRIDE { return true; }
4793
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004794 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004795 bool NeedsEnvironment() const OVERRIDE { return true; }
4796
Mingyao Yang0c365e62015-03-31 15:09:29 -07004797 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4798 bool CanThrow() const OVERRIDE { return true; }
4799
Calin Juravle10e244f2015-01-26 18:54:32 +00004800 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004801
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004802 HLoadClass* GetLoadClass() const {
4803 DCHECK(InputAt(0)->IsLoadClass());
4804 return InputAt(0)->AsLoadClass();
4805 }
4806
4807 HInstruction* GetLength() const {
4808 return InputAt(1);
4809 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004810
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004811 DECLARE_INSTRUCTION(NewArray);
4812
Artem Serovcced8ba2017-07-19 18:18:09 +01004813 protected:
4814 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004815};
4816
Vladimir Markofcb503c2016-05-18 12:48:17 +01004817class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004818 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004819 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004820 HInstruction* left,
4821 HInstruction* right,
4822 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304823 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4824 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004826 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004827
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004828 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004829
4830 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004831 return GetBlock()->GetGraph()->GetIntConstant(
4832 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004833 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004834 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 return GetBlock()->GetGraph()->GetLongConstant(
4836 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004837 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004838 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4839 return GetBlock()->GetGraph()->GetFloatConstant(
4840 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4841 }
4842 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4843 return GetBlock()->GetGraph()->GetDoubleConstant(
4844 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4845 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004846
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004847 DECLARE_INSTRUCTION(Add);
4848
Artem Serovcced8ba2017-07-19 18:18:09 +01004849 protected:
4850 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004851};
4852
Vladimir Markofcb503c2016-05-18 12:48:17 +01004853class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004854 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004855 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004856 HInstruction* left,
4857 HInstruction* right,
4858 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304859 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4860 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004861
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004862 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004863
4864 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004865 return GetBlock()->GetGraph()->GetIntConstant(
4866 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004867 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004868 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004869 return GetBlock()->GetGraph()->GetLongConstant(
4870 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004871 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004872 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4873 return GetBlock()->GetGraph()->GetFloatConstant(
4874 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4875 }
4876 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4877 return GetBlock()->GetGraph()->GetDoubleConstant(
4878 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4879 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004880
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004881 DECLARE_INSTRUCTION(Sub);
4882
Artem Serovcced8ba2017-07-19 18:18:09 +01004883 protected:
4884 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004885};
4886
Vladimir Markofcb503c2016-05-18 12:48:17 +01004887class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004888 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004889 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004890 HInstruction* left,
4891 HInstruction* right,
4892 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304893 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4894 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004895
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004896 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004897
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004898 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004899
4900 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004901 return GetBlock()->GetGraph()->GetIntConstant(
4902 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004903 }
4904 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004905 return GetBlock()->GetGraph()->GetLongConstant(
4906 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004907 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004908 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4909 return GetBlock()->GetGraph()->GetFloatConstant(
4910 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4911 }
4912 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4913 return GetBlock()->GetGraph()->GetDoubleConstant(
4914 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4915 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004916
4917 DECLARE_INSTRUCTION(Mul);
4918
Artem Serovcced8ba2017-07-19 18:18:09 +01004919 protected:
4920 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004921};
4922
Vladimir Markofcb503c2016-05-18 12:48:17 +01004923class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004924 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004925 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004926 HInstruction* left,
4927 HInstruction* right,
4928 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304929 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4930 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004931
Roland Levillain9867bc72015-08-05 10:21:34 +01004932 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004933 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004934 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004935 // Our graph structure ensures we never have 0 for `y` during
4936 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004937 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004938 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004939 return (y == -1) ? -x : x / y;
4940 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004941
Roland Levillain31dd3d62016-02-16 12:21:02 +00004942 template <typename T>
4943 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004944 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004945 return x / y;
4946 }
4947
Roland Levillain9867bc72015-08-05 10:21:34 +01004948 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004949 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004950 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004951 }
4952 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004953 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004954 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4955 }
4956 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4957 return GetBlock()->GetGraph()->GetFloatConstant(
4958 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4959 }
4960 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4961 return GetBlock()->GetGraph()->GetDoubleConstant(
4962 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004963 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004964
4965 DECLARE_INSTRUCTION(Div);
4966
Artem Serovcced8ba2017-07-19 18:18:09 +01004967 protected:
4968 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004969};
4970
Vladimir Markofcb503c2016-05-18 12:48:17 +01004971class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004972 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004973 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004974 HInstruction* left,
4975 HInstruction* right,
4976 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304977 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4978 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004979
Roland Levillain9867bc72015-08-05 10:21:34 +01004980 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004981 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004982 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004983 // Our graph structure ensures we never have 0 for `y` during
4984 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004985 DCHECK_NE(y, 0);
4986 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4987 return (y == -1) ? 0 : x % y;
4988 }
4989
Roland Levillain31dd3d62016-02-16 12:21:02 +00004990 template <typename T>
4991 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004992 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004993 return std::fmod(x, y);
4994 }
4995
Roland Levillain9867bc72015-08-05 10:21:34 +01004996 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004997 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004998 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004999 }
5000 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005001 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005002 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005003 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005004 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
5005 return GetBlock()->GetGraph()->GetFloatConstant(
5006 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5007 }
5008 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
5009 return GetBlock()->GetGraph()->GetDoubleConstant(
5010 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5011 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005012
5013 DECLARE_INSTRUCTION(Rem);
5014
Artem Serovcced8ba2017-07-19 18:18:09 +01005015 protected:
5016 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005017};
5018
Aart Bik3dad3412018-02-28 12:01:46 -08005019class HAbs FINAL : public HUnaryOperation {
5020 public:
5021 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5022 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5023
5024 // Evaluation for integral values.
5025 template <typename T> static T ComputeIntegral(T x) {
5026 return x < 0 ? -x : x;
5027 }
5028
5029 // Evaluation for floating-point values.
5030 // Note, as a "quality of implementation", rather than pure "spec compliance",
5031 // we require that Math.abs() clears the sign bit (but changes nothing else)
5032 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5033 // http://b/30758343
5034 template <typename T, typename S> static T ComputeFP(T x) {
5035 S bits = bit_cast<S, T>(x);
5036 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5037 }
5038
5039 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5040 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5041 }
5042 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5043 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5044 }
5045 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5046 return GetBlock()->GetGraph()->GetFloatConstant(
5047 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5048 }
5049 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5050 return GetBlock()->GetGraph()->GetDoubleConstant(
5051 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5052 }
5053
5054 DECLARE_INSTRUCTION(Abs);
5055
5056 protected:
5057 DEFAULT_COPY_CONSTRUCTOR(Abs);
5058};
5059
Vladimir Markofcb503c2016-05-18 12:48:17 +01005060class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005061 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005062 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5063 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005064 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305065 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005066 SetRawInputAt(0, value);
5067 }
5068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005069 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06005070
Calin Juravled0d48522014-11-04 16:40:20 +00005071 bool CanBeMoved() const OVERRIDE { return true; }
5072
Vladimir Marko372f10e2016-05-17 16:30:10 +01005073 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005074 return true;
5075 }
5076
5077 bool NeedsEnvironment() const OVERRIDE { return true; }
5078 bool CanThrow() const OVERRIDE { return true; }
5079
Calin Juravled0d48522014-11-04 16:40:20 +00005080 DECLARE_INSTRUCTION(DivZeroCheck);
5081
Artem Serovcced8ba2017-07-19 18:18:09 +01005082 protected:
5083 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005084};
5085
Vladimir Markofcb503c2016-05-18 12:48:17 +01005086class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005087 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005088 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005089 HInstruction* value,
5090 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005091 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305092 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005093 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5094 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005095 }
5096
Roland Levillain5b5b9312016-03-22 14:57:31 +00005097 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005098 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005099 return value << (distance & max_shift_distance);
5100 }
5101
5102 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005103 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005104 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005105 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005106 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005107 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005108 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005109 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005110 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5111 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5112 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5113 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005114 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005115 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5116 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005117 LOG(FATAL) << DebugName() << " is not defined for float values";
5118 UNREACHABLE();
5119 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005120 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5121 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005122 LOG(FATAL) << DebugName() << " is not defined for double values";
5123 UNREACHABLE();
5124 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005125
5126 DECLARE_INSTRUCTION(Shl);
5127
Artem Serovcced8ba2017-07-19 18:18:09 +01005128 protected:
5129 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005130};
5131
Vladimir Markofcb503c2016-05-18 12:48:17 +01005132class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005133 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005134 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005135 HInstruction* value,
5136 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005137 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305138 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005139 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5140 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005141 }
5142
Roland Levillain5b5b9312016-03-22 14:57:31 +00005143 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005144 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005145 return value >> (distance & max_shift_distance);
5146 }
5147
5148 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005149 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005150 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005151 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005152 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005153 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005154 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005155 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005156 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5157 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5158 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5159 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005160 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005161 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5162 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005163 LOG(FATAL) << DebugName() << " is not defined for float values";
5164 UNREACHABLE();
5165 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005166 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5167 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005168 LOG(FATAL) << DebugName() << " is not defined for double values";
5169 UNREACHABLE();
5170 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005171
5172 DECLARE_INSTRUCTION(Shr);
5173
Artem Serovcced8ba2017-07-19 18:18:09 +01005174 protected:
5175 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005176};
5177
Vladimir Markofcb503c2016-05-18 12:48:17 +01005178class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005179 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005180 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005181 HInstruction* value,
5182 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005183 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305184 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5186 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005187 }
5188
Roland Levillain5b5b9312016-03-22 14:57:31 +00005189 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005190 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005191 typedef typename std::make_unsigned<T>::type V;
5192 V ux = static_cast<V>(value);
5193 return static_cast<T>(ux >> (distance & max_shift_distance));
5194 }
5195
5196 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005197 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005198 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005199 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005200 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005201 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005202 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005203 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005204 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5205 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5206 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5207 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005208 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005209 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5210 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005211 LOG(FATAL) << DebugName() << " is not defined for float values";
5212 UNREACHABLE();
5213 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005214 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5215 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005216 LOG(FATAL) << DebugName() << " is not defined for double values";
5217 UNREACHABLE();
5218 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005219
5220 DECLARE_INSTRUCTION(UShr);
5221
Artem Serovcced8ba2017-07-19 18:18:09 +01005222 protected:
5223 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005224};
5225
Vladimir Markofcb503c2016-05-18 12:48:17 +01005226class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005227 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005228 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005229 HInstruction* left,
5230 HInstruction* right,
5231 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305232 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5233 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005234
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005235 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005236
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005237 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005238
5239 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005240 return GetBlock()->GetGraph()->GetIntConstant(
5241 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005242 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005243 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005244 return GetBlock()->GetGraph()->GetLongConstant(
5245 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005246 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005247 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5248 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5249 LOG(FATAL) << DebugName() << " is not defined for float values";
5250 UNREACHABLE();
5251 }
5252 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5253 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5254 LOG(FATAL) << DebugName() << " is not defined for double values";
5255 UNREACHABLE();
5256 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005257
5258 DECLARE_INSTRUCTION(And);
5259
Artem Serovcced8ba2017-07-19 18:18:09 +01005260 protected:
5261 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005262};
5263
Vladimir Markofcb503c2016-05-18 12:48:17 +01005264class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005265 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005266 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005267 HInstruction* left,
5268 HInstruction* right,
5269 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305270 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5271 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005272
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005273 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005274
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005275 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005276
5277 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005278 return GetBlock()->GetGraph()->GetIntConstant(
5279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005280 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005281 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005282 return GetBlock()->GetGraph()->GetLongConstant(
5283 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005284 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005285 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5286 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5287 LOG(FATAL) << DebugName() << " is not defined for float values";
5288 UNREACHABLE();
5289 }
5290 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5291 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5292 LOG(FATAL) << DebugName() << " is not defined for double values";
5293 UNREACHABLE();
5294 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005295
5296 DECLARE_INSTRUCTION(Or);
5297
Artem Serovcced8ba2017-07-19 18:18:09 +01005298 protected:
5299 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005300};
5301
Vladimir Markofcb503c2016-05-18 12:48:17 +01005302class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005303 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005304 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005305 HInstruction* left,
5306 HInstruction* right,
5307 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305308 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5309 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005310
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005311 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005312
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005313 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005314
5315 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005316 return GetBlock()->GetGraph()->GetIntConstant(
5317 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005318 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005319 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005320 return GetBlock()->GetGraph()->GetLongConstant(
5321 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005322 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005323 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5324 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5325 LOG(FATAL) << DebugName() << " is not defined for float values";
5326 UNREACHABLE();
5327 }
5328 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5329 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5330 LOG(FATAL) << DebugName() << " is not defined for double values";
5331 UNREACHABLE();
5332 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005333
5334 DECLARE_INSTRUCTION(Xor);
5335
Artem Serovcced8ba2017-07-19 18:18:09 +01005336 protected:
5337 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005338};
5339
Vladimir Markofcb503c2016-05-18 12:48:17 +01005340class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005341 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005342 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305343 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005344 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5345 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005346 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005347
Roland Levillain5b5b9312016-03-22 14:57:31 +00005348 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005349 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005350 typedef typename std::make_unsigned<T>::type V;
5351 V ux = static_cast<V>(value);
5352 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005353 return static_cast<T>(ux);
5354 } else {
5355 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005356 return static_cast<T>(ux >> (distance & max_shift_value)) |
5357 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005358 }
5359 }
5360
Roland Levillain5b5b9312016-03-22 14:57:31 +00005361 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005362 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005363 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005364 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005365 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005366 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005367 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005368 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005369 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5370 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5371 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5372 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005373 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005374 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5375 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005376 LOG(FATAL) << DebugName() << " is not defined for float values";
5377 UNREACHABLE();
5378 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005379 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5380 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005381 LOG(FATAL) << DebugName() << " is not defined for double values";
5382 UNREACHABLE();
5383 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005384
5385 DECLARE_INSTRUCTION(Ror);
5386
Artem Serovcced8ba2017-07-19 18:18:09 +01005387 protected:
5388 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005389};
5390
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005391// The value of a parameter in this method. Its location depends on
5392// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005393class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005394 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005395 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005396 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005397 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005398 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005399 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305400 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005401 dex_file_(dex_file),
5402 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005403 index_(index) {
5404 SetPackedFlag<kFlagIsThis>(is_this);
5405 SetPackedFlag<kFlagCanBeNull>(!is_this);
5406 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005407
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005408 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005409 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005410 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005411 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005412
Vladimir Markoa1de9182016-02-25 11:37:38 +00005413 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5414 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005415
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005416 DECLARE_INSTRUCTION(ParameterValue);
5417
Artem Serovcced8ba2017-07-19 18:18:09 +01005418 protected:
5419 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5420
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005421 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005422 // Whether or not the parameter value corresponds to 'this' argument.
5423 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5424 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5425 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5426 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5427 "Too many packed fields.");
5428
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005429 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005430 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005431 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005432 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005433 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005434};
5435
Vladimir Markofcb503c2016-05-18 12:48:17 +01005436class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005437 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005438 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305439 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5440 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005441
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005442 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005443 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005444 return true;
5445 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005446
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005447 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005448
5449 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005450 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005451 }
5452 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005453 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005454 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005455 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5456 LOG(FATAL) << DebugName() << " is not defined for float values";
5457 UNREACHABLE();
5458 }
5459 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5460 LOG(FATAL) << DebugName() << " is not defined for double values";
5461 UNREACHABLE();
5462 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005463
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005464 DECLARE_INSTRUCTION(Not);
5465
Artem Serovcced8ba2017-07-19 18:18:09 +01005466 protected:
5467 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005468};
5469
Vladimir Markofcb503c2016-05-18 12:48:17 +01005470class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005471 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005472 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305473 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5474 }
David Brazdil66d126e2015-04-03 16:02:44 +01005475
5476 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005477 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005478 return true;
5479 }
5480
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005481 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005482 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005483 return !x;
5484 }
5485
Roland Levillain9867bc72015-08-05 10:21:34 +01005486 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005487 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005488 }
5489 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5490 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005491 UNREACHABLE();
5492 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005493 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5494 LOG(FATAL) << DebugName() << " is not defined for float values";
5495 UNREACHABLE();
5496 }
5497 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5498 LOG(FATAL) << DebugName() << " is not defined for double values";
5499 UNREACHABLE();
5500 }
David Brazdil66d126e2015-04-03 16:02:44 +01005501
5502 DECLARE_INSTRUCTION(BooleanNot);
5503
Artem Serovcced8ba2017-07-19 18:18:09 +01005504 protected:
5505 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005506};
5507
Vladimir Markofcb503c2016-05-18 12:48:17 +01005508class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005509 public:
5510 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005511 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305512 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005513 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005514 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005515 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005516 }
5517
5518 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005519 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5520 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005521
5522 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005523 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5524 return true;
5525 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005526
Mark Mendelle82549b2015-05-06 10:55:34 -04005527 // Try to statically evaluate the conversion and return a HConstant
5528 // containing the result. If the input cannot be converted, return nullptr.
5529 HConstant* TryStaticEvaluation() const;
5530
Roland Levillaindff1f282014-11-05 14:15:05 +00005531 DECLARE_INSTRUCTION(TypeConversion);
5532
Artem Serovcced8ba2017-07-19 18:18:09 +01005533 protected:
5534 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005535};
5536
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005537static constexpr uint32_t kNoRegNumber = -1;
5538
Vladimir Markofcb503c2016-05-18 12:48:17 +01005539class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005540 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005541 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5542 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005543 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305544 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005545 SetRawInputAt(0, value);
5546 }
5547
Artem Serovcced8ba2017-07-19 18:18:09 +01005548 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005549 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005550 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005551 return true;
5552 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005553
Calin Juravle10e244f2015-01-26 18:54:32 +00005554 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005555
Calin Juravle10e244f2015-01-26 18:54:32 +00005556 bool CanThrow() const OVERRIDE { return true; }
5557
5558 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005559
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005560 DECLARE_INSTRUCTION(NullCheck);
5561
Artem Serovcced8ba2017-07-19 18:18:09 +01005562 protected:
5563 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005564};
5565
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005566// Embeds an ArtField and all the information required by the compiler. We cache
5567// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005568class FieldInfo : public ValueObject {
5569 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005570 FieldInfo(ArtField* field,
5571 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005572 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005573 bool is_volatile,
5574 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005575 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005576 const DexFile& dex_file)
5577 : field_(field),
5578 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005579 field_type_(field_type),
5580 is_volatile_(is_volatile),
5581 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005582 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005583 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005584
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005585 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005586 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005587 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005588 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005589 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005590 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005591 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005592
5593 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005594 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005595 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005597 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005598 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005599 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005600 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005601};
5602
Vladimir Markofcb503c2016-05-18 12:48:17 +01005603class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005604 public:
5605 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005606 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005607 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005608 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005609 bool is_volatile,
5610 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005611 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005612 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005613 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305614 : HExpression(kInstanceFieldGet,
5615 field_type,
5616 SideEffects::FieldReadOfType(field_type, is_volatile),
5617 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005618 field_info_(field,
5619 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005620 field_type,
5621 is_volatile,
5622 field_idx,
5623 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005624 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005625 SetRawInputAt(0, value);
5626 }
5627
Artem Serovcced8ba2017-07-19 18:18:09 +01005628 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005629 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005630
Vladimir Marko372f10e2016-05-17 16:30:10 +01005631 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5632 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005633 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005634 }
5635
Calin Juravle641547a2015-04-21 22:08:51 +01005636 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005637 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005638 }
5639
5640 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005641 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5642 }
5643
Calin Juravle52c48962014-12-16 17:02:57 +00005644 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005645 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005646 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005647 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005648
Vladimir Marko61b92282017-10-11 13:23:17 +01005649 void SetType(DataType::Type new_type) {
5650 DCHECK(DataType::IsIntegralType(GetType()));
5651 DCHECK(DataType::IsIntegralType(new_type));
5652 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5653 SetPackedField<TypeField>(new_type);
5654 }
5655
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005656 DECLARE_INSTRUCTION(InstanceFieldGet);
5657
Artem Serovcced8ba2017-07-19 18:18:09 +01005658 protected:
5659 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5660
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005661 private:
5662 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005663};
5664
Vladimir Markofcb503c2016-05-18 12:48:17 +01005665class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005666 public:
5667 HInstanceFieldSet(HInstruction* object,
5668 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005669 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005670 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005671 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005672 bool is_volatile,
5673 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005674 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005675 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005676 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305677 : HTemplateInstruction(kInstanceFieldSet,
5678 SideEffects::FieldWriteOfType(field_type, is_volatile),
5679 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005680 field_info_(field,
5681 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005682 field_type,
5683 is_volatile,
5684 field_idx,
5685 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005686 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005687 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005688 SetRawInputAt(0, object);
5689 SetRawInputAt(1, value);
5690 }
5691
Artem Serovcced8ba2017-07-19 18:18:09 +01005692 bool IsClonable() const OVERRIDE { return true; }
5693
Calin Juravle641547a2015-04-21 22:08:51 +01005694 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005695 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005696 }
5697
Calin Juravle52c48962014-12-16 17:02:57 +00005698 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005699 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005700 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005701 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005702 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005703 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5704 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005705
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005706 DECLARE_INSTRUCTION(InstanceFieldSet);
5707
Artem Serovcced8ba2017-07-19 18:18:09 +01005708 protected:
5709 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5710
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005711 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5713 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5714 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5715 "Too many packed fields.");
5716
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005717 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005718};
5719
Vladimir Markofcb503c2016-05-18 12:48:17 +01005720class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005721 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005722 HArrayGet(HInstruction* array,
5723 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005725 uint32_t dex_pc)
5726 : HArrayGet(array,
5727 index,
5728 type,
5729 SideEffects::ArrayReadOfType(type),
5730 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305731 /* is_string_char_at */ false) {
5732 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005733
5734 HArrayGet(HInstruction* array,
5735 HInstruction* index,
5736 DataType::Type type,
5737 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005738 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005739 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305740 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005741 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005742 SetRawInputAt(0, array);
5743 SetRawInputAt(1, index);
5744 }
5745
Artem Serovcced8ba2017-07-19 18:18:09 +01005746 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005747 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005748 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005749 return true;
5750 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005751 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005752 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005753 // Currently, unless the array is the result of NewArray, the array access is always
5754 // preceded by some form of null NullCheck necessary for the bounds check, usually
5755 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5756 // dynamic BCE. There are cases when these could be removed to produce better code.
5757 // If we ever add optimizations to do so we should allow an implicit check here
5758 // (as long as the address falls in the first page).
5759 //
5760 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5761 // a = cond ? new int[1] : null;
5762 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005763 return false;
5764 }
5765
David Brazdil4833f5a2015-12-16 10:37:39 +00005766 bool IsEquivalentOf(HArrayGet* other) const {
5767 bool result = (GetDexPc() == other->GetDexPc());
5768 if (kIsDebugBuild && result) {
5769 DCHECK_EQ(GetBlock(), other->GetBlock());
5770 DCHECK_EQ(GetArray(), other->GetArray());
5771 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005772 if (DataType::IsIntOrLongType(GetType())) {
5773 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005774 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5776 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005777 }
5778 }
5779 return result;
5780 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005781
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005782 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5783
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005784 HInstruction* GetArray() const { return InputAt(0); }
5785 HInstruction* GetIndex() const { return InputAt(1); }
5786
Vladimir Marko61b92282017-10-11 13:23:17 +01005787 void SetType(DataType::Type new_type) {
5788 DCHECK(DataType::IsIntegralType(GetType()));
5789 DCHECK(DataType::IsIntegralType(new_type));
5790 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5791 SetPackedField<TypeField>(new_type);
5792 }
5793
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005794 DECLARE_INSTRUCTION(ArrayGet);
5795
Artem Serovcced8ba2017-07-19 18:18:09 +01005796 protected:
5797 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5798
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005799 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005800 // We treat a String as an array, creating the HArrayGet from String.charAt()
5801 // intrinsic in the instruction simplifier. We can always determine whether
5802 // a particular HArrayGet is actually a String.charAt() by looking at the type
5803 // of the input but that requires holding the mutator lock, so we prefer to use
5804 // a flag, so that code generators don't need to do the locking.
5805 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5806 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5807 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5808 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005809};
5810
Vladimir Markofcb503c2016-05-18 12:48:17 +01005811class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005812 public:
5813 HArraySet(HInstruction* array,
5814 HInstruction* index,
5815 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005816 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005817 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005818 : HArraySet(array,
5819 index,
5820 value,
5821 expected_component_type,
5822 // Make a best guess for side effects now, may be refined during SSA building.
5823 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305824 dex_pc) {
5825 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005826
5827 HArraySet(HInstruction* array,
5828 HInstruction* index,
5829 HInstruction* value,
5830 DataType::Type expected_component_type,
5831 SideEffects side_effects,
5832 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305833 : HTemplateInstruction(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005834 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005835 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005836 SetPackedFlag<kFlagValueCanBeNull>(true);
5837 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005838 SetRawInputAt(0, array);
5839 SetRawInputAt(1, index);
5840 SetRawInputAt(2, value);
5841 }
5842
Artem Serovcced8ba2017-07-19 18:18:09 +01005843 bool IsClonable() const OVERRIDE { return true; }
5844
Calin Juravle77520bc2015-01-12 18:45:46 +00005845 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005846 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005847 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005848 }
5849
Mingyao Yang81014cb2015-06-02 03:16:27 -07005850 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005851 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005852
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005853 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005854 // TODO: Same as for ArrayGet.
5855 return false;
5856 }
5857
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005858 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005859 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005860 }
5861
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005862 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005863 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005864 }
5865
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005866 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005867 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005868 }
5869
Vladimir Markoa1de9182016-02-25 11:37:38 +00005870 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5871 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5872 bool StaticTypeOfArrayIsObjectArray() const {
5873 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5874 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005875
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005876 HInstruction* GetArray() const { return InputAt(0); }
5877 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005878 HInstruction* GetValue() const { return InputAt(2); }
5879
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005880 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005881 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5882 }
5883
5884 static DataType::Type GetComponentType(DataType::Type value_type,
5885 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005886 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005887 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005888 // be correct, we also check what is the value type. If it is a floating
5889 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005890 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005891 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005892 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005893 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005896 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005897 }
5898
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005899 static SideEffects ComputeSideEffects(DataType::Type type) {
5900 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005901 }
5902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005903 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5904 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5905 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005906 }
5907
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005908 DECLARE_INSTRUCTION(ArraySet);
5909
Artem Serovcced8ba2017-07-19 18:18:09 +01005910 protected:
5911 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5912
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005913 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005914 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5915 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005916 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005917 static constexpr size_t kFlagNeedsTypeCheck =
5918 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5919 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005920 // Cached information for the reference_type_info_ so that codegen
5921 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005922 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5923 static constexpr size_t kNumberOfArraySetPackedBits =
5924 kFlagStaticTypeOfArrayIsObjectArray + 1;
5925 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5926 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005927 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005928};
5929
Vladimir Markofcb503c2016-05-18 12:48:17 +01005930class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005931 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005932 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305933 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005934 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005935 // Note that arrays do not change length, so the instruction does not
5936 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005937 SetRawInputAt(0, array);
5938 }
5939
Artem Serovcced8ba2017-07-19 18:18:09 +01005940 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005941 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005942 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005943 return true;
5944 }
Calin Juravle641547a2015-04-21 22:08:51 +01005945 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5946 return obj == InputAt(0);
5947 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005948
Vladimir Markodce016e2016-04-28 13:10:02 +01005949 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5950
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005951 DECLARE_INSTRUCTION(ArrayLength);
5952
Artem Serovcced8ba2017-07-19 18:18:09 +01005953 protected:
5954 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
5955
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005956 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005957 // We treat a String as an array, creating the HArrayLength from String.length()
5958 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5959 // determine whether a particular HArrayLength is actually a String.length() by
5960 // looking at the type of the input but that requires holding the mutator lock, so
5961 // we prefer to use a flag, so that code generators don't need to do the locking.
5962 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5963 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5964 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5965 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005966};
5967
Vladimir Markofcb503c2016-05-18 12:48:17 +01005968class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005969 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005970 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5971 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005972 HBoundsCheck(HInstruction* index,
5973 HInstruction* length,
5974 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00005975 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305976 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005977 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00005978 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005979 SetRawInputAt(0, index);
5980 SetRawInputAt(1, length);
5981 }
5982
Artem Serovcced8ba2017-07-19 18:18:09 +01005983 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005984 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005985 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005986 return true;
5987 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005988
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005989 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005990
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005991 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005992
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005993 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005994
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005995 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005996
5997 DECLARE_INSTRUCTION(BoundsCheck);
5998
Artem Serovcced8ba2017-07-19 18:18:09 +01005999 protected:
6000 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6001
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006002 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006003 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006004};
6005
Vladimir Markofcb503c2016-05-18 12:48:17 +01006006class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006007 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006008 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306009 : HTemplateInstruction(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
6010 slow_path_(nullptr) {
6011 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006012
Artem Serovcced8ba2017-07-19 18:18:09 +01006013 bool IsClonable() const OVERRIDE { return true; }
6014
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006015 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006016 return true;
6017 }
6018
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006019 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6020 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006021
6022 DECLARE_INSTRUCTION(SuspendCheck);
6023
Artem Serovcced8ba2017-07-19 18:18:09 +01006024 protected:
6025 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6026
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006027 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006028 // Only used for code generation, in order to share the same slow path between back edges
6029 // of a same loop.
6030 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006031};
6032
David Srbecky0cf44932015-12-09 14:09:59 +00006033// Pseudo-instruction which provides the native debugger with mapping information.
6034// It ensures that we can generate line number and local variables at this point.
6035class HNativeDebugInfo : public HTemplateInstruction<0> {
6036 public:
6037 explicit HNativeDebugInfo(uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306038 : HTemplateInstruction<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
6039 }
David Srbecky0cf44932015-12-09 14:09:59 +00006040
6041 bool NeedsEnvironment() const OVERRIDE {
6042 return true;
6043 }
6044
6045 DECLARE_INSTRUCTION(NativeDebugInfo);
6046
Artem Serovcced8ba2017-07-19 18:18:09 +01006047 protected:
6048 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006049};
6050
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006051/**
6052 * Instruction to load a Class object.
6053 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006055 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006056 // Determines how to load the Class.
6057 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006058 // We cannot load this class. See HSharpening::SharpenLoadClass.
6059 kInvalid = -1,
6060
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006061 // Use the Class* from the method's own ArtMethod*.
6062 kReferrersClass,
6063
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006064 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006065 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 kBootImageLinkTimePcRelative,
6067
6068 // Use a known boot image Class* address, embedded in the code by the codegen.
6069 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006070 kBootImageAddress,
6071
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006072 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
6073 // of the oat file.
6074 kBootImageClassTable,
6075
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006076 // Load from an entry in the .bss section using a PC-relative load.
6077 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6078 kBssEntry,
6079
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006080 // Load from the root table associated with the JIT compiled method.
6081 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006082
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006083 // Load using a simple runtime call. This is the fall-back load kind when
6084 // the codegen is unable to use another appropriate kind.
6085 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006086
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006087 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006088 };
6089
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006090 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006091 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006092 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006093 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006094 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006095 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006096 bool needs_access_check)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306097 : HInstruction(kLoadClass, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006098 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006099 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006100 dex_file_(dex_file),
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006101 klass_(klass),
6102 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006103 // Referrers class should not need access check. We never inline unverified
6104 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006105 DCHECK(!is_referrers_class || !needs_access_check);
6106
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006107 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006108 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006109 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006110 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006111 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006112 }
6113
Artem Serovcced8ba2017-07-19 18:18:09 +01006114 bool IsClonable() const OVERRIDE { return true; }
6115
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006116 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006117
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006118 LoadKind GetLoadKind() const {
6119 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006120 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006121
6122 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006123
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006124 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006125
Andreas Gampea5b09a62016-11-17 15:21:22 -08006126 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006127
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006128 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006129
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006130 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006131 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006132 }
6133
Calin Juravle0ba218d2015-05-19 18:46:01 +01006134 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006135 // The entrypoint the code generator is going to call does not do
6136 // clinit of the class.
6137 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006138 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006139 }
6140
6141 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006142 return NeedsAccessCheck() ||
6143 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006144 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006145 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006146 }
6147
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006148 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006149 return NeedsAccessCheck() ||
6150 MustGenerateClinitCheck() ||
6151 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6152 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6153 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006154 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006155 GetLoadKind() == LoadKind::kBssEntry) &&
6156 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006157 }
6158
Calin Juravleacf735c2015-02-12 15:25:22 +00006159 ReferenceTypeInfo GetLoadedClassRTI() {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006160 return loaded_class_rti_;
Calin Juravleacf735c2015-02-12 15:25:22 +00006161 }
6162
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006163 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
6164 // Make sure we only set exact types (the loaded class should never be merged).
6165 DCHECK(rti.IsExact());
6166 loaded_class_rti_ = rti;
Calin Juravleacf735c2015-02-12 15:25:22 +00006167 }
6168
Andreas Gampea5b09a62016-11-17 15:21:22 -08006169 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006170 const DexFile& GetDexFile() const { return dex_file_; }
6171
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006172 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006173 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006174 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006175
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006176 static SideEffects SideEffectsForArchRuntimeCalls() {
6177 return SideEffects::CanTriggerGC();
6178 }
6179
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006180 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006181 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006182 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006183 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006184
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006185 void MarkInBootImage() {
6186 SetPackedFlag<kFlagIsInBootImage>(true);
6187 }
6188
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006189 void AddSpecialInput(HInstruction* special_input);
6190
6191 using HInstruction::GetInputRecords; // Keep the const version visible.
6192 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6193 return ArrayRef<HUserRecord<HInstruction*>>(
6194 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6195 }
6196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006197 DataType::Type GetType() const OVERRIDE {
6198 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006199 }
6200
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006201 Handle<mirror::Class> GetClass() const {
6202 return klass_;
6203 }
6204
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006205 DECLARE_INSTRUCTION(LoadClass);
6206
Artem Serovcced8ba2017-07-19 18:18:09 +01006207 protected:
6208 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6209
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006210 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006211 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006212 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006213 // Whether this instruction must generate the initialization check.
6214 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006215 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006216 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6217 static constexpr size_t kFieldLoadKindSize =
6218 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006219 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006220 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006221 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6222
6223 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006224 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006225 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006226 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006227 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006228 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006229 }
6230
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006231 void SetLoadKindInternal(LoadKind load_kind);
6232
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006233 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006234 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006235 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006236 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006237
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006238 // A type index and dex file where the class can be accessed. The dex file can be:
6239 // - The compiling method's dex file if the class is defined there too.
6240 // - The compiling method's dex file if the class is referenced there.
6241 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006242 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006243 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006245
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006246 Handle<mirror::Class> klass_;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006247
6248 ReferenceTypeInfo loaded_class_rti_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006249};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006250std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6251
6252// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006253inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6254 // The load kind should be determined before inserting the instruction to the graph.
6255 DCHECK(GetBlock() == nullptr);
6256 DCHECK(GetEnvironment() == nullptr);
6257 SetPackedField<LoadKindField>(load_kind);
6258 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6259 special_input_ = HUserRecord<HInstruction*>(nullptr);
6260 }
6261 if (!NeedsEnvironment()) {
6262 SetSideEffects(SideEffects::None());
6263 }
6264}
6265
6266// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006267inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006268 // The special input is used for PC-relative loads on some architectures,
6269 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006270 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006271 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006272 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006273 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006274 DCHECK(special_input_.GetInstruction() == nullptr);
6275 special_input_ = HUserRecord<HInstruction*>(special_input);
6276 special_input->AddUseAt(this, 0);
6277}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006278
Vladimir Marko372f10e2016-05-17 16:30:10 +01006279class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006280 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006281 // Determines how to load the String.
6282 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006283 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006284 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006285 kBootImageLinkTimePcRelative,
6286
6287 // Use a known boot image String* address, embedded in the code by the codegen.
6288 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006289 kBootImageAddress,
6290
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006291 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
6292 // of the oat file.
6293 kBootImageInternTable,
6294
Vladimir Markoaad75c62016-10-03 08:46:48 +00006295 // Load from an entry in the .bss section using a PC-relative load.
6296 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6297 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006298
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006299 // Load from the root table associated with the JIT compiled method.
6300 kJitTableAddress,
6301
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006302 // Load using a simple runtime call. This is the fall-back load kind when
6303 // the codegen is unable to use another appropriate kind.
6304 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006305
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006306 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006307 };
6308
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006309 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006310 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006311 const DexFile& dex_file,
6312 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306313 : HInstruction(kLoadString, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006314 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006315 string_index_(string_index),
6316 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006317 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006318 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006319
Artem Serovcced8ba2017-07-19 18:18:09 +01006320 bool IsClonable() const OVERRIDE { return true; }
6321
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006322 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006323
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006324 LoadKind GetLoadKind() const {
6325 return GetPackedField<LoadKindField>();
6326 }
6327
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006328 const DexFile& GetDexFile() const {
6329 return dex_file_;
6330 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006331
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006332 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006333 return string_index_;
6334 }
6335
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006336 Handle<mirror::String> GetString() const {
6337 return string_;
6338 }
6339
6340 void SetString(Handle<mirror::String> str) {
6341 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006342 }
6343
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006344 bool CanBeMoved() const OVERRIDE { return true; }
6345
Vladimir Marko372f10e2016-05-17 16:30:10 +01006346 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006347
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006348 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006349
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006350 // Will call the runtime if we need to load the string through
6351 // the dex cache and the string is not guaranteed to be there yet.
6352 bool NeedsEnvironment() const OVERRIDE {
6353 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006354 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006355 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006356 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006357 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006358 return false;
6359 }
Vladimir Marko4d1be492017-01-06 14:43:11 +00006360 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006361 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006362
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006363 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006364 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006365 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006366
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006367 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006368 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006369
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006370 static SideEffects SideEffectsForArchRuntimeCalls() {
6371 return SideEffects::CanTriggerGC();
6372 }
6373
Vladimir Marko372f10e2016-05-17 16:30:10 +01006374 void AddSpecialInput(HInstruction* special_input);
6375
6376 using HInstruction::GetInputRecords; // Keep the const version visible.
6377 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6378 return ArrayRef<HUserRecord<HInstruction*>>(
6379 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006380 }
6381
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006382 DataType::Type GetType() const OVERRIDE {
6383 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006384 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006385
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006386 DECLARE_INSTRUCTION(LoadString);
6387
Artem Serovcced8ba2017-07-19 18:18:09 +01006388 protected:
6389 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6390
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006391 private:
Vladimir Marko4d1be492017-01-06 14:43:11 +00006392 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006393 static constexpr size_t kFieldLoadKindSize =
6394 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6395 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006396 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006397 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006398
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006399 void SetLoadKindInternal(LoadKind load_kind);
6400
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006401 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006402 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006403 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006404 HUserRecord<HInstruction*> special_input_;
6405
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006406 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006407 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006408
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006409 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006410};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006411std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6412
6413// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006414inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6415 // The load kind should be determined before inserting the instruction to the graph.
6416 DCHECK(GetBlock() == nullptr);
6417 DCHECK(GetEnvironment() == nullptr);
6418 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6419 SetPackedField<LoadKindField>(load_kind);
6420 if (load_kind != LoadKind::kRuntimeCall) {
6421 special_input_ = HUserRecord<HInstruction*>(nullptr);
6422 }
6423 if (!NeedsEnvironment()) {
6424 SetSideEffects(SideEffects::None());
6425 }
6426}
6427
6428// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006429inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006430 // The special input is used for PC-relative loads on some architectures,
6431 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006432 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006433 GetLoadKind() == LoadKind::kBootImageAddress ||
6434 GetLoadKind() == LoadKind::kBootImageInternTable ||
6435 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006436 // HLoadString::GetInputRecords() returns an empty array at this point,
6437 // so use the GetInputRecords() from the base class to set the input record.
6438 DCHECK(special_input_.GetInstruction() == nullptr);
6439 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006440 special_input->AddUseAt(this, 0);
6441}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006442
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006443/**
6444 * Performs an initialization check on its Class object input.
6445 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006446class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006447 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006448 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006449 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306450 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006451 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006452 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006453 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006454 SetRawInputAt(0, constant);
6455 }
6456
Artem Serovcced8ba2017-07-19 18:18:09 +01006457 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006458 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006459 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006460 return true;
6461 }
6462
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006463 bool NeedsEnvironment() const OVERRIDE {
6464 // May call runtime to initialize the class.
6465 return true;
6466 }
6467
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006468 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006469
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006470 HLoadClass* GetLoadClass() const {
6471 DCHECK(InputAt(0)->IsLoadClass());
6472 return InputAt(0)->AsLoadClass();
6473 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006474
6475 DECLARE_INSTRUCTION(ClinitCheck);
6476
Artem Serovcced8ba2017-07-19 18:18:09 +01006477
6478 protected:
6479 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006480};
6481
Vladimir Markofcb503c2016-05-18 12:48:17 +01006482class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006483 public:
6484 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006485 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006486 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006487 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006488 bool is_volatile,
6489 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006490 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006491 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006492 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306493 : HExpression(kStaticFieldGet,
6494 field_type,
6495 SideEffects::FieldReadOfType(field_type, is_volatile),
6496 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006497 field_info_(field,
6498 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006499 field_type,
6500 is_volatile,
6501 field_idx,
6502 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006503 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006504 SetRawInputAt(0, cls);
6505 }
6506
Calin Juravle52c48962014-12-16 17:02:57 +00006507
Artem Serovcced8ba2017-07-19 18:18:09 +01006508 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006509 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006510
Vladimir Marko372f10e2016-05-17 16:30:10 +01006511 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6512 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006513 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006514 }
6515
6516 size_t ComputeHashCode() const OVERRIDE {
6517 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6518 }
6519
Calin Juravle52c48962014-12-16 17:02:57 +00006520 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006521 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006522 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006523 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006524
Vladimir Marko61b92282017-10-11 13:23:17 +01006525 void SetType(DataType::Type new_type) {
6526 DCHECK(DataType::IsIntegralType(GetType()));
6527 DCHECK(DataType::IsIntegralType(new_type));
6528 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6529 SetPackedField<TypeField>(new_type);
6530 }
6531
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006532 DECLARE_INSTRUCTION(StaticFieldGet);
6533
Artem Serovcced8ba2017-07-19 18:18:09 +01006534 protected:
6535 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6536
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006537 private:
6538 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006539};
6540
Vladimir Markofcb503c2016-05-18 12:48:17 +01006541class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006542 public:
6543 HStaticFieldSet(HInstruction* cls,
6544 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006545 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006546 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006547 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006548 bool is_volatile,
6549 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006550 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006551 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006552 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306553 : HTemplateInstruction(kStaticFieldSet,
6554 SideEffects::FieldWriteOfType(field_type, is_volatile),
6555 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006556 field_info_(field,
6557 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006558 field_type,
6559 is_volatile,
6560 field_idx,
6561 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006562 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006563 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006564 SetRawInputAt(0, cls);
6565 SetRawInputAt(1, value);
6566 }
6567
Artem Serovcced8ba2017-07-19 18:18:09 +01006568 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006569 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006570 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006571 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006572 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006573
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006574 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006575 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6576 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006577
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006578 DECLARE_INSTRUCTION(StaticFieldSet);
6579
Artem Serovcced8ba2017-07-19 18:18:09 +01006580 protected:
6581 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6582
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006583 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006584 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6585 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6586 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6587 "Too many packed fields.");
6588
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006589 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006590};
6591
Vladimir Markofcb503c2016-05-18 12:48:17 +01006592class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006593 public:
6594 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006595 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006596 uint32_t field_index,
6597 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306598 : HExpression(kUnresolvedInstanceFieldGet,
6599 field_type,
6600 SideEffects::AllExceptGCDependency(),
6601 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006602 field_index_(field_index) {
6603 SetRawInputAt(0, obj);
6604 }
6605
Artem Serovcced8ba2017-07-19 18:18:09 +01006606 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006607 bool NeedsEnvironment() const OVERRIDE { return true; }
6608 bool CanThrow() const OVERRIDE { return true; }
6609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006610 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006611 uint32_t GetFieldIndex() const { return field_index_; }
6612
6613 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6614
Artem Serovcced8ba2017-07-19 18:18:09 +01006615 protected:
6616 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6617
Calin Juravlee460d1d2015-09-29 04:52:17 +01006618 private:
6619 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006620};
6621
Vladimir Markofcb503c2016-05-18 12:48:17 +01006622class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006623 public:
6624 HUnresolvedInstanceFieldSet(HInstruction* obj,
6625 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006626 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006627 uint32_t field_index,
6628 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306629 : HTemplateInstruction(kUnresolvedInstanceFieldSet,
6630 SideEffects::AllExceptGCDependency(),
6631 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006632 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006633 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006634 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006635 SetRawInputAt(0, obj);
6636 SetRawInputAt(1, value);
6637 }
6638
Artem Serovcced8ba2017-07-19 18:18:09 +01006639 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006640 bool NeedsEnvironment() const OVERRIDE { return true; }
6641 bool CanThrow() const OVERRIDE { return true; }
6642
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006643 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006644 uint32_t GetFieldIndex() const { return field_index_; }
6645
6646 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6647
Artem Serovcced8ba2017-07-19 18:18:09 +01006648 protected:
6649 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6650
Calin Juravlee460d1d2015-09-29 04:52:17 +01006651 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006652 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6653 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006654 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006655 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6656 kFieldFieldType + kFieldFieldTypeSize;
6657 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6658 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006659 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006660
Calin Juravlee460d1d2015-09-29 04:52:17 +01006661 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006662};
6663
Vladimir Markofcb503c2016-05-18 12:48:17 +01006664class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006665 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006666 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006667 uint32_t field_index,
6668 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306669 : HExpression(kUnresolvedStaticFieldGet,
6670 field_type,
6671 SideEffects::AllExceptGCDependency(),
6672 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006673 field_index_(field_index) {
6674 }
6675
Artem Serovcced8ba2017-07-19 18:18:09 +01006676 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006677 bool NeedsEnvironment() const OVERRIDE { return true; }
6678 bool CanThrow() const OVERRIDE { return true; }
6679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006680 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006681 uint32_t GetFieldIndex() const { return field_index_; }
6682
6683 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6684
Artem Serovcced8ba2017-07-19 18:18:09 +01006685 protected:
6686 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6687
Calin Juravlee460d1d2015-09-29 04:52:17 +01006688 private:
6689 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006690};
6691
Vladimir Markofcb503c2016-05-18 12:48:17 +01006692class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006693 public:
6694 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006695 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006696 uint32_t field_index,
6697 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306698 : HTemplateInstruction(kUnresolvedStaticFieldSet,
6699 SideEffects::AllExceptGCDependency(),
6700 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006701 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006702 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006703 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006704 SetRawInputAt(0, value);
6705 }
6706
Artem Serovcced8ba2017-07-19 18:18:09 +01006707 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006708 bool NeedsEnvironment() const OVERRIDE { return true; }
6709 bool CanThrow() const OVERRIDE { return true; }
6710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006711 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006712 uint32_t GetFieldIndex() const { return field_index_; }
6713
6714 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6715
Artem Serovcced8ba2017-07-19 18:18:09 +01006716 protected:
6717 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6718
Calin Juravlee460d1d2015-09-29 04:52:17 +01006719 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006720 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6721 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006722 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006723 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6724 kFieldFieldType + kFieldFieldTypeSize;
6725 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6726 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006728
Calin Juravlee460d1d2015-09-29 04:52:17 +01006729 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006730};
6731
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006732// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006733class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006734 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006735 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306736 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6737 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006738
David Brazdilbbd733e2015-08-18 17:48:17 +01006739 bool CanBeNull() const OVERRIDE { return false; }
6740
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006741 DECLARE_INSTRUCTION(LoadException);
6742
Artem Serovcced8ba2017-07-19 18:18:09 +01006743 protected:
6744 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006745};
6746
David Brazdilcb1c0552015-08-04 16:22:25 +01006747// Implicit part of move-exception which clears thread-local exception storage.
6748// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006749class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006750 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006751 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306752 : HTemplateInstruction(kClearException, SideEffects::AllWrites(), dex_pc) {
6753 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006754
6755 DECLARE_INSTRUCTION(ClearException);
6756
Artem Serovcced8ba2017-07-19 18:18:09 +01006757 protected:
6758 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006759};
6760
Vladimir Markofcb503c2016-05-18 12:48:17 +01006761class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006762 public:
6763 HThrow(HInstruction* exception, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306764 : HTemplateInstruction(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006765 SetRawInputAt(0, exception);
6766 }
6767
6768 bool IsControlFlow() const OVERRIDE { return true; }
6769
6770 bool NeedsEnvironment() const OVERRIDE { return true; }
6771
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006772 bool CanThrow() const OVERRIDE { return true; }
6773
Aart Bika8b8e9b2018-01-09 11:01:02 -08006774 bool AlwaysThrows() const OVERRIDE { return true; }
6775
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006776 DECLARE_INSTRUCTION(Throw);
6777
Artem Serovcced8ba2017-07-19 18:18:09 +01006778 protected:
6779 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006780};
6781
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006782/**
6783 * Implementation strategies for the code generator of a HInstanceOf
6784 * or `HCheckCast`.
6785 */
6786enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006787 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006788 kExactCheck, // Can do a single class compare.
6789 kClassHierarchyCheck, // Can just walk the super class chain.
6790 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6791 kInterfaceCheck, // No optimization yet when checking against an interface.
6792 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006793 kArrayCheck, // No optimization yet when checking against a generic array.
6794 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006795};
6796
Roland Levillain86503782016-02-11 19:07:30 +00006797std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6798
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006799class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006800 public:
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006801 HInstanceOf(HInstruction* object,
6802 HLoadClass* target_class,
6803 TypeCheckKind check_kind,
6804 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306805 : HExpression(kInstanceOf,
6806 DataType::Type::kBool,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006807 SideEffectsForArchRuntimeCalls(check_kind),
6808 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006809 SetPackedField<TypeCheckKindField>(check_kind);
6810 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006811 SetRawInputAt(0, object);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006812 SetRawInputAt(1, target_class);
Vladimir Marko87584542017-12-12 17:47:52 +00006813 }
6814
6815 HLoadClass* GetTargetClass() const {
6816 HInstruction* load_class = InputAt(1);
6817 DCHECK(load_class->IsLoadClass());
6818 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006819 }
6820
Artem Serovcced8ba2017-07-19 18:18:09 +01006821 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006822 bool CanBeMoved() const OVERRIDE { return true; }
6823
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006824 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6825 return true;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006826 }
6827
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00006828 bool NeedsEnvironment() const OVERRIDE {
6829 return CanCallRuntime(GetTypeCheckKind());
6830 }
6831
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006832 // Used only in code generation.
6833 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6834 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6835 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6836 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6837
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006838 static bool CanCallRuntime(TypeCheckKind check_kind) {
6839 // Mips currently does runtime calls for any other checks.
6840 return check_kind != TypeCheckKind::kExactCheck;
6841 }
6842
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006843 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006844 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006845 }
6846
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006847 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006848
Artem Serovcced8ba2017-07-19 18:18:09 +01006849 protected:
6850 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006851
6852 private:
6853 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6854 static constexpr size_t kFieldTypeCheckKindSize =
6855 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6856 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6857 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6858 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6859 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006860};
6861
Vladimir Markofcb503c2016-05-18 12:48:17 +01006862class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006863 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006864 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306865 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006866 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6867 SetPackedFlag<kFlagUpperCanBeNull>(true);
6868 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006869 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006870 SetRawInputAt(0, input);
6871 }
6872
Artem Serovcced8ba2017-07-19 18:18:09 +01006873 bool IsClonable() const OVERRIDE { return true; }
6874
David Brazdilf5552582015-12-27 13:36:12 +00006875 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006876 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006877 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006878 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006879
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006880 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006881 DCHECK(GetUpperCanBeNull() || !can_be_null);
6882 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006883 }
6884
Vladimir Markoa1de9182016-02-25 11:37:38 +00006885 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006886
Calin Juravleb1498f62015-02-16 13:13:29 +00006887 DECLARE_INSTRUCTION(BoundType);
6888
Artem Serovcced8ba2017-07-19 18:18:09 +01006889 protected:
6890 DEFAULT_COPY_CONSTRUCTOR(BoundType);
6891
Calin Juravleb1498f62015-02-16 13:13:29 +00006892 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006893 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6894 // is false then CanBeNull() cannot be true).
6895 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6896 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6897 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6898 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6899
Calin Juravleb1498f62015-02-16 13:13:29 +00006900 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006901 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6902 // It is used to bound the type in cases like:
6903 // if (x instanceof ClassX) {
6904 // // uper_bound_ will be ClassX
6905 // }
David Brazdilf5552582015-12-27 13:36:12 +00006906 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006907};
6908
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006909class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006910 public:
6911 HCheckCast(HInstruction* object,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006912 HLoadClass* target_class,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006913 TypeCheckKind check_kind,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006914 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306915 : HTemplateInstruction(kCheckCast, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006916 SetPackedField<TypeCheckKindField>(check_kind);
6917 SetPackedFlag<kFlagMustDoNullCheck>(true);
6918 SetRawInputAt(0, object);
6919 SetRawInputAt(1, target_class);
6920 }
6921
6922 HLoadClass* GetTargetClass() const {
6923 HInstruction* load_class = InputAt(1);
6924 DCHECK(load_class->IsLoadClass());
6925 return load_class->AsLoadClass();
6926 }
6927
6928 bool IsClonable() const OVERRIDE { return true; }
6929 bool CanBeMoved() const OVERRIDE { return true; }
6930
6931 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6932 return true;
6933 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006934
6935 bool NeedsEnvironment() const OVERRIDE {
6936 // Instruction may throw a CheckCastError.
6937 return true;
6938 }
6939
6940 bool CanThrow() const OVERRIDE { return true; }
6941
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006942 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6943 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6944 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6945 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6946
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006947 DECLARE_INSTRUCTION(CheckCast);
6948
Artem Serovcced8ba2017-07-19 18:18:09 +01006949 protected:
6950 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006951
6952 private:
6953 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6954 static constexpr size_t kFieldTypeCheckKindSize =
6955 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6956 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6957 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6958 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6959 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006960};
6961
Andreas Gampe26de38b2016-07-27 17:53:11 -07006962/**
6963 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6964 * @details We define the combined barrier types that are actually required
6965 * by the Java Memory Model, rather than using exactly the terminology from
6966 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6967 * primitives. Note that the JSR-133 cookbook generally does not deal with
6968 * store atomicity issues, and the recipes there are not always entirely sufficient.
6969 * The current recipe is as follows:
6970 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6971 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6972 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6973 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6974 * class has final fields.
6975 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6976 * store-to-memory instructions. Only generated together with non-temporal stores.
6977 */
6978enum MemBarrierKind {
6979 kAnyStore,
6980 kLoadAny,
6981 kStoreStore,
6982 kAnyAny,
6983 kNTStoreStore,
6984 kLastBarrierKind = kNTStoreStore
6985};
6986std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6987
Vladimir Markofcb503c2016-05-18 12:48:17 +01006988class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006989 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006990 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006991 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306992 kMemoryBarrier,
6993 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
6994 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006995 SetPackedField<BarrierKindField>(barrier_kind);
6996 }
Calin Juravle27df7582015-04-17 19:12:31 +01006997
Artem Serovcced8ba2017-07-19 18:18:09 +01006998 bool IsClonable() const OVERRIDE { return true; }
6999
Vladimir Markoa1de9182016-02-25 11:37:38 +00007000 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007001
7002 DECLARE_INSTRUCTION(MemoryBarrier);
7003
Artem Serovcced8ba2017-07-19 18:18:09 +01007004 protected:
7005 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7006
Calin Juravle27df7582015-04-17 19:12:31 +01007007 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007008 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7009 static constexpr size_t kFieldBarrierKindSize =
7010 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7011 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7012 kFieldBarrierKind + kFieldBarrierKindSize;
7013 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7014 "Too many packed fields.");
7015 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007016};
7017
Igor Murashkind01745e2017-04-05 16:40:31 -07007018// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7019// the specified object(s), with respect to any subsequent store that might "publish"
7020// (i.e. make visible) the specified object to another thread.
7021//
7022// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7023// for all final fields (that were set) at the end of the invoked constructor.
7024//
7025// The constructor fence models the freeze actions for the final fields of an object
7026// being constructed (semantically at the end of the constructor). Constructor fences
7027// have a per-object affinity; two separate objects being constructed get two separate
7028// constructor fences.
7029//
7030// (Note: that if calling a super-constructor or forwarding to another constructor,
7031// the freezes would happen at the end of *that* constructor being invoked).
7032//
7033// The memory model guarantees that when the object being constructed is "published" after
7034// constructor completion (i.e. escapes the current thread via a store), then any final field
7035// writes must be observable on other threads (once they observe that publication).
7036//
7037// Further, anything written before the freeze, and read by dereferencing through the final field,
7038// must also be visible (so final object field could itself have an object with non-final fields;
7039// yet the freeze must also extend to them).
7040//
7041// Constructor example:
7042//
7043// class HasFinal {
7044// final int field; Optimizing IR for <init>()V:
7045// HasFinal() {
7046// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7047// // freeze(this.field); HConstructorFence(this)
7048// } HReturn
7049// }
7050//
7051// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7052// already-initialized classes; in that case the allocation must act as a "default-initializer"
7053// of the object which effectively writes the class pointer "final field".
7054//
7055// For example, we can model default-initialiation as roughly the equivalent of the following:
7056//
7057// class Object {
7058// private final Class header;
7059// }
7060//
7061// Java code: Optimizing IR:
7062//
7063// T new_instance<T>() {
7064// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7065// obj.header = T.class; // header write is done by above call.
7066// // freeze(obj.header) HConstructorFence(obj)
7067// return (T)obj;
7068// }
7069//
7070// See also:
7071// * CompilerDriver::RequiresConstructorBarrier
7072// * QuasiAtomic::ThreadFenceForConstructor
7073//
7074class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7075 // A fence has variable inputs because the inputs can be removed
7076 // after prepare_for_register_allocation phase.
7077 // (TODO: In the future a fence could freeze multiple objects
7078 // after merging two fences together.)
7079 public:
7080 // `fence_object` is the reference that needs to be protected for correct publication.
7081 //
7082 // It makes sense in the following situations:
7083 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7084 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7085 //
7086 // After construction the `fence_object` becomes the 0th input.
7087 // This is not an input in a real sense, but just a convenient place to stash the information
7088 // about the associated object.
7089 HConstructorFence(HInstruction* fence_object,
7090 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007091 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007092 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7093 // constraints described in the class header. We claim that these SideEffects constraints
7094 // enforce a superset of the real constraints.
7095 //
7096 // The ordering described above is conservatively modeled with SideEffects as follows:
7097 //
7098 // * To prevent reordering of the publication stores:
7099 // ----> "Reads of objects" is the initial SideEffect.
7100 // * For every primitive final field store in the constructor:
7101 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7102 // * If there are any stores to reference final fields in the constructor:
7103 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7104 // that are reachable from `fence_object` also need to be prevented for reordering
7105 // (and we do not want to do alias analysis to figure out what those stores are).
7106 //
7107 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7108 // even more conservative approach than the one described above, and prevents all of the
7109 // above reordering without analyzing any of the instructions in the constructor.
7110 //
7111 // If in a later phase we discover that there are no writes to reference final fields,
7112 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307113 : HVariableInputSizeInstruction(kConstructorFence,
7114 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007115 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007116 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007117 /* number_of_inputs */ 1,
7118 kArenaAllocConstructorFenceInputs) {
7119 DCHECK(fence_object != nullptr);
7120 SetRawInputAt(0, fence_object);
7121 }
7122
7123 // The object associated with this constructor fence.
7124 //
7125 // (Note: This will be null after the prepare_for_register_allocation phase,
7126 // as all constructor fence inputs are removed there).
7127 HInstruction* GetFenceObject() const {
7128 return InputAt(0);
7129 }
7130
7131 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7132 // - Delete `fence_use` from `this`'s use list.
7133 // - Delete `this` from `fence_use`'s inputs list.
7134 // - If the `fence_use` is dead, remove it from the graph.
7135 //
7136 // A fence is considered dead once it no longer has any uses
7137 // and all of the inputs are dead.
7138 //
7139 // This must *not* be called during/after prepare_for_register_allocation,
7140 // because that removes all the inputs to the fences but the fence is actually
7141 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007142 //
7143 // Returns how many HConstructorFence instructions were removed from graph.
7144 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007145
Igor Murashkindd018df2017-08-09 10:38:31 -07007146 // Combine all inputs of `this` and `other` instruction and remove
7147 // `other` from the graph.
7148 //
7149 // Inputs are unique after the merge.
7150 //
7151 // Requirement: `this` must not be the same as `other.
7152 void Merge(HConstructorFence* other);
7153
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007154 // Check if this constructor fence is protecting
7155 // an HNewInstance or HNewArray that is also the immediate
7156 // predecessor of `this`.
7157 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007158 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7159 // to be one of the inputs of `this`.
7160 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007161 // Returns the associated HNewArray or HNewInstance,
7162 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007163 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007164
Igor Murashkind01745e2017-04-05 16:40:31 -07007165 DECLARE_INSTRUCTION(ConstructorFence);
7166
Artem Serovcced8ba2017-07-19 18:18:09 +01007167 protected:
7168 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007169};
7170
Vladimir Markofcb503c2016-05-18 12:48:17 +01007171class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007172 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007173 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007174 kEnter,
7175 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007176 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007177 };
7178
7179 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007180 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307181 kMonitorOperation,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007182 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7183 dex_pc) {
7184 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007185 SetRawInputAt(0, object);
7186 }
7187
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007188 // Instruction may go into runtime, so we need an environment.
7189 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007190
7191 bool CanThrow() const OVERRIDE {
7192 // Verifier guarantees that monitor-exit cannot throw.
7193 // This is important because it allows the HGraphBuilder to remove
7194 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7195 return IsEnter();
7196 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007197
Vladimir Markoa1de9182016-02-25 11:37:38 +00007198 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7199 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007200
7201 DECLARE_INSTRUCTION(MonitorOperation);
7202
Artem Serovcced8ba2017-07-19 18:18:09 +01007203 protected:
7204 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7205
Calin Juravle52c48962014-12-16 17:02:57 +00007206 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007207 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7208 static constexpr size_t kFieldOperationKindSize =
7209 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7210 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7211 kFieldOperationKind + kFieldOperationKindSize;
7212 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7213 "Too many packed fields.");
7214 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007215};
7216
Vladimir Markofcb503c2016-05-18 12:48:17 +01007217class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007218 public:
7219 HSelect(HInstruction* condition,
7220 HInstruction* true_value,
7221 HInstruction* false_value,
7222 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307223 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007224 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7225
7226 // First input must be `true_value` or `false_value` to allow codegens to
7227 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7228 // that architectures which implement HSelect as a conditional move also
7229 // will not need to invert the condition.
7230 SetRawInputAt(0, false_value);
7231 SetRawInputAt(1, true_value);
7232 SetRawInputAt(2, condition);
7233 }
7234
Artem Serovcced8ba2017-07-19 18:18:09 +01007235 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007236 HInstruction* GetFalseValue() const { return InputAt(0); }
7237 HInstruction* GetTrueValue() const { return InputAt(1); }
7238 HInstruction* GetCondition() const { return InputAt(2); }
7239
7240 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007241 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7242 return true;
7243 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007244
7245 bool CanBeNull() const OVERRIDE {
7246 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7247 }
7248
7249 DECLARE_INSTRUCTION(Select);
7250
Artem Serovcced8ba2017-07-19 18:18:09 +01007251 protected:
7252 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007253};
7254
Vladimir Markof9f64412015-09-02 14:05:49 +01007255class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007256 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007257 MoveOperands(Location source,
7258 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007259 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007260 HInstruction* instruction)
7261 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007262
7263 Location GetSource() const { return source_; }
7264 Location GetDestination() const { return destination_; }
7265
7266 void SetSource(Location value) { source_ = value; }
7267 void SetDestination(Location value) { destination_ = value; }
7268
7269 // The parallel move resolver marks moves as "in-progress" by clearing the
7270 // destination (but not the source).
7271 Location MarkPending() {
7272 DCHECK(!IsPending());
7273 Location dest = destination_;
7274 destination_ = Location::NoLocation();
7275 return dest;
7276 }
7277
7278 void ClearPending(Location dest) {
7279 DCHECK(IsPending());
7280 destination_ = dest;
7281 }
7282
7283 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007284 DCHECK(source_.IsValid() || destination_.IsInvalid());
7285 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007286 }
7287
7288 // True if this blocks a move from the given location.
7289 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007290 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007291 }
7292
7293 // A move is redundant if it's been eliminated, if its source and
7294 // destination are the same, or if its destination is unneeded.
7295 bool IsRedundant() const {
7296 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7297 }
7298
7299 // We clear both operands to indicate move that's been eliminated.
7300 void Eliminate() {
7301 source_ = destination_ = Location::NoLocation();
7302 }
7303
7304 bool IsEliminated() const {
7305 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7306 return source_.IsInvalid();
7307 }
7308
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007309 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007310
Nicolas Geoffray90218252015-04-15 11:56:51 +01007311 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007312 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007313 }
7314
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007315 HInstruction* GetInstruction() const { return instruction_; }
7316
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007317 private:
7318 Location source_;
7319 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007320 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007321 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007322 // The instruction this move is assocatied with. Null when this move is
7323 // for moving an input in the expected locations of user (including a phi user).
7324 // This is only used in debug mode, to ensure we do not connect interval siblings
7325 // in the same parallel move.
7326 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007327};
7328
Roland Levillainc9285912015-12-18 10:38:42 +00007329std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7330
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007331static constexpr size_t kDefaultNumberOfMoves = 4;
7332
Vladimir Markofcb503c2016-05-18 12:48:17 +01007333class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007334 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007335 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307336 : HTemplateInstruction(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007337 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007338 moves_.reserve(kDefaultNumberOfMoves);
7339 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007340
Nicolas Geoffray90218252015-04-15 11:56:51 +01007341 void AddMove(Location source,
7342 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007343 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007344 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007345 DCHECK(source.IsValid());
7346 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007347 if (kIsDebugBuild) {
7348 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007349 for (const MoveOperands& move : moves_) {
7350 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007351 // Special case the situation where the move is for the spill slot
7352 // of the instruction.
7353 if ((GetPrevious() == instruction)
7354 || ((GetPrevious() == nullptr)
7355 && instruction->IsPhi()
7356 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007357 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007358 << "Doing parallel moves for the same instruction.";
7359 } else {
7360 DCHECK(false) << "Doing parallel moves for the same instruction.";
7361 }
7362 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007363 }
7364 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007365 for (const MoveOperands& move : moves_) {
7366 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007367 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007368 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007369 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007370 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007371 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007372 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007373 }
7374
Vladimir Marko225b6462015-09-28 12:17:40 +01007375 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007376 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007377 }
7378
Vladimir Marko225b6462015-09-28 12:17:40 +01007379 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007380
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007381 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007382
Artem Serovcced8ba2017-07-19 18:18:09 +01007383 protected:
7384 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7385
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007386 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007387 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007388};
7389
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007390// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7391// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7392// never used across anything that can trigger GC.
7393// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7394// So we represent it by the type `DataType::Type::kInt`.
7395class HIntermediateAddress FINAL : public HExpression<2> {
7396 public:
7397 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307398 : HExpression(kIntermediateAddress,
7399 DataType::Type::kInt32,
7400 SideEffects::DependsOnGC(),
7401 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007402 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7403 DataType::Size(DataType::Type::kReference))
7404 << "kPrimInt and kPrimNot have different sizes.";
7405 SetRawInputAt(0, base_address);
7406 SetRawInputAt(1, offset);
7407 }
7408
Artem Serovcced8ba2017-07-19 18:18:09 +01007409 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007410 bool CanBeMoved() const OVERRIDE { return true; }
7411 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7412 return true;
7413 }
7414 bool IsActualObject() const OVERRIDE { return false; }
7415
7416 HInstruction* GetBaseAddress() const { return InputAt(0); }
7417 HInstruction* GetOffset() const { return InputAt(1); }
7418
7419 DECLARE_INSTRUCTION(IntermediateAddress);
7420
Artem Serovcced8ba2017-07-19 18:18:09 +01007421 protected:
7422 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007423};
7424
7425
Mark Mendell0616ae02015-04-17 12:49:27 -04007426} // namespace art
7427
Aart Bikf8f5a162017-02-06 15:35:29 -08007428#include "nodes_vector.h"
7429
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007430#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7431#include "nodes_shared.h"
7432#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007433#ifdef ART_ENABLE_CODEGEN_mips
7434#include "nodes_mips.h"
7435#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007436#ifdef ART_ENABLE_CODEGEN_x86
7437#include "nodes_x86.h"
7438#endif
7439
7440namespace art {
7441
Igor Murashkin6ef45672017-08-08 13:59:55 -07007442class OptimizingCompilerStats;
7443
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007444class HGraphVisitor : public ValueObject {
7445 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007446 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7447 : stats_(stats),
7448 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007449 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007450
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007451 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007452 virtual void VisitBasicBlock(HBasicBlock* block);
7453
Roland Levillain633021e2014-10-01 14:12:25 +01007454 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007455 void VisitInsertionOrder();
7456
Roland Levillain633021e2014-10-01 14:12:25 +01007457 // Visit the graph following dominator tree reverse post-order.
7458 void VisitReversePostOrder();
7459
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007460 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007461
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007462 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007463#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007464 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7465
7466 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7467
7468#undef DECLARE_VISIT_INSTRUCTION
7469
Igor Murashkin6ef45672017-08-08 13:59:55 -07007470 protected:
7471 OptimizingCompilerStats* stats_;
7472
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007473 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007474 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007475
7476 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7477};
7478
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007479class HGraphDelegateVisitor : public HGraphVisitor {
7480 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007481 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7482 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007483 virtual ~HGraphDelegateVisitor() {}
7484
7485 // Visit functions that delegate to to super class.
7486#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007487 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007488
7489 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7490
7491#undef DECLARE_VISIT_INSTRUCTION
7492
7493 private:
7494 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7495};
7496
Artem Serovcced8ba2017-07-19 18:18:09 +01007497// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7498// and remove the old instruction.
7499HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7500
7501// Create a clone for each clonable instructions/phis and replace the original with the clone.
7502//
7503// Used for testing individual instruction cloner.
7504class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7505 public:
7506 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007507 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007508
7509 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7510 if (instruction->IsClonable()) {
7511 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007512 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007513 }
7514 }
7515
Artem Serov7f4aff62017-06-21 17:02:18 +01007516 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007517
7518 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007519 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007520
7521 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7522};
7523
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007524// Iterator over the blocks that art part of the loop. Includes blocks part
7525// of an inner loop. The order in which the blocks are iterated is on their
7526// block id.
7527class HBlocksInLoopIterator : public ValueObject {
7528 public:
7529 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7530 : blocks_in_loop_(info.GetBlocks()),
7531 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7532 index_(0) {
7533 if (!blocks_in_loop_.IsBitSet(index_)) {
7534 Advance();
7535 }
7536 }
7537
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007538 bool Done() const { return index_ == blocks_.size(); }
7539 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007540 void Advance() {
7541 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007542 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007543 if (blocks_in_loop_.IsBitSet(index_)) {
7544 break;
7545 }
7546 }
7547 }
7548
7549 private:
7550 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007551 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007552 size_t index_;
7553
7554 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7555};
7556
Mingyao Yang3584bce2015-05-19 16:01:59 -07007557// Iterator over the blocks that art part of the loop. Includes blocks part
7558// of an inner loop. The order in which the blocks are iterated is reverse
7559// post order.
7560class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7561 public:
7562 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7563 : blocks_in_loop_(info.GetBlocks()),
7564 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7565 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007566 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007567 Advance();
7568 }
7569 }
7570
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007571 bool Done() const { return index_ == blocks_.size(); }
7572 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007573 void Advance() {
7574 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007575 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7576 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007577 break;
7578 }
7579 }
7580 }
7581
7582 private:
7583 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007584 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007585 size_t index_;
7586
7587 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7588};
7589
Aart Bikf3e61ee2017-04-12 17:09:20 -07007590// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007591inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007592 if (constant->IsIntConstant()) {
7593 return constant->AsIntConstant()->GetValue();
7594 } else if (constant->IsLongConstant()) {
7595 return constant->AsLongConstant()->GetValue();
7596 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007597 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007598 return 0;
7599 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007600}
7601
Aart Bikf3e61ee2017-04-12 17:09:20 -07007602// Returns true iff instruction is an integral constant (and sets value on success).
7603inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7604 if (instruction->IsIntConstant()) {
7605 *value = instruction->AsIntConstant()->GetValue();
7606 return true;
7607 } else if (instruction->IsLongConstant()) {
7608 *value = instruction->AsLongConstant()->GetValue();
7609 return true;
7610 } else if (instruction->IsNullConstant()) {
7611 *value = 0;
7612 return true;
7613 }
7614 return false;
7615}
7616
Aart Bik0148de42017-09-05 09:25:01 -07007617// Returns true iff instruction is the given integral constant.
7618inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7619 int64_t val = 0;
7620 return IsInt64AndGet(instruction, &val) && val == value;
7621}
7622
7623// Returns true iff instruction is a zero bit pattern.
7624inline bool IsZeroBitPattern(HInstruction* instruction) {
7625 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7626}
7627
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007628#define INSTRUCTION_TYPE_CHECK(type, super) \
7629 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7630 inline const H##type* HInstruction::As##type() const { \
7631 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7632 } \
7633 inline H##type* HInstruction::As##type() { \
7634 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7635 }
7636
7637 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7638#undef INSTRUCTION_TYPE_CHECK
7639
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007640// Create space in `blocks` for adding `number_of_new_blocks` entries
7641// starting at location `at`. Blocks after `at` are moved accordingly.
7642inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7643 size_t number_of_new_blocks,
7644 size_t after) {
7645 DCHECK_LT(after, blocks->size());
7646 size_t old_size = blocks->size();
7647 size_t new_size = old_size + number_of_new_blocks;
7648 blocks->resize(new_size);
7649 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7650}
7651
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007652/*
7653 * Hunt "under the hood" of array lengths (leading to array references),
7654 * null checks (also leading to array references), and new arrays
7655 * (leading to the actual length). This makes it more likely related
7656 * instructions become actually comparable.
7657 */
7658inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7659 while (instruction->IsArrayLength() ||
7660 instruction->IsNullCheck() ||
7661 instruction->IsNewArray()) {
7662 instruction = instruction->IsNewArray()
7663 ? instruction->AsNewArray()->GetLength()
7664 : instruction->InputAt(0);
7665 }
7666 return instruction;
7667}
7668
Artem Serov21c7e6f2017-07-27 16:04:42 +01007669void RemoveEnvironmentUses(HInstruction* instruction);
7670bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7671void ResetEnvironmentInputRecords(HInstruction* instruction);
7672
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007673} // namespace art
7674
7675#endif // ART_COMPILER_OPTIMIZING_NODES_H_