blob: 0534685e5f61ca9095eb6d8cf274db2e07b2882c [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) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001340 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001341 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(ArrayGet, Instruction) \
1343 M(ArrayLength, Instruction) \
1344 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001345 M(Below, Condition) \
1346 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001347 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001349 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001350 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001351 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001352 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001353 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001354 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001355 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001356 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001357 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001358 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001359 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001360 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001361 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001362 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(Exit, Instruction) \
1364 M(FloatConstant, Constant) \
1365 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(GreaterThan, Condition) \
1367 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001369 M(InstanceFieldGet, Instruction) \
1370 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001371 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001373 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001374 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001375 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001376 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001377 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001378 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001379 M(LessThan, Condition) \
1380 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001381 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001382 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001383 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001384 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001385 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001386 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001387 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001388 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001389 M(Neg, UnaryOperation) \
1390 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001391 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001392 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001393 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001394 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001395 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001396 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001397 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001398 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001400 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001401 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(Return, Instruction) \
1403 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001404 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001405 M(Shl, BinaryOperation) \
1406 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001407 M(StaticFieldGet, Instruction) \
1408 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001409 M(UnresolvedInstanceFieldGet, Instruction) \
1410 M(UnresolvedInstanceFieldSet, Instruction) \
1411 M(UnresolvedStaticFieldGet, Instruction) \
1412 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001413 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001415 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001416 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001417 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001418 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001419 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001420 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001421 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001422 M(VecExtractScalar, VecUnaryOperation) \
1423 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001424 M(VecCnv, VecUnaryOperation) \
1425 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001426 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001427 M(VecNot, VecUnaryOperation) \
1428 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001429 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001430 M(VecSub, VecBinaryOperation) \
1431 M(VecMul, VecBinaryOperation) \
1432 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001433 M(VecMin, VecBinaryOperation) \
1434 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001435 M(VecAnd, VecBinaryOperation) \
1436 M(VecAndNot, VecBinaryOperation) \
1437 M(VecOr, VecBinaryOperation) \
1438 M(VecXor, VecBinaryOperation) \
1439 M(VecShl, VecBinaryOperation) \
1440 M(VecShr, VecBinaryOperation) \
1441 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001442 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001443 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001444 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001445 M(VecLoad, VecMemoryOperation) \
1446 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001447
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001448/*
1449 * Instructions, shared across several (not all) architectures.
1450 */
1451#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1452#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1453#else
1454#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001455 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001456 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001457 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001458 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001459#endif
1460
Alexandre Ramesef20f712015-06-09 10:29:30 +01001461#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1462
1463#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1464
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001465#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001466#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001467#else
1468#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1469 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001470 M(MipsPackedSwitch, Instruction) \
1471 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001472#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001473
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001474#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1475
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001476#ifndef ART_ENABLE_CODEGEN_x86
1477#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1478#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001479#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1480 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001481 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001482 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001483 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001484#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001485
1486#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1487
1488#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1489 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001490 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001491 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1492 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001493 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001494 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001495 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1496 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1497
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001498#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1499 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001500 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001501 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001502 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001503 M(Invoke, Instruction) \
1504 M(VecOperation, Instruction) \
1505 M(VecUnaryOperation, VecOperation) \
1506 M(VecBinaryOperation, VecOperation) \
1507 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001508
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001509#define FOR_EACH_INSTRUCTION(M) \
1510 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1511 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1512
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001513#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001514FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1515#undef FORWARD_DECLARATION
1516
Vladimir Marko372f10e2016-05-17 16:30:10 +01001517#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001518 private: \
1519 H##type& operator=(const H##type&) = delete; \
1520 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001521 const char* DebugName() const OVERRIDE { return #type; } \
1522 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1523 return other->Is##type(); \
1524 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001525 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1526 DCHECK(IsClonable()); \
1527 return new (arena) H##type(*this->As##type()); \
1528 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001529 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001530
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001531#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001532 private: \
1533 H##type& operator=(const H##type&) = delete; \
1534 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001535 bool Is##type() const { return As##type() != nullptr; } \
1536 const H##type* As##type() const { return this; } \
1537 H##type* As##type() { return this; }
1538
Artem Serovcced8ba2017-07-19 18:18:09 +01001539#define DEFAULT_COPY_CONSTRUCTOR(type) \
1540 explicit H##type(const H##type& other) = default;
1541
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001542template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001543class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1544 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001545 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001546 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001547 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001548 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001549 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001550 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001551 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001553 private:
David Brazdiled596192015-01-23 10:39:45 +00001554 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001555 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001556
1557 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001558 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001559
Vladimir Marko46817b82016-03-29 12:21:58 +01001560 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001561
1562 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1563};
1564
David Brazdiled596192015-01-23 10:39:45 +00001565template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001566using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001567
David Brazdil1abb4192015-02-17 18:33:36 +00001568// This class is used by HEnvironment and HInstruction classes to record the
1569// instructions they use and pointers to the corresponding HUseListNodes kept
1570// by the used instructions.
1571template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001572class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001573 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001574 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1575 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001576
Vladimir Marko46817b82016-03-29 12:21:58 +01001577 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1578 : HUserRecord(old_record.instruction_, before_use_node) {}
1579 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1580 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001581 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001582 }
1583
1584 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001585 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1586 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001587
1588 private:
1589 // Instruction used by the user.
1590 HInstruction* instruction_;
1591
Vladimir Marko46817b82016-03-29 12:21:58 +01001592 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1593 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001594};
1595
Vladimir Markoe9004912016-06-16 16:50:52 +01001596// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1597// This is used for HInstruction::GetInputs() to return a container wrapper providing
1598// HInstruction* values even though the underlying container has HUserRecord<>s.
1599struct HInputExtractor {
1600 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1601 return record.GetInstruction();
1602 }
1603 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1604 return record.GetInstruction();
1605 }
1606};
1607
1608using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1609using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1610
Aart Bik854a02b2015-07-14 16:07:00 -07001611/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001612 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001613 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001614 * For write/read dependences on fields/arrays, the dependence analysis uses
1615 * type disambiguation (e.g. a float field write cannot modify the value of an
1616 * integer field read) and the access type (e.g. a reference array write cannot
1617 * modify the value of a reference field read [although it may modify the
1618 * reference fetch prior to reading the field, which is represented by its own
1619 * write/read dependence]). The analysis makes conservative points-to
1620 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1621 * the same, and any reference read depends on any reference read without
1622 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001623 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001624 * The internal representation uses 38-bit and is described in the table below.
1625 * The first line indicates the side effect, and for field/array accesses the
1626 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001627 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 * The two numbered lines below indicate the bit position in the bitfield (read
1629 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001630 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1632 * +-------------+---------+---------+--------------+---------+---------+
1633 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1634 * | 3 |333333322|222222221| 1 |111111110|000000000|
1635 * | 7 |654321098|765432109| 8 |765432109|876543210|
1636 *
1637 * Note that, to ease the implementation, 'changes' bits are least significant
1638 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001639 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001640class SideEffects : public ValueObject {
1641 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001642 SideEffects() : flags_(0) {}
1643
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001644 static SideEffects None() {
1645 return SideEffects(0);
1646 }
1647
1648 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001649 return SideEffects(kAllChangeBits | kAllDependOnBits);
1650 }
1651
1652 static SideEffects AllChanges() {
1653 return SideEffects(kAllChangeBits);
1654 }
1655
1656 static SideEffects AllDependencies() {
1657 return SideEffects(kAllDependOnBits);
1658 }
1659
1660 static SideEffects AllExceptGCDependency() {
1661 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1662 }
1663
1664 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001665 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001666 }
1667
Aart Bik34c3ba92015-07-20 14:08:59 -07001668 static SideEffects AllWrites() {
1669 return SideEffects(kAllWrites);
1670 }
1671
1672 static SideEffects AllReads() {
1673 return SideEffects(kAllReads);
1674 }
1675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001676 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001677 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001678 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001679 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001680 }
1681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001682 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001683 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684 }
1685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001687 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001688 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001689 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001690 }
1691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001692 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001693 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001694 }
1695
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001696 static SideEffects CanTriggerGC() {
1697 return SideEffects(1ULL << kCanTriggerGCBit);
1698 }
1699
1700 static SideEffects DependsOnGC() {
1701 return SideEffects(1ULL << kDependsOnGCBit);
1702 }
1703
Aart Bik854a02b2015-07-14 16:07:00 -07001704 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001705 SideEffects Union(SideEffects other) const {
1706 return SideEffects(flags_ | other.flags_);
1707 }
1708
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001709 SideEffects Exclusion(SideEffects other) const {
1710 return SideEffects(flags_ & ~other.flags_);
1711 }
1712
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001713 void Add(SideEffects other) {
1714 flags_ |= other.flags_;
1715 }
1716
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001717 bool Includes(SideEffects other) const {
1718 return (other.flags_ & flags_) == other.flags_;
1719 }
1720
1721 bool HasSideEffects() const {
1722 return (flags_ & kAllChangeBits);
1723 }
1724
1725 bool HasDependencies() const {
1726 return (flags_ & kAllDependOnBits);
1727 }
1728
1729 // Returns true if there are no side effects or dependencies.
1730 bool DoesNothing() const {
1731 return flags_ == 0;
1732 }
1733
Aart Bik854a02b2015-07-14 16:07:00 -07001734 // Returns true if something is written.
1735 bool DoesAnyWrite() const {
1736 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001737 }
1738
Aart Bik854a02b2015-07-14 16:07:00 -07001739 // Returns true if something is read.
1740 bool DoesAnyRead() const {
1741 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001742 }
1743
Aart Bik854a02b2015-07-14 16:07:00 -07001744 // Returns true if potentially everything is written and read
1745 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001746 bool DoesAllReadWrite() const {
1747 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1748 }
1749
Aart Bik854a02b2015-07-14 16:07:00 -07001750 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001751 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001752 }
1753
Roland Levillain0d5a2812015-11-13 10:07:31 +00001754 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001755 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1757 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001758 }
1759
1760 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001761 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001762 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001763 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001764 for (int s = kLastBit; s >= 0; s--) {
1765 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1766 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1767 // This is a bit for the GC side effect.
1768 if (current_bit_is_set) {
1769 flags += "GC";
1770 }
Aart Bik854a02b2015-07-14 16:07:00 -07001771 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001772 } else {
1773 // This is a bit for the array/field analysis.
1774 // The underscore character stands for the 'can trigger GC' bit.
1775 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1776 if (current_bit_is_set) {
1777 flags += kDebug[s];
1778 }
1779 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1780 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1781 flags += "|";
1782 }
1783 }
Aart Bik854a02b2015-07-14 16:07:00 -07001784 }
1785 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001786 }
1787
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001788 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 private:
1791 static constexpr int kFieldArrayAnalysisBits = 9;
1792
1793 static constexpr int kFieldWriteOffset = 0;
1794 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1795 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1796 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1797
1798 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1799
1800 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1801 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1802 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1803 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1804
1805 static constexpr int kLastBit = kDependsOnGCBit;
1806 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1807
1808 // Aliases.
1809
1810 static_assert(kChangeBits == kDependOnBits,
1811 "the 'change' bits should match the 'depend on' bits.");
1812
1813 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1814 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1815 static constexpr uint64_t kAllWrites =
1816 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1817 static constexpr uint64_t kAllReads =
1818 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001819
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001820 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001821 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001822 int shift;
1823 switch (type) {
1824 case DataType::Type::kReference: shift = 0; break;
1825 case DataType::Type::kBool: shift = 1; break;
1826 case DataType::Type::kInt8: shift = 2; break;
1827 case DataType::Type::kUint16: shift = 3; break;
1828 case DataType::Type::kInt16: shift = 4; break;
1829 case DataType::Type::kInt32: shift = 5; break;
1830 case DataType::Type::kInt64: shift = 6; break;
1831 case DataType::Type::kFloat32: shift = 7; break;
1832 case DataType::Type::kFloat64: shift = 8; break;
1833 default:
1834 LOG(FATAL) << "Unexpected data type " << type;
1835 UNREACHABLE();
1836 }
Aart Bik854a02b2015-07-14 16:07:00 -07001837 DCHECK_LE(kFieldWriteOffset, shift);
1838 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001839 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001840 }
1841
1842 // Private constructor on direct flags value.
1843 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1844
1845 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001846};
1847
David Brazdiled596192015-01-23 10:39:45 +00001848// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001849class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001850 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001851 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001852 size_t number_of_vregs,
1853 ArtMethod* method,
1854 uint32_t dex_pc,
1855 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001856 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1857 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001858 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001859 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001860 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001861 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001862 }
1863
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001864 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1865 const HEnvironment& to_copy,
1866 HInstruction* holder)
1867 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001868 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001869 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001870 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001871 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001872
Vladimir Markoec32f642017-06-02 10:51:55 +01001873 void AllocateLocations() {
1874 DCHECK(locations_.empty());
1875 locations_.resize(vregs_.size());
1876 }
1877
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001878 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001879 if (parent_ != nullptr) {
1880 parent_->SetAndCopyParentChain(allocator, parent);
1881 } else {
1882 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1883 parent_->CopyFrom(parent);
1884 if (parent->GetParent() != nullptr) {
1885 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1886 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001887 }
David Brazdiled596192015-01-23 10:39:45 +00001888 }
1889
Vladimir Marko69d310e2017-10-09 14:12:23 +01001890 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001891 void CopyFrom(HEnvironment* environment);
1892
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001893 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1894 // input to the loop phi instead. This is for inserting instructions that
1895 // require an environment (like HDeoptimization) in the loop pre-header.
1896 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001897
1898 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001899 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001900 }
1901
1902 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001903 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001904 }
1905
David Brazdil1abb4192015-02-17 18:33:36 +00001906 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001907
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001908 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001909
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001910 HEnvironment* GetParent() const { return parent_; }
1911
1912 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001913 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001914 }
1915
1916 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001917 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 }
1919
1920 uint32_t GetDexPc() const {
1921 return dex_pc_;
1922 }
1923
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001924 ArtMethod* GetMethod() const {
1925 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001926 }
1927
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001928 HInstruction* GetHolder() const {
1929 return holder_;
1930 }
1931
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001932
1933 bool IsFromInlinedInvoke() const {
1934 return GetParent() != nullptr;
1935 }
1936
David Brazdiled596192015-01-23 10:39:45 +00001937 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001938 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1939 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001940 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001941 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001942 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001943
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001944 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001945 HInstruction* const holder_;
1946
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001947 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001948
1949 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1950};
1951
Vladimir Markof9f64412015-09-02 14:05:49 +01001952class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001953 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301954#define DECLARE_KIND(type, super) k##type,
1955 enum InstructionKind {
1956 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1957 kLastInstructionKind
1958 };
1959#undef DECLARE_KIND
1960
1961 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001962 : previous_(nullptr),
1963 next_(nullptr),
1964 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001965 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001966 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001967 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001968 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001969 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001970 locations_(nullptr),
1971 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001972 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001973 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001974 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301975 SetPackedField<InstructionKindField>(kind);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001976 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1977 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001978
Dave Allison20dfc792014-06-16 20:44:29 -07001979 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001980
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001981
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001982 HInstruction* GetNext() const { return next_; }
1983 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001984
Calin Juravle77520bc2015-01-12 18:45:46 +00001985 HInstruction* GetNextDisregardingMoves() const;
1986 HInstruction* GetPreviousDisregardingMoves() const;
1987
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001988 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001989 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001990 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001991 bool IsInBlock() const { return block_ != nullptr; }
1992 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001993 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1994 bool IsIrreducibleLoopHeaderPhi() const {
1995 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1996 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001997
Vladimir Marko372f10e2016-05-17 16:30:10 +01001998 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1999
2000 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2001 // One virtual method is enough, just const_cast<> and then re-add the const.
2002 return ArrayRef<const HUserRecord<HInstruction*>>(
2003 const_cast<HInstruction*>(this)->GetInputRecords());
2004 }
2005
Vladimir Markoe9004912016-06-16 16:50:52 +01002006 HInputsRef GetInputs() {
2007 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002008 }
2009
Vladimir Markoe9004912016-06-16 16:50:52 +01002010 HConstInputsRef GetInputs() const {
2011 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002012 }
2013
2014 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002015 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016
Aart Bik2767f4b2016-10-28 15:03:53 -07002017 bool HasInput(HInstruction* input) const {
2018 for (const HInstruction* i : GetInputs()) {
2019 if (i == input) {
2020 return true;
2021 }
2022 }
2023 return false;
2024 }
2025
Vladimir Marko372f10e2016-05-17 16:30:10 +01002026 void SetRawInputAt(size_t index, HInstruction* input) {
2027 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2028 }
2029
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002030 virtual void Accept(HGraphVisitor* visitor) = 0;
2031 virtual const char* DebugName() const = 0;
2032
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002033 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002035 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002036
2037 uint32_t GetDexPc() const { return dex_pc_; }
2038
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002039 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002040
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002041 // Can the instruction throw?
2042 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2043 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002044 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002045
2046 // Does the instruction always throw an exception unconditionally?
2047 virtual bool AlwaysThrows() const { return false; }
2048
David Brazdilec16f792015-08-19 15:04:01 +01002049 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002050
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002051 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002052 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002053
Calin Juravle10e244f2015-01-26 18:54:32 +00002054 // Does not apply for all instructions, but having this at top level greatly
2055 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002056 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002057 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002058 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002059 return true;
2060 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002061
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002062 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002063 return false;
2064 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002065
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002066 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002068 }
2069
Calin Juravle2e768302015-07-28 14:41:11 +00002070 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002071
Calin Juravle61d544b2015-02-23 16:46:57 +00002072 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002074 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002075 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002076 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002077
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002078 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002079 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002080 // Note: fixup_end remains valid across push_front().
2081 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2082 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002083 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002084 uses_.push_front(*new_node);
2085 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002086 }
2087
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002088 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002089 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002090 // Note: env_fixup_end remains valid across push_front().
2091 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2092 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002093 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002094 env_uses_.push_front(*new_node);
2095 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002096 }
2097
David Brazdil1abb4192015-02-17 18:33:36 +00002098 void RemoveAsUserOfInput(size_t input) {
2099 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002100 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2101 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2102 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002103 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104
Vladimir Marko372f10e2016-05-17 16:30:10 +01002105 void RemoveAsUserOfAllInputs() {
2106 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2107 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2108 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2109 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2110 }
2111 }
2112
David Brazdil1abb4192015-02-17 18:33:36 +00002113 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2114 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002115
Vladimir Marko46817b82016-03-29 12:21:58 +01002116 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2117 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2118 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002119 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002120 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002121 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002122
Aart Bikcc42be02016-10-20 16:14:16 -07002123 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002124 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002125 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002126 !CanThrow() &&
2127 !IsSuspendCheck() &&
2128 !IsControlFlow() &&
2129 !IsNativeDebugInfo() &&
2130 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002131 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002132 !IsMemoryBarrier() &&
2133 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002134 }
2135
Aart Bikcc42be02016-10-20 16:14:16 -07002136 bool IsDeadAndRemovable() const {
2137 return IsRemovable() && !HasUses();
2138 }
2139
Roland Levillain6c82d402014-10-13 16:10:27 +01002140 // Does this instruction strictly dominate `other_instruction`?
2141 // Returns false if this instruction and `other_instruction` are the same.
2142 // Aborts if this instruction and `other_instruction` are both phis.
2143 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002144
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002145 int GetId() const { return id_; }
2146 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002147
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002148 int GetSsaIndex() const { return ssa_index_; }
2149 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2150 bool HasSsaIndex() const { return ssa_index_ != -1; }
2151
2152 bool HasEnvironment() const { return environment_ != nullptr; }
2153 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002154 // Set the `environment_` field. Raw because this method does not
2155 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002156 void SetRawEnvironment(HEnvironment* environment) {
2157 DCHECK(environment_ == nullptr);
2158 DCHECK_EQ(environment->GetHolder(), this);
2159 environment_ = environment;
2160 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002161
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002162 void InsertRawEnvironment(HEnvironment* environment) {
2163 DCHECK(environment_ != nullptr);
2164 DCHECK_EQ(environment->GetHolder(), this);
2165 DCHECK(environment->GetParent() == nullptr);
2166 environment->parent_ = environment_;
2167 environment_ = environment;
2168 }
2169
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002170 void RemoveEnvironment();
2171
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002172 // Set the environment of this instruction, copying it from `environment`. While
2173 // copying, the uses lists are being updated.
2174 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002175 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002176 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002177 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002178 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002179 if (environment->GetParent() != nullptr) {
2180 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2181 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002182 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002183
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002184 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2185 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002186 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002187 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002188 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002189 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002190 if (environment->GetParent() != nullptr) {
2191 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2192 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002193 }
2194
Nicolas Geoffray39468442014-09-02 15:17:15 +01002195 // Returns the number of entries in the environment. Typically, that is the
2196 // number of dex registers in a method. It could be more in case of inlining.
2197 size_t EnvironmentSize() const;
2198
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002199 LocationSummary* GetLocations() const { return locations_; }
2200 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002201
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002202 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002203 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002204 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002205
Alexandre Rames188d4312015-04-09 18:30:21 +01002206 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2207 // uses of this instruction by `other` are *not* updated.
2208 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2209 ReplaceWith(other);
2210 other->ReplaceInput(this, use_index);
2211 }
2212
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002213 // Move `this` instruction before `cursor`
2214 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002215
Vladimir Markofb337ea2015-11-25 15:25:10 +00002216 // Move `this` before its first user and out of any loops. If there is no
2217 // out-of-loop user that dominates all other users, move the instruction
2218 // to the end of the out-of-loop common dominator of the user's blocks.
2219 //
2220 // This can be used only on non-throwing instructions with no side effects that
2221 // have at least one use but no environment uses.
2222 void MoveBeforeFirstUserAndOutOfLoops();
2223
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002224#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002225 bool Is##type() const; \
2226 const H##type* As##type() const; \
2227 H##type* As##type();
2228
2229 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2230#undef INSTRUCTION_TYPE_CHECK
2231
2232#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002233 bool Is##type() const { return (As##type() != nullptr); } \
2234 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002235 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002236 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002237#undef INSTRUCTION_TYPE_CHECK
2238
Artem Serovcced8ba2017-07-19 18:18:09 +01002239 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2240 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2241 // the instruction then the behaviour of this function is undefined.
2242 //
2243 // Note: It is semantically valid to create a clone of the instruction only until
2244 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2245 // copied.
2246 //
2247 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2248 // 'explicit HInstruction(const HInstruction& other)' for details.
2249 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2250 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2251 DebugName() << " " << GetId();
2252 UNREACHABLE();
2253 }
2254
2255 // Return whether instruction can be cloned (copied).
2256 virtual bool IsClonable() const { return false; }
2257
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002258 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002259 // TODO: this method is used by LICM and GVN with possibly different
2260 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002261 virtual bool CanBeMoved() const { return false; }
2262
2263 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002264 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002265 return false;
2266 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002267
2268 // Returns whether any data encoded in the two instructions is equal.
2269 // This method does not look at the inputs. Both instructions must be
2270 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002271 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002272 return false;
2273 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002274
2275 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002276 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002277 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002278 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002280 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2281 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2282 // the virtual function because the __attribute__((__pure__)) doesn't really
2283 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302284 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002285
2286 virtual size_t ComputeHashCode() const {
2287 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002288 for (const HInstruction* input : GetInputs()) {
2289 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002290 }
2291 return result;
2292 }
2293
2294 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002295 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002296 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002297
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002298 size_t GetLifetimePosition() const { return lifetime_position_; }
2299 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2300 LiveInterval* GetLiveInterval() const { return live_interval_; }
2301 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2302 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2303
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002304 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2305
2306 // Returns whether the code generation of the instruction will require to have access
2307 // to the current method. Such instructions are:
2308 // (1): Instructions that require an environment, as calling the runtime requires
2309 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002310 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2311 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002312 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002313 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002314 }
2315
Vladimir Markodc151b22015-10-15 18:02:30 +01002316 // Returns whether the code generation of the instruction will require to have access
2317 // to the dex cache of the current method's declaring class via the current method.
2318 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002319
Mark Mendellc4701932015-04-10 13:18:51 -04002320 // Does this instruction have any use in an environment before
2321 // control flow hits 'other'?
2322 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2323
2324 // Remove all references to environment uses of this instruction.
2325 // The caller must ensure that this is safe to do.
2326 void RemoveEnvironmentUsers();
2327
Vladimir Markoa1de9182016-02-25 11:37:38 +00002328 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2329 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002330
David Brazdil1abb4192015-02-17 18:33:36 +00002331 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002332 // If set, the machine code for this instruction is assumed to be generated by
2333 // its users. Used by liveness analysis to compute use positions accordingly.
2334 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2335 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302336 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2337 static constexpr size_t kFieldInstructionKindSize =
2338 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
2339 static constexpr size_t kNumberOfGenericPackedBits =
2340 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002341 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2342
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302343 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2344 "Too many generic packed fields");
2345
Vladimir Marko372f10e2016-05-17 16:30:10 +01002346 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2347 return GetInputRecords()[i];
2348 }
2349
2350 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2351 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2352 input_records[index] = input;
2353 }
David Brazdil1abb4192015-02-17 18:33:36 +00002354
Vladimir Markoa1de9182016-02-25 11:37:38 +00002355 uint32_t GetPackedFields() const {
2356 return packed_fields_;
2357 }
2358
2359 template <size_t flag>
2360 bool GetPackedFlag() const {
2361 return (packed_fields_ & (1u << flag)) != 0u;
2362 }
2363
2364 template <size_t flag>
2365 void SetPackedFlag(bool value = true) {
2366 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2367 }
2368
2369 template <typename BitFieldType>
2370 typename BitFieldType::value_type GetPackedField() const {
2371 return BitFieldType::Decode(packed_fields_);
2372 }
2373
2374 template <typename BitFieldType>
2375 void SetPackedField(typename BitFieldType::value_type value) {
2376 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2377 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2378 }
2379
Artem Serovcced8ba2017-07-19 18:18:09 +01002380 // Copy construction for the instruction (used for Clone function).
2381 //
2382 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2383 // prepare_for_register_allocator are not copied (set to default values).
2384 //
2385 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2386 // fine for most of them. However for some of the instructions a custom copy constructor must be
2387 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2388 // for copying them).
2389 explicit HInstruction(const HInstruction& other)
2390 : previous_(nullptr),
2391 next_(nullptr),
2392 block_(nullptr),
2393 dex_pc_(other.dex_pc_),
2394 id_(-1),
2395 ssa_index_(-1),
2396 packed_fields_(other.packed_fields_),
2397 environment_(nullptr),
2398 locations_(nullptr),
2399 live_interval_(nullptr),
2400 lifetime_position_(kNoLifetime),
2401 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302402 reference_type_handle_(other.reference_type_handle_) {
2403 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002404
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002405 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302406 using InstructionKindField =
2407 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2408
Vladimir Marko46817b82016-03-29 12:21:58 +01002409 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2410 auto before_use_node = uses_.before_begin();
2411 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2412 HInstruction* user = use_node->GetUser();
2413 size_t input_index = use_node->GetIndex();
2414 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2415 before_use_node = use_node;
2416 }
2417 }
2418
2419 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2420 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2421 if (next != uses_.end()) {
2422 HInstruction* next_user = next->GetUser();
2423 size_t next_index = next->GetIndex();
2424 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2425 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2426 }
2427 }
2428
2429 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2430 auto before_env_use_node = env_uses_.before_begin();
2431 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2432 HEnvironment* user = env_use_node->GetUser();
2433 size_t input_index = env_use_node->GetIndex();
2434 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2435 before_env_use_node = env_use_node;
2436 }
2437 }
2438
2439 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2440 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2441 if (next != env_uses_.end()) {
2442 HEnvironment* next_user = next->GetUser();
2443 size_t next_index = next->GetIndex();
2444 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2445 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2446 }
2447 }
David Brazdil1abb4192015-02-17 18:33:36 +00002448
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002449 HInstruction* previous_;
2450 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002451 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002452 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002453
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002454 // An instruction gets an id when it is added to the graph.
2455 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002456 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002457 int id_;
2458
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002459 // When doing liveness analysis, instructions that have uses get an SSA index.
2460 int ssa_index_;
2461
Vladimir Markoa1de9182016-02-25 11:37:38 +00002462 // Packed fields.
2463 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002464
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002465 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002466 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002467
2468 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002469 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002470
Nicolas Geoffray39468442014-09-02 15:17:15 +01002471 // The environment associated with this instruction. Not null if the instruction
2472 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002473 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002474
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002475 // Set by the code generator.
2476 LocationSummary* locations_;
2477
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002478 // Set by the liveness analysis.
2479 LiveInterval* live_interval_;
2480
2481 // Set by the liveness analysis, this is the position in a linear
2482 // order of blocks where this instruction's live interval start.
2483 size_t lifetime_position_;
2484
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002485 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002486
Vladimir Markoa1de9182016-02-25 11:37:38 +00002487 // The reference handle part of the reference type info.
2488 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002489 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002490 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002491
David Brazdil1abb4192015-02-17 18:33:36 +00002492 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002493 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002494 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002495 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002496 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002497};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002498std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002499
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002500// Iterates over the instructions, while preserving the next instruction
2501// in case the current instruction gets removed from the list by the user
2502// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002503class HInstructionIterator : public ValueObject {
2504 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002505 explicit HInstructionIterator(const HInstructionList& instructions)
2506 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002507 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002508 }
2509
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002510 bool Done() const { return instruction_ == nullptr; }
2511 HInstruction* Current() const { return instruction_; }
2512 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002513 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002514 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515 }
2516
2517 private:
2518 HInstruction* instruction_;
2519 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002520
2521 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002522};
2523
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002524// Iterates over the instructions without saving the next instruction,
2525// therefore handling changes in the graph potentially made by the user
2526// of this iterator.
2527class HInstructionIteratorHandleChanges : public ValueObject {
2528 public:
2529 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2530 : instruction_(instructions.first_instruction_) {
2531 }
2532
2533 bool Done() const { return instruction_ == nullptr; }
2534 HInstruction* Current() const { return instruction_; }
2535 void Advance() {
2536 instruction_ = instruction_->GetNext();
2537 }
2538
2539 private:
2540 HInstruction* instruction_;
2541
2542 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2543};
2544
2545
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002546class HBackwardInstructionIterator : public ValueObject {
2547 public:
2548 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2549 : instruction_(instructions.last_instruction_) {
2550 next_ = Done() ? nullptr : instruction_->GetPrevious();
2551 }
2552
2553 bool Done() const { return instruction_ == nullptr; }
2554 HInstruction* Current() const { return instruction_; }
2555 void Advance() {
2556 instruction_ = next_;
2557 next_ = Done() ? nullptr : instruction_->GetPrevious();
2558 }
2559
2560 private:
2561 HInstruction* instruction_;
2562 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002563
2564 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002565};
2566
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002567class HVariableInputSizeInstruction : public HInstruction {
2568 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002569 using HInstruction::GetInputRecords; // Keep the const version visible.
2570 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2571 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2572 }
2573
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002574 void AddInput(HInstruction* input);
2575 void InsertInputAt(size_t index, HInstruction* input);
2576 void RemoveInputAt(size_t index);
2577
Igor Murashkind01745e2017-04-05 16:40:31 -07002578 // Removes all the inputs.
2579 // Also removes this instructions from each input's use list
2580 // (for non-environment uses only).
2581 void RemoveAllInputs();
2582
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002583 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302584 HVariableInputSizeInstruction(InstructionKind inst_kind,
2585 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002586 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002587 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002588 size_t number_of_inputs,
2589 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302590 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002591 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002592
Artem Serovcced8ba2017-07-19 18:18:09 +01002593 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002594
Artem Serovcced8ba2017-07-19 18:18:09 +01002595 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002596};
2597
Vladimir Markof9f64412015-09-02 14:05:49 +01002598template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002599class HTemplateInstruction: public HInstruction {
2600 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302601 HTemplateInstruction<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2602 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002603 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002604
Vladimir Marko372f10e2016-05-17 16:30:10 +01002605 using HInstruction::GetInputRecords; // Keep the const version visible.
2606 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2607 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002608 }
2609
Artem Serovcced8ba2017-07-19 18:18:09 +01002610 protected:
2611 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2612
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002613 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002614 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002615
2616 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002617};
2618
Vladimir Markof9f64412015-09-02 14:05:49 +01002619// HTemplateInstruction specialization for N=0.
2620template<>
2621class HTemplateInstruction<0>: public HInstruction {
2622 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302623 explicit HTemplateInstruction<0>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2624 : HInstruction(kind, side_effects, dex_pc) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002625
Vladimir Markof9f64412015-09-02 14:05:49 +01002626 virtual ~HTemplateInstruction() {}
2627
Vladimir Marko372f10e2016-05-17 16:30:10 +01002628 using HInstruction::GetInputRecords; // Keep the const version visible.
2629 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2630 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002631 }
2632
Artem Serovcced8ba2017-07-19 18:18:09 +01002633 protected:
2634 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2635
Vladimir Markof9f64412015-09-02 14:05:49 +01002636 private:
2637 friend class SsaBuilder;
2638};
2639
Dave Allison20dfc792014-06-16 20:44:29 -07002640template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002641class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002642 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302643 using HInstruction::InstructionKind;
2644 HExpression<N>(InstructionKind kind,
2645 DataType::Type type,
2646 SideEffects side_effects,
2647 uint32_t dex_pc)
2648 : HTemplateInstruction<N>(kind, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002649 this->template SetPackedField<TypeField>(type);
2650 }
Dave Allison20dfc792014-06-16 20:44:29 -07002651 virtual ~HExpression() {}
2652
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002653 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002654 return TypeField::Decode(this->GetPackedFields());
2655 }
Dave Allison20dfc792014-06-16 20:44:29 -07002656
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002657 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002658 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2659 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002661 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2662 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2663 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002664 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002665 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002666};
2667
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002668// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2669// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002670class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002671 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002672 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302673 : HTemplateInstruction(kReturnVoid, SideEffects::None(), dex_pc) {
2674 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002675
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002676 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002677
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002678 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002679
Artem Serovcced8ba2017-07-19 18:18:09 +01002680 protected:
2681 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002682};
2683
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002684// Represents dex's RETURN opcodes. A HReturn is a control flow
2685// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002686class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002687 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002688 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302689 : HTemplateInstruction(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002690 SetRawInputAt(0, value);
2691 }
2692
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002693 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002694
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002695 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002696
Artem Serovcced8ba2017-07-19 18:18:09 +01002697 protected:
2698 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002699};
2700
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002701class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002702 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002703 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002704 uint32_t reg_number,
2705 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002707 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002708 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302709 kPhi,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002710 SideEffects::None(),
2711 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002712 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002713 number_of_inputs,
2714 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002715 reg_number_(reg_number) {
2716 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002717 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002718 // Phis are constructed live and marked dead if conflicting or unused.
2719 // Individual steps of SsaBuilder should assume that if a phi has been
2720 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2721 SetPackedFlag<kFlagIsLive>(true);
2722 SetPackedFlag<kFlagCanBeNull>(true);
2723 }
2724
Artem Serovcced8ba2017-07-19 18:18:09 +01002725 bool IsClonable() const OVERRIDE { return true; }
2726
David Brazdildee58d62016-04-07 09:54:26 +00002727 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728 static DataType::Type ToPhiType(DataType::Type type) {
2729 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002730 }
2731
2732 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2735 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002736 // Make sure that only valid type changes occur. The following are allowed:
2737 // (1) int -> float/ref (primitive type propagation),
2738 // (2) long -> double (primitive type propagation).
2739 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2741 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2742 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002743 SetPackedField<TypeField>(new_type);
2744 }
2745
2746 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2747 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2748
2749 uint32_t GetRegNumber() const { return reg_number_; }
2750
2751 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2752 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2753 bool IsDead() const { return !IsLive(); }
2754 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2755
Vladimir Markoe9004912016-06-16 16:50:52 +01002756 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002757 return other != nullptr
2758 && other->IsPhi()
2759 && other->AsPhi()->GetBlock() == GetBlock()
2760 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2761 }
2762
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002763 bool HasEquivalentPhi() const {
2764 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2765 return true;
2766 }
2767 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2768 return true;
2769 }
2770 return false;
2771 }
2772
David Brazdildee58d62016-04-07 09:54:26 +00002773 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2774 // An equivalent phi is a phi having the same dex register and type.
2775 // It assumes that phis with the same dex register are adjacent.
2776 HPhi* GetNextEquivalentPhiWithSameType() {
2777 HInstruction* next = GetNext();
2778 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2779 if (next->GetType() == GetType()) {
2780 return next->AsPhi();
2781 }
2782 next = next->GetNext();
2783 }
2784 return nullptr;
2785 }
2786
2787 DECLARE_INSTRUCTION(Phi);
2788
Artem Serovcced8ba2017-07-19 18:18:09 +01002789 protected:
2790 DEFAULT_COPY_CONSTRUCTOR(Phi);
2791
David Brazdildee58d62016-04-07 09:54:26 +00002792 private:
2793 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2794 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002795 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002796 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2797 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2798 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2799 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002801
David Brazdildee58d62016-04-07 09:54:26 +00002802 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002803};
2804
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002805// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002806// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002807// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002808class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002809 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302810 explicit HExit(uint32_t dex_pc = kNoDexPc)
2811 : HTemplateInstruction(kExit, SideEffects::None(), dex_pc) {
2812 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002814 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002815
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002816 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002817
Artem Serovcced8ba2017-07-19 18:18:09 +01002818 protected:
2819 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002820};
2821
2822// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002823class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002824 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302825 explicit HGoto(uint32_t dex_pc = kNoDexPc)
2826 : HTemplateInstruction(kGoto, SideEffects::None(), dex_pc) {
2827 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002828
Artem Serovcced8ba2017-07-19 18:18:09 +01002829 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002830 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002831
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002832 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002833 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002834 }
2835
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002836 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002837
Artem Serovcced8ba2017-07-19 18:18:09 +01002838 protected:
2839 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002840};
2841
Roland Levillain9867bc72015-08-05 10:21:34 +01002842class HConstant : public HExpression<0> {
2843 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302844 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2845 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2846 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002847
2848 bool CanBeMoved() const OVERRIDE { return true; }
2849
Roland Levillain1a653882016-03-18 18:05:57 +00002850 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002851 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002852 // Is this constant 0 in the arithmetic sense?
2853 virtual bool IsArithmeticZero() const { return false; }
2854 // Is this constant a 0-bit pattern?
2855 virtual bool IsZeroBitPattern() const { return false; }
2856 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002857 virtual bool IsOne() const { return false; }
2858
David Brazdil77a48ae2015-09-15 12:34:04 +00002859 virtual uint64_t GetValueAsUint64() const = 0;
2860
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002861 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002862
Artem Serovcced8ba2017-07-19 18:18:09 +01002863 protected:
2864 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002865};
2866
Vladimir Markofcb503c2016-05-18 12:48:17 +01002867class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002868 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002869 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002870 return true;
2871 }
2872
David Brazdil77a48ae2015-09-15 12:34:04 +00002873 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2874
Roland Levillain9867bc72015-08-05 10:21:34 +01002875 size_t ComputeHashCode() const OVERRIDE { return 0; }
2876
Roland Levillain1a653882016-03-18 18:05:57 +00002877 // The null constant representation is a 0-bit pattern.
2878 virtual bool IsZeroBitPattern() const { return true; }
2879
Roland Levillain9867bc72015-08-05 10:21:34 +01002880 DECLARE_INSTRUCTION(NullConstant);
2881
Artem Serovcced8ba2017-07-19 18:18:09 +01002882 protected:
2883 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2884
Roland Levillain9867bc72015-08-05 10:21:34 +01002885 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002886 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302887 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2888 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002889
2890 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002891};
2892
2893// Constants of the type int. Those can be from Dex instructions, or
2894// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002895class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002896 public:
2897 int32_t GetValue() const { return value_; }
2898
David Brazdil9f389d42015-10-01 14:32:56 +01002899 uint64_t GetValueAsUint64() const OVERRIDE {
2900 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2901 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002902
Vladimir Marko372f10e2016-05-17 16:30:10 +01002903 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002904 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002905 return other->AsIntConstant()->value_ == value_;
2906 }
2907
2908 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2909
2910 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002911 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2912 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2914
Roland Levillain1a653882016-03-18 18:05:57 +00002915 // Integer constants are used to encode Boolean values as well,
2916 // where 1 means true and 0 means false.
2917 bool IsTrue() const { return GetValue() == 1; }
2918 bool IsFalse() const { return GetValue() == 0; }
2919
Roland Levillain9867bc72015-08-05 10:21:34 +01002920 DECLARE_INSTRUCTION(IntConstant);
2921
Artem Serovcced8ba2017-07-19 18:18:09 +01002922 protected:
2923 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2924
Roland Levillain9867bc72015-08-05 10:21:34 +01002925 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002926 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302927 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2928 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002929 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302930 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2931 value_(value ? 1 : 0) {
2932 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002933
2934 const int32_t value_;
2935
2936 friend class HGraph;
2937 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2938 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002939};
2940
Vladimir Markofcb503c2016-05-18 12:48:17 +01002941class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002942 public:
2943 int64_t GetValue() const { return value_; }
2944
David Brazdil77a48ae2015-09-15 12:34:04 +00002945 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2946
Vladimir Marko372f10e2016-05-17 16:30:10 +01002947 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002948 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002949 return other->AsLongConstant()->value_ == value_;
2950 }
2951
2952 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2953
2954 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002955 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2956 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002957 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2958
2959 DECLARE_INSTRUCTION(LongConstant);
2960
Artem Serovcced8ba2017-07-19 18:18:09 +01002961 protected:
2962 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2963
Roland Levillain9867bc72015-08-05 10:21:34 +01002964 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002965 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302966 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2967 value_(value) {
2968 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002969
2970 const int64_t value_;
2971
2972 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002973};
Dave Allison20dfc792014-06-16 20:44:29 -07002974
Vladimir Markofcb503c2016-05-18 12:48:17 +01002975class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002976 public:
2977 float GetValue() const { return value_; }
2978
2979 uint64_t GetValueAsUint64() const OVERRIDE {
2980 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2981 }
2982
Vladimir Marko372f10e2016-05-17 16:30:10 +01002983 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002984 DCHECK(other->IsFloatConstant()) << other->DebugName();
2985 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2986 }
2987
2988 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2989
2990 bool IsMinusOne() const OVERRIDE {
2991 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2992 }
Roland Levillain1a653882016-03-18 18:05:57 +00002993 bool IsArithmeticZero() const OVERRIDE {
2994 return std::fpclassify(value_) == FP_ZERO;
2995 }
2996 bool IsArithmeticPositiveZero() const {
2997 return IsArithmeticZero() && !std::signbit(value_);
2998 }
2999 bool IsArithmeticNegativeZero() const {
3000 return IsArithmeticZero() && std::signbit(value_);
3001 }
3002 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003003 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003004 }
3005 bool IsOne() const OVERRIDE {
3006 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3007 }
3008 bool IsNaN() const {
3009 return std::isnan(value_);
3010 }
3011
3012 DECLARE_INSTRUCTION(FloatConstant);
3013
Artem Serovcced8ba2017-07-19 18:18:09 +01003014 protected:
3015 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3016
Roland Levillain31dd3d62016-02-16 12:21:02 +00003017 private:
3018 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303019 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3020 value_(value) {
3021 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303023 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3024 value_(bit_cast<float, int32_t>(value)) {
3025 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003026
3027 const float value_;
3028
3029 // Only the SsaBuilder and HGraph can create floating-point constants.
3030 friend class SsaBuilder;
3031 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003032};
3033
Vladimir Markofcb503c2016-05-18 12:48:17 +01003034class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003035 public:
3036 double GetValue() const { return value_; }
3037
3038 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3039
Vladimir Marko372f10e2016-05-17 16:30:10 +01003040 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003041 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3042 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3043 }
3044
3045 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3046
3047 bool IsMinusOne() const OVERRIDE {
3048 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3049 }
Roland Levillain1a653882016-03-18 18:05:57 +00003050 bool IsArithmeticZero() const OVERRIDE {
3051 return std::fpclassify(value_) == FP_ZERO;
3052 }
3053 bool IsArithmeticPositiveZero() const {
3054 return IsArithmeticZero() && !std::signbit(value_);
3055 }
3056 bool IsArithmeticNegativeZero() const {
3057 return IsArithmeticZero() && std::signbit(value_);
3058 }
3059 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003060 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003061 }
3062 bool IsOne() const OVERRIDE {
3063 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3064 }
3065 bool IsNaN() const {
3066 return std::isnan(value_);
3067 }
3068
3069 DECLARE_INSTRUCTION(DoubleConstant);
3070
Artem Serovcced8ba2017-07-19 18:18:09 +01003071 protected:
3072 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3073
Roland Levillain31dd3d62016-02-16 12:21:02 +00003074 private:
3075 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303076 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3077 value_(value) {
3078 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303080 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3081 value_(bit_cast<double, int64_t>(value)) {
3082 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003083
3084 const double value_;
3085
3086 // Only the SsaBuilder and HGraph can create floating-point constants.
3087 friend class SsaBuilder;
3088 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003089};
3090
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003091// Conditional branch. A block ending with an HIf instruction must have
3092// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003093class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003094 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003095 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303096 : HTemplateInstruction(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003097 SetRawInputAt(0, input);
3098 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003099
Artem Serovcced8ba2017-07-19 18:18:09 +01003100 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003101 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003102
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003103 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003104 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003105 }
3106
3107 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003108 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003109 }
3110
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003111 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003112
Artem Serovcced8ba2017-07-19 18:18:09 +01003113 protected:
3114 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003115};
3116
David Brazdilfc6a86a2015-06-26 10:33:45 +00003117
3118// Abstract instruction which marks the beginning and/or end of a try block and
3119// links it to the respective exception handlers. Behaves the same as a Goto in
3120// non-exceptional control flow.
3121// Normal-flow successor is stored at index zero, exception handlers under
3122// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003123class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003124 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003125 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003126 kEntry,
3127 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003128 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003129 };
3130
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003131 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303132 : HTemplateInstruction(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003133 SetPackedField<BoundaryKindField>(kind);
3134 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003135
3136 bool IsControlFlow() const OVERRIDE { return true; }
3137
3138 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003139 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003140
David Brazdild26a4112015-11-10 11:07:31 +00003141 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3142 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3143 }
3144
David Brazdilfc6a86a2015-06-26 10:33:45 +00003145 // Returns whether `handler` is among its exception handlers (non-zero index
3146 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003147 bool HasExceptionHandler(const HBasicBlock& handler) const {
3148 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003149 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003150 }
3151
3152 // If not present already, adds `handler` to its block's list of exception
3153 // handlers.
3154 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003155 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003156 GetBlock()->AddSuccessor(handler);
3157 }
3158 }
3159
Vladimir Markoa1de9182016-02-25 11:37:38 +00003160 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3161 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003162
David Brazdilffee3d32015-07-06 11:48:53 +01003163 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3164
David Brazdilfc6a86a2015-06-26 10:33:45 +00003165 DECLARE_INSTRUCTION(TryBoundary);
3166
Artem Serovcced8ba2017-07-19 18:18:09 +01003167 protected:
3168 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3169
David Brazdilfc6a86a2015-06-26 10:33:45 +00003170 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003171 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3172 static constexpr size_t kFieldBoundaryKindSize =
3173 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3174 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3175 kFieldBoundaryKind + kFieldBoundaryKindSize;
3176 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3177 "Too many packed fields.");
3178 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003179};
3180
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003181// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003182class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003183 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003184 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3185 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003186 HDeoptimize(ArenaAllocator* allocator,
3187 HInstruction* cond,
3188 DeoptimizationKind kind,
3189 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003190 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303191 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003192 SideEffects::All(),
3193 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003194 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003195 /* number_of_inputs */ 1,
3196 kArenaAllocMisc) {
3197 SetPackedFlag<kFieldCanBeMoved>(false);
3198 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003199 SetRawInputAt(0, cond);
3200 }
3201
Artem Serovcced8ba2017-07-19 18:18:09 +01003202 bool IsClonable() const OVERRIDE { return true; }
3203
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003204 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3205 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3206 // instead of `guard`.
3207 // We set CanTriggerGC to prevent any intermediate address to be live
3208 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003209 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003210 HInstruction* cond,
3211 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003212 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003213 uint32_t dex_pc)
3214 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303215 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003216 SideEffects::CanTriggerGC(),
3217 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003218 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003219 /* number_of_inputs */ 2,
3220 kArenaAllocMisc) {
3221 SetPackedFlag<kFieldCanBeMoved>(true);
3222 SetPackedField<DeoptimizeKindField>(kind);
3223 SetRawInputAt(0, cond);
3224 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003225 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003226
3227 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3228
3229 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3230 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3231 }
3232
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003233 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003234
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003235 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003236
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003237 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003238
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 DataType::Type GetType() const OVERRIDE {
3240 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003241 }
3242
3243 bool GuardsAnInput() const {
3244 return InputCount() == 2;
3245 }
3246
3247 HInstruction* GuardedInput() const {
3248 DCHECK(GuardsAnInput());
3249 return InputAt(1);
3250 }
3251
3252 void RemoveGuard() {
3253 RemoveInputAt(1);
3254 }
3255
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003256 DECLARE_INSTRUCTION(Deoptimize);
3257
Artem Serovcced8ba2017-07-19 18:18:09 +01003258 protected:
3259 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3260
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003261 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003262 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3263 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3264 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003265 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003266 static constexpr size_t kNumberOfDeoptimizePackedBits =
3267 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3268 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3269 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003270 using DeoptimizeKindField =
3271 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003272};
3273
Mingyao Yang063fc772016-08-02 11:02:54 -07003274// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3275// The compiled code checks this flag value in a guard before devirtualized call and
3276// if it's true, starts to do deoptimization.
3277// It has a 4-byte slot on stack.
3278// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003279class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003280 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003281 // CHA guards are only optimized in a separate pass and it has no side effects
3282 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003283 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303284 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
3285 SideEffects::None(),
3286 dex_pc,
3287 allocator,
3288 0,
3289 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003290 }
3291
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003292 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003293
3294 // We do all CHA guard elimination/motion in a single pass, after which there is no
3295 // further guard elimination/motion since a guard might have been used for justification
3296 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3297 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003298 bool CanBeMoved() const OVERRIDE { return false; }
3299
3300 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3301
Artem Serovcced8ba2017-07-19 18:18:09 +01003302 protected:
3303 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003304};
3305
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003306// Represents the ArtMethod that was passed as a first argument to
3307// the method. It is used by instructions that depend on it, like
3308// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003309class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003310 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003311 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303312 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3313 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003314
3315 DECLARE_INSTRUCTION(CurrentMethod);
3316
Artem Serovcced8ba2017-07-19 18:18:09 +01003317 protected:
3318 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003319};
3320
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003321// Fetches an ArtMethod from the virtual table or the interface method table
3322// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003323class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003324 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003325 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003326 kVTable,
3327 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003328 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003329 };
3330 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003331 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003332 TableKind kind,
3333 size_t index,
3334 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303335 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003336 index_(index) {
3337 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003338 SetRawInputAt(0, cls);
3339 }
3340
Artem Serovcced8ba2017-07-19 18:18:09 +01003341 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003342 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003343 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003344 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003345 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003346 }
3347
Vladimir Markoa1de9182016-02-25 11:37:38 +00003348 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003349 size_t GetIndex() const { return index_; }
3350
3351 DECLARE_INSTRUCTION(ClassTableGet);
3352
Artem Serovcced8ba2017-07-19 18:18:09 +01003353 protected:
3354 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3355
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003356 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003357 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3358 static constexpr size_t kFieldTableKindSize =
3359 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3360 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3361 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3362 "Too many packed fields.");
3363 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3364
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003365 // The index of the ArtMethod in the table.
3366 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003367};
3368
Mark Mendellfe57faa2015-09-18 09:26:15 -04003369// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3370// have one successor for each entry in the switch table, and the final successor
3371// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003372class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003373 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003374 HPackedSwitch(int32_t start_value,
3375 uint32_t num_entries,
3376 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003377 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303378 : HTemplateInstruction(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003379 start_value_(start_value),
3380 num_entries_(num_entries) {
3381 SetRawInputAt(0, input);
3382 }
3383
Artem Serovcced8ba2017-07-19 18:18:09 +01003384 bool IsClonable() const OVERRIDE { return true; }
3385
Mark Mendellfe57faa2015-09-18 09:26:15 -04003386 bool IsControlFlow() const OVERRIDE { return true; }
3387
3388 int32_t GetStartValue() const { return start_value_; }
3389
Vladimir Marko211c2112015-09-24 16:52:33 +01003390 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003391
3392 HBasicBlock* GetDefaultBlock() const {
3393 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003394 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003395 }
3396 DECLARE_INSTRUCTION(PackedSwitch);
3397
Artem Serovcced8ba2017-07-19 18:18:09 +01003398 protected:
3399 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3400
Mark Mendellfe57faa2015-09-18 09:26:15 -04003401 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003402 const int32_t start_value_;
3403 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003404};
3405
Roland Levillain88cb1752014-10-20 16:36:47 +01003406class HUnaryOperation : public HExpression<1> {
3407 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303408 HUnaryOperation(InstructionKind kind,
3409 DataType::Type result_type,
3410 HInstruction* input,
3411 uint32_t dex_pc = kNoDexPc)
3412 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003413 SetRawInputAt(0, input);
3414 }
3415
Artem Serovcced8ba2017-07-19 18:18:09 +01003416 // All of the UnaryOperation instructions are clonable.
3417 bool IsClonable() const OVERRIDE { return true; }
3418
Roland Levillain88cb1752014-10-20 16:36:47 +01003419 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003420 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003421
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003422 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003423 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003424 return true;
3425 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003426
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 // Try to statically evaluate `this` and return a HConstant
3428 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003429 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003430 HConstant* TryStaticEvaluation() const;
3431
3432 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003433 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3434 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003435 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3436 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003437
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003438 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003439
Artem Serovcced8ba2017-07-19 18:18:09 +01003440 protected:
3441 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003442};
3443
Dave Allison20dfc792014-06-16 20:44:29 -07003444class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003445 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303446 HBinaryOperation(InstructionKind kind,
3447 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003448 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003449 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003450 SideEffects side_effects = SideEffects::None(),
3451 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303452 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003453 SetRawInputAt(0, left);
3454 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003455 }
3456
Artem Serovcced8ba2017-07-19 18:18:09 +01003457 // All of the BinaryOperation instructions are clonable.
3458 bool IsClonable() const OVERRIDE { return true; }
3459
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003460 HInstruction* GetLeft() const { return InputAt(0); }
3461 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003462 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003463
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003464 virtual bool IsCommutative() const { return false; }
3465
3466 // Put constant on the right.
3467 // Returns whether order is changed.
3468 bool OrderInputsWithConstantOnTheRight() {
3469 HInstruction* left = InputAt(0);
3470 HInstruction* right = InputAt(1);
3471 if (left->IsConstant() && !right->IsConstant()) {
3472 ReplaceInput(right, 0);
3473 ReplaceInput(left, 1);
3474 return true;
3475 }
3476 return false;
3477 }
3478
3479 // Order inputs by instruction id, but favor constant on the right side.
3480 // This helps GVN for commutative ops.
3481 void OrderInputs() {
3482 DCHECK(IsCommutative());
3483 HInstruction* left = InputAt(0);
3484 HInstruction* right = InputAt(1);
3485 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3486 return;
3487 }
3488 if (OrderInputsWithConstantOnTheRight()) {
3489 return;
3490 }
3491 // Order according to instruction id.
3492 if (left->GetId() > right->GetId()) {
3493 ReplaceInput(right, 0);
3494 ReplaceInput(left, 1);
3495 }
3496 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003497
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003498 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003499 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003500 return true;
3501 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003502
Roland Levillain31dd3d62016-02-16 12:21:02 +00003503 // Try to statically evaluate `this` and return a HConstant
3504 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003505 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003506 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003507
3508 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003509 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3510 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003511 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3512 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003513 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003514 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3515 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003516 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3517 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003518 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3519 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003520 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003521 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3522 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003523
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003524 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003525 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003526 HConstant* GetConstantRight() const;
3527
3528 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003529 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003530 HInstruction* GetLeastConstantLeft() const;
3531
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003532 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003533
Artem Serovcced8ba2017-07-19 18:18:09 +01003534 protected:
3535 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003536};
3537
Mark Mendellc4701932015-04-10 13:18:51 -04003538// The comparison bias applies for floating point operations and indicates how NaN
3539// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003540enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003541 kNoBias, // bias is not applicable (i.e. for long operation)
3542 kGtBias, // return 1 for NaN comparisons
3543 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003544 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003545};
3546
Roland Levillain31dd3d62016-02-16 12:21:02 +00003547std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3548
Dave Allison20dfc792014-06-16 20:44:29 -07003549class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003550 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303551 HCondition(InstructionKind kind,
3552 HInstruction* first,
3553 HInstruction* second,
3554 uint32_t dex_pc = kNoDexPc)
3555 : HBinaryOperation(kind,
3556 DataType::Type::kBool,
3557 first,
3558 second,
3559 SideEffects::None(),
3560 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003561 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3562 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003563
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003564 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003565 // `instruction`, and disregard moves in between.
3566 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003567
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003568 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003569
3570 virtual IfCondition GetCondition() const = 0;
3571
Mark Mendellc4701932015-04-10 13:18:51 -04003572 virtual IfCondition GetOppositeCondition() const = 0;
3573
Vladimir Markoa1de9182016-02-25 11:37:38 +00003574 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003575 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3576
Vladimir Markoa1de9182016-02-25 11:37:38 +00003577 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3578 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003579
Vladimir Marko372f10e2016-05-17 16:30:10 +01003580 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003581 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003582 }
3583
Roland Levillain4fa13f62015-07-06 18:11:54 +01003584 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003585 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003586 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003587 if (if_cond == kCondNE) {
3588 return true;
3589 } else if (if_cond == kCondEQ) {
3590 return false;
3591 }
3592 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003593 }
3594
3595 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003596 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003597 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003598 if (if_cond == kCondEQ) {
3599 return true;
3600 } else if (if_cond == kCondNE) {
3601 return false;
3602 }
3603 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003604 }
3605
Roland Levillain31dd3d62016-02-16 12:21:02 +00003606 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003607 // Needed if we merge a HCompare into a HCondition.
3608 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3609 static constexpr size_t kFieldComparisonBiasSize =
3610 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3611 static constexpr size_t kNumberOfConditionPackedBits =
3612 kFieldComparisonBias + kFieldComparisonBiasSize;
3613 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3614 using ComparisonBiasField =
3615 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3616
Roland Levillain31dd3d62016-02-16 12:21:02 +00003617 template <typename T>
3618 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3619
3620 template <typename T>
3621 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003622 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003623 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3624 // Handle the bias.
3625 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3626 }
3627
3628 // Return an integer constant containing the result of a condition evaluated at compile time.
3629 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3630 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3631 }
3632
Artem Serovcced8ba2017-07-19 18:18:09 +01003633 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003634};
3635
3636// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003637class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003639 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303640 : HCondition(kEqual, first, second, dex_pc) {
3641 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003642
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003643 bool IsCommutative() const OVERRIDE { return true; }
3644
Vladimir Marko9e23df52015-11-10 17:14:35 +00003645 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3646 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003647 return MakeConstantCondition(true, GetDexPc());
3648 }
3649 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3650 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3651 }
3652 // In the following Evaluate methods, a HCompare instruction has
3653 // been merged into this HEqual instruction; evaluate it as
3654 // `Compare(x, y) == 0`.
3655 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3656 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3657 GetDexPc());
3658 }
3659 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3660 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3661 }
3662 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3663 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003664 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003665
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003666 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003667
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003668 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003669 return kCondEQ;
3670 }
3671
Mark Mendellc4701932015-04-10 13:18:51 -04003672 IfCondition GetOppositeCondition() const OVERRIDE {
3673 return kCondNE;
3674 }
3675
Artem Serovcced8ba2017-07-19 18:18:09 +01003676 protected:
3677 DEFAULT_COPY_CONSTRUCTOR(Equal);
3678
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003679 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003680 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003681};
3682
Vladimir Markofcb503c2016-05-18 12:48:17 +01003683class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003684 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303685 HNotEqual(HInstruction* first, HInstruction* second,
3686 uint32_t dex_pc = kNoDexPc)
3687 : HCondition(kNotEqual, first, second, dex_pc) {
3688 }
Dave Allison20dfc792014-06-16 20:44:29 -07003689
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003690 bool IsCommutative() const OVERRIDE { return true; }
3691
Vladimir Marko9e23df52015-11-10 17:14:35 +00003692 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3693 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003694 return MakeConstantCondition(false, GetDexPc());
3695 }
3696 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3697 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3698 }
3699 // In the following Evaluate methods, a HCompare instruction has
3700 // been merged into this HNotEqual instruction; evaluate it as
3701 // `Compare(x, y) != 0`.
3702 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3703 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3704 }
3705 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3706 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3707 }
3708 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3709 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003710 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003711
Dave Allison20dfc792014-06-16 20:44:29 -07003712 DECLARE_INSTRUCTION(NotEqual);
3713
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003714 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003715 return kCondNE;
3716 }
3717
Mark Mendellc4701932015-04-10 13:18:51 -04003718 IfCondition GetOppositeCondition() const OVERRIDE {
3719 return kCondEQ;
3720 }
3721
Artem Serovcced8ba2017-07-19 18:18:09 +01003722 protected:
3723 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3724
Dave Allison20dfc792014-06-16 20:44:29 -07003725 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003726 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003727};
3728
Vladimir Markofcb503c2016-05-18 12:48:17 +01003729class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003730 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303731 HLessThan(HInstruction* first, HInstruction* second,
3732 uint32_t dex_pc = kNoDexPc)
3733 : HCondition(kLessThan, first, second, dex_pc) {
3734 }
Dave Allison20dfc792014-06-16 20:44:29 -07003735
Roland Levillain9867bc72015-08-05 10:21:34 +01003736 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003737 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003738 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003739 // In the following Evaluate methods, a HCompare instruction has
3740 // been merged into this HLessThan instruction; evaluate it as
3741 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003742 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3744 }
3745 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3746 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3747 }
3748 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3749 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003750 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003751
Dave Allison20dfc792014-06-16 20:44:29 -07003752 DECLARE_INSTRUCTION(LessThan);
3753
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003754 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003755 return kCondLT;
3756 }
3757
Mark Mendellc4701932015-04-10 13:18:51 -04003758 IfCondition GetOppositeCondition() const OVERRIDE {
3759 return kCondGE;
3760 }
3761
Artem Serovcced8ba2017-07-19 18:18:09 +01003762 protected:
3763 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3764
Dave Allison20dfc792014-06-16 20:44:29 -07003765 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003766 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003767};
3768
Vladimir Markofcb503c2016-05-18 12:48:17 +01003769class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003770 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303771 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3772 uint32_t dex_pc = kNoDexPc)
3773 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3774 }
Dave Allison20dfc792014-06-16 20:44:29 -07003775
Roland Levillain9867bc72015-08-05 10:21:34 +01003776 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003777 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003778 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003779 // In the following Evaluate methods, a HCompare instruction has
3780 // been merged into this HLessThanOrEqual instruction; evaluate it as
3781 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003782 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003783 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3784 }
3785 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3786 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3787 }
3788 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3789 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003790 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003791
Dave Allison20dfc792014-06-16 20:44:29 -07003792 DECLARE_INSTRUCTION(LessThanOrEqual);
3793
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003794 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003795 return kCondLE;
3796 }
3797
Mark Mendellc4701932015-04-10 13:18:51 -04003798 IfCondition GetOppositeCondition() const OVERRIDE {
3799 return kCondGT;
3800 }
3801
Artem Serovcced8ba2017-07-19 18:18:09 +01003802 protected:
3803 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3804
Dave Allison20dfc792014-06-16 20:44:29 -07003805 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003806 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003807};
3808
Vladimir Markofcb503c2016-05-18 12:48:17 +01003809class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003810 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003811 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303812 : HCondition(kGreaterThan, first, second, dex_pc) {
3813 }
Dave Allison20dfc792014-06-16 20:44:29 -07003814
Roland Levillain9867bc72015-08-05 10:21:34 +01003815 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003816 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003817 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003818 // In the following Evaluate methods, a HCompare instruction has
3819 // been merged into this HGreaterThan instruction; evaluate it as
3820 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003821 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003822 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3823 }
3824 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3825 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3826 }
3827 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3828 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003829 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003830
Dave Allison20dfc792014-06-16 20:44:29 -07003831 DECLARE_INSTRUCTION(GreaterThan);
3832
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003833 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003834 return kCondGT;
3835 }
3836
Mark Mendellc4701932015-04-10 13:18:51 -04003837 IfCondition GetOppositeCondition() const OVERRIDE {
3838 return kCondLE;
3839 }
3840
Artem Serovcced8ba2017-07-19 18:18:09 +01003841 protected:
3842 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3843
Dave Allison20dfc792014-06-16 20:44:29 -07003844 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003845 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003846};
3847
Vladimir Markofcb503c2016-05-18 12:48:17 +01003848class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003849 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003850 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303851 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3852 }
Dave Allison20dfc792014-06-16 20:44:29 -07003853
Roland Levillain9867bc72015-08-05 10:21:34 +01003854 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003855 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003856 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003857 // In the following Evaluate methods, a HCompare instruction has
3858 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3859 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003860 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3862 }
3863 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3864 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3865 }
3866 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3867 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003868 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003869
Dave Allison20dfc792014-06-16 20:44:29 -07003870 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3871
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003872 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003873 return kCondGE;
3874 }
3875
Mark Mendellc4701932015-04-10 13:18:51 -04003876 IfCondition GetOppositeCondition() const OVERRIDE {
3877 return kCondLT;
3878 }
3879
Artem Serovcced8ba2017-07-19 18:18:09 +01003880 protected:
3881 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3882
Dave Allison20dfc792014-06-16 20:44:29 -07003883 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003884 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003885};
3886
Vladimir Markofcb503c2016-05-18 12:48:17 +01003887class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003888 public:
3889 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303890 : HCondition(kBelow, first, second, dex_pc) {
3891 }
Aart Bike9f37602015-10-09 11:15:55 -07003892
3893 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003894 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003895 }
3896 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003897 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3898 }
3899 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3900 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3901 LOG(FATAL) << DebugName() << " is not defined for float values";
3902 UNREACHABLE();
3903 }
3904 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3905 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3906 LOG(FATAL) << DebugName() << " is not defined for double values";
3907 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003908 }
3909
3910 DECLARE_INSTRUCTION(Below);
3911
3912 IfCondition GetCondition() const OVERRIDE {
3913 return kCondB;
3914 }
3915
3916 IfCondition GetOppositeCondition() const OVERRIDE {
3917 return kCondAE;
3918 }
3919
Artem Serovcced8ba2017-07-19 18:18:09 +01003920 protected:
3921 DEFAULT_COPY_CONSTRUCTOR(Below);
3922
Aart Bike9f37602015-10-09 11:15:55 -07003923 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003924 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003925 return MakeUnsigned(x) < MakeUnsigned(y);
3926 }
Aart Bike9f37602015-10-09 11:15:55 -07003927};
3928
Vladimir Markofcb503c2016-05-18 12:48:17 +01003929class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003930 public:
3931 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303932 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3933 }
Aart Bike9f37602015-10-09 11:15:55 -07003934
3935 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003936 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003937 }
3938 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003939 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3940 }
3941 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3942 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3943 LOG(FATAL) << DebugName() << " is not defined for float values";
3944 UNREACHABLE();
3945 }
3946 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3947 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3948 LOG(FATAL) << DebugName() << " is not defined for double values";
3949 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003950 }
3951
3952 DECLARE_INSTRUCTION(BelowOrEqual);
3953
3954 IfCondition GetCondition() const OVERRIDE {
3955 return kCondBE;
3956 }
3957
3958 IfCondition GetOppositeCondition() const OVERRIDE {
3959 return kCondA;
3960 }
3961
Artem Serovcced8ba2017-07-19 18:18:09 +01003962 protected:
3963 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3964
Aart Bike9f37602015-10-09 11:15:55 -07003965 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003966 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003967 return MakeUnsigned(x) <= MakeUnsigned(y);
3968 }
Aart Bike9f37602015-10-09 11:15:55 -07003969};
3970
Vladimir Markofcb503c2016-05-18 12:48:17 +01003971class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003972 public:
3973 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303974 : HCondition(kAbove, first, second, dex_pc) {
3975 }
Aart Bike9f37602015-10-09 11:15:55 -07003976
3977 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003978 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003979 }
3980 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003981 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3982 }
3983 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3984 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3985 LOG(FATAL) << DebugName() << " is not defined for float values";
3986 UNREACHABLE();
3987 }
3988 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3989 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3990 LOG(FATAL) << DebugName() << " is not defined for double values";
3991 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003992 }
3993
3994 DECLARE_INSTRUCTION(Above);
3995
3996 IfCondition GetCondition() const OVERRIDE {
3997 return kCondA;
3998 }
3999
4000 IfCondition GetOppositeCondition() const OVERRIDE {
4001 return kCondBE;
4002 }
4003
Artem Serovcced8ba2017-07-19 18:18:09 +01004004 protected:
4005 DEFAULT_COPY_CONSTRUCTOR(Above);
4006
Aart Bike9f37602015-10-09 11:15:55 -07004007 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004008 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004009 return MakeUnsigned(x) > MakeUnsigned(y);
4010 }
Aart Bike9f37602015-10-09 11:15:55 -07004011};
4012
Vladimir Markofcb503c2016-05-18 12:48:17 +01004013class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004014 public:
4015 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304016 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4017 }
Aart Bike9f37602015-10-09 11:15:55 -07004018
4019 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004020 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004021 }
4022 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004023 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4024 }
4025 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4026 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4027 LOG(FATAL) << DebugName() << " is not defined for float values";
4028 UNREACHABLE();
4029 }
4030 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4031 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4032 LOG(FATAL) << DebugName() << " is not defined for double values";
4033 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004034 }
4035
4036 DECLARE_INSTRUCTION(AboveOrEqual);
4037
4038 IfCondition GetCondition() const OVERRIDE {
4039 return kCondAE;
4040 }
4041
4042 IfCondition GetOppositeCondition() const OVERRIDE {
4043 return kCondB;
4044 }
4045
Artem Serovcced8ba2017-07-19 18:18:09 +01004046 protected:
4047 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4048
Aart Bike9f37602015-10-09 11:15:55 -07004049 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004050 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004051 return MakeUnsigned(x) >= MakeUnsigned(y);
4052 }
Aart Bike9f37602015-10-09 11:15:55 -07004053};
Dave Allison20dfc792014-06-16 20:44:29 -07004054
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004055// Instruction to check how two inputs compare to each other.
4056// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004057class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004058 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004059 // Note that `comparison_type` is the type of comparison performed
4060 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004061 // HCompare instruction (which is always DataType::Type::kInt).
4062 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004063 HInstruction* first,
4064 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004065 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004066 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304067 : HBinaryOperation(kCompare,
4068 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004069 first,
4070 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004071 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004072 dex_pc) {
4073 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004074 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4075 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004076 }
4077
Roland Levillain9867bc72015-08-05 10:21:34 +01004078 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004079 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4080
4081 template <typename T>
4082 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004083 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004084 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4085 // Handle the bias.
4086 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4087 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004088
Roland Levillain9867bc72015-08-05 10:21:34 +01004089 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004090 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4091 // reach this code path when processing a freshly built HIR
4092 // graph. However HCompare integer instructions can be synthesized
4093 // by the instruction simplifier to implement IntegerCompare and
4094 // IntegerSignum intrinsics, so we have to handle this case.
4095 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004096 }
4097 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004098 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4099 }
4100 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4101 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4102 }
4103 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4104 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004105 }
4106
Vladimir Marko372f10e2016-05-17 16:30:10 +01004107 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004108 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004109 }
4110
Vladimir Markoa1de9182016-02-25 11:37:38 +00004111 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004112
Roland Levillain31dd3d62016-02-16 12:21:02 +00004113 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004114 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004115 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004116 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004117 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004118 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004120 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004121 // Comparisons do not require a runtime call in any back end.
4122 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004123 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004124
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004125 DECLARE_INSTRUCTION(Compare);
4126
Roland Levillain31dd3d62016-02-16 12:21:02 +00004127 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004128 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4129 static constexpr size_t kFieldComparisonBiasSize =
4130 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4131 static constexpr size_t kNumberOfComparePackedBits =
4132 kFieldComparisonBias + kFieldComparisonBiasSize;
4133 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4134 using ComparisonBiasField =
4135 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4136
Roland Levillain31dd3d62016-02-16 12:21:02 +00004137 // Return an integer constant containing the result of a comparison evaluated at compile time.
4138 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4139 DCHECK(value == -1 || value == 0 || value == 1) << value;
4140 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4141 }
4142
Artem Serovcced8ba2017-07-19 18:18:09 +01004143 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004144};
4145
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004146class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004147 public:
4148 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004149 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004150 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004151 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004152 bool finalizable,
4153 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304154 : HExpression(kNewInstance,
4155 DataType::Type::kReference,
4156 SideEffects::CanTriggerGC(),
4157 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004158 type_index_(type_index),
4159 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004160 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004161 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004162 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004163 }
4164
Artem Serovcced8ba2017-07-19 18:18:09 +01004165 bool IsClonable() const OVERRIDE { return true; }
4166
Andreas Gampea5b09a62016-11-17 15:21:22 -08004167 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004168 const DexFile& GetDexFile() const { return dex_file_; }
4169
4170 // Calls runtime so needs an environment.
4171 bool NeedsEnvironment() const OVERRIDE { return true; }
4172
Mingyao Yang062157f2016-03-02 10:15:36 -08004173 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4174 bool CanThrow() const OVERRIDE { return true; }
4175
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004176 bool NeedsChecks() const {
4177 return entrypoint_ == kQuickAllocObjectWithChecks;
4178 }
David Brazdil6de19382016-01-08 17:37:10 +00004179
Vladimir Markoa1de9182016-02-25 11:37:38 +00004180 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004181
4182 bool CanBeNull() const OVERRIDE { return false; }
4183
4184 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4185
4186 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4187 entrypoint_ = entrypoint;
4188 }
4189
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004190 HLoadClass* GetLoadClass() const {
4191 HInstruction* input = InputAt(0);
4192 if (input->IsClinitCheck()) {
4193 input = input->InputAt(0);
4194 }
4195 DCHECK(input->IsLoadClass());
4196 return input->AsLoadClass();
4197 }
4198
David Brazdil6de19382016-01-08 17:37:10 +00004199 bool IsStringAlloc() const;
4200
4201 DECLARE_INSTRUCTION(NewInstance);
4202
Artem Serovcced8ba2017-07-19 18:18:09 +01004203 protected:
4204 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4205
David Brazdil6de19382016-01-08 17:37:10 +00004206 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004207 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004208 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4209 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4210 "Too many packed fields.");
4211
Andreas Gampea5b09a62016-11-17 15:21:22 -08004212 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004213 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004214 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004215};
4216
Agi Csaki05f20562015-08-19 14:58:14 -07004217enum IntrinsicNeedsEnvironmentOrCache {
4218 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4219 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004220};
4221
Aart Bik5d75afe2015-12-14 11:57:01 -08004222enum IntrinsicSideEffects {
4223 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4224 kReadSideEffects, // Intrinsic may read heap memory.
4225 kWriteSideEffects, // Intrinsic may write heap memory.
4226 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4227};
4228
4229enum IntrinsicExceptions {
4230 kNoThrow, // Intrinsic does not throw any exceptions.
4231 kCanThrow // Intrinsic may throw exceptions.
4232};
4233
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004234class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004235 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004236 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004237
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004238 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004239 SetRawInputAt(index, argument);
4240 }
4241
Roland Levillain3e3d7332015-04-28 11:00:54 +01004242 // Return the number of arguments. This number can be lower than
4243 // the number of inputs returned by InputCount(), as some invoke
4244 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4245 // inputs at the end of their list of inputs.
4246 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4247
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004248 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004249
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004250 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4251
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004252 InvokeType GetInvokeType() const {
4253 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004254 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004255
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004256 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004257 return intrinsic_;
4258 }
4259
Aart Bik5d75afe2015-12-14 11:57:01 -08004260 void SetIntrinsic(Intrinsics intrinsic,
4261 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4262 IntrinsicSideEffects side_effects,
4263 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004264
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004265 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004266 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004267 }
4268
Aart Bikff7d89c2016-11-07 08:49:28 -08004269 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4270
Vladimir Markoa1de9182016-02-25 11:37:38 +00004271 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004272
Aart Bika8b8e9b2018-01-09 11:01:02 -08004273 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4274
4275 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4276
Aart Bik71bf7b42016-11-16 10:17:46 -08004277 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004278
Vladimir Marko372f10e2016-05-17 16:30:10 +01004279 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004280 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4281 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004282
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004283 uint32_t* GetIntrinsicOptimizations() {
4284 return &intrinsic_optimizations_;
4285 }
4286
4287 const uint32_t* GetIntrinsicOptimizations() const {
4288 return &intrinsic_optimizations_;
4289 }
4290
4291 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4292
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004293 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004294 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004295
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004296 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004297
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004298 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004299 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4300 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004301 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4302 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004303 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004304 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004305 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004306 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004307 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4308 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004309 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004310 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004311 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004312
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304313 HInvoke(InstructionKind kind,
4314 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004315 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004316 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004317 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004318 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004319 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004320 ArtMethod* resolved_method,
4321 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004322 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304323 kind,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004324 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4325 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004326 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004327 number_of_arguments + number_of_other_inputs,
4328 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004329 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004330 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004331 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004332 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004333 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004334 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004335 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004336 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004337 }
4338
Artem Serovcced8ba2017-07-19 18:18:09 +01004339 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4340
Roland Levillain3e3d7332015-04-28 11:00:54 +01004341 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004342 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004343 const uint32_t dex_method_index_;
4344 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004345
4346 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4347 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004348};
4349
Vladimir Markofcb503c2016-05-18 12:48:17 +01004350class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004351 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004352 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004353 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004354 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004355 uint32_t dex_pc,
4356 uint32_t dex_method_index,
4357 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304358 : HInvoke(kInvokeUnresolved,
4359 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004360 number_of_arguments,
4361 0u /* number_of_other_inputs */,
4362 return_type,
4363 dex_pc,
4364 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004365 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004366 invoke_type) {
4367 }
4368
Artem Serovcced8ba2017-07-19 18:18:09 +01004369 bool IsClonable() const OVERRIDE { return true; }
4370
Calin Juravle175dc732015-08-25 15:42:32 +01004371 DECLARE_INSTRUCTION(InvokeUnresolved);
4372
Artem Serovcced8ba2017-07-19 18:18:09 +01004373 protected:
4374 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004375};
4376
Orion Hodsonac141392017-01-13 11:53:47 +00004377class HInvokePolymorphic FINAL : public HInvoke {
4378 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004379 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004380 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004381 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004382 uint32_t dex_pc,
4383 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304384 : HInvoke(kInvokePolymorphic,
4385 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004386 number_of_arguments,
4387 0u /* number_of_other_inputs */,
4388 return_type,
4389 dex_pc,
4390 dex_method_index,
4391 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304392 kVirtual) {
4393 }
Orion Hodsonac141392017-01-13 11:53:47 +00004394
Artem Serovcced8ba2017-07-19 18:18:09 +01004395 bool IsClonable() const OVERRIDE { return true; }
4396
Orion Hodsonac141392017-01-13 11:53:47 +00004397 DECLARE_INSTRUCTION(InvokePolymorphic);
4398
Artem Serovcced8ba2017-07-19 18:18:09 +01004399 protected:
4400 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004401};
4402
Vladimir Markofcb503c2016-05-18 12:48:17 +01004403class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004404 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004405 // Requirements of this method call regarding the class
4406 // initialization (clinit) check of its declaring class.
4407 enum class ClinitCheckRequirement {
4408 kNone, // Class already initialized.
4409 kExplicit, // Static call having explicit clinit check as last input.
4410 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004411 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004412 };
4413
Vladimir Marko58155012015-08-19 12:49:41 +00004414 // Determines how to load the target ArtMethod*.
4415 enum class MethodLoadKind {
4416 // Use a String init ArtMethod* loaded from Thread entrypoints.
4417 kStringInit,
4418
4419 // Use the method's own ArtMethod* loaded by the register allocator.
4420 kRecursive,
4421
Vladimir Marko65979462017-05-19 17:25:12 +01004422 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4423 // Used for boot image methods referenced by boot image code.
4424 kBootImageLinkTimePcRelative,
4425
Vladimir Marko58155012015-08-19 12:49:41 +00004426 // Use ArtMethod* at a known address, embed the direct address in the code.
4427 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4428 kDirectAddress,
4429
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004430 // Load from an entry in the .bss section using a PC-relative load.
4431 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4432 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004433
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004434 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4435 // used when other kinds are unimplemented on a particular architecture.
4436 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004437 };
4438
4439 // Determines the location of the code pointer.
4440 enum class CodePtrLocation {
4441 // Recursive call, use local PC-relative call instruction.
4442 kCallSelf,
4443
Vladimir Marko58155012015-08-19 12:49:41 +00004444 // Use code pointer from the ArtMethod*.
4445 // Used when we don't know the target code. This is also the last-resort-kind used when
4446 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4447 kCallArtMethod,
4448 };
4449
4450 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004451 MethodLoadKind method_load_kind;
4452 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004453 // The method load data holds
4454 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4455 // Note that there are multiple string init methods, each having its own offset.
4456 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004457 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004458 };
4459
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004460 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004461 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004462 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004463 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004464 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004465 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004466 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004467 InvokeType invoke_type,
4468 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004469 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304470 : HInvoke(kInvokeStaticOrDirect,
4471 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004472 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004473 // There is potentially one extra argument for the HCurrentMethod node, and
4474 // potentially one other if the clinit check is explicit, and potentially
4475 // one other if the method is a string factory.
4476 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004477 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004478 return_type,
4479 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004480 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004481 resolved_method,
4482 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004483 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004484 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004485 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4486 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004487
Artem Serovcced8ba2017-07-19 18:18:09 +01004488 bool IsClonable() const OVERRIDE { return true; }
4489
Vladimir Markodc151b22015-10-15 18:02:30 +01004490 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004491 bool had_current_method_input = HasCurrentMethodInput();
4492 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4493
4494 // Using the current method is the default and once we find a better
4495 // method load kind, we should not go back to using the current method.
4496 DCHECK(had_current_method_input || !needs_current_method_input);
4497
4498 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004499 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4500 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004501 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004502 dispatch_info_ = dispatch_info;
4503 }
4504
Aart Bik6daebeb2017-04-03 14:35:41 -07004505 DispatchInfo GetDispatchInfo() const {
4506 return dispatch_info_;
4507 }
4508
Vladimir Markoc53c0792015-11-19 15:48:33 +00004509 void AddSpecialInput(HInstruction* input) {
4510 // We allow only one special input.
4511 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4512 DCHECK(InputCount() == GetSpecialInputIndex() ||
4513 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4514 InsertInputAt(GetSpecialInputIndex(), input);
4515 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004516
Vladimir Marko372f10e2016-05-17 16:30:10 +01004517 using HInstruction::GetInputRecords; // Keep the const version visible.
4518 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4519 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4520 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4521 DCHECK(!input_records.empty());
4522 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4523 HInstruction* last_input = input_records.back().GetInstruction();
4524 // Note: `last_input` may be null during arguments setup.
4525 if (last_input != nullptr) {
4526 // `last_input` is the last input of a static invoke marked as having
4527 // an explicit clinit check. It must either be:
4528 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4529 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4530 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4531 }
4532 }
4533 return input_records;
4534 }
4535
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004536 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004537 // We access the method via the dex cache so we can't do an implicit null check.
4538 // TODO: for intrinsics we can generate implicit null checks.
4539 return false;
4540 }
4541
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004542 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004543 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004544 }
4545
Vladimir Markoc53c0792015-11-19 15:48:33 +00004546 // Get the index of the special input, if any.
4547 //
David Brazdil6de19382016-01-08 17:37:10 +00004548 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4549 // method pointer; otherwise there may be one platform-specific special input,
4550 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004551 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004552 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004553
Vladimir Marko58155012015-08-19 12:49:41 +00004554 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4555 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4556 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004557 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004558 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004559 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004560 bool HasPcRelativeMethodLoadKind() const {
4561 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004562 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004563 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004564 bool HasCurrentMethodInput() const {
4565 // This function can be called only after the invoke has been fully initialized by the builder.
4566 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004567 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004568 return true;
4569 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004570 DCHECK(InputCount() == GetSpecialInputIndex() ||
4571 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004572 return false;
4573 }
4574 }
Vladimir Marko58155012015-08-19 12:49:41 +00004575
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004576 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004577 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004578 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004579 }
4580
4581 uint64_t GetMethodAddress() const {
4582 DCHECK(HasMethodAddress());
4583 return dispatch_info_.method_load_data;
4584 }
4585
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004586 const DexFile& GetDexFileForPcRelativeDexCache() const;
4587
Vladimir Markoa1de9182016-02-25 11:37:38 +00004588 ClinitCheckRequirement GetClinitCheckRequirement() const {
4589 return GetPackedField<ClinitCheckRequirementField>();
4590 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004591
Roland Levillain4c0eb422015-04-24 16:43:49 +01004592 // Is this instruction a call to a static method?
4593 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004594 return GetInvokeType() == kStatic;
4595 }
4596
4597 MethodReference GetTargetMethod() const {
4598 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004599 }
4600
Vladimir Markofbb184a2015-11-13 14:47:00 +00004601 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4602 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4603 // instruction; only relevant for static calls with explicit clinit check.
4604 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004605 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004606 size_t last_input_index = inputs_.size() - 1u;
4607 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004608 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004609 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004610 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004611 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004612 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004613 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004614 }
4615
4616 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004617 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004618 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004619 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004620 }
4621
4622 // Is this a call to a static method whose declaring class has an
4623 // implicit intialization check requirement?
4624 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004625 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004626 }
4627
Vladimir Markob554b5a2015-11-06 12:57:55 +00004628 // Does this method load kind need the current method as an input?
4629 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004630 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004631 }
4632
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004633 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004634
Artem Serovcced8ba2017-07-19 18:18:09 +01004635 protected:
4636 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4637
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004638 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004639 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004640 static constexpr size_t kFieldClinitCheckRequirementSize =
4641 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4642 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4643 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4644 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4645 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004646 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4647 kFieldClinitCheckRequirement,
4648 kFieldClinitCheckRequirementSize>;
4649
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004650 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004651 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004652 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004653};
Vladimir Markof64242a2015-12-01 14:58:23 +00004654std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004655std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004656
Vladimir Markofcb503c2016-05-18 12:48:17 +01004657class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004658 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004659 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004660 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004661 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004662 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004663 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004664 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004665 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304666 : HInvoke(kInvokeVirtual,
4667 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004668 number_of_arguments,
4669 0u,
4670 return_type,
4671 dex_pc,
4672 dex_method_index,
4673 resolved_method,
4674 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304675 vtable_index_(vtable_index) {
4676 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004677
Artem Serovcced8ba2017-07-19 18:18:09 +01004678 bool IsClonable() const OVERRIDE { return true; }
4679
Aart Bik71bf7b42016-11-16 10:17:46 -08004680 bool CanBeNull() const OVERRIDE {
4681 switch (GetIntrinsic()) {
4682 case Intrinsics::kThreadCurrentThread:
4683 case Intrinsics::kStringBufferAppend:
4684 case Intrinsics::kStringBufferToString:
4685 case Intrinsics::kStringBuilderAppend:
4686 case Intrinsics::kStringBuilderToString:
4687 return false;
4688 default:
4689 return HInvoke::CanBeNull();
4690 }
4691 }
4692
Calin Juravle641547a2015-04-21 22:08:51 +01004693 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004694 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004695 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004696 }
4697
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004698 uint32_t GetVTableIndex() const { return vtable_index_; }
4699
4700 DECLARE_INSTRUCTION(InvokeVirtual);
4701
Artem Serovcced8ba2017-07-19 18:18:09 +01004702 protected:
4703 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4704
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004705 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004706 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004707 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004708};
4709
Vladimir Markofcb503c2016-05-18 12:48:17 +01004710class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004711 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004712 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004713 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004714 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004715 uint32_t dex_pc,
4716 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004717 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004718 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304719 : HInvoke(kInvokeInterface,
4720 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004721 number_of_arguments,
4722 0u,
4723 return_type,
4724 dex_pc,
4725 dex_method_index,
4726 resolved_method,
4727 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304728 imt_index_(imt_index) {
4729 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004730
Artem Serovcced8ba2017-07-19 18:18:09 +01004731 bool IsClonable() const OVERRIDE { return true; }
4732
Calin Juravle641547a2015-04-21 22:08:51 +01004733 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004734 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004735 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004736 }
4737
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004738 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4739 // The assembly stub currently needs it.
4740 return true;
4741 }
4742
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004743 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004744
4745 DECLARE_INSTRUCTION(InvokeInterface);
4746
Artem Serovcced8ba2017-07-19 18:18:09 +01004747 protected:
4748 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4749
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004750 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004751 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004752 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004753};
4754
Vladimir Markofcb503c2016-05-18 12:48:17 +01004755class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004756 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004757 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304758 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004759 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004760 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004761
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004762 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004763
4764 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004765 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004766 }
4767 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004768 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004769 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004770 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4771 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4772 }
4773 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4774 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4775 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004776
Roland Levillain88cb1752014-10-20 16:36:47 +01004777 DECLARE_INSTRUCTION(Neg);
4778
Artem Serovcced8ba2017-07-19 18:18:09 +01004779 protected:
4780 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004781};
4782
Vladimir Markofcb503c2016-05-18 12:48:17 +01004783class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004784 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004785 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304786 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004787 SetRawInputAt(0, cls);
4788 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004789 }
4790
Artem Serovcced8ba2017-07-19 18:18:09 +01004791 bool IsClonable() const OVERRIDE { return true; }
4792
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004793 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004794 bool NeedsEnvironment() const OVERRIDE { return true; }
4795
Mingyao Yang0c365e62015-03-31 15:09:29 -07004796 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4797 bool CanThrow() const OVERRIDE { return true; }
4798
Calin Juravle10e244f2015-01-26 18:54:32 +00004799 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004800
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004801 HLoadClass* GetLoadClass() const {
4802 DCHECK(InputAt(0)->IsLoadClass());
4803 return InputAt(0)->AsLoadClass();
4804 }
4805
4806 HInstruction* GetLength() const {
4807 return InputAt(1);
4808 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004809
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004810 DECLARE_INSTRUCTION(NewArray);
4811
Artem Serovcced8ba2017-07-19 18:18:09 +01004812 protected:
4813 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004814};
4815
Vladimir Markofcb503c2016-05-18 12:48:17 +01004816class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004817 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004818 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004819 HInstruction* left,
4820 HInstruction* right,
4821 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304822 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4823 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004824
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004825 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004826
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004827 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004828
4829 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004830 return GetBlock()->GetGraph()->GetIntConstant(
4831 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004832 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004834 return GetBlock()->GetGraph()->GetLongConstant(
4835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004836 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004837 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4838 return GetBlock()->GetGraph()->GetFloatConstant(
4839 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4840 }
4841 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4842 return GetBlock()->GetGraph()->GetDoubleConstant(
4843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4844 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004845
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004846 DECLARE_INSTRUCTION(Add);
4847
Artem Serovcced8ba2017-07-19 18:18:09 +01004848 protected:
4849 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004850};
4851
Vladimir Markofcb503c2016-05-18 12:48:17 +01004852class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004853 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004854 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004855 HInstruction* left,
4856 HInstruction* right,
4857 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304858 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4859 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004860
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004861 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004862
4863 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004864 return GetBlock()->GetGraph()->GetIntConstant(
4865 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004866 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004867 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004868 return GetBlock()->GetGraph()->GetLongConstant(
4869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004870 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004871 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4872 return GetBlock()->GetGraph()->GetFloatConstant(
4873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4874 }
4875 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4876 return GetBlock()->GetGraph()->GetDoubleConstant(
4877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4878 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004879
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004880 DECLARE_INSTRUCTION(Sub);
4881
Artem Serovcced8ba2017-07-19 18:18:09 +01004882 protected:
4883 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004884};
4885
Vladimir Markofcb503c2016-05-18 12:48:17 +01004886class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004887 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004888 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004889 HInstruction* left,
4890 HInstruction* right,
4891 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304892 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4893 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004894
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004895 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004896
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004897 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004898
4899 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004900 return GetBlock()->GetGraph()->GetIntConstant(
4901 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004902 }
4903 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004904 return GetBlock()->GetGraph()->GetLongConstant(
4905 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004906 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004907 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4908 return GetBlock()->GetGraph()->GetFloatConstant(
4909 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4910 }
4911 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4912 return GetBlock()->GetGraph()->GetDoubleConstant(
4913 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4914 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004915
4916 DECLARE_INSTRUCTION(Mul);
4917
Artem Serovcced8ba2017-07-19 18:18:09 +01004918 protected:
4919 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004920};
4921
Vladimir Markofcb503c2016-05-18 12:48:17 +01004922class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004923 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004924 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004925 HInstruction* left,
4926 HInstruction* right,
4927 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304928 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4929 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004930
Roland Levillain9867bc72015-08-05 10:21:34 +01004931 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004932 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004933 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004934 // Our graph structure ensures we never have 0 for `y` during
4935 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004936 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004937 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004938 return (y == -1) ? -x : x / y;
4939 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004940
Roland Levillain31dd3d62016-02-16 12:21:02 +00004941 template <typename T>
4942 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004943 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004944 return x / y;
4945 }
4946
Roland Levillain9867bc72015-08-05 10:21:34 +01004947 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004948 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004949 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004950 }
4951 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004952 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004953 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4954 }
4955 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4956 return GetBlock()->GetGraph()->GetFloatConstant(
4957 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4958 }
4959 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4960 return GetBlock()->GetGraph()->GetDoubleConstant(
4961 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004962 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004963
4964 DECLARE_INSTRUCTION(Div);
4965
Artem Serovcced8ba2017-07-19 18:18:09 +01004966 protected:
4967 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004968};
4969
Vladimir Markofcb503c2016-05-18 12:48:17 +01004970class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004971 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004972 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004973 HInstruction* left,
4974 HInstruction* right,
4975 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304976 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4977 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004978
Roland Levillain9867bc72015-08-05 10:21:34 +01004979 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004980 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004981 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004982 // Our graph structure ensures we never have 0 for `y` during
4983 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004984 DCHECK_NE(y, 0);
4985 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4986 return (y == -1) ? 0 : x % y;
4987 }
4988
Roland Levillain31dd3d62016-02-16 12:21:02 +00004989 template <typename T>
4990 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004991 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004992 return std::fmod(x, y);
4993 }
4994
Roland Levillain9867bc72015-08-05 10:21:34 +01004995 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004996 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004997 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004998 }
4999 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005000 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005001 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005002 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005003 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
5004 return GetBlock()->GetGraph()->GetFloatConstant(
5005 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5006 }
5007 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
5008 return GetBlock()->GetGraph()->GetDoubleConstant(
5009 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5010 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005011
5012 DECLARE_INSTRUCTION(Rem);
5013
Artem Serovcced8ba2017-07-19 18:18:09 +01005014 protected:
5015 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005016};
5017
Vladimir Markofcb503c2016-05-18 12:48:17 +01005018class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005019 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005020 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5021 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005022 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305023 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005024 SetRawInputAt(0, value);
5025 }
5026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005027 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06005028
Calin Juravled0d48522014-11-04 16:40:20 +00005029 bool CanBeMoved() const OVERRIDE { return true; }
5030
Vladimir Marko372f10e2016-05-17 16:30:10 +01005031 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005032 return true;
5033 }
5034
5035 bool NeedsEnvironment() const OVERRIDE { return true; }
5036 bool CanThrow() const OVERRIDE { return true; }
5037
Calin Juravled0d48522014-11-04 16:40:20 +00005038 DECLARE_INSTRUCTION(DivZeroCheck);
5039
Artem Serovcced8ba2017-07-19 18:18:09 +01005040 protected:
5041 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005042};
5043
Vladimir Markofcb503c2016-05-18 12:48:17 +01005044class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005045 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005046 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005047 HInstruction* value,
5048 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005049 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305050 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005051 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5052 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005053 }
5054
Roland Levillain5b5b9312016-03-22 14:57:31 +00005055 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005056 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005057 return value << (distance & max_shift_distance);
5058 }
5059
5060 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005061 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005062 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005063 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005064 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005065 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005066 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005067 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005068 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5069 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5070 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5071 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005072 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005073 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5074 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005075 LOG(FATAL) << DebugName() << " is not defined for float values";
5076 UNREACHABLE();
5077 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005078 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5079 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005080 LOG(FATAL) << DebugName() << " is not defined for double values";
5081 UNREACHABLE();
5082 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005083
5084 DECLARE_INSTRUCTION(Shl);
5085
Artem Serovcced8ba2017-07-19 18:18:09 +01005086 protected:
5087 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005088};
5089
Vladimir Markofcb503c2016-05-18 12:48:17 +01005090class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005091 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005092 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005093 HInstruction* value,
5094 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005095 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305096 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005097 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5098 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005099 }
5100
Roland Levillain5b5b9312016-03-22 14:57:31 +00005101 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005102 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005103 return value >> (distance & max_shift_distance);
5104 }
5105
5106 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005107 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005108 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005109 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005110 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005111 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005112 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005113 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005114 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5115 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5116 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5117 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005118 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005119 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5120 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005121 LOG(FATAL) << DebugName() << " is not defined for float values";
5122 UNREACHABLE();
5123 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005124 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5125 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005126 LOG(FATAL) << DebugName() << " is not defined for double values";
5127 UNREACHABLE();
5128 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005129
5130 DECLARE_INSTRUCTION(Shr);
5131
Artem Serovcced8ba2017-07-19 18:18:09 +01005132 protected:
5133 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005134};
5135
Vladimir Markofcb503c2016-05-18 12:48:17 +01005136class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005137 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005138 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005139 HInstruction* value,
5140 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305142 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005143 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5144 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005145 }
5146
Roland Levillain5b5b9312016-03-22 14:57:31 +00005147 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005148 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005149 typedef typename std::make_unsigned<T>::type V;
5150 V ux = static_cast<V>(value);
5151 return static_cast<T>(ux >> (distance & max_shift_distance));
5152 }
5153
5154 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005155 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005156 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005157 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005158 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005159 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005160 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005161 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005162 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5163 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5164 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5165 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005166 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005167 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5168 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005169 LOG(FATAL) << DebugName() << " is not defined for float values";
5170 UNREACHABLE();
5171 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005172 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5173 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005174 LOG(FATAL) << DebugName() << " is not defined for double values";
5175 UNREACHABLE();
5176 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005177
5178 DECLARE_INSTRUCTION(UShr);
5179
Artem Serovcced8ba2017-07-19 18:18:09 +01005180 protected:
5181 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005182};
5183
Vladimir Markofcb503c2016-05-18 12:48:17 +01005184class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005185 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005186 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005187 HInstruction* left,
5188 HInstruction* right,
5189 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305190 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5191 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005192
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005193 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005194
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005195 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005196
5197 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005198 return GetBlock()->GetGraph()->GetIntConstant(
5199 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005200 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005201 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005202 return GetBlock()->GetGraph()->GetLongConstant(
5203 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005204 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005205 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5206 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5207 LOG(FATAL) << DebugName() << " is not defined for float values";
5208 UNREACHABLE();
5209 }
5210 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5211 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5212 LOG(FATAL) << DebugName() << " is not defined for double values";
5213 UNREACHABLE();
5214 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005215
5216 DECLARE_INSTRUCTION(And);
5217
Artem Serovcced8ba2017-07-19 18:18:09 +01005218 protected:
5219 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005220};
5221
Vladimir Markofcb503c2016-05-18 12:48:17 +01005222class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005223 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005224 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005225 HInstruction* left,
5226 HInstruction* right,
5227 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305228 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5229 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005230
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005231 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005232
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005233 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005234
5235 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005236 return GetBlock()->GetGraph()->GetIntConstant(
5237 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005238 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005239 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005240 return GetBlock()->GetGraph()->GetLongConstant(
5241 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005242 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005243 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5244 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5245 LOG(FATAL) << DebugName() << " is not defined for float values";
5246 UNREACHABLE();
5247 }
5248 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5249 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5250 LOG(FATAL) << DebugName() << " is not defined for double values";
5251 UNREACHABLE();
5252 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005253
5254 DECLARE_INSTRUCTION(Or);
5255
Artem Serovcced8ba2017-07-19 18:18:09 +01005256 protected:
5257 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005258};
5259
Vladimir Markofcb503c2016-05-18 12:48:17 +01005260class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005261 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005262 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005263 HInstruction* left,
5264 HInstruction* right,
5265 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305266 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5267 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005268
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005269 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005270
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005271 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005272
5273 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005274 return GetBlock()->GetGraph()->GetIntConstant(
5275 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005276 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005277 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005278 return GetBlock()->GetGraph()->GetLongConstant(
5279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005280 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005281 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5282 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5283 LOG(FATAL) << DebugName() << " is not defined for float values";
5284 UNREACHABLE();
5285 }
5286 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5287 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5288 LOG(FATAL) << DebugName() << " is not defined for double values";
5289 UNREACHABLE();
5290 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005291
5292 DECLARE_INSTRUCTION(Xor);
5293
Artem Serovcced8ba2017-07-19 18:18:09 +01005294 protected:
5295 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005296};
5297
Vladimir Markofcb503c2016-05-18 12:48:17 +01005298class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005299 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005300 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305301 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005302 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5303 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005304 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005305
Roland Levillain5b5b9312016-03-22 14:57:31 +00005306 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005307 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005308 typedef typename std::make_unsigned<T>::type V;
5309 V ux = static_cast<V>(value);
5310 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005311 return static_cast<T>(ux);
5312 } else {
5313 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005314 return static_cast<T>(ux >> (distance & max_shift_value)) |
5315 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005316 }
5317 }
5318
Roland Levillain5b5b9312016-03-22 14:57:31 +00005319 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005320 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005321 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005322 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005323 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005324 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005325 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005326 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005327 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5328 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5329 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5330 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005331 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005332 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5333 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005334 LOG(FATAL) << DebugName() << " is not defined for float values";
5335 UNREACHABLE();
5336 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005337 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5338 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005339 LOG(FATAL) << DebugName() << " is not defined for double values";
5340 UNREACHABLE();
5341 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005342
5343 DECLARE_INSTRUCTION(Ror);
5344
Artem Serovcced8ba2017-07-19 18:18:09 +01005345 protected:
5346 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005347};
5348
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005349// The value of a parameter in this method. Its location depends on
5350// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005351class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005352 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005353 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005354 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005355 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005357 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305358 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005359 dex_file_(dex_file),
5360 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005361 index_(index) {
5362 SetPackedFlag<kFlagIsThis>(is_this);
5363 SetPackedFlag<kFlagCanBeNull>(!is_this);
5364 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005365
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005366 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005367 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005368 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005369 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005370
Vladimir Markoa1de9182016-02-25 11:37:38 +00005371 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5372 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005373
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005374 DECLARE_INSTRUCTION(ParameterValue);
5375
Artem Serovcced8ba2017-07-19 18:18:09 +01005376 protected:
5377 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5378
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005379 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005380 // Whether or not the parameter value corresponds to 'this' argument.
5381 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5382 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5383 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5384 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5385 "Too many packed fields.");
5386
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005387 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005388 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005389 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005390 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005391 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005392};
5393
Vladimir Markofcb503c2016-05-18 12:48:17 +01005394class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005395 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005396 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305397 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5398 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005399
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005400 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005401 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005402 return true;
5403 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005404
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005405 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005406
5407 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005408 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005409 }
5410 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005411 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005412 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005413 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5414 LOG(FATAL) << DebugName() << " is not defined for float values";
5415 UNREACHABLE();
5416 }
5417 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5418 LOG(FATAL) << DebugName() << " is not defined for double values";
5419 UNREACHABLE();
5420 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005421
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005422 DECLARE_INSTRUCTION(Not);
5423
Artem Serovcced8ba2017-07-19 18:18:09 +01005424 protected:
5425 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005426};
5427
Vladimir Markofcb503c2016-05-18 12:48:17 +01005428class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005429 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005430 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305431 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5432 }
David Brazdil66d126e2015-04-03 16:02:44 +01005433
5434 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005435 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005436 return true;
5437 }
5438
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005439 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005440 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005441 return !x;
5442 }
5443
Roland Levillain9867bc72015-08-05 10:21:34 +01005444 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005445 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005446 }
5447 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5448 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005449 UNREACHABLE();
5450 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005451 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5452 LOG(FATAL) << DebugName() << " is not defined for float values";
5453 UNREACHABLE();
5454 }
5455 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5456 LOG(FATAL) << DebugName() << " is not defined for double values";
5457 UNREACHABLE();
5458 }
David Brazdil66d126e2015-04-03 16:02:44 +01005459
5460 DECLARE_INSTRUCTION(BooleanNot);
5461
Artem Serovcced8ba2017-07-19 18:18:09 +01005462 protected:
5463 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005464};
5465
Vladimir Markofcb503c2016-05-18 12:48:17 +01005466class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005467 public:
5468 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005469 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305470 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005471 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005472 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005473 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005474 }
5475
5476 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005477 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5478 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005479
5480 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005481 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5482 return true;
5483 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005484
Mark Mendelle82549b2015-05-06 10:55:34 -04005485 // Try to statically evaluate the conversion and return a HConstant
5486 // containing the result. If the input cannot be converted, return nullptr.
5487 HConstant* TryStaticEvaluation() const;
5488
Roland Levillaindff1f282014-11-05 14:15:05 +00005489 DECLARE_INSTRUCTION(TypeConversion);
5490
Artem Serovcced8ba2017-07-19 18:18:09 +01005491 protected:
5492 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005493};
5494
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005495static constexpr uint32_t kNoRegNumber = -1;
5496
Vladimir Markofcb503c2016-05-18 12:48:17 +01005497class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005498 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005499 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5500 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005501 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305502 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005503 SetRawInputAt(0, value);
5504 }
5505
Artem Serovcced8ba2017-07-19 18:18:09 +01005506 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005507 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005508 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005509 return true;
5510 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005511
Calin Juravle10e244f2015-01-26 18:54:32 +00005512 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005513
Calin Juravle10e244f2015-01-26 18:54:32 +00005514 bool CanThrow() const OVERRIDE { return true; }
5515
5516 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005517
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005518 DECLARE_INSTRUCTION(NullCheck);
5519
Artem Serovcced8ba2017-07-19 18:18:09 +01005520 protected:
5521 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005522};
5523
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005524// Embeds an ArtField and all the information required by the compiler. We cache
5525// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005526class FieldInfo : public ValueObject {
5527 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005528 FieldInfo(ArtField* field,
5529 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005531 bool is_volatile,
5532 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005533 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005534 const DexFile& dex_file)
5535 : field_(field),
5536 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005537 field_type_(field_type),
5538 is_volatile_(is_volatile),
5539 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005540 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005541 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005542
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005543 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005544 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005545 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005546 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005547 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005548 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005549 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005550
5551 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005552 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005553 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005554 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005555 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005556 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005557 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005558 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005559};
5560
Vladimir Markofcb503c2016-05-18 12:48:17 +01005561class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005562 public:
5563 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005564 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005565 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005566 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005567 bool is_volatile,
5568 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005569 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005570 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005571 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305572 : HExpression(kInstanceFieldGet,
5573 field_type,
5574 SideEffects::FieldReadOfType(field_type, is_volatile),
5575 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005576 field_info_(field,
5577 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005578 field_type,
5579 is_volatile,
5580 field_idx,
5581 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005582 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005583 SetRawInputAt(0, value);
5584 }
5585
Artem Serovcced8ba2017-07-19 18:18:09 +01005586 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005587 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005588
Vladimir Marko372f10e2016-05-17 16:30:10 +01005589 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5590 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005591 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005592 }
5593
Calin Juravle641547a2015-04-21 22:08:51 +01005594 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005595 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005596 }
5597
5598 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005599 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5600 }
5601
Calin Juravle52c48962014-12-16 17:02:57 +00005602 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005603 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005604 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005605 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005606
Vladimir Marko61b92282017-10-11 13:23:17 +01005607 void SetType(DataType::Type new_type) {
5608 DCHECK(DataType::IsIntegralType(GetType()));
5609 DCHECK(DataType::IsIntegralType(new_type));
5610 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5611 SetPackedField<TypeField>(new_type);
5612 }
5613
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005614 DECLARE_INSTRUCTION(InstanceFieldGet);
5615
Artem Serovcced8ba2017-07-19 18:18:09 +01005616 protected:
5617 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5618
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005619 private:
5620 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005621};
5622
Vladimir Markofcb503c2016-05-18 12:48:17 +01005623class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005624 public:
5625 HInstanceFieldSet(HInstruction* object,
5626 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005627 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005628 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005629 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005630 bool is_volatile,
5631 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005632 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005633 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005634 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305635 : HTemplateInstruction(kInstanceFieldSet,
5636 SideEffects::FieldWriteOfType(field_type, is_volatile),
5637 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005638 field_info_(field,
5639 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005640 field_type,
5641 is_volatile,
5642 field_idx,
5643 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005644 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005645 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005646 SetRawInputAt(0, object);
5647 SetRawInputAt(1, value);
5648 }
5649
Artem Serovcced8ba2017-07-19 18:18:09 +01005650 bool IsClonable() const OVERRIDE { return true; }
5651
Calin Juravle641547a2015-04-21 22:08:51 +01005652 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005653 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005654 }
5655
Calin Juravle52c48962014-12-16 17:02:57 +00005656 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005657 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005658 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005659 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005660 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005661 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5662 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005663
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005664 DECLARE_INSTRUCTION(InstanceFieldSet);
5665
Artem Serovcced8ba2017-07-19 18:18:09 +01005666 protected:
5667 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5668
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005669 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005670 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5671 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5672 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5673 "Too many packed fields.");
5674
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005675 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005676};
5677
Vladimir Markofcb503c2016-05-18 12:48:17 +01005678class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005679 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005680 HArrayGet(HInstruction* array,
5681 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005682 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005683 uint32_t dex_pc)
5684 : HArrayGet(array,
5685 index,
5686 type,
5687 SideEffects::ArrayReadOfType(type),
5688 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305689 /* is_string_char_at */ false) {
5690 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005691
5692 HArrayGet(HInstruction* array,
5693 HInstruction* index,
5694 DataType::Type type,
5695 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005696 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005697 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305698 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005699 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005700 SetRawInputAt(0, array);
5701 SetRawInputAt(1, index);
5702 }
5703
Artem Serovcced8ba2017-07-19 18:18:09 +01005704 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005705 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005706 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005707 return true;
5708 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005709 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005710 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005711 // Currently, unless the array is the result of NewArray, the array access is always
5712 // preceded by some form of null NullCheck necessary for the bounds check, usually
5713 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5714 // dynamic BCE. There are cases when these could be removed to produce better code.
5715 // If we ever add optimizations to do so we should allow an implicit check here
5716 // (as long as the address falls in the first page).
5717 //
5718 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5719 // a = cond ? new int[1] : null;
5720 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005721 return false;
5722 }
5723
David Brazdil4833f5a2015-12-16 10:37:39 +00005724 bool IsEquivalentOf(HArrayGet* other) const {
5725 bool result = (GetDexPc() == other->GetDexPc());
5726 if (kIsDebugBuild && result) {
5727 DCHECK_EQ(GetBlock(), other->GetBlock());
5728 DCHECK_EQ(GetArray(), other->GetArray());
5729 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005730 if (DataType::IsIntOrLongType(GetType())) {
5731 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005732 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005733 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5734 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005735 }
5736 }
5737 return result;
5738 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005739
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005740 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5741
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005742 HInstruction* GetArray() const { return InputAt(0); }
5743 HInstruction* GetIndex() const { return InputAt(1); }
5744
Vladimir Marko61b92282017-10-11 13:23:17 +01005745 void SetType(DataType::Type new_type) {
5746 DCHECK(DataType::IsIntegralType(GetType()));
5747 DCHECK(DataType::IsIntegralType(new_type));
5748 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5749 SetPackedField<TypeField>(new_type);
5750 }
5751
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005752 DECLARE_INSTRUCTION(ArrayGet);
5753
Artem Serovcced8ba2017-07-19 18:18:09 +01005754 protected:
5755 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5756
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005757 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005758 // We treat a String as an array, creating the HArrayGet from String.charAt()
5759 // intrinsic in the instruction simplifier. We can always determine whether
5760 // a particular HArrayGet is actually a String.charAt() by looking at the type
5761 // of the input but that requires holding the mutator lock, so we prefer to use
5762 // a flag, so that code generators don't need to do the locking.
5763 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5764 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5765 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5766 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005767};
5768
Vladimir Markofcb503c2016-05-18 12:48:17 +01005769class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005770 public:
5771 HArraySet(HInstruction* array,
5772 HInstruction* index,
5773 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005775 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005776 : HArraySet(array,
5777 index,
5778 value,
5779 expected_component_type,
5780 // Make a best guess for side effects now, may be refined during SSA building.
5781 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305782 dex_pc) {
5783 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005784
5785 HArraySet(HInstruction* array,
5786 HInstruction* index,
5787 HInstruction* value,
5788 DataType::Type expected_component_type,
5789 SideEffects side_effects,
5790 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305791 : HTemplateInstruction(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005792 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005793 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005794 SetPackedFlag<kFlagValueCanBeNull>(true);
5795 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005796 SetRawInputAt(0, array);
5797 SetRawInputAt(1, index);
5798 SetRawInputAt(2, value);
5799 }
5800
Artem Serovcced8ba2017-07-19 18:18:09 +01005801 bool IsClonable() const OVERRIDE { return true; }
5802
Calin Juravle77520bc2015-01-12 18:45:46 +00005803 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005804 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005805 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005806 }
5807
Mingyao Yang81014cb2015-06-02 03:16:27 -07005808 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005809 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005810
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005811 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005812 // TODO: Same as for ArrayGet.
5813 return false;
5814 }
5815
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005816 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005817 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005818 }
5819
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005820 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005821 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005822 }
5823
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005824 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005825 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005826 }
5827
Vladimir Markoa1de9182016-02-25 11:37:38 +00005828 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5829 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5830 bool StaticTypeOfArrayIsObjectArray() const {
5831 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5832 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005833
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005834 HInstruction* GetArray() const { return InputAt(0); }
5835 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005836 HInstruction* GetValue() const { return InputAt(2); }
5837
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005838 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005839 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5840 }
5841
5842 static DataType::Type GetComponentType(DataType::Type value_type,
5843 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005844 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005845 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005846 // be correct, we also check what is the value type. If it is a floating
5847 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005848 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005849 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005850 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005851 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005852
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005853 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005854 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005855 }
5856
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005857 static SideEffects ComputeSideEffects(DataType::Type type) {
5858 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005859 }
5860
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005861 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5862 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5863 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005864 }
5865
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005866 DECLARE_INSTRUCTION(ArraySet);
5867
Artem Serovcced8ba2017-07-19 18:18:09 +01005868 protected:
5869 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5870
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005871 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005872 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5873 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005874 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005875 static constexpr size_t kFlagNeedsTypeCheck =
5876 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5877 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005878 // Cached information for the reference_type_info_ so that codegen
5879 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005880 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5881 static constexpr size_t kNumberOfArraySetPackedBits =
5882 kFlagStaticTypeOfArrayIsObjectArray + 1;
5883 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5884 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005886};
5887
Vladimir Markofcb503c2016-05-18 12:48:17 +01005888class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005889 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005890 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305891 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005892 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005893 // Note that arrays do not change length, so the instruction does not
5894 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005895 SetRawInputAt(0, array);
5896 }
5897
Artem Serovcced8ba2017-07-19 18:18:09 +01005898 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005899 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005900 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005901 return true;
5902 }
Calin Juravle641547a2015-04-21 22:08:51 +01005903 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5904 return obj == InputAt(0);
5905 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005906
Vladimir Markodce016e2016-04-28 13:10:02 +01005907 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5908
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005909 DECLARE_INSTRUCTION(ArrayLength);
5910
Artem Serovcced8ba2017-07-19 18:18:09 +01005911 protected:
5912 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
5913
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005914 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005915 // We treat a String as an array, creating the HArrayLength from String.length()
5916 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5917 // determine whether a particular HArrayLength is actually a String.length() by
5918 // looking at the type of the input but that requires holding the mutator lock, so
5919 // we prefer to use a flag, so that code generators don't need to do the locking.
5920 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5921 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5922 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5923 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005924};
5925
Vladimir Markofcb503c2016-05-18 12:48:17 +01005926class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005927 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005928 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5929 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005930 HBoundsCheck(HInstruction* index,
5931 HInstruction* length,
5932 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00005933 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305934 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005935 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00005936 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005937 SetRawInputAt(0, index);
5938 SetRawInputAt(1, length);
5939 }
5940
Artem Serovcced8ba2017-07-19 18:18:09 +01005941 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005942 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005943 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005944 return true;
5945 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005946
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005947 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005948
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005949 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005950
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005951 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005952
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005953 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005954
5955 DECLARE_INSTRUCTION(BoundsCheck);
5956
Artem Serovcced8ba2017-07-19 18:18:09 +01005957 protected:
5958 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
5959
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005960 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005961 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005962};
5963
Vladimir Markofcb503c2016-05-18 12:48:17 +01005964class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005965 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005966 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305967 : HTemplateInstruction(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
5968 slow_path_(nullptr) {
5969 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005970
Artem Serovcced8ba2017-07-19 18:18:09 +01005971 bool IsClonable() const OVERRIDE { return true; }
5972
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005973 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005974 return true;
5975 }
5976
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005977 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5978 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005979
5980 DECLARE_INSTRUCTION(SuspendCheck);
5981
Artem Serovcced8ba2017-07-19 18:18:09 +01005982 protected:
5983 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
5984
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005985 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005986 // Only used for code generation, in order to share the same slow path between back edges
5987 // of a same loop.
5988 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005989};
5990
David Srbecky0cf44932015-12-09 14:09:59 +00005991// Pseudo-instruction which provides the native debugger with mapping information.
5992// It ensures that we can generate line number and local variables at this point.
5993class HNativeDebugInfo : public HTemplateInstruction<0> {
5994 public:
5995 explicit HNativeDebugInfo(uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305996 : HTemplateInstruction<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
5997 }
David Srbecky0cf44932015-12-09 14:09:59 +00005998
5999 bool NeedsEnvironment() const OVERRIDE {
6000 return true;
6001 }
6002
6003 DECLARE_INSTRUCTION(NativeDebugInfo);
6004
Artem Serovcced8ba2017-07-19 18:18:09 +01006005 protected:
6006 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006007};
6008
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006009/**
6010 * Instruction to load a Class object.
6011 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006012class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006013 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006014 // Determines how to load the Class.
6015 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006016 // We cannot load this class. See HSharpening::SharpenLoadClass.
6017 kInvalid = -1,
6018
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006019 // Use the Class* from the method's own ArtMethod*.
6020 kReferrersClass,
6021
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006022 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006023 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006024 kBootImageLinkTimePcRelative,
6025
6026 // Use a known boot image Class* address, embedded in the code by the codegen.
6027 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006028 kBootImageAddress,
6029
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006030 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
6031 // of the oat file.
6032 kBootImageClassTable,
6033
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006034 // Load from an entry in the .bss section using a PC-relative load.
6035 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6036 kBssEntry,
6037
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006038 // Load from the root table associated with the JIT compiled method.
6039 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006040
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006041 // Load using a simple runtime call. This is the fall-back load kind when
6042 // the codegen is unable to use another appropriate kind.
6043 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006044
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006045 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 };
6047
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006048 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006049 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006050 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006051 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006052 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006053 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006054 bool needs_access_check)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306055 : HInstruction(kLoadClass, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006056 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006057 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006058 dex_file_(dex_file),
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006059 klass_(klass),
6060 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006061 // Referrers class should not need access check. We never inline unverified
6062 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006063 DCHECK(!is_referrers_class || !needs_access_check);
6064
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006065 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006066 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006067 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006068 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006070 }
6071
Artem Serovcced8ba2017-07-19 18:18:09 +01006072 bool IsClonable() const OVERRIDE { return true; }
6073
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006074 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006075
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006076 LoadKind GetLoadKind() const {
6077 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006078 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006079
6080 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006082 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006083
Andreas Gampea5b09a62016-11-17 15:21:22 -08006084 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006085
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006086 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006087
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006088 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006089 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006090 }
6091
Calin Juravle0ba218d2015-05-19 18:46:01 +01006092 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006093 // The entrypoint the code generator is going to call does not do
6094 // clinit of the class.
6095 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006096 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006097 }
6098
6099 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006100 return NeedsAccessCheck() ||
6101 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006102 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006103 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006104 }
6105
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006106 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006107 return NeedsAccessCheck() ||
6108 MustGenerateClinitCheck() ||
6109 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6110 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6111 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006112 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006113 GetLoadKind() == LoadKind::kBssEntry) &&
6114 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006115 }
6116
Calin Juravleacf735c2015-02-12 15:25:22 +00006117 ReferenceTypeInfo GetLoadedClassRTI() {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006118 return loaded_class_rti_;
Calin Juravleacf735c2015-02-12 15:25:22 +00006119 }
6120
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006121 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
6122 // Make sure we only set exact types (the loaded class should never be merged).
6123 DCHECK(rti.IsExact());
6124 loaded_class_rti_ = rti;
Calin Juravleacf735c2015-02-12 15:25:22 +00006125 }
6126
Andreas Gampea5b09a62016-11-17 15:21:22 -08006127 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006128 const DexFile& GetDexFile() const { return dex_file_; }
6129
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006130 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006131 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006132 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006133
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006134 static SideEffects SideEffectsForArchRuntimeCalls() {
6135 return SideEffects::CanTriggerGC();
6136 }
6137
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006138 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006139 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006140 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006141 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006142
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006143 void MarkInBootImage() {
6144 SetPackedFlag<kFlagIsInBootImage>(true);
6145 }
6146
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006147 void AddSpecialInput(HInstruction* special_input);
6148
6149 using HInstruction::GetInputRecords; // Keep the const version visible.
6150 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6151 return ArrayRef<HUserRecord<HInstruction*>>(
6152 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6153 }
6154
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006155 DataType::Type GetType() const OVERRIDE {
6156 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006157 }
6158
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006159 Handle<mirror::Class> GetClass() const {
6160 return klass_;
6161 }
6162
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006163 DECLARE_INSTRUCTION(LoadClass);
6164
Artem Serovcced8ba2017-07-19 18:18:09 +01006165 protected:
6166 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6167
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006168 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006169 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006170 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006171 // Whether this instruction must generate the initialization check.
6172 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006173 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006174 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6175 static constexpr size_t kFieldLoadKindSize =
6176 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006177 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006178 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006179 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6180
6181 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006182 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006183 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006184 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006185 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006186 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006187 }
6188
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006189 void SetLoadKindInternal(LoadKind load_kind);
6190
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006191 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006192 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006193 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006194 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006195
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006196 // A type index and dex file where the class can be accessed. The dex file can be:
6197 // - The compiling method's dex file if the class is defined there too.
6198 // - The compiling method's dex file if the class is referenced there.
6199 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006200 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006201 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006202 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006203
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006204 Handle<mirror::Class> klass_;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006205
6206 ReferenceTypeInfo loaded_class_rti_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006207};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006208std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6209
6210// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006211inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6212 // The load kind should be determined before inserting the instruction to the graph.
6213 DCHECK(GetBlock() == nullptr);
6214 DCHECK(GetEnvironment() == nullptr);
6215 SetPackedField<LoadKindField>(load_kind);
6216 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6217 special_input_ = HUserRecord<HInstruction*>(nullptr);
6218 }
6219 if (!NeedsEnvironment()) {
6220 SetSideEffects(SideEffects::None());
6221 }
6222}
6223
6224// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006225inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006226 // The special input is used for PC-relative loads on some architectures,
6227 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006228 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006229 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006230 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006231 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006232 DCHECK(special_input_.GetInstruction() == nullptr);
6233 special_input_ = HUserRecord<HInstruction*>(special_input);
6234 special_input->AddUseAt(this, 0);
6235}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006236
Vladimir Marko372f10e2016-05-17 16:30:10 +01006237class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006238 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006239 // Determines how to load the String.
6240 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006241 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006242 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006243 kBootImageLinkTimePcRelative,
6244
6245 // Use a known boot image String* address, embedded in the code by the codegen.
6246 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006247 kBootImageAddress,
6248
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006249 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
6250 // of the oat file.
6251 kBootImageInternTable,
6252
Vladimir Markoaad75c62016-10-03 08:46:48 +00006253 // Load from an entry in the .bss section using a PC-relative load.
6254 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6255 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006256
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006257 // Load from the root table associated with the JIT compiled method.
6258 kJitTableAddress,
6259
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006260 // Load using a simple runtime call. This is the fall-back load kind when
6261 // the codegen is unable to use another appropriate kind.
6262 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006263
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006264 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006265 };
6266
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006267 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006268 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006269 const DexFile& dex_file,
6270 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306271 : HInstruction(kLoadString, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006272 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006273 string_index_(string_index),
6274 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006275 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006276 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006277
Artem Serovcced8ba2017-07-19 18:18:09 +01006278 bool IsClonable() const OVERRIDE { return true; }
6279
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006280 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006281
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006282 LoadKind GetLoadKind() const {
6283 return GetPackedField<LoadKindField>();
6284 }
6285
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006286 const DexFile& GetDexFile() const {
6287 return dex_file_;
6288 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006289
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006290 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006291 return string_index_;
6292 }
6293
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006294 Handle<mirror::String> GetString() const {
6295 return string_;
6296 }
6297
6298 void SetString(Handle<mirror::String> str) {
6299 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006300 }
6301
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006302 bool CanBeMoved() const OVERRIDE { return true; }
6303
Vladimir Marko372f10e2016-05-17 16:30:10 +01006304 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006305
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006306 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006307
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006308 // Will call the runtime if we need to load the string through
6309 // the dex cache and the string is not guaranteed to be there yet.
6310 bool NeedsEnvironment() const OVERRIDE {
6311 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006312 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006313 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006314 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006315 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006316 return false;
6317 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006318 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006319 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006320
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006321 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006322 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006323 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006324
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006325 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006326 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006327
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006328 static SideEffects SideEffectsForArchRuntimeCalls() {
6329 return SideEffects::CanTriggerGC();
6330 }
6331
Vladimir Marko372f10e2016-05-17 16:30:10 +01006332 void AddSpecialInput(HInstruction* special_input);
6333
6334 using HInstruction::GetInputRecords; // Keep the const version visible.
6335 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6336 return ArrayRef<HUserRecord<HInstruction*>>(
6337 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006338 }
6339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006340 DataType::Type GetType() const OVERRIDE {
6341 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006342 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006343
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006344 DECLARE_INSTRUCTION(LoadString);
6345
Artem Serovcced8ba2017-07-19 18:18:09 +01006346 protected:
6347 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6348
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006349 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006350 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006351 static constexpr size_t kFieldLoadKindSize =
6352 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6353 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006354 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006355 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006356
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006357 void SetLoadKindInternal(LoadKind load_kind);
6358
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006359 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006360 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006361 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006362 HUserRecord<HInstruction*> special_input_;
6363
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006364 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006365 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006366
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006367 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006368};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006369std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6370
6371// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006372inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6373 // The load kind should be determined before inserting the instruction to the graph.
6374 DCHECK(GetBlock() == nullptr);
6375 DCHECK(GetEnvironment() == nullptr);
6376 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6377 SetPackedField<LoadKindField>(load_kind);
6378 if (load_kind != LoadKind::kRuntimeCall) {
6379 special_input_ = HUserRecord<HInstruction*>(nullptr);
6380 }
6381 if (!NeedsEnvironment()) {
6382 SetSideEffects(SideEffects::None());
6383 }
6384}
6385
6386// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006387inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006388 // The special input is used for PC-relative loads on some architectures,
6389 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006390 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006391 GetLoadKind() == LoadKind::kBootImageAddress ||
6392 GetLoadKind() == LoadKind::kBootImageInternTable ||
6393 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006394 // HLoadString::GetInputRecords() returns an empty array at this point,
6395 // so use the GetInputRecords() from the base class to set the input record.
6396 DCHECK(special_input_.GetInstruction() == nullptr);
6397 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006398 special_input->AddUseAt(this, 0);
6399}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006400
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006401/**
6402 * Performs an initialization check on its Class object input.
6403 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006404class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006405 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006406 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006407 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306408 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006409 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006410 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006411 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006412 SetRawInputAt(0, constant);
6413 }
6414
Artem Serovcced8ba2017-07-19 18:18:09 +01006415 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006416 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006417 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006418 return true;
6419 }
6420
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006421 bool NeedsEnvironment() const OVERRIDE {
6422 // May call runtime to initialize the class.
6423 return true;
6424 }
6425
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006426 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006427
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006428 HLoadClass* GetLoadClass() const {
6429 DCHECK(InputAt(0)->IsLoadClass());
6430 return InputAt(0)->AsLoadClass();
6431 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006432
6433 DECLARE_INSTRUCTION(ClinitCheck);
6434
Artem Serovcced8ba2017-07-19 18:18:09 +01006435
6436 protected:
6437 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006438};
6439
Vladimir Markofcb503c2016-05-18 12:48:17 +01006440class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006441 public:
6442 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006443 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006444 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006445 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006446 bool is_volatile,
6447 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006448 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006449 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006450 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306451 : HExpression(kStaticFieldGet,
6452 field_type,
6453 SideEffects::FieldReadOfType(field_type, is_volatile),
6454 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006455 field_info_(field,
6456 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006457 field_type,
6458 is_volatile,
6459 field_idx,
6460 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006461 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006462 SetRawInputAt(0, cls);
6463 }
6464
Calin Juravle52c48962014-12-16 17:02:57 +00006465
Artem Serovcced8ba2017-07-19 18:18:09 +01006466 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006467 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006468
Vladimir Marko372f10e2016-05-17 16:30:10 +01006469 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6470 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006471 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006472 }
6473
6474 size_t ComputeHashCode() const OVERRIDE {
6475 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6476 }
6477
Calin Juravle52c48962014-12-16 17:02:57 +00006478 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006479 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006480 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006481 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006482
Vladimir Marko61b92282017-10-11 13:23:17 +01006483 void SetType(DataType::Type new_type) {
6484 DCHECK(DataType::IsIntegralType(GetType()));
6485 DCHECK(DataType::IsIntegralType(new_type));
6486 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6487 SetPackedField<TypeField>(new_type);
6488 }
6489
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006490 DECLARE_INSTRUCTION(StaticFieldGet);
6491
Artem Serovcced8ba2017-07-19 18:18:09 +01006492 protected:
6493 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6494
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006495 private:
6496 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006497};
6498
Vladimir Markofcb503c2016-05-18 12:48:17 +01006499class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006500 public:
6501 HStaticFieldSet(HInstruction* cls,
6502 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006503 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006504 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006505 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006506 bool is_volatile,
6507 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006508 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006509 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006510 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306511 : HTemplateInstruction(kStaticFieldSet,
6512 SideEffects::FieldWriteOfType(field_type, is_volatile),
6513 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006514 field_info_(field,
6515 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006516 field_type,
6517 is_volatile,
6518 field_idx,
6519 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006520 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006521 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006522 SetRawInputAt(0, cls);
6523 SetRawInputAt(1, value);
6524 }
6525
Artem Serovcced8ba2017-07-19 18:18:09 +01006526 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006527 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006528 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006529 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006530 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006531
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006532 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006533 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6534 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006535
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006536 DECLARE_INSTRUCTION(StaticFieldSet);
6537
Artem Serovcced8ba2017-07-19 18:18:09 +01006538 protected:
6539 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6540
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006541 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006542 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6543 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6544 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6545 "Too many packed fields.");
6546
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006547 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006548};
6549
Vladimir Markofcb503c2016-05-18 12:48:17 +01006550class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006551 public:
6552 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006553 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006554 uint32_t field_index,
6555 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306556 : HExpression(kUnresolvedInstanceFieldGet,
6557 field_type,
6558 SideEffects::AllExceptGCDependency(),
6559 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006560 field_index_(field_index) {
6561 SetRawInputAt(0, obj);
6562 }
6563
Artem Serovcced8ba2017-07-19 18:18:09 +01006564 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006565 bool NeedsEnvironment() const OVERRIDE { return true; }
6566 bool CanThrow() const OVERRIDE { return true; }
6567
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006568 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006569 uint32_t GetFieldIndex() const { return field_index_; }
6570
6571 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6572
Artem Serovcced8ba2017-07-19 18:18:09 +01006573 protected:
6574 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6575
Calin Juravlee460d1d2015-09-29 04:52:17 +01006576 private:
6577 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006578};
6579
Vladimir Markofcb503c2016-05-18 12:48:17 +01006580class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006581 public:
6582 HUnresolvedInstanceFieldSet(HInstruction* obj,
6583 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006584 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006585 uint32_t field_index,
6586 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306587 : HTemplateInstruction(kUnresolvedInstanceFieldSet,
6588 SideEffects::AllExceptGCDependency(),
6589 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006590 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006591 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006592 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006593 SetRawInputAt(0, obj);
6594 SetRawInputAt(1, value);
6595 }
6596
Artem Serovcced8ba2017-07-19 18:18:09 +01006597 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006598 bool NeedsEnvironment() const OVERRIDE { return true; }
6599 bool CanThrow() const OVERRIDE { return true; }
6600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006601 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006602 uint32_t GetFieldIndex() const { return field_index_; }
6603
6604 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6605
Artem Serovcced8ba2017-07-19 18:18:09 +01006606 protected:
6607 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6608
Calin Juravlee460d1d2015-09-29 04:52:17 +01006609 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006610 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6611 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006612 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006613 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6614 kFieldFieldType + kFieldFieldTypeSize;
6615 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6616 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006617 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006618
Calin Juravlee460d1d2015-09-29 04:52:17 +01006619 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006620};
6621
Vladimir Markofcb503c2016-05-18 12:48:17 +01006622class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006623 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006624 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006625 uint32_t field_index,
6626 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306627 : HExpression(kUnresolvedStaticFieldGet,
6628 field_type,
6629 SideEffects::AllExceptGCDependency(),
6630 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006631 field_index_(field_index) {
6632 }
6633
Artem Serovcced8ba2017-07-19 18:18:09 +01006634 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006635 bool NeedsEnvironment() const OVERRIDE { return true; }
6636 bool CanThrow() const OVERRIDE { return true; }
6637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006638 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006639 uint32_t GetFieldIndex() const { return field_index_; }
6640
6641 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6642
Artem Serovcced8ba2017-07-19 18:18:09 +01006643 protected:
6644 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6645
Calin Juravlee460d1d2015-09-29 04:52:17 +01006646 private:
6647 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006648};
6649
Vladimir Markofcb503c2016-05-18 12:48:17 +01006650class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006651 public:
6652 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006653 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006654 uint32_t field_index,
6655 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306656 : HTemplateInstruction(kUnresolvedStaticFieldSet,
6657 SideEffects::AllExceptGCDependency(),
6658 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006659 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006660 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006661 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006662 SetRawInputAt(0, value);
6663 }
6664
Artem Serovcced8ba2017-07-19 18:18:09 +01006665 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006666 bool NeedsEnvironment() const OVERRIDE { return true; }
6667 bool CanThrow() const OVERRIDE { return true; }
6668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006669 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006670 uint32_t GetFieldIndex() const { return field_index_; }
6671
6672 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6673
Artem Serovcced8ba2017-07-19 18:18:09 +01006674 protected:
6675 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6676
Calin Juravlee460d1d2015-09-29 04:52:17 +01006677 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006678 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6679 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006680 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006681 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6682 kFieldFieldType + kFieldFieldTypeSize;
6683 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6684 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006685 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006686
Calin Juravlee460d1d2015-09-29 04:52:17 +01006687 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006688};
6689
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006690// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006691class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006693 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306694 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6695 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006696
David Brazdilbbd733e2015-08-18 17:48:17 +01006697 bool CanBeNull() const OVERRIDE { return false; }
6698
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006699 DECLARE_INSTRUCTION(LoadException);
6700
Artem Serovcced8ba2017-07-19 18:18:09 +01006701 protected:
6702 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006703};
6704
David Brazdilcb1c0552015-08-04 16:22:25 +01006705// Implicit part of move-exception which clears thread-local exception storage.
6706// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006707class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006708 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006709 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306710 : HTemplateInstruction(kClearException, SideEffects::AllWrites(), dex_pc) {
6711 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006712
6713 DECLARE_INSTRUCTION(ClearException);
6714
Artem Serovcced8ba2017-07-19 18:18:09 +01006715 protected:
6716 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006717};
6718
Vladimir Markofcb503c2016-05-18 12:48:17 +01006719class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006720 public:
6721 HThrow(HInstruction* exception, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306722 : HTemplateInstruction(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006723 SetRawInputAt(0, exception);
6724 }
6725
6726 bool IsControlFlow() const OVERRIDE { return true; }
6727
6728 bool NeedsEnvironment() const OVERRIDE { return true; }
6729
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006730 bool CanThrow() const OVERRIDE { return true; }
6731
Aart Bika8b8e9b2018-01-09 11:01:02 -08006732 bool AlwaysThrows() const OVERRIDE { return true; }
6733
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006734 DECLARE_INSTRUCTION(Throw);
6735
Artem Serovcced8ba2017-07-19 18:18:09 +01006736 protected:
6737 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006738};
6739
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006740/**
6741 * Implementation strategies for the code generator of a HInstanceOf
6742 * or `HCheckCast`.
6743 */
6744enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006745 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006746 kExactCheck, // Can do a single class compare.
6747 kClassHierarchyCheck, // Can just walk the super class chain.
6748 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6749 kInterfaceCheck, // No optimization yet when checking against an interface.
6750 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006751 kArrayCheck, // No optimization yet when checking against a generic array.
6752 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006753};
6754
Roland Levillain86503782016-02-11 19:07:30 +00006755std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6756
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006757class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006758 public:
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006759 HInstanceOf(HInstruction* object,
6760 HLoadClass* target_class,
6761 TypeCheckKind check_kind,
6762 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306763 : HExpression(kInstanceOf,
6764 DataType::Type::kBool,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006765 SideEffectsForArchRuntimeCalls(check_kind),
6766 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006767 SetPackedField<TypeCheckKindField>(check_kind);
6768 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006769 SetRawInputAt(0, object);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006770 SetRawInputAt(1, target_class);
Vladimir Marko87584542017-12-12 17:47:52 +00006771 }
6772
6773 HLoadClass* GetTargetClass() const {
6774 HInstruction* load_class = InputAt(1);
6775 DCHECK(load_class->IsLoadClass());
6776 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006777 }
6778
Artem Serovcced8ba2017-07-19 18:18:09 +01006779 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006780 bool CanBeMoved() const OVERRIDE { return true; }
6781
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006782 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6783 return true;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006784 }
6785
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00006786 bool NeedsEnvironment() const OVERRIDE {
6787 return CanCallRuntime(GetTypeCheckKind());
6788 }
6789
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006790 // Used only in code generation.
6791 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6792 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6793 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6794 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6795
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006796 static bool CanCallRuntime(TypeCheckKind check_kind) {
6797 // Mips currently does runtime calls for any other checks.
6798 return check_kind != TypeCheckKind::kExactCheck;
6799 }
6800
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006801 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006802 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006803 }
6804
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006805 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006806
Artem Serovcced8ba2017-07-19 18:18:09 +01006807 protected:
6808 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006809
6810 private:
6811 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6812 static constexpr size_t kFieldTypeCheckKindSize =
6813 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6814 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6815 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6816 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6817 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006818};
6819
Vladimir Markofcb503c2016-05-18 12:48:17 +01006820class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006821 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006822 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306823 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006824 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6825 SetPackedFlag<kFlagUpperCanBeNull>(true);
6826 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006827 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006828 SetRawInputAt(0, input);
6829 }
6830
Artem Serovcced8ba2017-07-19 18:18:09 +01006831 bool IsClonable() const OVERRIDE { return true; }
6832
David Brazdilf5552582015-12-27 13:36:12 +00006833 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006834 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006835 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006836 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006837
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006838 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006839 DCHECK(GetUpperCanBeNull() || !can_be_null);
6840 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006841 }
6842
Vladimir Markoa1de9182016-02-25 11:37:38 +00006843 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006844
Calin Juravleb1498f62015-02-16 13:13:29 +00006845 DECLARE_INSTRUCTION(BoundType);
6846
Artem Serovcced8ba2017-07-19 18:18:09 +01006847 protected:
6848 DEFAULT_COPY_CONSTRUCTOR(BoundType);
6849
Calin Juravleb1498f62015-02-16 13:13:29 +00006850 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006851 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6852 // is false then CanBeNull() cannot be true).
6853 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6854 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6855 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6856 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6857
Calin Juravleb1498f62015-02-16 13:13:29 +00006858 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006859 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6860 // It is used to bound the type in cases like:
6861 // if (x instanceof ClassX) {
6862 // // uper_bound_ will be ClassX
6863 // }
David Brazdilf5552582015-12-27 13:36:12 +00006864 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006865};
6866
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006867class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006868 public:
6869 HCheckCast(HInstruction* object,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006870 HLoadClass* target_class,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006871 TypeCheckKind check_kind,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006872 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306873 : HTemplateInstruction(kCheckCast, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006874 SetPackedField<TypeCheckKindField>(check_kind);
6875 SetPackedFlag<kFlagMustDoNullCheck>(true);
6876 SetRawInputAt(0, object);
6877 SetRawInputAt(1, target_class);
6878 }
6879
6880 HLoadClass* GetTargetClass() const {
6881 HInstruction* load_class = InputAt(1);
6882 DCHECK(load_class->IsLoadClass());
6883 return load_class->AsLoadClass();
6884 }
6885
6886 bool IsClonable() const OVERRIDE { return true; }
6887 bool CanBeMoved() const OVERRIDE { return true; }
6888
6889 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6890 return true;
6891 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006892
6893 bool NeedsEnvironment() const OVERRIDE {
6894 // Instruction may throw a CheckCastError.
6895 return true;
6896 }
6897
6898 bool CanThrow() const OVERRIDE { return true; }
6899
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006900 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6901 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6902 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6903 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6904
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006905 DECLARE_INSTRUCTION(CheckCast);
6906
Artem Serovcced8ba2017-07-19 18:18:09 +01006907 protected:
6908 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006909
6910 private:
6911 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6912 static constexpr size_t kFieldTypeCheckKindSize =
6913 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6914 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6915 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6916 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6917 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006918};
6919
Andreas Gampe26de38b2016-07-27 17:53:11 -07006920/**
6921 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6922 * @details We define the combined barrier types that are actually required
6923 * by the Java Memory Model, rather than using exactly the terminology from
6924 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6925 * primitives. Note that the JSR-133 cookbook generally does not deal with
6926 * store atomicity issues, and the recipes there are not always entirely sufficient.
6927 * The current recipe is as follows:
6928 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6929 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6930 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6931 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6932 * class has final fields.
6933 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6934 * store-to-memory instructions. Only generated together with non-temporal stores.
6935 */
6936enum MemBarrierKind {
6937 kAnyStore,
6938 kLoadAny,
6939 kStoreStore,
6940 kAnyAny,
6941 kNTStoreStore,
6942 kLastBarrierKind = kNTStoreStore
6943};
6944std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6945
Vladimir Markofcb503c2016-05-18 12:48:17 +01006946class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006947 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006948 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006949 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306950 kMemoryBarrier,
6951 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
6952 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006953 SetPackedField<BarrierKindField>(barrier_kind);
6954 }
Calin Juravle27df7582015-04-17 19:12:31 +01006955
Artem Serovcced8ba2017-07-19 18:18:09 +01006956 bool IsClonable() const OVERRIDE { return true; }
6957
Vladimir Markoa1de9182016-02-25 11:37:38 +00006958 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006959
6960 DECLARE_INSTRUCTION(MemoryBarrier);
6961
Artem Serovcced8ba2017-07-19 18:18:09 +01006962 protected:
6963 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
6964
Calin Juravle27df7582015-04-17 19:12:31 +01006965 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006966 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6967 static constexpr size_t kFieldBarrierKindSize =
6968 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6969 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6970 kFieldBarrierKind + kFieldBarrierKindSize;
6971 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6972 "Too many packed fields.");
6973 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006974};
6975
Igor Murashkind01745e2017-04-05 16:40:31 -07006976// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6977// the specified object(s), with respect to any subsequent store that might "publish"
6978// (i.e. make visible) the specified object to another thread.
6979//
6980// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6981// for all final fields (that were set) at the end of the invoked constructor.
6982//
6983// The constructor fence models the freeze actions for the final fields of an object
6984// being constructed (semantically at the end of the constructor). Constructor fences
6985// have a per-object affinity; two separate objects being constructed get two separate
6986// constructor fences.
6987//
6988// (Note: that if calling a super-constructor or forwarding to another constructor,
6989// the freezes would happen at the end of *that* constructor being invoked).
6990//
6991// The memory model guarantees that when the object being constructed is "published" after
6992// constructor completion (i.e. escapes the current thread via a store), then any final field
6993// writes must be observable on other threads (once they observe that publication).
6994//
6995// Further, anything written before the freeze, and read by dereferencing through the final field,
6996// must also be visible (so final object field could itself have an object with non-final fields;
6997// yet the freeze must also extend to them).
6998//
6999// Constructor example:
7000//
7001// class HasFinal {
7002// final int field; Optimizing IR for <init>()V:
7003// HasFinal() {
7004// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7005// // freeze(this.field); HConstructorFence(this)
7006// } HReturn
7007// }
7008//
7009// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7010// already-initialized classes; in that case the allocation must act as a "default-initializer"
7011// of the object which effectively writes the class pointer "final field".
7012//
7013// For example, we can model default-initialiation as roughly the equivalent of the following:
7014//
7015// class Object {
7016// private final Class header;
7017// }
7018//
7019// Java code: Optimizing IR:
7020//
7021// T new_instance<T>() {
7022// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7023// obj.header = T.class; // header write is done by above call.
7024// // freeze(obj.header) HConstructorFence(obj)
7025// return (T)obj;
7026// }
7027//
7028// See also:
7029// * CompilerDriver::RequiresConstructorBarrier
7030// * QuasiAtomic::ThreadFenceForConstructor
7031//
7032class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7033 // A fence has variable inputs because the inputs can be removed
7034 // after prepare_for_register_allocation phase.
7035 // (TODO: In the future a fence could freeze multiple objects
7036 // after merging two fences together.)
7037 public:
7038 // `fence_object` is the reference that needs to be protected for correct publication.
7039 //
7040 // It makes sense in the following situations:
7041 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7042 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7043 //
7044 // After construction the `fence_object` becomes the 0th input.
7045 // This is not an input in a real sense, but just a convenient place to stash the information
7046 // about the associated object.
7047 HConstructorFence(HInstruction* fence_object,
7048 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007049 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007050 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7051 // constraints described in the class header. We claim that these SideEffects constraints
7052 // enforce a superset of the real constraints.
7053 //
7054 // The ordering described above is conservatively modeled with SideEffects as follows:
7055 //
7056 // * To prevent reordering of the publication stores:
7057 // ----> "Reads of objects" is the initial SideEffect.
7058 // * For every primitive final field store in the constructor:
7059 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7060 // * If there are any stores to reference final fields in the constructor:
7061 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7062 // that are reachable from `fence_object` also need to be prevented for reordering
7063 // (and we do not want to do alias analysis to figure out what those stores are).
7064 //
7065 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7066 // even more conservative approach than the one described above, and prevents all of the
7067 // above reordering without analyzing any of the instructions in the constructor.
7068 //
7069 // If in a later phase we discover that there are no writes to reference final fields,
7070 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307071 : HVariableInputSizeInstruction(kConstructorFence,
7072 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007073 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007074 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007075 /* number_of_inputs */ 1,
7076 kArenaAllocConstructorFenceInputs) {
7077 DCHECK(fence_object != nullptr);
7078 SetRawInputAt(0, fence_object);
7079 }
7080
7081 // The object associated with this constructor fence.
7082 //
7083 // (Note: This will be null after the prepare_for_register_allocation phase,
7084 // as all constructor fence inputs are removed there).
7085 HInstruction* GetFenceObject() const {
7086 return InputAt(0);
7087 }
7088
7089 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7090 // - Delete `fence_use` from `this`'s use list.
7091 // - Delete `this` from `fence_use`'s inputs list.
7092 // - If the `fence_use` is dead, remove it from the graph.
7093 //
7094 // A fence is considered dead once it no longer has any uses
7095 // and all of the inputs are dead.
7096 //
7097 // This must *not* be called during/after prepare_for_register_allocation,
7098 // because that removes all the inputs to the fences but the fence is actually
7099 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007100 //
7101 // Returns how many HConstructorFence instructions were removed from graph.
7102 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007103
Igor Murashkindd018df2017-08-09 10:38:31 -07007104 // Combine all inputs of `this` and `other` instruction and remove
7105 // `other` from the graph.
7106 //
7107 // Inputs are unique after the merge.
7108 //
7109 // Requirement: `this` must not be the same as `other.
7110 void Merge(HConstructorFence* other);
7111
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007112 // Check if this constructor fence is protecting
7113 // an HNewInstance or HNewArray that is also the immediate
7114 // predecessor of `this`.
7115 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007116 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7117 // to be one of the inputs of `this`.
7118 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007119 // Returns the associated HNewArray or HNewInstance,
7120 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007121 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007122
Igor Murashkind01745e2017-04-05 16:40:31 -07007123 DECLARE_INSTRUCTION(ConstructorFence);
7124
Artem Serovcced8ba2017-07-19 18:18:09 +01007125 protected:
7126 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007127};
7128
Vladimir Markofcb503c2016-05-18 12:48:17 +01007129class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007130 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007131 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007132 kEnter,
7133 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007134 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007135 };
7136
7137 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007138 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307139 kMonitorOperation,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007140 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7141 dex_pc) {
7142 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007143 SetRawInputAt(0, object);
7144 }
7145
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007146 // Instruction may go into runtime, so we need an environment.
7147 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007148
7149 bool CanThrow() const OVERRIDE {
7150 // Verifier guarantees that monitor-exit cannot throw.
7151 // This is important because it allows the HGraphBuilder to remove
7152 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7153 return IsEnter();
7154 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007155
Vladimir Markoa1de9182016-02-25 11:37:38 +00007156 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7157 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007158
7159 DECLARE_INSTRUCTION(MonitorOperation);
7160
Artem Serovcced8ba2017-07-19 18:18:09 +01007161 protected:
7162 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7163
Calin Juravle52c48962014-12-16 17:02:57 +00007164 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007165 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7166 static constexpr size_t kFieldOperationKindSize =
7167 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7168 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7169 kFieldOperationKind + kFieldOperationKindSize;
7170 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7171 "Too many packed fields.");
7172 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007173};
7174
Vladimir Markofcb503c2016-05-18 12:48:17 +01007175class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007176 public:
7177 HSelect(HInstruction* condition,
7178 HInstruction* true_value,
7179 HInstruction* false_value,
7180 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307181 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007182 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7183
7184 // First input must be `true_value` or `false_value` to allow codegens to
7185 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7186 // that architectures which implement HSelect as a conditional move also
7187 // will not need to invert the condition.
7188 SetRawInputAt(0, false_value);
7189 SetRawInputAt(1, true_value);
7190 SetRawInputAt(2, condition);
7191 }
7192
Artem Serovcced8ba2017-07-19 18:18:09 +01007193 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007194 HInstruction* GetFalseValue() const { return InputAt(0); }
7195 HInstruction* GetTrueValue() const { return InputAt(1); }
7196 HInstruction* GetCondition() const { return InputAt(2); }
7197
7198 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007199 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7200 return true;
7201 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007202
7203 bool CanBeNull() const OVERRIDE {
7204 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7205 }
7206
7207 DECLARE_INSTRUCTION(Select);
7208
Artem Serovcced8ba2017-07-19 18:18:09 +01007209 protected:
7210 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007211};
7212
Vladimir Markof9f64412015-09-02 14:05:49 +01007213class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007214 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007215 MoveOperands(Location source,
7216 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007217 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007218 HInstruction* instruction)
7219 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007220
7221 Location GetSource() const { return source_; }
7222 Location GetDestination() const { return destination_; }
7223
7224 void SetSource(Location value) { source_ = value; }
7225 void SetDestination(Location value) { destination_ = value; }
7226
7227 // The parallel move resolver marks moves as "in-progress" by clearing the
7228 // destination (but not the source).
7229 Location MarkPending() {
7230 DCHECK(!IsPending());
7231 Location dest = destination_;
7232 destination_ = Location::NoLocation();
7233 return dest;
7234 }
7235
7236 void ClearPending(Location dest) {
7237 DCHECK(IsPending());
7238 destination_ = dest;
7239 }
7240
7241 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007242 DCHECK(source_.IsValid() || destination_.IsInvalid());
7243 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007244 }
7245
7246 // True if this blocks a move from the given location.
7247 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007248 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007249 }
7250
7251 // A move is redundant if it's been eliminated, if its source and
7252 // destination are the same, or if its destination is unneeded.
7253 bool IsRedundant() const {
7254 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7255 }
7256
7257 // We clear both operands to indicate move that's been eliminated.
7258 void Eliminate() {
7259 source_ = destination_ = Location::NoLocation();
7260 }
7261
7262 bool IsEliminated() const {
7263 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7264 return source_.IsInvalid();
7265 }
7266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007267 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007268
Nicolas Geoffray90218252015-04-15 11:56:51 +01007269 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007270 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007271 }
7272
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007273 HInstruction* GetInstruction() const { return instruction_; }
7274
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007275 private:
7276 Location source_;
7277 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007278 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007279 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007280 // The instruction this move is assocatied with. Null when this move is
7281 // for moving an input in the expected locations of user (including a phi user).
7282 // This is only used in debug mode, to ensure we do not connect interval siblings
7283 // in the same parallel move.
7284 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007285};
7286
Roland Levillainc9285912015-12-18 10:38:42 +00007287std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7288
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007289static constexpr size_t kDefaultNumberOfMoves = 4;
7290
Vladimir Markofcb503c2016-05-18 12:48:17 +01007291class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007292 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007293 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307294 : HTemplateInstruction(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007295 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007296 moves_.reserve(kDefaultNumberOfMoves);
7297 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007298
Nicolas Geoffray90218252015-04-15 11:56:51 +01007299 void AddMove(Location source,
7300 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007301 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007302 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007303 DCHECK(source.IsValid());
7304 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007305 if (kIsDebugBuild) {
7306 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007307 for (const MoveOperands& move : moves_) {
7308 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007309 // Special case the situation where the move is for the spill slot
7310 // of the instruction.
7311 if ((GetPrevious() == instruction)
7312 || ((GetPrevious() == nullptr)
7313 && instruction->IsPhi()
7314 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007315 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007316 << "Doing parallel moves for the same instruction.";
7317 } else {
7318 DCHECK(false) << "Doing parallel moves for the same instruction.";
7319 }
7320 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007321 }
7322 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007323 for (const MoveOperands& move : moves_) {
7324 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007325 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007326 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007327 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007328 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007329 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007330 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007331 }
7332
Vladimir Marko225b6462015-09-28 12:17:40 +01007333 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007334 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007335 }
7336
Vladimir Marko225b6462015-09-28 12:17:40 +01007337 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007338
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007339 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007340
Artem Serovcced8ba2017-07-19 18:18:09 +01007341 protected:
7342 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7343
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007344 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007345 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007346};
7347
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007348// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7349// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7350// never used across anything that can trigger GC.
7351// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7352// So we represent it by the type `DataType::Type::kInt`.
7353class HIntermediateAddress FINAL : public HExpression<2> {
7354 public:
7355 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307356 : HExpression(kIntermediateAddress,
7357 DataType::Type::kInt32,
7358 SideEffects::DependsOnGC(),
7359 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007360 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7361 DataType::Size(DataType::Type::kReference))
7362 << "kPrimInt and kPrimNot have different sizes.";
7363 SetRawInputAt(0, base_address);
7364 SetRawInputAt(1, offset);
7365 }
7366
Artem Serovcced8ba2017-07-19 18:18:09 +01007367 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007368 bool CanBeMoved() const OVERRIDE { return true; }
7369 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7370 return true;
7371 }
7372 bool IsActualObject() const OVERRIDE { return false; }
7373
7374 HInstruction* GetBaseAddress() const { return InputAt(0); }
7375 HInstruction* GetOffset() const { return InputAt(1); }
7376
7377 DECLARE_INSTRUCTION(IntermediateAddress);
7378
Artem Serovcced8ba2017-07-19 18:18:09 +01007379 protected:
7380 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007381};
7382
7383
Mark Mendell0616ae02015-04-17 12:49:27 -04007384} // namespace art
7385
Aart Bikf8f5a162017-02-06 15:35:29 -08007386#include "nodes_vector.h"
7387
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007388#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7389#include "nodes_shared.h"
7390#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007391#ifdef ART_ENABLE_CODEGEN_mips
7392#include "nodes_mips.h"
7393#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007394#ifdef ART_ENABLE_CODEGEN_x86
7395#include "nodes_x86.h"
7396#endif
7397
7398namespace art {
7399
Igor Murashkin6ef45672017-08-08 13:59:55 -07007400class OptimizingCompilerStats;
7401
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007402class HGraphVisitor : public ValueObject {
7403 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007404 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7405 : stats_(stats),
7406 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007407 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007408
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007409 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007410 virtual void VisitBasicBlock(HBasicBlock* block);
7411
Roland Levillain633021e2014-10-01 14:12:25 +01007412 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007413 void VisitInsertionOrder();
7414
Roland Levillain633021e2014-10-01 14:12:25 +01007415 // Visit the graph following dominator tree reverse post-order.
7416 void VisitReversePostOrder();
7417
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007418 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007419
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007420 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007421#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007422 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7423
7424 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7425
7426#undef DECLARE_VISIT_INSTRUCTION
7427
Igor Murashkin6ef45672017-08-08 13:59:55 -07007428 protected:
7429 OptimizingCompilerStats* stats_;
7430
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007431 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007432 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007433
7434 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7435};
7436
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007437class HGraphDelegateVisitor : public HGraphVisitor {
7438 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007439 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7440 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007441 virtual ~HGraphDelegateVisitor() {}
7442
7443 // Visit functions that delegate to to super class.
7444#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007445 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007446
7447 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7448
7449#undef DECLARE_VISIT_INSTRUCTION
7450
7451 private:
7452 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7453};
7454
Artem Serovcced8ba2017-07-19 18:18:09 +01007455// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7456// and remove the old instruction.
7457HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7458
7459// Create a clone for each clonable instructions/phis and replace the original with the clone.
7460//
7461// Used for testing individual instruction cloner.
7462class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7463 public:
7464 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007465 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007466
7467 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7468 if (instruction->IsClonable()) {
7469 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007470 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007471 }
7472 }
7473
Artem Serov7f4aff62017-06-21 17:02:18 +01007474 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007475
7476 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007477 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007478
7479 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7480};
7481
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007482// Iterator over the blocks that art part of the loop. Includes blocks part
7483// of an inner loop. The order in which the blocks are iterated is on their
7484// block id.
7485class HBlocksInLoopIterator : public ValueObject {
7486 public:
7487 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7488 : blocks_in_loop_(info.GetBlocks()),
7489 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7490 index_(0) {
7491 if (!blocks_in_loop_.IsBitSet(index_)) {
7492 Advance();
7493 }
7494 }
7495
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007496 bool Done() const { return index_ == blocks_.size(); }
7497 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007498 void Advance() {
7499 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007500 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007501 if (blocks_in_loop_.IsBitSet(index_)) {
7502 break;
7503 }
7504 }
7505 }
7506
7507 private:
7508 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007509 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007510 size_t index_;
7511
7512 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7513};
7514
Mingyao Yang3584bce2015-05-19 16:01:59 -07007515// Iterator over the blocks that art part of the loop. Includes blocks part
7516// of an inner loop. The order in which the blocks are iterated is reverse
7517// post order.
7518class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7519 public:
7520 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7521 : blocks_in_loop_(info.GetBlocks()),
7522 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7523 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007524 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007525 Advance();
7526 }
7527 }
7528
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007529 bool Done() const { return index_ == blocks_.size(); }
7530 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007531 void Advance() {
7532 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007533 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7534 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007535 break;
7536 }
7537 }
7538 }
7539
7540 private:
7541 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007542 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007543 size_t index_;
7544
7545 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7546};
7547
Aart Bikf3e61ee2017-04-12 17:09:20 -07007548// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007549inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007550 if (constant->IsIntConstant()) {
7551 return constant->AsIntConstant()->GetValue();
7552 } else if (constant->IsLongConstant()) {
7553 return constant->AsLongConstant()->GetValue();
7554 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007555 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007556 return 0;
7557 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007558}
7559
Aart Bikf3e61ee2017-04-12 17:09:20 -07007560// Returns true iff instruction is an integral constant (and sets value on success).
7561inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7562 if (instruction->IsIntConstant()) {
7563 *value = instruction->AsIntConstant()->GetValue();
7564 return true;
7565 } else if (instruction->IsLongConstant()) {
7566 *value = instruction->AsLongConstant()->GetValue();
7567 return true;
7568 } else if (instruction->IsNullConstant()) {
7569 *value = 0;
7570 return true;
7571 }
7572 return false;
7573}
7574
Aart Bik0148de42017-09-05 09:25:01 -07007575// Returns true iff instruction is the given integral constant.
7576inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7577 int64_t val = 0;
7578 return IsInt64AndGet(instruction, &val) && val == value;
7579}
7580
7581// Returns true iff instruction is a zero bit pattern.
7582inline bool IsZeroBitPattern(HInstruction* instruction) {
7583 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7584}
7585
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007586#define INSTRUCTION_TYPE_CHECK(type, super) \
7587 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7588 inline const H##type* HInstruction::As##type() const { \
7589 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7590 } \
7591 inline H##type* HInstruction::As##type() { \
7592 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7593 }
7594
7595 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7596#undef INSTRUCTION_TYPE_CHECK
7597
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007598// Create space in `blocks` for adding `number_of_new_blocks` entries
7599// starting at location `at`. Blocks after `at` are moved accordingly.
7600inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7601 size_t number_of_new_blocks,
7602 size_t after) {
7603 DCHECK_LT(after, blocks->size());
7604 size_t old_size = blocks->size();
7605 size_t new_size = old_size + number_of_new_blocks;
7606 blocks->resize(new_size);
7607 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7608}
7609
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007610/*
7611 * Hunt "under the hood" of array lengths (leading to array references),
7612 * null checks (also leading to array references), and new arrays
7613 * (leading to the actual length). This makes it more likely related
7614 * instructions become actually comparable.
7615 */
7616inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7617 while (instruction->IsArrayLength() ||
7618 instruction->IsNullCheck() ||
7619 instruction->IsNewArray()) {
7620 instruction = instruction->IsNewArray()
7621 ? instruction->AsNewArray()->GetLength()
7622 : instruction->InputAt(0);
7623 }
7624 return instruction;
7625}
7626
Artem Serov21c7e6f2017-07-27 16:04:42 +01007627void RemoveEnvironmentUses(HInstruction* instruction);
7628bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7629void ResetEnvironmentInputRecords(HInstruction* instruction);
7630
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007631} // namespace art
7632
7633#endif // ART_COMPILER_OPTIMIZING_NODES_H_