blob: 846b185b5907a56e6b7dfc50687a3aa781d8afe9 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010031#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file.h"
34#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080035#include "dex/invoke_type.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000036#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000037#include "handle.h"
38#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010039#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010040#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000041#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000042#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010043#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
Vladimir Markoca6fff82017-10-03 14:49:14 +010048class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000049class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070051class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010052class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000056class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000059class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000060class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000062class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070063class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010064class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010065class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010066class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010067class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000068class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010069class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000070class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000071
Mathieu Chartier736b5602015-09-02 14:54:11 -070072namespace mirror {
73class DexCache;
74} // namespace mirror
75
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076static const int kDefaultNumberOfBlocks = 8;
77static const int kDefaultNumberOfSuccessors = 2;
78static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010079static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010080static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Roland Levillain5b5b9312016-03-22 14:57:31 +000083// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
84static constexpr int32_t kMaxIntShiftDistance = 0x1f;
85// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
86static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000087
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010088static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070089static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010091static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
92
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060093static constexpr uint32_t kNoDexPc = -1;
94
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010095inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
96 // For the purposes of the compiler, the dex files must actually be the same object
97 // if we want to safely treat them as the same. This is especially important for JIT
98 // as custom class loaders can open the same underlying file (or memory) multiple
99 // times and provide different class resolution but no two class loaders should ever
100 // use the same DexFile object - doing so is an unsupported hack that can lead to
101 // all sorts of weird failures.
102 return &lhs == &rhs;
103}
104
Dave Allison20dfc792014-06-16 20:44:29 -0700105enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700106 // All types.
107 kCondEQ, // ==
108 kCondNE, // !=
109 // Signed integers and floating-point numbers.
110 kCondLT, // <
111 kCondLE, // <=
112 kCondGT, // >
113 kCondGE, // >=
114 // Unsigned integers.
115 kCondB, // <
116 kCondBE, // <=
117 kCondA, // >
118 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100119 // First and last aliases.
120 kCondFirst = kCondEQ,
121 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700122};
123
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000124enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000125 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000126 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000127 kAnalysisFailThrowCatchLoop,
128 kAnalysisFailAmbiguousArrayOp,
129 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000130};
131
Andreas Gampe9186ced2016-12-12 14:28:21 -0800132template <typename T>
133static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
134 return static_cast<typename std::make_unsigned<T>::type>(x);
135}
136
Vladimir Markof9f64412015-09-02 14:05:49 +0100137class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 public:
139 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
140
141 void AddInstruction(HInstruction* instruction);
142 void RemoveInstruction(HInstruction* instruction);
143
David Brazdilc3d743f2015-04-22 13:40:50 +0100144 // Insert `instruction` before/after an existing instruction `cursor`.
145 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
146 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
147
Roland Levillain6b469232014-09-25 10:10:38 +0100148 // Return true if this list contains `instruction`.
149 bool Contains(HInstruction* instruction) const;
150
Roland Levillainccc07a92014-09-16 14:48:16 +0100151 // Return true if `instruction1` is found before `instruction2` in
152 // this instruction list and false otherwise. Abort if none
153 // of these instructions is found.
154 bool FoundBefore(const HInstruction* instruction1,
155 const HInstruction* instruction2) const;
156
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000157 bool IsEmpty() const { return first_instruction_ == nullptr; }
158 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
159
160 // Update the block of all instructions to be `block`.
161 void SetBlockOfInstructions(HBasicBlock* block) const;
162
163 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000164 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000165 void Add(const HInstructionList& instruction_list);
166
David Brazdil2d7352b2015-04-20 14:52:42 +0100167 // Return the number of instructions in the list. This is an expensive operation.
168 size_t CountSize() const;
169
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100170 private:
171 HInstruction* first_instruction_;
172 HInstruction* last_instruction_;
173
174 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000175 friend class HGraph;
176 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100177 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000178 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100179 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180
181 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
182};
183
David Brazdil4833f5a2015-12-16 10:37:39 +0000184class ReferenceTypeInfo : ValueObject {
185 public:
186 typedef Handle<mirror::Class> TypeHandle;
187
Vladimir Markoa1de9182016-02-25 11:37:38 +0000188 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
189
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700190 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100191 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
192 }
193
Vladimir Markoa1de9182016-02-25 11:37:38 +0000194 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000195 return ReferenceTypeInfo(type_handle, is_exact);
196 }
197
198 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
199
Vladimir Markof39745e2016-01-26 12:16:55 +0000200 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return handle.GetReference() != nullptr;
202 }
203
Vladimir Marko456307a2016-04-19 14:12:13 +0000204 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000205 return IsValidHandle(type_handle_);
206 }
207
208 bool IsExact() const { return is_exact_; }
209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 DCHECK(IsValid());
212 return GetTypeHandle()->IsObjectClass();
213 }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return GetTypeHandle()->IsStringClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return GetTypeHandle()->IsInterface();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsArrayClass();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsPrimitiveArray();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
243 }
244
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 DCHECK(IsValid());
247 if (!IsExact()) return false;
248 if (!IsArrayClass()) return false;
249 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
250 }
251
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000253 DCHECK(IsValid());
254 if (!IsExact()) return false;
255 if (!IsArrayClass()) return false;
256 if (!rti.IsArrayClass()) return false;
257 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
258 rti.GetTypeHandle()->GetComponentType());
259 }
260
261 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
262
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000264 DCHECK(IsValid());
265 DCHECK(rti.IsValid());
266 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
267 }
268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 DCHECK(IsValid());
271 DCHECK(rti.IsValid());
272 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
273 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
274 }
275
276 // Returns true if the type information provide the same amount of details.
277 // Note that it does not mean that the instructions have the same actual type
278 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000280 if (!IsValid() && !rti.IsValid()) {
281 // Invalid types are equal.
282 return true;
283 }
284 if (!IsValid() || !rti.IsValid()) {
285 // One is valid, the other not.
286 return false;
287 }
288 return IsExact() == rti.IsExact()
289 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
290 }
291
292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000293 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
294 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
295 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000296
297 // The class of the object.
298 TypeHandle type_handle_;
299 // Whether or not the type is exact or a superclass of the actual type.
300 // Whether or not we have any information about this type.
301 bool is_exact_;
302};
303
304std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
305
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100307class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100309 HGraph(ArenaAllocator* allocator,
310 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 const DexFile& dex_file,
312 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100314 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000316 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100318 : allocator_(allocator),
319 arena_stack_(arena_stack),
320 blocks_(allocator->Adapter(kArenaAllocBlockList)),
321 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
322 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700323 entry_block_(nullptr),
324 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100325 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100326 number_of_vregs_(0),
327 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000328 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400329 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000330 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700331 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800332 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000333 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000334 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100336 dex_file_(dex_file),
337 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100338 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100339 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800340 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100343 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
344 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
345 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100348 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000349 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700350 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100351 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 blocks_.reserve(kDefaultNumberOfBlocks);
353 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354
David Brazdilbadd8262016-02-02 16:28:56 +0000355 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700356 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000357
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 ArenaAllocator* GetAllocator() const { return allocator_; }
359 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100360 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
361
David Brazdil69ba7b72015-06-23 18:27:30 +0100362 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000363 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 HBasicBlock* GetEntryBlock() const { return entry_block_; }
366 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100367 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000368
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000369 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
370 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000371
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100373
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100374 void ComputeDominanceInformation();
375 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000376 void ClearLoopInformation();
377 void FindBackEdges(ArenaBitVector* visited);
378 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100379 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100380 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000381
David Brazdil4833f5a2015-12-16 10:37:39 +0000382 // Analyze all natural loops in this graph. Returns a code specifying that it
383 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000385 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386
David Brazdilffee3d32015-07-06 11:48:53 +0100387 // Iterate over blocks to compute try block membership. Needs reverse post
388 // order and loop information.
389 void ComputeTryBlockInformation();
390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000392 // Returns the instruction to replace the invoke expression or null if the
393 // invoke is for a void method. Note that the caller is responsible for replacing
394 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000395 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000397 // Update the loop and try membership of `block`, which was spawned from `reference`.
398 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
399 // should be the new back edge.
400 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
401 HBasicBlock* reference,
402 bool replace_if_back_edge);
403
Mingyao Yang3584bce2015-05-19 16:01:59 -0700404 // Need to add a couple of blocks to test if the loop body is entered and
405 // put deoptimization instructions, etc.
406 void TransformLoopHeaderForBCE(HBasicBlock* header);
407
Aart Bikf8f5a162017-02-06 15:35:29 -0800408 // Adds a new loop directly after the loop with the given header and exit.
409 // Returns the new preheader.
410 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
411 HBasicBlock* body,
412 HBasicBlock* exit);
413
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000414 // Removes `block` from the graph. Assumes `block` has been disconnected from
415 // other blocks and has no instructions or phis.
416 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000417
David Brazdilfc6a86a2015-06-26 10:33:45 +0000418 // Splits the edge between `block` and `successor` while preserving the
419 // indices in the predecessor/successor lists. If there are multiple edges
420 // between the blocks, the lowest indices are used.
421 // Returns the new block which is empty and has the same dex pc as `successor`.
422 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100424 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100425 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000426
427 // Transform a loop into a format with a single preheader.
428 //
429 // Each phi in the header should be split: original one in the header should only hold
430 // inputs reachable from the back edges and a single input from the preheader. The newly created
431 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
432 //
433 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
434 // that no longer have this property.
435 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
436
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100437 void SimplifyLoop(HBasicBlock* header);
438
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000439 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100440 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000441 return current_instruction_id_++;
442 }
443
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000444 int32_t GetCurrentInstructionId() const {
445 return current_instruction_id_;
446 }
447
448 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100449 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000450 current_instruction_id_ = id;
451 }
452
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100453 uint16_t GetMaximumNumberOfOutVRegs() const {
454 return maximum_number_of_out_vregs_;
455 }
456
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000457 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
458 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100459 }
460
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100461 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
462 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
463 }
464
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000465 void UpdateTemporariesVRegSlots(size_t slots) {
466 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100467 }
468
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000469 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100470 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100472 }
473
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100474 void SetNumberOfVRegs(uint16_t number_of_vregs) {
475 number_of_vregs_ = number_of_vregs;
476 }
477
478 uint16_t GetNumberOfVRegs() const {
479 return number_of_vregs_;
480 }
481
482 void SetNumberOfInVRegs(uint16_t value) {
483 number_of_in_vregs_ = value;
484 }
485
David Brazdildee58d62016-04-07 09:54:26 +0000486 uint16_t GetNumberOfInVRegs() const {
487 return number_of_in_vregs_;
488 }
489
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100490 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100491 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100492 return number_of_vregs_ - number_of_in_vregs_;
493 }
494
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100495 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100496 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100498
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100499 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
500 DCHECK(GetReversePostOrder()[0] == entry_block_);
501 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
502 }
503
504 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
505 return ReverseRange(GetReversePostOrder());
506 }
507
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100508 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100509 return linear_order_;
510 }
511
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100512 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
513 return ReverseRange(GetLinearOrder());
514 }
515
Mark Mendell1152c922015-04-24 17:06:35 -0400516 bool HasBoundsChecks() const {
517 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800518 }
519
Mark Mendell1152c922015-04-24 17:06:35 -0400520 void SetHasBoundsChecks(bool value) {
521 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800522 }
523
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000524 bool IsDebuggable() const { return debuggable_; }
525
David Brazdil8d5b8b22015-03-24 10:51:52 +0000526 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 // already, it is created and inserted into the graph. This method is only for
528 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100529 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000530
531 // TODO: This is problematic for the consistency of reference type propagation
532 // because it can be created anytime after the pass and thus it will be left
533 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600534 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000535
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600536 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
537 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000538 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
540 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000541 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600542 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
543 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000544 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600545 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
546 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000547 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000548
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100549 HCurrentMethod* GetCurrentMethod();
550
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100551 const DexFile& GetDexFile() const {
552 return dex_file_;
553 }
554
555 uint32_t GetMethodIdx() const {
556 return method_idx_;
557 }
558
Igor Murashkind01745e2017-04-05 16:40:31 -0700559 // Get the method name (without the signature), e.g. "<init>"
560 const char* GetMethodName() const;
561
562 // Get the pretty method name (class + name + optionally signature).
563 std::string PrettyMethod(bool with_signature = true) const;
564
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100565 InvokeType GetInvokeType() const {
566 return invoke_type_;
567 }
568
Mark Mendellc4701932015-04-10 13:18:51 -0400569 InstructionSet GetInstructionSet() const {
570 return instruction_set_;
571 }
572
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000573 bool IsCompilingOsr() const { return osr_; }
574
Mingyao Yang063fc772016-08-02 11:02:54 -0700575 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
576 return cha_single_implementation_list_;
577 }
578
579 void AddCHASingleImplementationDependency(ArtMethod* method) {
580 cha_single_implementation_list_.insert(method);
581 }
582
583 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800584 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700585 }
586
David Brazdil77a48ae2015-09-15 12:34:04 +0000587 bool HasTryCatch() const { return has_try_catch_; }
588 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100589
Aart Bikb13c65b2017-03-21 20:14:07 -0700590 bool HasSIMD() const { return has_simd_; }
591 void SetHasSIMD(bool value) { has_simd_ = value; }
592
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800593 bool HasLoops() const { return has_loops_; }
594 void SetHasLoops(bool value) { has_loops_ = value; }
595
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000596 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
597 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
598
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100599 ArtMethod* GetArtMethod() const { return art_method_; }
600 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
601
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100602 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500603 // The instruction has been inserted into the graph, either as a constant, or
604 // before cursor.
605 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
606
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000607 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
608
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800609 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
610 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
611 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
612
David Brazdil2d7352b2015-04-20 14:52:42 +0100613 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000614 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100615 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 template <class InstructionType, typename ValueType>
618 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600619 ArenaSafeMap<ValueType, InstructionType*>* cache,
620 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 // Try to find an existing constant of the given value.
622 InstructionType* constant = nullptr;
623 auto cached_constant = cache->find(value);
624 if (cached_constant != cache->end()) {
625 constant = cached_constant->second;
626 }
627
628 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100629 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000630 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100631 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 cache->Overwrite(value, constant);
633 InsertConstant(constant);
634 }
635 return constant;
636 }
637
David Brazdil8d5b8b22015-03-24 10:51:52 +0000638 void InsertConstant(HConstant* instruction);
639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 // Cache a float constant into the graph. This method should only be
641 // called by the SsaBuilder when creating "equivalent" instructions.
642 void CacheFloatConstant(HFloatConstant* constant);
643
644 // See CacheFloatConstant comment.
645 void CacheDoubleConstant(HDoubleConstant* constant);
646
Vladimir Markoca6fff82017-10-03 14:49:14 +0100647 ArenaAllocator* const allocator_;
648 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000649
650 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000655
Aart Bik281c6812016-08-26 11:31:48 -0700656 // List of blocks to perform a linear order tree traversal. Unlike the reverse
657 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100659
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000660 HBasicBlock* entry_block_;
661 HBasicBlock* exit_block_;
662
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100663 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100664 uint16_t maximum_number_of_out_vregs_;
665
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100666 // The number of virtual registers in this method. Contains the parameters.
667 uint16_t number_of_vregs_;
668
669 // The number of virtual registers used by parameters of this method.
670 uint16_t number_of_in_vregs_;
671
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000672 // Number of vreg size slots that the temporaries use (used in baseline compiler).
673 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100674
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800675 // Flag whether there are bounds checks in the graph. We can skip
676 // BCE if it's false. It's only best effort to keep it up to date in
677 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400678 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800679
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800680 // Flag whether there are try/catch blocks in the graph. We will skip
681 // try/catch-related passes if it's false. It's only best effort to keep
682 // it up to date in the presence of code elimination so there might be
683 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000684 bool has_try_catch_;
685
Aart Bikb13c65b2017-03-21 20:14:07 -0700686 // Flag whether SIMD instructions appear in the graph. If true, the
687 // code generators may have to be more careful spilling the wider
688 // contents of SIMD registers.
689 bool has_simd_;
690
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800691 // Flag whether there are any loops in the graph. We can skip loop
692 // optimization if it's false. It's only best effort to keep it up
693 // to date in the presence of code elimination so there might be false
694 // positives.
695 bool has_loops_;
696
697 // Flag whether there are any irreducible loops in the graph. It's only
698 // best effort to keep it up to date in the presence of code elimination
699 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000700 bool has_irreducible_loops_;
701
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000702 // Indicates whether the graph should be compiled in a way that
703 // ensures full debuggability. If false, we can apply more
704 // aggressive optimizations that may limit the level of debugging.
705 const bool debuggable_;
706
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000707 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000708 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100710 // The dex file from which the method is from.
711 const DexFile& dex_file_;
712
713 // The method index in the dex file.
714 const uint32_t method_idx_;
715
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100716 // If inlined, this encodes how the callee is being invoked.
717 const InvokeType invoke_type_;
718
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100719 // Whether the graph has been transformed to SSA form. Only used
720 // in debug mode to ensure we are not using properties only valid
721 // for non-SSA form (like the number of temporaries).
722 bool in_ssa_form_;
723
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800724 // Number of CHA guards in the graph. Used to short-circuit the
725 // CHA guard optimization pass when there is no CHA guard left.
726 uint32_t number_of_cha_guards_;
727
Mathieu Chartiere401d142015-04-22 13:56:20 -0700728 const InstructionSet instruction_set_;
729
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000730 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000731 HNullConstant* cached_null_constant_;
732 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000733 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000734 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000736
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100737 HCurrentMethod* cached_current_method_;
738
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100739 // The ArtMethod this graph is for. Note that for AOT, it may be null,
740 // for example for methods whose declaring class could not be resolved
741 // (such as when the superclass could not be found).
742 ArtMethod* art_method_;
743
David Brazdil4833f5a2015-12-16 10:37:39 +0000744 // Keep the RTI of inexact Object to avoid having to pass stack handle
745 // collection pointer to passes which may create NullConstant.
746 ReferenceTypeInfo inexact_object_rti_;
747
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000748 // Whether we are compiling this graph for on stack replacement: this will
749 // make all loops seen as irreducible and emit special stack maps to mark
750 // compiled code entries which the interpreter can directly jump to.
751 const bool osr_;
752
Mingyao Yang063fc772016-08-02 11:02:54 -0700753 // List of methods that are assumed to have single implementation.
754 ArenaSet<ArtMethod*> cha_single_implementation_list_;
755
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000756 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100757 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000758 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000759 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760 DISALLOW_COPY_AND_ASSIGN(HGraph);
761};
762
Vladimir Markof9f64412015-09-02 14:05:49 +0100763class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000764 public:
765 HLoopInformation(HBasicBlock* header, HGraph* graph)
766 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100767 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000768 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100769 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100770 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100771 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100772 blocks_(graph->GetAllocator(),
773 graph->GetBlocks().size(),
774 true,
775 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776 back_edges_.reserve(kDefaultNumberOfBackEdges);
777 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000779 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100780 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781
782 void Dump(std::ostream& os);
783
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100784 HBasicBlock* GetHeader() const {
785 return header_;
786 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000787
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000788 void SetHeader(HBasicBlock* block) {
789 header_ = block;
790 }
791
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100792 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
793 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
794 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
795
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000796 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100797 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 }
799
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100800 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100801 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 }
803
David Brazdil46e2a392015-03-16 17:31:52 +0000804 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100805 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100806 }
807
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000808 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100809 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000810 }
811
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100812 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100813
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100814 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100816 }
817
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100818 // Returns the lifetime position of the back edge that has the
819 // greatest lifetime position.
820 size_t GetLifetimeEnd() const;
821
822 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100823 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100824 }
825
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000826 // Finds blocks that are part of this loop.
827 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100828
Artem Serov7f4aff62017-06-21 17:02:18 +0100829 // Updates blocks population of the loop and all of its outer' ones recursively after the
830 // population of the inner loop is updated.
831 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
832
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 // Returns whether this loop information contains `block`.
834 // Note that this loop information *must* be populated before entering this function.
835 bool Contains(const HBasicBlock& block) const;
836
837 // Returns whether this loop information is an inner loop of `other`.
838 // Note that `other` *must* be populated before entering this function.
839 bool IsIn(const HLoopInformation& other) const;
840
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800841 // Returns true if instruction is not defined within this loop.
842 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700843
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100844 const ArenaBitVector& GetBlocks() const { return blocks_; }
845
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000846 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000847 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000849 void ClearAllBlocks() {
850 blocks_.ClearAllBits();
851 }
852
David Brazdil3f4a5222016-05-06 12:46:21 +0100853 bool HasBackEdgeNotDominatedByHeader() const;
854
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100855 bool IsPopulated() const {
856 return blocks_.GetHighestBitSet() != -1;
857 }
858
Anton Shaminf89381f2016-05-16 16:44:13 +0600859 bool DominatesAllBackEdges(HBasicBlock* block);
860
David Sehrc757dec2016-11-04 15:48:34 -0700861 bool HasExitEdge() const;
862
Artem Serov7f4aff62017-06-21 17:02:18 +0100863 // Resets back edge and blocks-in-loop data.
864 void ResetBasicBlockData() {
865 back_edges_.clear();
866 ClearAllBlocks();
867 }
868
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000869 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100870 // Internal recursive implementation of `Populate`.
871 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100872 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100873
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100875 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000876 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100877 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100878 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100879 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000880
881 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
882};
883
David Brazdilec16f792015-08-19 15:04:01 +0100884// Stores try/catch information for basic blocks.
885// Note that HGraph is constructed so that catch blocks cannot simultaneously
886// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100887class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100888 public:
889 // Try block information constructor.
890 explicit TryCatchInformation(const HTryBoundary& try_entry)
891 : try_entry_(&try_entry),
892 catch_dex_file_(nullptr),
893 catch_type_index_(DexFile::kDexNoIndex16) {
894 DCHECK(try_entry_ != nullptr);
895 }
896
897 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800898 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100899 : try_entry_(nullptr),
900 catch_dex_file_(&dex_file),
901 catch_type_index_(catch_type_index) {}
902
903 bool IsTryBlock() const { return try_entry_ != nullptr; }
904
905 const HTryBoundary& GetTryEntry() const {
906 DCHECK(IsTryBlock());
907 return *try_entry_;
908 }
909
910 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
911
912 bool IsCatchAllTypeIndex() const {
913 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800914 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100915 }
916
Andreas Gampea5b09a62016-11-17 15:21:22 -0800917 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100918 DCHECK(IsCatchBlock());
919 return catch_type_index_;
920 }
921
922 const DexFile& GetCatchDexFile() const {
923 DCHECK(IsCatchBlock());
924 return *catch_dex_file_;
925 }
926
927 private:
928 // One of possibly several TryBoundary instructions entering the block's try.
929 // Only set for try blocks.
930 const HTryBoundary* try_entry_;
931
932 // Exception type information. Only set for catch blocks.
933 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800934 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100935};
936
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100937static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100938static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940// A block in a method. Contains the list of instructions represented
941// as a double linked list. Each block knows its predecessors and
942// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100943
Vladimir Markof9f64412015-09-02 14:05:49 +0100944class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000945 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700946 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100948 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
949 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000950 loop_information_(nullptr),
951 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100952 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100953 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100954 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100955 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000956 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000957 try_catch_information_(nullptr) {
958 predecessors_.reserve(kDefaultNumberOfPredecessors);
959 successors_.reserve(kDefaultNumberOfSuccessors);
960 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
961 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962
Vladimir Marko60584552015-09-03 13:35:12 +0000963 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100964 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965 }
966
Vladimir Marko60584552015-09-03 13:35:12 +0000967 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100968 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969 }
970
David Brazdild26a4112015-11-10 11:07:31 +0000971 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
972 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
973
Vladimir Marko60584552015-09-03 13:35:12 +0000974 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
975 return ContainsElement(successors_, block, start_from);
976 }
977
978 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100979 return dominated_blocks_;
980 }
981
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100982 bool IsEntryBlock() const {
983 return graph_->GetEntryBlock() == this;
984 }
985
986 bool IsExitBlock() const {
987 return graph_->GetExitBlock() == this;
988 }
989
David Brazdil46e2a392015-03-16 17:31:52 +0000990 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200991 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700992 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000993 bool IsSingleTryBoundary() const;
994
995 // Returns true if this block emits nothing but a jump.
996 bool IsSingleJump() const {
997 HLoopInformation* loop_info = GetLoopInformation();
998 return (IsSingleGoto() || IsSingleTryBoundary())
999 // Back edges generate a suspend check.
1000 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1001 }
David Brazdil46e2a392015-03-16 17:31:52 +00001002
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001003 void AddBackEdge(HBasicBlock* back_edge) {
1004 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001005 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001006 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001007 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001008 loop_information_->AddBackEdge(back_edge);
1009 }
1010
Artem Serov7f4aff62017-06-21 17:02:18 +01001011 // Registers a back edge; if the block was not a loop header before the call associates a newly
1012 // created loop info with it.
1013 //
1014 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1015 // info for all blocks during back edges recalculation.
1016 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1017 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1018 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1019 }
1020 loop_information_->AddBackEdge(back_edge);
1021 }
1022
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001023 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001024 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001025
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001026 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001027 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001028 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001029
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001030 HBasicBlock* GetDominator() const { return dominator_; }
1031 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001032 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1033
1034 void RemoveDominatedBlock(HBasicBlock* block) {
1035 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001036 }
Vladimir Marko60584552015-09-03 13:35:12 +00001037
1038 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1039 ReplaceElement(dominated_blocks_, existing, new_block);
1040 }
1041
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001042 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001043
1044 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001045 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001046 }
1047
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1049 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001050 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001051 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001052 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1053 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001055 HInstruction* GetFirstInstructionDisregardMoves() const;
1056
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001057 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001058 successors_.push_back(block);
1059 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060 }
1061
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001062 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1063 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001064 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001065 new_block->predecessors_.push_back(this);
1066 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001067 }
1068
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001069 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1070 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001072 new_block->successors_.push_back(this);
1073 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001074 }
1075
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001076 // Insert `this` between `predecessor` and `successor. This method
1077 // preserves the indicies, and will update the first edge found between
1078 // `predecessor` and `successor`.
1079 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1080 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001081 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001082 successor->predecessors_[predecessor_index] = this;
1083 predecessor->successors_[successor_index] = this;
1084 successors_.push_back(successor);
1085 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001086 }
1087
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001088 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001089 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001090 }
1091
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001093 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 }
1095
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001096 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001097 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 }
1099
1100 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001101 predecessors_.push_back(block);
1102 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001103 }
1104
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001105 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001106 DCHECK_EQ(predecessors_.size(), 2u);
1107 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001108 }
1109
David Brazdil769c9e52015-04-27 13:54:09 +01001110 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001111 DCHECK_EQ(successors_.size(), 2u);
1112 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001113 }
1114
David Brazdilfc6a86a2015-06-26 10:33:45 +00001115 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001116 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001117 }
1118
David Brazdilfc6a86a2015-06-26 10:33:45 +00001119 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001120 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001121 }
1122
David Brazdilfc6a86a2015-06-26 10:33:45 +00001123 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001124 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001125 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001126 }
1127
1128 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001129 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001130 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001131 }
1132
1133 // Returns whether the first occurrence of `predecessor` in the list of
1134 // predecessors is at index `idx`.
1135 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001136 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001137 return GetPredecessorIndexOf(predecessor) == idx;
1138 }
1139
David Brazdild7558da2015-09-22 13:04:14 +01001140 // Create a new block between this block and its predecessors. The new block
1141 // is added to the graph, all predecessor edges are relinked to it and an edge
1142 // is created to `this`. Returns the new empty block. Reverse post order or
1143 // loop and try/catch information are not updated.
1144 HBasicBlock* CreateImmediateDominator();
1145
David Brazdilfc6a86a2015-06-26 10:33:45 +00001146 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001147 // created, latter block. Note that this method will add the block to the
1148 // graph, create a Goto at the end of the former block and will create an edge
1149 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001150 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001151 HBasicBlock* SplitBefore(HInstruction* cursor);
1152
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001153 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001154 // created block. Note that this method just updates raw block information,
1155 // like predecessors, successors, dominators, and instruction list. It does not
1156 // update the graph, reverse post order, loop information, nor make sure the
1157 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001158 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1159
1160 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1161 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001162
1163 // Merge `other` at the end of `this`. Successors and dominated blocks of
1164 // `other` are changed to be successors and dominated blocks of `this`. Note
1165 // that this method does not update the graph, reverse post order, loop
1166 // information, nor make sure the blocks are consistent (for example ending
1167 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001168 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001169
1170 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1171 // of `this` are moved to `other`.
1172 // Note that this method does not update the graph, reverse post order, loop
1173 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001174 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001175 void ReplaceWith(HBasicBlock* other);
1176
Aart Bik6b69e0a2017-01-11 10:20:43 -08001177 // Merges the instructions of `other` at the end of `this`.
1178 void MergeInstructionsWith(HBasicBlock* other);
1179
David Brazdil2d7352b2015-04-20 14:52:42 +01001180 // Merge `other` at the end of `this`. This method updates loops, reverse post
1181 // order, links to predecessors, successors, dominators and deletes the block
1182 // from the graph. The two blocks must be successive, i.e. `this` the only
1183 // predecessor of `other` and vice versa.
1184 void MergeWith(HBasicBlock* other);
1185
1186 // Disconnects `this` from all its predecessors, successors and dominator,
1187 // removes it from all loops it is included in and eventually from the graph.
1188 // The block must not dominate any other block. Predecessors and successors
1189 // are safely updated.
1190 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001193 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001194 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001195 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001196 // Replace phi `initial` with `replacement` within this block.
1197 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001198 // Replace instruction `initial` with `replacement` within this block.
1199 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1200 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001201 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001202 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001203 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1204 // instruction list. With 'ensure_safety' set to true, it verifies that the
1205 // instruction is not in use and removes it from the use lists of its inputs.
1206 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1207 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001208 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001209
1210 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001211 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001212 }
1213
Roland Levillain6b879dd2014-09-22 17:13:44 +01001214 bool IsLoopPreHeaderFirstPredecessor() const {
1215 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001216 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001217 }
1218
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001219 bool IsFirstPredecessorBackEdge() const {
1220 DCHECK(IsLoopHeader());
1221 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1222 }
1223
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001224 HLoopInformation* GetLoopInformation() const {
1225 return loop_information_;
1226 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001227
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001228 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001229 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 void SetInLoop(HLoopInformation* info) {
1232 if (IsLoopHeader()) {
1233 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001234 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001235 loop_information_ = info;
1236 } else if (loop_information_->Contains(*info->GetHeader())) {
1237 // Block is currently part of an outer loop. Make it part of this inner loop.
1238 // Note that a non loop header having a loop information means this loop information
1239 // has already been populated
1240 loop_information_ = info;
1241 } else {
1242 // Block is part of an inner loop. Do not update the loop information.
1243 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1244 // at this point, because this method is being called while populating `info`.
1245 }
1246 }
1247
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001248 // Raw update of the loop information.
1249 void SetLoopInformation(HLoopInformation* info) {
1250 loop_information_ = info;
1251 }
1252
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001253 bool IsInLoop() const { return loop_information_ != nullptr; }
1254
David Brazdilec16f792015-08-19 15:04:01 +01001255 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1256
1257 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1258 try_catch_information_ = try_catch_information;
1259 }
1260
1261 bool IsTryBlock() const {
1262 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1263 }
1264
1265 bool IsCatchBlock() const {
1266 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1267 }
David Brazdilffee3d32015-07-06 11:48:53 +01001268
1269 // Returns the try entry that this block's successors should have. They will
1270 // be in the same try, unless the block ends in a try boundary. In that case,
1271 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001272 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001273
David Brazdild7558da2015-09-22 13:04:14 +01001274 bool HasThrowingInstructions() const;
1275
David Brazdila4b8c212015-05-07 09:59:30 +01001276 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001277 bool Dominates(HBasicBlock* block) const;
1278
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001279 size_t GetLifetimeStart() const { return lifetime_start_; }
1280 size_t GetLifetimeEnd() const { return lifetime_end_; }
1281
1282 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1283 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1284
David Brazdil8d5b8b22015-03-24 10:51:52 +00001285 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001286 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001287 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001288 bool HasSinglePhi() const;
1289
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001290 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001291 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001292 ArenaVector<HBasicBlock*> predecessors_;
1293 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001294 HInstructionList instructions_;
1295 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001296 HLoopInformation* loop_information_;
1297 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001298 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001299 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001300 // The dex program counter of the first instruction of this block.
1301 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001302 size_t lifetime_start_;
1303 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001304 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001305
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001306 friend class HGraph;
1307 friend class HInstruction;
1308
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001309 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1310};
1311
David Brazdilb2bd1c52015-03-25 11:17:37 +00001312// Iterates over the LoopInformation of all loops which contain 'block'
1313// from the innermost to the outermost.
1314class HLoopInformationOutwardIterator : public ValueObject {
1315 public:
1316 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1317 : current_(block.GetLoopInformation()) {}
1318
1319 bool Done() const { return current_ == nullptr; }
1320
1321 void Advance() {
1322 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001323 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001324 }
1325
1326 HLoopInformation* Current() const {
1327 DCHECK(!Done());
1328 return current_;
1329 }
1330
1331 private:
1332 HLoopInformation* current_;
1333
1334 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1335};
1336
Alexandre Ramesef20f712015-06-09 10:29:30 +01001337#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001338 M(Above, Condition) \
1339 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001340 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001341 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001342 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001343 M(ArrayGet, Instruction) \
1344 M(ArrayLength, Instruction) \
1345 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001346 M(Below, Condition) \
1347 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001348 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001349 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001350 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001351 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001352 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001353 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001354 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001355 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001356 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001357 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001358 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001359 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001360 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001361 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001362 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001363 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001364 M(Exit, Instruction) \
1365 M(FloatConstant, Constant) \
1366 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(GreaterThan, Condition) \
1368 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001370 M(InstanceFieldGet, Instruction) \
1371 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001372 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001373 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001374 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001375 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001376 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001377 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001378 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001379 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001380 M(LessThan, Condition) \
1381 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001382 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001383 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001384 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001385 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001386 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001387 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001388 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001389 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001390 M(Neg, UnaryOperation) \
1391 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001392 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001393 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001394 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001395 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001397 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001398 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001400 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001401 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001402 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001403 M(Return, Instruction) \
1404 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001405 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001406 M(Shl, BinaryOperation) \
1407 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001408 M(StaticFieldGet, Instruction) \
1409 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001410 M(UnresolvedInstanceFieldGet, Instruction) \
1411 M(UnresolvedInstanceFieldSet, Instruction) \
1412 M(UnresolvedStaticFieldGet, Instruction) \
1413 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001414 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001415 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001416 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001417 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001418 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001419 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001420 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001421 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001422 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001423 M(VecExtractScalar, VecUnaryOperation) \
1424 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001425 M(VecCnv, VecUnaryOperation) \
1426 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001427 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001428 M(VecNot, VecUnaryOperation) \
1429 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001430 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001431 M(VecSub, VecBinaryOperation) \
1432 M(VecMul, VecBinaryOperation) \
1433 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001434 M(VecMin, VecBinaryOperation) \
1435 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001436 M(VecAnd, VecBinaryOperation) \
1437 M(VecAndNot, VecBinaryOperation) \
1438 M(VecOr, VecBinaryOperation) \
1439 M(VecXor, VecBinaryOperation) \
1440 M(VecShl, VecBinaryOperation) \
1441 M(VecShr, VecBinaryOperation) \
1442 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001443 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001444 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001445 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001446 M(VecLoad, VecMemoryOperation) \
1447 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001448
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001449/*
1450 * Instructions, shared across several (not all) architectures.
1451 */
1452#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1453#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1454#else
1455#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001456 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001457 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001458 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001459 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001460#endif
1461
Alexandre Ramesef20f712015-06-09 10:29:30 +01001462#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1463
1464#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1465
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001466#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001467#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001468#else
1469#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1470 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001471 M(MipsPackedSwitch, Instruction) \
1472 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001473#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001474
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001475#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1476
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001477#ifndef ART_ENABLE_CODEGEN_x86
1478#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1479#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001480#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1481 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001482 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001483 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001484 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001485#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001486
1487#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1488
1489#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1490 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001491 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001492 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1493 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001494 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001495 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001496 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1497 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1498
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001499#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1500 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001501 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001502 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001503 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001504 M(Invoke, Instruction) \
1505 M(VecOperation, Instruction) \
1506 M(VecUnaryOperation, VecOperation) \
1507 M(VecBinaryOperation, VecOperation) \
1508 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001509
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001510#define FOR_EACH_INSTRUCTION(M) \
1511 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1512 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1513
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001514#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001515FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1516#undef FORWARD_DECLARATION
1517
Vladimir Marko372f10e2016-05-17 16:30:10 +01001518#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001519 private: \
1520 H##type& operator=(const H##type&) = delete; \
1521 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001522 const char* DebugName() const OVERRIDE { return #type; } \
1523 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1524 return other->Is##type(); \
1525 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001526 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1527 DCHECK(IsClonable()); \
1528 return new (arena) H##type(*this->As##type()); \
1529 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001530 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001531
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001532#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001533 private: \
1534 H##type& operator=(const H##type&) = delete; \
1535 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001536 bool Is##type() const { return As##type() != nullptr; } \
1537 const H##type* As##type() const { return this; } \
1538 H##type* As##type() { return this; }
1539
Artem Serovcced8ba2017-07-19 18:18:09 +01001540#define DEFAULT_COPY_CONSTRUCTOR(type) \
1541 explicit H##type(const H##type& other) = default;
1542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001543template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001544class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1545 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001546 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001547 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001548 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001549 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001551 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001552 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001554 private:
David Brazdiled596192015-01-23 10:39:45 +00001555 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001556 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001557
1558 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001559 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001560
Vladimir Marko46817b82016-03-29 12:21:58 +01001561 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001562
1563 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1564};
1565
David Brazdiled596192015-01-23 10:39:45 +00001566template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001567using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001568
David Brazdil1abb4192015-02-17 18:33:36 +00001569// This class is used by HEnvironment and HInstruction classes to record the
1570// instructions they use and pointers to the corresponding HUseListNodes kept
1571// by the used instructions.
1572template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001573class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001574 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001575 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1576 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001577
Vladimir Marko46817b82016-03-29 12:21:58 +01001578 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1579 : HUserRecord(old_record.instruction_, before_use_node) {}
1580 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1581 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001582 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001583 }
1584
1585 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001586 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1587 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001588
1589 private:
1590 // Instruction used by the user.
1591 HInstruction* instruction_;
1592
Vladimir Marko46817b82016-03-29 12:21:58 +01001593 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1594 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001595};
1596
Vladimir Markoe9004912016-06-16 16:50:52 +01001597// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1598// This is used for HInstruction::GetInputs() to return a container wrapper providing
1599// HInstruction* values even though the underlying container has HUserRecord<>s.
1600struct HInputExtractor {
1601 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1602 return record.GetInstruction();
1603 }
1604 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1605 return record.GetInstruction();
1606 }
1607};
1608
1609using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1610using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1611
Aart Bik854a02b2015-07-14 16:07:00 -07001612/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001614 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 * For write/read dependences on fields/arrays, the dependence analysis uses
1616 * type disambiguation (e.g. a float field write cannot modify the value of an
1617 * integer field read) and the access type (e.g. a reference array write cannot
1618 * modify the value of a reference field read [although it may modify the
1619 * reference fetch prior to reading the field, which is represented by its own
1620 * write/read dependence]). The analysis makes conservative points-to
1621 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1622 * the same, and any reference read depends on any reference read without
1623 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001624 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 * The internal representation uses 38-bit and is described in the table below.
1626 * The first line indicates the side effect, and for field/array accesses the
1627 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001628 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 * The two numbered lines below indicate the bit position in the bitfield (read
1630 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001631 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1633 * +-------------+---------+---------+--------------+---------+---------+
1634 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1635 * | 3 |333333322|222222221| 1 |111111110|000000000|
1636 * | 7 |654321098|765432109| 8 |765432109|876543210|
1637 *
1638 * Note that, to ease the implementation, 'changes' bits are least significant
1639 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001640 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641class SideEffects : public ValueObject {
1642 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001643 SideEffects() : flags_(0) {}
1644
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001645 static SideEffects None() {
1646 return SideEffects(0);
1647 }
1648
1649 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 return SideEffects(kAllChangeBits | kAllDependOnBits);
1651 }
1652
1653 static SideEffects AllChanges() {
1654 return SideEffects(kAllChangeBits);
1655 }
1656
1657 static SideEffects AllDependencies() {
1658 return SideEffects(kAllDependOnBits);
1659 }
1660
1661 static SideEffects AllExceptGCDependency() {
1662 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1663 }
1664
1665 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001666 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001667 }
1668
Aart Bik34c3ba92015-07-20 14:08:59 -07001669 static SideEffects AllWrites() {
1670 return SideEffects(kAllWrites);
1671 }
1672
1673 static SideEffects AllReads() {
1674 return SideEffects(kAllReads);
1675 }
1676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001677 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001678 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001679 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001680 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001681 }
1682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001684 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001685 }
1686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001688 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001689 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001690 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001691 }
1692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001693 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001694 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001695 }
1696
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001697 static SideEffects CanTriggerGC() {
1698 return SideEffects(1ULL << kCanTriggerGCBit);
1699 }
1700
1701 static SideEffects DependsOnGC() {
1702 return SideEffects(1ULL << kDependsOnGCBit);
1703 }
1704
Aart Bik854a02b2015-07-14 16:07:00 -07001705 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001706 SideEffects Union(SideEffects other) const {
1707 return SideEffects(flags_ | other.flags_);
1708 }
1709
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001710 SideEffects Exclusion(SideEffects other) const {
1711 return SideEffects(flags_ & ~other.flags_);
1712 }
1713
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001714 void Add(SideEffects other) {
1715 flags_ |= other.flags_;
1716 }
1717
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001718 bool Includes(SideEffects other) const {
1719 return (other.flags_ & flags_) == other.flags_;
1720 }
1721
1722 bool HasSideEffects() const {
1723 return (flags_ & kAllChangeBits);
1724 }
1725
1726 bool HasDependencies() const {
1727 return (flags_ & kAllDependOnBits);
1728 }
1729
1730 // Returns true if there are no side effects or dependencies.
1731 bool DoesNothing() const {
1732 return flags_ == 0;
1733 }
1734
Aart Bik854a02b2015-07-14 16:07:00 -07001735 // Returns true if something is written.
1736 bool DoesAnyWrite() const {
1737 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001738 }
1739
Aart Bik854a02b2015-07-14 16:07:00 -07001740 // Returns true if something is read.
1741 bool DoesAnyRead() const {
1742 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001743 }
1744
Aart Bik854a02b2015-07-14 16:07:00 -07001745 // Returns true if potentially everything is written and read
1746 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001747 bool DoesAllReadWrite() const {
1748 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1749 }
1750
Aart Bik854a02b2015-07-14 16:07:00 -07001751 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001752 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001753 }
1754
Roland Levillain0d5a2812015-11-13 10:07:31 +00001755 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001756 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001757 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1758 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001759 }
1760
1761 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001762 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001763 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001764 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001765 for (int s = kLastBit; s >= 0; s--) {
1766 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1767 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1768 // This is a bit for the GC side effect.
1769 if (current_bit_is_set) {
1770 flags += "GC";
1771 }
Aart Bik854a02b2015-07-14 16:07:00 -07001772 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001773 } else {
1774 // This is a bit for the array/field analysis.
1775 // The underscore character stands for the 'can trigger GC' bit.
1776 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1777 if (current_bit_is_set) {
1778 flags += kDebug[s];
1779 }
1780 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1781 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1782 flags += "|";
1783 }
1784 }
Aart Bik854a02b2015-07-14 16:07:00 -07001785 }
1786 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001787 }
1788
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001789 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 private:
1792 static constexpr int kFieldArrayAnalysisBits = 9;
1793
1794 static constexpr int kFieldWriteOffset = 0;
1795 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1796 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1797 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1798
1799 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1800
1801 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1802 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1803 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1804 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1805
1806 static constexpr int kLastBit = kDependsOnGCBit;
1807 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1808
1809 // Aliases.
1810
1811 static_assert(kChangeBits == kDependOnBits,
1812 "the 'change' bits should match the 'depend on' bits.");
1813
1814 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1815 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1816 static constexpr uint64_t kAllWrites =
1817 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1818 static constexpr uint64_t kAllReads =
1819 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001820
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001821 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001823 int shift;
1824 switch (type) {
1825 case DataType::Type::kReference: shift = 0; break;
1826 case DataType::Type::kBool: shift = 1; break;
1827 case DataType::Type::kInt8: shift = 2; break;
1828 case DataType::Type::kUint16: shift = 3; break;
1829 case DataType::Type::kInt16: shift = 4; break;
1830 case DataType::Type::kInt32: shift = 5; break;
1831 case DataType::Type::kInt64: shift = 6; break;
1832 case DataType::Type::kFloat32: shift = 7; break;
1833 case DataType::Type::kFloat64: shift = 8; break;
1834 default:
1835 LOG(FATAL) << "Unexpected data type " << type;
1836 UNREACHABLE();
1837 }
Aart Bik854a02b2015-07-14 16:07:00 -07001838 DCHECK_LE(kFieldWriteOffset, shift);
1839 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001840 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001841 }
1842
1843 // Private constructor on direct flags value.
1844 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1845
1846 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001847};
1848
David Brazdiled596192015-01-23 10:39:45 +00001849// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001850class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001851 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001852 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001853 size_t number_of_vregs,
1854 ArtMethod* method,
1855 uint32_t dex_pc,
1856 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001857 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1858 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001859 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001860 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001861 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001862 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001863 }
1864
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001865 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1866 const HEnvironment& to_copy,
1867 HInstruction* holder)
1868 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001869 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001870 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001871 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001872 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001873
Vladimir Markoec32f642017-06-02 10:51:55 +01001874 void AllocateLocations() {
1875 DCHECK(locations_.empty());
1876 locations_.resize(vregs_.size());
1877 }
1878
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001879 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001880 if (parent_ != nullptr) {
1881 parent_->SetAndCopyParentChain(allocator, parent);
1882 } else {
1883 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1884 parent_->CopyFrom(parent);
1885 if (parent->GetParent() != nullptr) {
1886 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1887 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001888 }
David Brazdiled596192015-01-23 10:39:45 +00001889 }
1890
Vladimir Marko69d310e2017-10-09 14:12:23 +01001891 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001892 void CopyFrom(HEnvironment* environment);
1893
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001894 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1895 // input to the loop phi instead. This is for inserting instructions that
1896 // require an environment (like HDeoptimization) in the loop pre-header.
1897 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001898
1899 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001900 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001901 }
1902
1903 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001904 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001905 }
1906
David Brazdil1abb4192015-02-17 18:33:36 +00001907 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001908
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001909 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001910
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001911 HEnvironment* GetParent() const { return parent_; }
1912
1913 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001914 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001915 }
1916
1917 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001918 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001919 }
1920
1921 uint32_t GetDexPc() const {
1922 return dex_pc_;
1923 }
1924
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001925 ArtMethod* GetMethod() const {
1926 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001927 }
1928
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001929 HInstruction* GetHolder() const {
1930 return holder_;
1931 }
1932
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001933
1934 bool IsFromInlinedInvoke() const {
1935 return GetParent() != nullptr;
1936 }
1937
David Brazdiled596192015-01-23 10:39:45 +00001938 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001939 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1940 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001941 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001942 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001943 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001944
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001945 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001946 HInstruction* const holder_;
1947
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001948 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001949
1950 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1951};
1952
Vladimir Markof9f64412015-09-02 14:05:49 +01001953class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001954 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301955#define DECLARE_KIND(type, super) k##type,
1956 enum InstructionKind {
1957 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1958 kLastInstructionKind
1959 };
1960#undef DECLARE_KIND
1961
1962 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001963 : previous_(nullptr),
1964 next_(nullptr),
1965 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001966 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001967 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001968 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001969 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001970 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001971 locations_(nullptr),
1972 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001973 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001974 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001975 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301976 SetPackedField<InstructionKindField>(kind);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001977 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1978 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001979
Dave Allison20dfc792014-06-16 20:44:29 -07001980 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001981
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001982
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001983 HInstruction* GetNext() const { return next_; }
1984 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985
Calin Juravle77520bc2015-01-12 18:45:46 +00001986 HInstruction* GetNextDisregardingMoves() const;
1987 HInstruction* GetPreviousDisregardingMoves() const;
1988
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001989 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001990 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001991 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001992 bool IsInBlock() const { return block_ != nullptr; }
1993 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001994 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1995 bool IsIrreducibleLoopHeaderPhi() const {
1996 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1997 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001998
Vladimir Marko372f10e2016-05-17 16:30:10 +01001999 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2000
2001 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2002 // One virtual method is enough, just const_cast<> and then re-add the const.
2003 return ArrayRef<const HUserRecord<HInstruction*>>(
2004 const_cast<HInstruction*>(this)->GetInputRecords());
2005 }
2006
Vladimir Markoe9004912016-06-16 16:50:52 +01002007 HInputsRef GetInputs() {
2008 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002009 }
2010
Vladimir Markoe9004912016-06-16 16:50:52 +01002011 HConstInputsRef GetInputs() const {
2012 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002013 }
2014
2015 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002016 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017
Aart Bik2767f4b2016-10-28 15:03:53 -07002018 bool HasInput(HInstruction* input) const {
2019 for (const HInstruction* i : GetInputs()) {
2020 if (i == input) {
2021 return true;
2022 }
2023 }
2024 return false;
2025 }
2026
Vladimir Marko372f10e2016-05-17 16:30:10 +01002027 void SetRawInputAt(size_t index, HInstruction* input) {
2028 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2029 }
2030
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002031 virtual void Accept(HGraphVisitor* visitor) = 0;
2032 virtual const char* DebugName() const = 0;
2033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002035
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002036 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002037
2038 uint32_t GetDexPc() const { return dex_pc_; }
2039
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002040 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002041
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002042 // Can the instruction throw?
2043 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2044 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002045 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002046
2047 // Does the instruction always throw an exception unconditionally?
2048 virtual bool AlwaysThrows() const { return false; }
2049
David Brazdilec16f792015-08-19 15:04:01 +01002050 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002051
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002052 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002053 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002054
Calin Juravle10e244f2015-01-26 18:54:32 +00002055 // Does not apply for all instructions, but having this at top level greatly
2056 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002057 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002058 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002060 return true;
2061 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002062
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002063 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002064 return false;
2065 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002066
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002067 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002068 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002069 }
2070
Calin Juravle2e768302015-07-28 14:41:11 +00002071 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002072
Calin Juravle61d544b2015-02-23 16:46:57 +00002073 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002075 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002076 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002077 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002078
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002079 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002080 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002081 // Note: fixup_end remains valid across push_front().
2082 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2083 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002084 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002085 uses_.push_front(*new_node);
2086 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002087 }
2088
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002089 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002090 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002091 // Note: env_fixup_end remains valid across push_front().
2092 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2093 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002094 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002095 env_uses_.push_front(*new_node);
2096 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002097 }
2098
David Brazdil1abb4192015-02-17 18:33:36 +00002099 void RemoveAsUserOfInput(size_t input) {
2100 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002101 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2102 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2103 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002104 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002105
Vladimir Marko372f10e2016-05-17 16:30:10 +01002106 void RemoveAsUserOfAllInputs() {
2107 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2108 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2109 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2110 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2111 }
2112 }
2113
David Brazdil1abb4192015-02-17 18:33:36 +00002114 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2115 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002116
Vladimir Marko46817b82016-03-29 12:21:58 +01002117 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2118 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2119 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002120 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002121 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002122 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123
Aart Bikcc42be02016-10-20 16:14:16 -07002124 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002125 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002126 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002127 !CanThrow() &&
2128 !IsSuspendCheck() &&
2129 !IsControlFlow() &&
2130 !IsNativeDebugInfo() &&
2131 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002132 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002133 !IsMemoryBarrier() &&
2134 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002135 }
2136
Aart Bikcc42be02016-10-20 16:14:16 -07002137 bool IsDeadAndRemovable() const {
2138 return IsRemovable() && !HasUses();
2139 }
2140
Roland Levillain6c82d402014-10-13 16:10:27 +01002141 // Does this instruction strictly dominate `other_instruction`?
2142 // Returns false if this instruction and `other_instruction` are the same.
2143 // Aborts if this instruction and `other_instruction` are both phis.
2144 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002145
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002146 int GetId() const { return id_; }
2147 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002148
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002149 int GetSsaIndex() const { return ssa_index_; }
2150 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2151 bool HasSsaIndex() const { return ssa_index_ != -1; }
2152
2153 bool HasEnvironment() const { return environment_ != nullptr; }
2154 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002155 // Set the `environment_` field. Raw because this method does not
2156 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002157 void SetRawEnvironment(HEnvironment* environment) {
2158 DCHECK(environment_ == nullptr);
2159 DCHECK_EQ(environment->GetHolder(), this);
2160 environment_ = environment;
2161 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002162
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002163 void InsertRawEnvironment(HEnvironment* environment) {
2164 DCHECK(environment_ != nullptr);
2165 DCHECK_EQ(environment->GetHolder(), this);
2166 DCHECK(environment->GetParent() == nullptr);
2167 environment->parent_ = environment_;
2168 environment_ = environment;
2169 }
2170
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002171 void RemoveEnvironment();
2172
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002173 // Set the environment of this instruction, copying it from `environment`. While
2174 // copying, the uses lists are being updated.
2175 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002176 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002177 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002178 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002179 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002180 if (environment->GetParent() != nullptr) {
2181 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2182 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002183 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002184
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002185 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2186 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002187 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002188 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002189 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002190 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002191 if (environment->GetParent() != nullptr) {
2192 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2193 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002194 }
2195
Nicolas Geoffray39468442014-09-02 15:17:15 +01002196 // Returns the number of entries in the environment. Typically, that is the
2197 // number of dex registers in a method. It could be more in case of inlining.
2198 size_t EnvironmentSize() const;
2199
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002200 LocationSummary* GetLocations() const { return locations_; }
2201 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002202
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002204 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002205 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002206
Alexandre Rames188d4312015-04-09 18:30:21 +01002207 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2208 // uses of this instruction by `other` are *not* updated.
2209 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2210 ReplaceWith(other);
2211 other->ReplaceInput(this, use_index);
2212 }
2213
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002214 // Move `this` instruction before `cursor`
2215 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002216
Vladimir Markofb337ea2015-11-25 15:25:10 +00002217 // Move `this` before its first user and out of any loops. If there is no
2218 // out-of-loop user that dominates all other users, move the instruction
2219 // to the end of the out-of-loop common dominator of the user's blocks.
2220 //
2221 // This can be used only on non-throwing instructions with no side effects that
2222 // have at least one use but no environment uses.
2223 void MoveBeforeFirstUserAndOutOfLoops();
2224
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002225#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002226 bool Is##type() const; \
2227 const H##type* As##type() const; \
2228 H##type* As##type();
2229
2230 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2231#undef INSTRUCTION_TYPE_CHECK
2232
2233#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002234 bool Is##type() const { return (As##type() != nullptr); } \
2235 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002236 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002237 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002238#undef INSTRUCTION_TYPE_CHECK
2239
Artem Serovcced8ba2017-07-19 18:18:09 +01002240 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2241 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2242 // the instruction then the behaviour of this function is undefined.
2243 //
2244 // Note: It is semantically valid to create a clone of the instruction only until
2245 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2246 // copied.
2247 //
2248 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2249 // 'explicit HInstruction(const HInstruction& other)' for details.
2250 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2251 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2252 DebugName() << " " << GetId();
2253 UNREACHABLE();
2254 }
2255
2256 // Return whether instruction can be cloned (copied).
2257 virtual bool IsClonable() const { return false; }
2258
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002259 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002260 // TODO: this method is used by LICM and GVN with possibly different
2261 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002262 virtual bool CanBeMoved() const { return false; }
2263
2264 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002265 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002266 return false;
2267 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002268
2269 // Returns whether any data encoded in the two instructions is equal.
2270 // This method does not look at the inputs. Both instructions must be
2271 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002272 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002273 return false;
2274 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002275
2276 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002277 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002278 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002279 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002280
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002281 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2282 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2283 // the virtual function because the __attribute__((__pure__)) doesn't really
2284 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302285 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002286
2287 virtual size_t ComputeHashCode() const {
2288 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002289 for (const HInstruction* input : GetInputs()) {
2290 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002291 }
2292 return result;
2293 }
2294
2295 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002296 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002297 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002298
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002299 size_t GetLifetimePosition() const { return lifetime_position_; }
2300 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2301 LiveInterval* GetLiveInterval() const { return live_interval_; }
2302 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2303 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2304
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002305 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2306
2307 // Returns whether the code generation of the instruction will require to have access
2308 // to the current method. Such instructions are:
2309 // (1): Instructions that require an environment, as calling the runtime requires
2310 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002311 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2312 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002313 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002314 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002315 }
2316
Vladimir Markodc151b22015-10-15 18:02:30 +01002317 // Returns whether the code generation of the instruction will require to have access
2318 // to the dex cache of the current method's declaring class via the current method.
2319 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002320
Mark Mendellc4701932015-04-10 13:18:51 -04002321 // Does this instruction have any use in an environment before
2322 // control flow hits 'other'?
2323 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2324
2325 // Remove all references to environment uses of this instruction.
2326 // The caller must ensure that this is safe to do.
2327 void RemoveEnvironmentUsers();
2328
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2330 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002331
David Brazdil1abb4192015-02-17 18:33:36 +00002332 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002333 // If set, the machine code for this instruction is assumed to be generated by
2334 // its users. Used by liveness analysis to compute use positions accordingly.
2335 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2336 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302337 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2338 static constexpr size_t kFieldInstructionKindSize =
2339 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
2340 static constexpr size_t kNumberOfGenericPackedBits =
2341 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002342 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2343
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302344 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2345 "Too many generic packed fields");
2346
Vladimir Marko372f10e2016-05-17 16:30:10 +01002347 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2348 return GetInputRecords()[i];
2349 }
2350
2351 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2352 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2353 input_records[index] = input;
2354 }
David Brazdil1abb4192015-02-17 18:33:36 +00002355
Vladimir Markoa1de9182016-02-25 11:37:38 +00002356 uint32_t GetPackedFields() const {
2357 return packed_fields_;
2358 }
2359
2360 template <size_t flag>
2361 bool GetPackedFlag() const {
2362 return (packed_fields_ & (1u << flag)) != 0u;
2363 }
2364
2365 template <size_t flag>
2366 void SetPackedFlag(bool value = true) {
2367 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2368 }
2369
2370 template <typename BitFieldType>
2371 typename BitFieldType::value_type GetPackedField() const {
2372 return BitFieldType::Decode(packed_fields_);
2373 }
2374
2375 template <typename BitFieldType>
2376 void SetPackedField(typename BitFieldType::value_type value) {
2377 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2378 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2379 }
2380
Artem Serovcced8ba2017-07-19 18:18:09 +01002381 // Copy construction for the instruction (used for Clone function).
2382 //
2383 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2384 // prepare_for_register_allocator are not copied (set to default values).
2385 //
2386 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2387 // fine for most of them. However for some of the instructions a custom copy constructor must be
2388 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2389 // for copying them).
2390 explicit HInstruction(const HInstruction& other)
2391 : previous_(nullptr),
2392 next_(nullptr),
2393 block_(nullptr),
2394 dex_pc_(other.dex_pc_),
2395 id_(-1),
2396 ssa_index_(-1),
2397 packed_fields_(other.packed_fields_),
2398 environment_(nullptr),
2399 locations_(nullptr),
2400 live_interval_(nullptr),
2401 lifetime_position_(kNoLifetime),
2402 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302403 reference_type_handle_(other.reference_type_handle_) {
2404 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002405
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002406 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302407 using InstructionKindField =
2408 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2409
Vladimir Marko46817b82016-03-29 12:21:58 +01002410 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2411 auto before_use_node = uses_.before_begin();
2412 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2413 HInstruction* user = use_node->GetUser();
2414 size_t input_index = use_node->GetIndex();
2415 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2416 before_use_node = use_node;
2417 }
2418 }
2419
2420 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2421 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2422 if (next != uses_.end()) {
2423 HInstruction* next_user = next->GetUser();
2424 size_t next_index = next->GetIndex();
2425 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2426 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2427 }
2428 }
2429
2430 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2431 auto before_env_use_node = env_uses_.before_begin();
2432 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2433 HEnvironment* user = env_use_node->GetUser();
2434 size_t input_index = env_use_node->GetIndex();
2435 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2436 before_env_use_node = env_use_node;
2437 }
2438 }
2439
2440 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2441 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2442 if (next != env_uses_.end()) {
2443 HEnvironment* next_user = next->GetUser();
2444 size_t next_index = next->GetIndex();
2445 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2446 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2447 }
2448 }
David Brazdil1abb4192015-02-17 18:33:36 +00002449
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002450 HInstruction* previous_;
2451 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002452 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002453 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002455 // An instruction gets an id when it is added to the graph.
2456 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002457 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002458 int id_;
2459
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002460 // When doing liveness analysis, instructions that have uses get an SSA index.
2461 int ssa_index_;
2462
Vladimir Markoa1de9182016-02-25 11:37:38 +00002463 // Packed fields.
2464 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002465
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002466 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002467 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002468
2469 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002470 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002471
Nicolas Geoffray39468442014-09-02 15:17:15 +01002472 // The environment associated with this instruction. Not null if the instruction
2473 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002474 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002475
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002476 // Set by the code generator.
2477 LocationSummary* locations_;
2478
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002479 // Set by the liveness analysis.
2480 LiveInterval* live_interval_;
2481
2482 // Set by the liveness analysis, this is the position in a linear
2483 // order of blocks where this instruction's live interval start.
2484 size_t lifetime_position_;
2485
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002486 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002487
Vladimir Markoa1de9182016-02-25 11:37:38 +00002488 // The reference handle part of the reference type info.
2489 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002490 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002491 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002492
David Brazdil1abb4192015-02-17 18:33:36 +00002493 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002494 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002495 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002496 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002497 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002499std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002500
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002501// Iterates over the instructions, while preserving the next instruction
2502// in case the current instruction gets removed from the list by the user
2503// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002504class HInstructionIterator : public ValueObject {
2505 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002506 explicit HInstructionIterator(const HInstructionList& instructions)
2507 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002508 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002509 }
2510
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002511 bool Done() const { return instruction_ == nullptr; }
2512 HInstruction* Current() const { return instruction_; }
2513 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002514 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002515 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002516 }
2517
2518 private:
2519 HInstruction* instruction_;
2520 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002521
2522 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002523};
2524
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002525// Iterates over the instructions without saving the next instruction,
2526// therefore handling changes in the graph potentially made by the user
2527// of this iterator.
2528class HInstructionIteratorHandleChanges : public ValueObject {
2529 public:
2530 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2531 : instruction_(instructions.first_instruction_) {
2532 }
2533
2534 bool Done() const { return instruction_ == nullptr; }
2535 HInstruction* Current() const { return instruction_; }
2536 void Advance() {
2537 instruction_ = instruction_->GetNext();
2538 }
2539
2540 private:
2541 HInstruction* instruction_;
2542
2543 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2544};
2545
2546
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002547class HBackwardInstructionIterator : public ValueObject {
2548 public:
2549 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2550 : instruction_(instructions.last_instruction_) {
2551 next_ = Done() ? nullptr : instruction_->GetPrevious();
2552 }
2553
2554 bool Done() const { return instruction_ == nullptr; }
2555 HInstruction* Current() const { return instruction_; }
2556 void Advance() {
2557 instruction_ = next_;
2558 next_ = Done() ? nullptr : instruction_->GetPrevious();
2559 }
2560
2561 private:
2562 HInstruction* instruction_;
2563 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002564
2565 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002566};
2567
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002568class HVariableInputSizeInstruction : public HInstruction {
2569 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002570 using HInstruction::GetInputRecords; // Keep the const version visible.
2571 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2572 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2573 }
2574
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002575 void AddInput(HInstruction* input);
2576 void InsertInputAt(size_t index, HInstruction* input);
2577 void RemoveInputAt(size_t index);
2578
Igor Murashkind01745e2017-04-05 16:40:31 -07002579 // Removes all the inputs.
2580 // Also removes this instructions from each input's use list
2581 // (for non-environment uses only).
2582 void RemoveAllInputs();
2583
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002584 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302585 HVariableInputSizeInstruction(InstructionKind inst_kind,
2586 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002587 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002588 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002589 size_t number_of_inputs,
2590 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302591 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002592 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002593
Artem Serovcced8ba2017-07-19 18:18:09 +01002594 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002595
Artem Serovcced8ba2017-07-19 18:18:09 +01002596 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002597};
2598
Vladimir Markof9f64412015-09-02 14:05:49 +01002599template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002600class HTemplateInstruction: public HInstruction {
2601 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302602 HTemplateInstruction<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2603 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002604 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002605
Vladimir Marko372f10e2016-05-17 16:30:10 +01002606 using HInstruction::GetInputRecords; // Keep the const version visible.
2607 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2608 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002609 }
2610
Artem Serovcced8ba2017-07-19 18:18:09 +01002611 protected:
2612 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2613
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002614 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002615 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002616
2617 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002618};
2619
Vladimir Markof9f64412015-09-02 14:05:49 +01002620// HTemplateInstruction specialization for N=0.
2621template<>
2622class HTemplateInstruction<0>: public HInstruction {
2623 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302624 explicit HTemplateInstruction<0>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2625 : HInstruction(kind, side_effects, dex_pc) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002626
Vladimir Markof9f64412015-09-02 14:05:49 +01002627 virtual ~HTemplateInstruction() {}
2628
Vladimir Marko372f10e2016-05-17 16:30:10 +01002629 using HInstruction::GetInputRecords; // Keep the const version visible.
2630 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2631 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002632 }
2633
Artem Serovcced8ba2017-07-19 18:18:09 +01002634 protected:
2635 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2636
Vladimir Markof9f64412015-09-02 14:05:49 +01002637 private:
2638 friend class SsaBuilder;
2639};
2640
Dave Allison20dfc792014-06-16 20:44:29 -07002641template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002642class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002643 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302644 using HInstruction::InstructionKind;
2645 HExpression<N>(InstructionKind kind,
2646 DataType::Type type,
2647 SideEffects side_effects,
2648 uint32_t dex_pc)
2649 : HTemplateInstruction<N>(kind, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002650 this->template SetPackedField<TypeField>(type);
2651 }
Dave Allison20dfc792014-06-16 20:44:29 -07002652 virtual ~HExpression() {}
2653
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002655 return TypeField::Decode(this->GetPackedFields());
2656 }
Dave Allison20dfc792014-06-16 20:44:29 -07002657
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002658 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002659 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2660 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002662 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2663 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2664 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002666 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002667};
2668
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002669// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2670// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002671class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002672 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002673 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302674 : HTemplateInstruction(kReturnVoid, SideEffects::None(), dex_pc) {
2675 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002676
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002677 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002678
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002679 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002680
Artem Serovcced8ba2017-07-19 18:18:09 +01002681 protected:
2682 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002683};
2684
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002685// Represents dex's RETURN opcodes. A HReturn is a control flow
2686// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002687class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002688 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002689 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302690 : HTemplateInstruction(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002691 SetRawInputAt(0, value);
2692 }
2693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002694 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002695
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002696 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002697
Artem Serovcced8ba2017-07-19 18:18:09 +01002698 protected:
2699 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002700};
2701
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002702class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002703 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002704 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002705 uint32_t reg_number,
2706 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002707 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002708 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002709 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302710 kPhi,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002711 SideEffects::None(),
2712 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002713 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002714 number_of_inputs,
2715 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002716 reg_number_(reg_number) {
2717 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002719 // Phis are constructed live and marked dead if conflicting or unused.
2720 // Individual steps of SsaBuilder should assume that if a phi has been
2721 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2722 SetPackedFlag<kFlagIsLive>(true);
2723 SetPackedFlag<kFlagCanBeNull>(true);
2724 }
2725
Artem Serovcced8ba2017-07-19 18:18:09 +01002726 bool IsClonable() const OVERRIDE { return true; }
2727
David Brazdildee58d62016-04-07 09:54:26 +00002728 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002729 static DataType::Type ToPhiType(DataType::Type type) {
2730 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002731 }
2732
2733 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002735 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2736 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002737 // Make sure that only valid type changes occur. The following are allowed:
2738 // (1) int -> float/ref (primitive type propagation),
2739 // (2) long -> double (primitive type propagation).
2740 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2742 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2743 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002744 SetPackedField<TypeField>(new_type);
2745 }
2746
2747 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2748 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2749
2750 uint32_t GetRegNumber() const { return reg_number_; }
2751
2752 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2753 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2754 bool IsDead() const { return !IsLive(); }
2755 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2756
Vladimir Markoe9004912016-06-16 16:50:52 +01002757 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002758 return other != nullptr
2759 && other->IsPhi()
2760 && other->AsPhi()->GetBlock() == GetBlock()
2761 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2762 }
2763
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002764 bool HasEquivalentPhi() const {
2765 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2766 return true;
2767 }
2768 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2769 return true;
2770 }
2771 return false;
2772 }
2773
David Brazdildee58d62016-04-07 09:54:26 +00002774 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2775 // An equivalent phi is a phi having the same dex register and type.
2776 // It assumes that phis with the same dex register are adjacent.
2777 HPhi* GetNextEquivalentPhiWithSameType() {
2778 HInstruction* next = GetNext();
2779 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2780 if (next->GetType() == GetType()) {
2781 return next->AsPhi();
2782 }
2783 next = next->GetNext();
2784 }
2785 return nullptr;
2786 }
2787
2788 DECLARE_INSTRUCTION(Phi);
2789
Artem Serovcced8ba2017-07-19 18:18:09 +01002790 protected:
2791 DEFAULT_COPY_CONSTRUCTOR(Phi);
2792
David Brazdildee58d62016-04-07 09:54:26 +00002793 private:
2794 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2795 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002796 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002797 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2798 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2799 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2800 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002802
David Brazdildee58d62016-04-07 09:54:26 +00002803 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002804};
2805
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002806// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002807// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002808// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002809class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002810 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302811 explicit HExit(uint32_t dex_pc = kNoDexPc)
2812 : HTemplateInstruction(kExit, SideEffects::None(), dex_pc) {
2813 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002814
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002815 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002816
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002817 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002818
Artem Serovcced8ba2017-07-19 18:18:09 +01002819 protected:
2820 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002821};
2822
2823// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002824class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002825 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302826 explicit HGoto(uint32_t dex_pc = kNoDexPc)
2827 : HTemplateInstruction(kGoto, SideEffects::None(), dex_pc) {
2828 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002829
Artem Serovcced8ba2017-07-19 18:18:09 +01002830 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002831 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002832
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002833 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002834 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002835 }
2836
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002837 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002838
Artem Serovcced8ba2017-07-19 18:18:09 +01002839 protected:
2840 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002841};
2842
Roland Levillain9867bc72015-08-05 10:21:34 +01002843class HConstant : public HExpression<0> {
2844 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302845 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2846 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2847 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002848
2849 bool CanBeMoved() const OVERRIDE { return true; }
2850
Roland Levillain1a653882016-03-18 18:05:57 +00002851 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002852 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002853 // Is this constant 0 in the arithmetic sense?
2854 virtual bool IsArithmeticZero() const { return false; }
2855 // Is this constant a 0-bit pattern?
2856 virtual bool IsZeroBitPattern() const { return false; }
2857 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002858 virtual bool IsOne() const { return false; }
2859
David Brazdil77a48ae2015-09-15 12:34:04 +00002860 virtual uint64_t GetValueAsUint64() const = 0;
2861
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002862 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002863
Artem Serovcced8ba2017-07-19 18:18:09 +01002864 protected:
2865 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002866};
2867
Vladimir Markofcb503c2016-05-18 12:48:17 +01002868class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002869 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002870 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002871 return true;
2872 }
2873
David Brazdil77a48ae2015-09-15 12:34:04 +00002874 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2875
Roland Levillain9867bc72015-08-05 10:21:34 +01002876 size_t ComputeHashCode() const OVERRIDE { return 0; }
2877
Roland Levillain1a653882016-03-18 18:05:57 +00002878 // The null constant representation is a 0-bit pattern.
2879 virtual bool IsZeroBitPattern() const { return true; }
2880
Roland Levillain9867bc72015-08-05 10:21:34 +01002881 DECLARE_INSTRUCTION(NullConstant);
2882
Artem Serovcced8ba2017-07-19 18:18:09 +01002883 protected:
2884 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2885
Roland Levillain9867bc72015-08-05 10:21:34 +01002886 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302888 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2889 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002890
2891 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002892};
2893
2894// Constants of the type int. Those can be from Dex instructions, or
2895// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002896class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002897 public:
2898 int32_t GetValue() const { return value_; }
2899
David Brazdil9f389d42015-10-01 14:32:56 +01002900 uint64_t GetValueAsUint64() const OVERRIDE {
2901 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2902 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002903
Vladimir Marko372f10e2016-05-17 16:30:10 +01002904 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002905 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002906 return other->AsIntConstant()->value_ == value_;
2907 }
2908
2909 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2910
2911 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002912 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2913 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2915
Roland Levillain1a653882016-03-18 18:05:57 +00002916 // Integer constants are used to encode Boolean values as well,
2917 // where 1 means true and 0 means false.
2918 bool IsTrue() const { return GetValue() == 1; }
2919 bool IsFalse() const { return GetValue() == 0; }
2920
Roland Levillain9867bc72015-08-05 10:21:34 +01002921 DECLARE_INSTRUCTION(IntConstant);
2922
Artem Serovcced8ba2017-07-19 18:18:09 +01002923 protected:
2924 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2925
Roland Levillain9867bc72015-08-05 10:21:34 +01002926 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002927 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302928 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2929 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002930 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302931 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2932 value_(value ? 1 : 0) {
2933 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002934
2935 const int32_t value_;
2936
2937 friend class HGraph;
2938 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2939 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002940};
2941
Vladimir Markofcb503c2016-05-18 12:48:17 +01002942class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002943 public:
2944 int64_t GetValue() const { return value_; }
2945
David Brazdil77a48ae2015-09-15 12:34:04 +00002946 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2947
Vladimir Marko372f10e2016-05-17 16:30:10 +01002948 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002949 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002950 return other->AsLongConstant()->value_ == value_;
2951 }
2952
2953 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2954
2955 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002956 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2957 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002958 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2959
2960 DECLARE_INSTRUCTION(LongConstant);
2961
Artem Serovcced8ba2017-07-19 18:18:09 +01002962 protected:
2963 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2964
Roland Levillain9867bc72015-08-05 10:21:34 +01002965 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002966 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302967 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2968 value_(value) {
2969 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002970
2971 const int64_t value_;
2972
2973 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002974};
Dave Allison20dfc792014-06-16 20:44:29 -07002975
Vladimir Markofcb503c2016-05-18 12:48:17 +01002976class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002977 public:
2978 float GetValue() const { return value_; }
2979
2980 uint64_t GetValueAsUint64() const OVERRIDE {
2981 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2982 }
2983
Vladimir Marko372f10e2016-05-17 16:30:10 +01002984 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002985 DCHECK(other->IsFloatConstant()) << other->DebugName();
2986 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2987 }
2988
2989 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2990
2991 bool IsMinusOne() const OVERRIDE {
2992 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2993 }
Roland Levillain1a653882016-03-18 18:05:57 +00002994 bool IsArithmeticZero() const OVERRIDE {
2995 return std::fpclassify(value_) == FP_ZERO;
2996 }
2997 bool IsArithmeticPositiveZero() const {
2998 return IsArithmeticZero() && !std::signbit(value_);
2999 }
3000 bool IsArithmeticNegativeZero() const {
3001 return IsArithmeticZero() && std::signbit(value_);
3002 }
3003 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003004 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003005 }
3006 bool IsOne() const OVERRIDE {
3007 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3008 }
3009 bool IsNaN() const {
3010 return std::isnan(value_);
3011 }
3012
3013 DECLARE_INSTRUCTION(FloatConstant);
3014
Artem Serovcced8ba2017-07-19 18:18:09 +01003015 protected:
3016 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3017
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018 private:
3019 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303020 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3021 value_(value) {
3022 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303024 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3025 value_(bit_cast<float, int32_t>(value)) {
3026 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027
3028 const float value_;
3029
3030 // Only the SsaBuilder and HGraph can create floating-point constants.
3031 friend class SsaBuilder;
3032 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003033};
3034
Vladimir Markofcb503c2016-05-18 12:48:17 +01003035class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 public:
3037 double GetValue() const { return value_; }
3038
3039 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3040
Vladimir Marko372f10e2016-05-17 16:30:10 +01003041 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003042 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3043 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3044 }
3045
3046 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3047
3048 bool IsMinusOne() const OVERRIDE {
3049 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3050 }
Roland Levillain1a653882016-03-18 18:05:57 +00003051 bool IsArithmeticZero() const OVERRIDE {
3052 return std::fpclassify(value_) == FP_ZERO;
3053 }
3054 bool IsArithmeticPositiveZero() const {
3055 return IsArithmeticZero() && !std::signbit(value_);
3056 }
3057 bool IsArithmeticNegativeZero() const {
3058 return IsArithmeticZero() && std::signbit(value_);
3059 }
3060 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003061 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062 }
3063 bool IsOne() const OVERRIDE {
3064 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3065 }
3066 bool IsNaN() const {
3067 return std::isnan(value_);
3068 }
3069
3070 DECLARE_INSTRUCTION(DoubleConstant);
3071
Artem Serovcced8ba2017-07-19 18:18:09 +01003072 protected:
3073 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3074
Roland Levillain31dd3d62016-02-16 12:21:02 +00003075 private:
3076 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303077 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3078 value_(value) {
3079 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003080 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303081 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3082 value_(bit_cast<double, int64_t>(value)) {
3083 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003084
3085 const double value_;
3086
3087 // Only the SsaBuilder and HGraph can create floating-point constants.
3088 friend class SsaBuilder;
3089 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003090};
3091
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003092// Conditional branch. A block ending with an HIf instruction must have
3093// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003094class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003095 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003096 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303097 : HTemplateInstruction(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003098 SetRawInputAt(0, input);
3099 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003100
Artem Serovcced8ba2017-07-19 18:18:09 +01003101 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003102 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003103
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003104 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003105 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003106 }
3107
3108 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003109 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003110 }
3111
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003112 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003113
Artem Serovcced8ba2017-07-19 18:18:09 +01003114 protected:
3115 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003116};
3117
David Brazdilfc6a86a2015-06-26 10:33:45 +00003118
3119// Abstract instruction which marks the beginning and/or end of a try block and
3120// links it to the respective exception handlers. Behaves the same as a Goto in
3121// non-exceptional control flow.
3122// Normal-flow successor is stored at index zero, exception handlers under
3123// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003124class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003125 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003126 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003127 kEntry,
3128 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003129 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003130 };
3131
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003132 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303133 : HTemplateInstruction(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003134 SetPackedField<BoundaryKindField>(kind);
3135 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003136
3137 bool IsControlFlow() const OVERRIDE { return true; }
3138
3139 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003140 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003141
David Brazdild26a4112015-11-10 11:07:31 +00003142 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3143 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3144 }
3145
David Brazdilfc6a86a2015-06-26 10:33:45 +00003146 // Returns whether `handler` is among its exception handlers (non-zero index
3147 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003148 bool HasExceptionHandler(const HBasicBlock& handler) const {
3149 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003150 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003151 }
3152
3153 // If not present already, adds `handler` to its block's list of exception
3154 // handlers.
3155 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003156 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003157 GetBlock()->AddSuccessor(handler);
3158 }
3159 }
3160
Vladimir Markoa1de9182016-02-25 11:37:38 +00003161 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3162 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003163
David Brazdilffee3d32015-07-06 11:48:53 +01003164 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3165
David Brazdilfc6a86a2015-06-26 10:33:45 +00003166 DECLARE_INSTRUCTION(TryBoundary);
3167
Artem Serovcced8ba2017-07-19 18:18:09 +01003168 protected:
3169 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3170
David Brazdilfc6a86a2015-06-26 10:33:45 +00003171 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003172 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3173 static constexpr size_t kFieldBoundaryKindSize =
3174 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3175 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3176 kFieldBoundaryKind + kFieldBoundaryKindSize;
3177 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3178 "Too many packed fields.");
3179 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003180};
3181
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003182// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003183class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003184 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003185 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3186 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003187 HDeoptimize(ArenaAllocator* allocator,
3188 HInstruction* cond,
3189 DeoptimizationKind kind,
3190 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003191 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303192 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003193 SideEffects::All(),
3194 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003195 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003196 /* number_of_inputs */ 1,
3197 kArenaAllocMisc) {
3198 SetPackedFlag<kFieldCanBeMoved>(false);
3199 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003200 SetRawInputAt(0, cond);
3201 }
3202
Artem Serovcced8ba2017-07-19 18:18:09 +01003203 bool IsClonable() const OVERRIDE { return true; }
3204
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003205 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3206 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3207 // instead of `guard`.
3208 // We set CanTriggerGC to prevent any intermediate address to be live
3209 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003210 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003211 HInstruction* cond,
3212 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003213 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003214 uint32_t dex_pc)
3215 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303216 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003217 SideEffects::CanTriggerGC(),
3218 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003219 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003220 /* number_of_inputs */ 2,
3221 kArenaAllocMisc) {
3222 SetPackedFlag<kFieldCanBeMoved>(true);
3223 SetPackedField<DeoptimizeKindField>(kind);
3224 SetRawInputAt(0, cond);
3225 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003226 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003227
3228 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3229
3230 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3231 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3232 }
3233
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003234 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003235
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003236 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003237
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003238 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 DataType::Type GetType() const OVERRIDE {
3241 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003242 }
3243
3244 bool GuardsAnInput() const {
3245 return InputCount() == 2;
3246 }
3247
3248 HInstruction* GuardedInput() const {
3249 DCHECK(GuardsAnInput());
3250 return InputAt(1);
3251 }
3252
3253 void RemoveGuard() {
3254 RemoveInputAt(1);
3255 }
3256
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003257 DECLARE_INSTRUCTION(Deoptimize);
3258
Artem Serovcced8ba2017-07-19 18:18:09 +01003259 protected:
3260 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3261
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003262 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003263 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3264 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3265 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003266 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003267 static constexpr size_t kNumberOfDeoptimizePackedBits =
3268 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3269 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3270 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003271 using DeoptimizeKindField =
3272 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003273};
3274
Mingyao Yang063fc772016-08-02 11:02:54 -07003275// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3276// The compiled code checks this flag value in a guard before devirtualized call and
3277// if it's true, starts to do deoptimization.
3278// It has a 4-byte slot on stack.
3279// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003280class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003281 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003282 // CHA guards are only optimized in a separate pass and it has no side effects
3283 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003284 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303285 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
3286 SideEffects::None(),
3287 dex_pc,
3288 allocator,
3289 0,
3290 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003291 }
3292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003293 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003294
3295 // We do all CHA guard elimination/motion in a single pass, after which there is no
3296 // further guard elimination/motion since a guard might have been used for justification
3297 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3298 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003299 bool CanBeMoved() const OVERRIDE { return false; }
3300
3301 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3302
Artem Serovcced8ba2017-07-19 18:18:09 +01003303 protected:
3304 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003305};
3306
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003307// Represents the ArtMethod that was passed as a first argument to
3308// the method. It is used by instructions that depend on it, like
3309// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003310class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003311 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003312 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303313 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3314 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003315
3316 DECLARE_INSTRUCTION(CurrentMethod);
3317
Artem Serovcced8ba2017-07-19 18:18:09 +01003318 protected:
3319 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003320};
3321
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003322// Fetches an ArtMethod from the virtual table or the interface method table
3323// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003324class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003325 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003326 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003327 kVTable,
3328 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003329 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003330 };
3331 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003332 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003333 TableKind kind,
3334 size_t index,
3335 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303336 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003337 index_(index) {
3338 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003339 SetRawInputAt(0, cls);
3340 }
3341
Artem Serovcced8ba2017-07-19 18:18:09 +01003342 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003343 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003344 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003345 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003346 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003347 }
3348
Vladimir Markoa1de9182016-02-25 11:37:38 +00003349 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003350 size_t GetIndex() const { return index_; }
3351
3352 DECLARE_INSTRUCTION(ClassTableGet);
3353
Artem Serovcced8ba2017-07-19 18:18:09 +01003354 protected:
3355 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3356
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003357 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003358 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3359 static constexpr size_t kFieldTableKindSize =
3360 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3361 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3362 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3363 "Too many packed fields.");
3364 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3365
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003366 // The index of the ArtMethod in the table.
3367 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003368};
3369
Mark Mendellfe57faa2015-09-18 09:26:15 -04003370// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3371// have one successor for each entry in the switch table, and the final successor
3372// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003373class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003374 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003375 HPackedSwitch(int32_t start_value,
3376 uint32_t num_entries,
3377 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003378 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303379 : HTemplateInstruction(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003380 start_value_(start_value),
3381 num_entries_(num_entries) {
3382 SetRawInputAt(0, input);
3383 }
3384
Artem Serovcced8ba2017-07-19 18:18:09 +01003385 bool IsClonable() const OVERRIDE { return true; }
3386
Mark Mendellfe57faa2015-09-18 09:26:15 -04003387 bool IsControlFlow() const OVERRIDE { return true; }
3388
3389 int32_t GetStartValue() const { return start_value_; }
3390
Vladimir Marko211c2112015-09-24 16:52:33 +01003391 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003392
3393 HBasicBlock* GetDefaultBlock() const {
3394 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003395 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003396 }
3397 DECLARE_INSTRUCTION(PackedSwitch);
3398
Artem Serovcced8ba2017-07-19 18:18:09 +01003399 protected:
3400 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3401
Mark Mendellfe57faa2015-09-18 09:26:15 -04003402 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003403 const int32_t start_value_;
3404 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003405};
3406
Roland Levillain88cb1752014-10-20 16:36:47 +01003407class HUnaryOperation : public HExpression<1> {
3408 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303409 HUnaryOperation(InstructionKind kind,
3410 DataType::Type result_type,
3411 HInstruction* input,
3412 uint32_t dex_pc = kNoDexPc)
3413 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003414 SetRawInputAt(0, input);
3415 }
3416
Artem Serovcced8ba2017-07-19 18:18:09 +01003417 // All of the UnaryOperation instructions are clonable.
3418 bool IsClonable() const OVERRIDE { return true; }
3419
Roland Levillain88cb1752014-10-20 16:36:47 +01003420 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003421 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003422
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003423 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003424 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003425 return true;
3426 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003427
Roland Levillain31dd3d62016-02-16 12:21:02 +00003428 // Try to statically evaluate `this` and return a HConstant
3429 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003430 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003431 HConstant* TryStaticEvaluation() const;
3432
3433 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003434 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3435 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003436 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3437 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003438
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003439 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003440
Artem Serovcced8ba2017-07-19 18:18:09 +01003441 protected:
3442 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003443};
3444
Dave Allison20dfc792014-06-16 20:44:29 -07003445class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003446 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303447 HBinaryOperation(InstructionKind kind,
3448 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003449 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003450 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003451 SideEffects side_effects = SideEffects::None(),
3452 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303453 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003454 SetRawInputAt(0, left);
3455 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003456 }
3457
Artem Serovcced8ba2017-07-19 18:18:09 +01003458 // All of the BinaryOperation instructions are clonable.
3459 bool IsClonable() const OVERRIDE { return true; }
3460
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003461 HInstruction* GetLeft() const { return InputAt(0); }
3462 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003463 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003464
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003465 virtual bool IsCommutative() const { return false; }
3466
3467 // Put constant on the right.
3468 // Returns whether order is changed.
3469 bool OrderInputsWithConstantOnTheRight() {
3470 HInstruction* left = InputAt(0);
3471 HInstruction* right = InputAt(1);
3472 if (left->IsConstant() && !right->IsConstant()) {
3473 ReplaceInput(right, 0);
3474 ReplaceInput(left, 1);
3475 return true;
3476 }
3477 return false;
3478 }
3479
3480 // Order inputs by instruction id, but favor constant on the right side.
3481 // This helps GVN for commutative ops.
3482 void OrderInputs() {
3483 DCHECK(IsCommutative());
3484 HInstruction* left = InputAt(0);
3485 HInstruction* right = InputAt(1);
3486 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3487 return;
3488 }
3489 if (OrderInputsWithConstantOnTheRight()) {
3490 return;
3491 }
3492 // Order according to instruction id.
3493 if (left->GetId() > right->GetId()) {
3494 ReplaceInput(right, 0);
3495 ReplaceInput(left, 1);
3496 }
3497 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003498
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003499 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003500 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003501 return true;
3502 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003503
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 // Try to statically evaluate `this` and return a HConstant
3505 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003506 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003507 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003508
3509 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3511 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003512 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3513 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003514 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003515 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3516 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003517 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3518 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003519 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3520 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003521 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003522 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3523 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003524
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003525 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003526 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003527 HConstant* GetConstantRight() const;
3528
3529 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003530 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003531 HInstruction* GetLeastConstantLeft() const;
3532
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003533 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003534
Artem Serovcced8ba2017-07-19 18:18:09 +01003535 protected:
3536 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003537};
3538
Mark Mendellc4701932015-04-10 13:18:51 -04003539// The comparison bias applies for floating point operations and indicates how NaN
3540// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003541enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003542 kNoBias, // bias is not applicable (i.e. for long operation)
3543 kGtBias, // return 1 for NaN comparisons
3544 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003545 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003546};
3547
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3549
Dave Allison20dfc792014-06-16 20:44:29 -07003550class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003551 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303552 HCondition(InstructionKind kind,
3553 HInstruction* first,
3554 HInstruction* second,
3555 uint32_t dex_pc = kNoDexPc)
3556 : HBinaryOperation(kind,
3557 DataType::Type::kBool,
3558 first,
3559 second,
3560 SideEffects::None(),
3561 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003562 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3563 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003564
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003565 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003566 // `instruction`, and disregard moves in between.
3567 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003568
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003569 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003570
3571 virtual IfCondition GetCondition() const = 0;
3572
Mark Mendellc4701932015-04-10 13:18:51 -04003573 virtual IfCondition GetOppositeCondition() const = 0;
3574
Vladimir Markoa1de9182016-02-25 11:37:38 +00003575 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003576 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3577
Vladimir Markoa1de9182016-02-25 11:37:38 +00003578 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3579 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003580
Vladimir Marko372f10e2016-05-17 16:30:10 +01003581 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003582 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003583 }
3584
Roland Levillain4fa13f62015-07-06 18:11:54 +01003585 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003586 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003587 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003588 if (if_cond == kCondNE) {
3589 return true;
3590 } else if (if_cond == kCondEQ) {
3591 return false;
3592 }
3593 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003594 }
3595
3596 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003597 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003598 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003599 if (if_cond == kCondEQ) {
3600 return true;
3601 } else if (if_cond == kCondNE) {
3602 return false;
3603 }
3604 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003605 }
3606
Roland Levillain31dd3d62016-02-16 12:21:02 +00003607 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003608 // Needed if we merge a HCompare into a HCondition.
3609 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3610 static constexpr size_t kFieldComparisonBiasSize =
3611 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3612 static constexpr size_t kNumberOfConditionPackedBits =
3613 kFieldComparisonBias + kFieldComparisonBiasSize;
3614 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3615 using ComparisonBiasField =
3616 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3617
Roland Levillain31dd3d62016-02-16 12:21:02 +00003618 template <typename T>
3619 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3620
3621 template <typename T>
3622 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003623 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3625 // Handle the bias.
3626 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3627 }
3628
3629 // Return an integer constant containing the result of a condition evaluated at compile time.
3630 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3631 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3632 }
3633
Artem Serovcced8ba2017-07-19 18:18:09 +01003634 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003635};
3636
3637// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003638class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003639 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003640 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303641 : HCondition(kEqual, first, second, dex_pc) {
3642 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003643
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003644 bool IsCommutative() const OVERRIDE { return true; }
3645
Vladimir Marko9e23df52015-11-10 17:14:35 +00003646 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3647 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003648 return MakeConstantCondition(true, GetDexPc());
3649 }
3650 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3651 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3652 }
3653 // In the following Evaluate methods, a HCompare instruction has
3654 // been merged into this HEqual instruction; evaluate it as
3655 // `Compare(x, y) == 0`.
3656 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3657 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3658 GetDexPc());
3659 }
3660 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3661 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3662 }
3663 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3664 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003665 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003666
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003667 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003668
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003669 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003670 return kCondEQ;
3671 }
3672
Mark Mendellc4701932015-04-10 13:18:51 -04003673 IfCondition GetOppositeCondition() const OVERRIDE {
3674 return kCondNE;
3675 }
3676
Artem Serovcced8ba2017-07-19 18:18:09 +01003677 protected:
3678 DEFAULT_COPY_CONSTRUCTOR(Equal);
3679
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003680 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003681 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003682};
3683
Vladimir Markofcb503c2016-05-18 12:48:17 +01003684class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003685 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303686 HNotEqual(HInstruction* first, HInstruction* second,
3687 uint32_t dex_pc = kNoDexPc)
3688 : HCondition(kNotEqual, first, second, dex_pc) {
3689 }
Dave Allison20dfc792014-06-16 20:44:29 -07003690
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003691 bool IsCommutative() const OVERRIDE { return true; }
3692
Vladimir Marko9e23df52015-11-10 17:14:35 +00003693 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3694 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003695 return MakeConstantCondition(false, GetDexPc());
3696 }
3697 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3698 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3699 }
3700 // In the following Evaluate methods, a HCompare instruction has
3701 // been merged into this HNotEqual instruction; evaluate it as
3702 // `Compare(x, y) != 0`.
3703 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3704 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3705 }
3706 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3707 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3708 }
3709 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3710 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003711 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003712
Dave Allison20dfc792014-06-16 20:44:29 -07003713 DECLARE_INSTRUCTION(NotEqual);
3714
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003715 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003716 return kCondNE;
3717 }
3718
Mark Mendellc4701932015-04-10 13:18:51 -04003719 IfCondition GetOppositeCondition() const OVERRIDE {
3720 return kCondEQ;
3721 }
3722
Artem Serovcced8ba2017-07-19 18:18:09 +01003723 protected:
3724 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3725
Dave Allison20dfc792014-06-16 20:44:29 -07003726 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003727 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003728};
3729
Vladimir Markofcb503c2016-05-18 12:48:17 +01003730class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003731 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303732 HLessThan(HInstruction* first, HInstruction* second,
3733 uint32_t dex_pc = kNoDexPc)
3734 : HCondition(kLessThan, first, second, dex_pc) {
3735 }
Dave Allison20dfc792014-06-16 20:44:29 -07003736
Roland Levillain9867bc72015-08-05 10:21:34 +01003737 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003738 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003739 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 // In the following Evaluate methods, a HCompare instruction has
3741 // been merged into this HLessThan instruction; evaluate it as
3742 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003743 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003744 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3745 }
3746 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3747 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3748 }
3749 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3750 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003751 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003752
Dave Allison20dfc792014-06-16 20:44:29 -07003753 DECLARE_INSTRUCTION(LessThan);
3754
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003755 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003756 return kCondLT;
3757 }
3758
Mark Mendellc4701932015-04-10 13:18:51 -04003759 IfCondition GetOppositeCondition() const OVERRIDE {
3760 return kCondGE;
3761 }
3762
Artem Serovcced8ba2017-07-19 18:18:09 +01003763 protected:
3764 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3765
Dave Allison20dfc792014-06-16 20:44:29 -07003766 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003767 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003768};
3769
Vladimir Markofcb503c2016-05-18 12:48:17 +01003770class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003771 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303772 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3773 uint32_t dex_pc = kNoDexPc)
3774 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3775 }
Dave Allison20dfc792014-06-16 20:44:29 -07003776
Roland Levillain9867bc72015-08-05 10:21:34 +01003777 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003778 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003779 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003780 // In the following Evaluate methods, a HCompare instruction has
3781 // been merged into this HLessThanOrEqual instruction; evaluate it as
3782 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003783 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003784 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3785 }
3786 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3787 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3788 }
3789 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3790 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003791 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003792
Dave Allison20dfc792014-06-16 20:44:29 -07003793 DECLARE_INSTRUCTION(LessThanOrEqual);
3794
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003795 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003796 return kCondLE;
3797 }
3798
Mark Mendellc4701932015-04-10 13:18:51 -04003799 IfCondition GetOppositeCondition() const OVERRIDE {
3800 return kCondGT;
3801 }
3802
Artem Serovcced8ba2017-07-19 18:18:09 +01003803 protected:
3804 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3805
Dave Allison20dfc792014-06-16 20:44:29 -07003806 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003807 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003808};
3809
Vladimir Markofcb503c2016-05-18 12:48:17 +01003810class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003812 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303813 : HCondition(kGreaterThan, first, second, dex_pc) {
3814 }
Dave Allison20dfc792014-06-16 20:44:29 -07003815
Roland Levillain9867bc72015-08-05 10:21:34 +01003816 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003817 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003818 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003819 // In the following Evaluate methods, a HCompare instruction has
3820 // been merged into this HGreaterThan instruction; evaluate it as
3821 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003822 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003823 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3824 }
3825 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3826 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3827 }
3828 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3829 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003830 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003831
Dave Allison20dfc792014-06-16 20:44:29 -07003832 DECLARE_INSTRUCTION(GreaterThan);
3833
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003834 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003835 return kCondGT;
3836 }
3837
Mark Mendellc4701932015-04-10 13:18:51 -04003838 IfCondition GetOppositeCondition() const OVERRIDE {
3839 return kCondLE;
3840 }
3841
Artem Serovcced8ba2017-07-19 18:18:09 +01003842 protected:
3843 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3844
Dave Allison20dfc792014-06-16 20:44:29 -07003845 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003846 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003847};
3848
Vladimir Markofcb503c2016-05-18 12:48:17 +01003849class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003850 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003851 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303852 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3853 }
Dave Allison20dfc792014-06-16 20:44:29 -07003854
Roland Levillain9867bc72015-08-05 10:21:34 +01003855 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003856 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003857 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003858 // In the following Evaluate methods, a HCompare instruction has
3859 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3860 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003861 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003862 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3863 }
3864 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3865 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3866 }
3867 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3868 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003869 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003870
Dave Allison20dfc792014-06-16 20:44:29 -07003871 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003873 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003874 return kCondGE;
3875 }
3876
Mark Mendellc4701932015-04-10 13:18:51 -04003877 IfCondition GetOppositeCondition() const OVERRIDE {
3878 return kCondLT;
3879 }
3880
Artem Serovcced8ba2017-07-19 18:18:09 +01003881 protected:
3882 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3883
Dave Allison20dfc792014-06-16 20:44:29 -07003884 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003885 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003886};
3887
Vladimir Markofcb503c2016-05-18 12:48:17 +01003888class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003889 public:
3890 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303891 : HCondition(kBelow, first, second, dex_pc) {
3892 }
Aart Bike9f37602015-10-09 11:15:55 -07003893
3894 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003895 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003896 }
3897 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003898 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3899 }
3900 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3901 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3902 LOG(FATAL) << DebugName() << " is not defined for float values";
3903 UNREACHABLE();
3904 }
3905 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3906 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3907 LOG(FATAL) << DebugName() << " is not defined for double values";
3908 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003909 }
3910
3911 DECLARE_INSTRUCTION(Below);
3912
3913 IfCondition GetCondition() const OVERRIDE {
3914 return kCondB;
3915 }
3916
3917 IfCondition GetOppositeCondition() const OVERRIDE {
3918 return kCondAE;
3919 }
3920
Artem Serovcced8ba2017-07-19 18:18:09 +01003921 protected:
3922 DEFAULT_COPY_CONSTRUCTOR(Below);
3923
Aart Bike9f37602015-10-09 11:15:55 -07003924 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003925 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003926 return MakeUnsigned(x) < MakeUnsigned(y);
3927 }
Aart Bike9f37602015-10-09 11:15:55 -07003928};
3929
Vladimir Markofcb503c2016-05-18 12:48:17 +01003930class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003931 public:
3932 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303933 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3934 }
Aart Bike9f37602015-10-09 11:15:55 -07003935
3936 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003937 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003938 }
3939 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003940 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3941 }
3942 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3943 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3944 LOG(FATAL) << DebugName() << " is not defined for float values";
3945 UNREACHABLE();
3946 }
3947 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3948 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3949 LOG(FATAL) << DebugName() << " is not defined for double values";
3950 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003951 }
3952
3953 DECLARE_INSTRUCTION(BelowOrEqual);
3954
3955 IfCondition GetCondition() const OVERRIDE {
3956 return kCondBE;
3957 }
3958
3959 IfCondition GetOppositeCondition() const OVERRIDE {
3960 return kCondA;
3961 }
3962
Artem Serovcced8ba2017-07-19 18:18:09 +01003963 protected:
3964 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3965
Aart Bike9f37602015-10-09 11:15:55 -07003966 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003967 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003968 return MakeUnsigned(x) <= MakeUnsigned(y);
3969 }
Aart Bike9f37602015-10-09 11:15:55 -07003970};
3971
Vladimir Markofcb503c2016-05-18 12:48:17 +01003972class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003973 public:
3974 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303975 : HCondition(kAbove, first, second, dex_pc) {
3976 }
Aart Bike9f37602015-10-09 11:15:55 -07003977
3978 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003979 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003980 }
3981 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003982 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3983 }
3984 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3985 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3986 LOG(FATAL) << DebugName() << " is not defined for float values";
3987 UNREACHABLE();
3988 }
3989 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3990 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3991 LOG(FATAL) << DebugName() << " is not defined for double values";
3992 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003993 }
3994
3995 DECLARE_INSTRUCTION(Above);
3996
3997 IfCondition GetCondition() const OVERRIDE {
3998 return kCondA;
3999 }
4000
4001 IfCondition GetOppositeCondition() const OVERRIDE {
4002 return kCondBE;
4003 }
4004
Artem Serovcced8ba2017-07-19 18:18:09 +01004005 protected:
4006 DEFAULT_COPY_CONSTRUCTOR(Above);
4007
Aart Bike9f37602015-10-09 11:15:55 -07004008 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004009 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004010 return MakeUnsigned(x) > MakeUnsigned(y);
4011 }
Aart Bike9f37602015-10-09 11:15:55 -07004012};
4013
Vladimir Markofcb503c2016-05-18 12:48:17 +01004014class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004015 public:
4016 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304017 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4018 }
Aart Bike9f37602015-10-09 11:15:55 -07004019
4020 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004021 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004022 }
4023 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004024 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4025 }
4026 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4027 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4028 LOG(FATAL) << DebugName() << " is not defined for float values";
4029 UNREACHABLE();
4030 }
4031 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4032 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4033 LOG(FATAL) << DebugName() << " is not defined for double values";
4034 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004035 }
4036
4037 DECLARE_INSTRUCTION(AboveOrEqual);
4038
4039 IfCondition GetCondition() const OVERRIDE {
4040 return kCondAE;
4041 }
4042
4043 IfCondition GetOppositeCondition() const OVERRIDE {
4044 return kCondB;
4045 }
4046
Artem Serovcced8ba2017-07-19 18:18:09 +01004047 protected:
4048 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4049
Aart Bike9f37602015-10-09 11:15:55 -07004050 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004051 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004052 return MakeUnsigned(x) >= MakeUnsigned(y);
4053 }
Aart Bike9f37602015-10-09 11:15:55 -07004054};
Dave Allison20dfc792014-06-16 20:44:29 -07004055
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004056// Instruction to check how two inputs compare to each other.
4057// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004058class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004059 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004060 // Note that `comparison_type` is the type of comparison performed
4061 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004062 // HCompare instruction (which is always DataType::Type::kInt).
4063 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004064 HInstruction* first,
4065 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004066 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004067 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304068 : HBinaryOperation(kCompare,
4069 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 first,
4071 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004072 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004073 dex_pc) {
4074 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4076 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004077 }
4078
Roland Levillain9867bc72015-08-05 10:21:34 +01004079 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004080 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4081
4082 template <typename T>
4083 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004084 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004085 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4086 // Handle the bias.
4087 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4088 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004089
Roland Levillain9867bc72015-08-05 10:21:34 +01004090 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004091 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4092 // reach this code path when processing a freshly built HIR
4093 // graph. However HCompare integer instructions can be synthesized
4094 // by the instruction simplifier to implement IntegerCompare and
4095 // IntegerSignum intrinsics, so we have to handle this case.
4096 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004097 }
4098 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004099 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4100 }
4101 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4102 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4103 }
4104 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4105 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004106 }
4107
Vladimir Marko372f10e2016-05-17 16:30:10 +01004108 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004109 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004110 }
4111
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004113
Roland Levillain31dd3d62016-02-16 12:21:02 +00004114 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004115 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004116 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004117 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004118 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004119 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004122 // Comparisons do not require a runtime call in any back end.
4123 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004124 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004125
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004126 DECLARE_INSTRUCTION(Compare);
4127
Roland Levillain31dd3d62016-02-16 12:21:02 +00004128 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004129 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4130 static constexpr size_t kFieldComparisonBiasSize =
4131 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4132 static constexpr size_t kNumberOfComparePackedBits =
4133 kFieldComparisonBias + kFieldComparisonBiasSize;
4134 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4135 using ComparisonBiasField =
4136 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4137
Roland Levillain31dd3d62016-02-16 12:21:02 +00004138 // Return an integer constant containing the result of a comparison evaluated at compile time.
4139 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4140 DCHECK(value == -1 || value == 0 || value == 1) << value;
4141 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4142 }
4143
Artem Serovcced8ba2017-07-19 18:18:09 +01004144 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004145};
4146
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004147class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004148 public:
4149 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004150 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004151 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004152 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004153 bool finalizable,
4154 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304155 : HExpression(kNewInstance,
4156 DataType::Type::kReference,
4157 SideEffects::CanTriggerGC(),
4158 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004159 type_index_(type_index),
4160 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004161 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004162 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004163 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004164 }
4165
Artem Serovcced8ba2017-07-19 18:18:09 +01004166 bool IsClonable() const OVERRIDE { return true; }
4167
Andreas Gampea5b09a62016-11-17 15:21:22 -08004168 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004169 const DexFile& GetDexFile() const { return dex_file_; }
4170
4171 // Calls runtime so needs an environment.
4172 bool NeedsEnvironment() const OVERRIDE { return true; }
4173
Mingyao Yang062157f2016-03-02 10:15:36 -08004174 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4175 bool CanThrow() const OVERRIDE { return true; }
4176
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004177 bool NeedsChecks() const {
4178 return entrypoint_ == kQuickAllocObjectWithChecks;
4179 }
David Brazdil6de19382016-01-08 17:37:10 +00004180
Vladimir Markoa1de9182016-02-25 11:37:38 +00004181 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004182
4183 bool CanBeNull() const OVERRIDE { return false; }
4184
4185 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4186
4187 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4188 entrypoint_ = entrypoint;
4189 }
4190
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004191 HLoadClass* GetLoadClass() const {
4192 HInstruction* input = InputAt(0);
4193 if (input->IsClinitCheck()) {
4194 input = input->InputAt(0);
4195 }
4196 DCHECK(input->IsLoadClass());
4197 return input->AsLoadClass();
4198 }
4199
David Brazdil6de19382016-01-08 17:37:10 +00004200 bool IsStringAlloc() const;
4201
4202 DECLARE_INSTRUCTION(NewInstance);
4203
Artem Serovcced8ba2017-07-19 18:18:09 +01004204 protected:
4205 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4206
David Brazdil6de19382016-01-08 17:37:10 +00004207 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004208 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004209 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4210 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4211 "Too many packed fields.");
4212
Andreas Gampea5b09a62016-11-17 15:21:22 -08004213 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004214 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004215 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004216};
4217
Agi Csaki05f20562015-08-19 14:58:14 -07004218enum IntrinsicNeedsEnvironmentOrCache {
4219 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4220 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004221};
4222
Aart Bik5d75afe2015-12-14 11:57:01 -08004223enum IntrinsicSideEffects {
4224 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4225 kReadSideEffects, // Intrinsic may read heap memory.
4226 kWriteSideEffects, // Intrinsic may write heap memory.
4227 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4228};
4229
4230enum IntrinsicExceptions {
4231 kNoThrow, // Intrinsic does not throw any exceptions.
4232 kCanThrow // Intrinsic may throw exceptions.
4233};
4234
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004235class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004236 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004237 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004238
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004239 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004240 SetRawInputAt(index, argument);
4241 }
4242
Roland Levillain3e3d7332015-04-28 11:00:54 +01004243 // Return the number of arguments. This number can be lower than
4244 // the number of inputs returned by InputCount(), as some invoke
4245 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4246 // inputs at the end of their list of inputs.
4247 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4248
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004249 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004250
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004251 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4252
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004253 InvokeType GetInvokeType() const {
4254 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004255 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004256
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004257 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004258 return intrinsic_;
4259 }
4260
Aart Bik5d75afe2015-12-14 11:57:01 -08004261 void SetIntrinsic(Intrinsics intrinsic,
4262 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4263 IntrinsicSideEffects side_effects,
4264 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004265
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004266 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004267 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004268 }
4269
Aart Bikff7d89c2016-11-07 08:49:28 -08004270 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4271
Vladimir Markoa1de9182016-02-25 11:37:38 +00004272 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004273
Aart Bika8b8e9b2018-01-09 11:01:02 -08004274 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4275
4276 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4277
Aart Bik71bf7b42016-11-16 10:17:46 -08004278 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004279
Vladimir Marko372f10e2016-05-17 16:30:10 +01004280 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004281 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4282 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004283
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004284 uint32_t* GetIntrinsicOptimizations() {
4285 return &intrinsic_optimizations_;
4286 }
4287
4288 const uint32_t* GetIntrinsicOptimizations() const {
4289 return &intrinsic_optimizations_;
4290 }
4291
4292 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4293
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004294 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004295 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004296
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004297 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004298
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004299 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004300 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4301 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004302 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4303 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004304 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004305 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004306 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004307 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004308 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4309 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004310 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004311 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004312 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004313
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304314 HInvoke(InstructionKind kind,
4315 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004316 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004317 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004319 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004320 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004321 ArtMethod* resolved_method,
4322 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004323 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304324 kind,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004325 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4326 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004327 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004328 number_of_arguments + number_of_other_inputs,
4329 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004330 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004331 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004332 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004333 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004334 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004335 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004336 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004337 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004338 }
4339
Artem Serovcced8ba2017-07-19 18:18:09 +01004340 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4341
Roland Levillain3e3d7332015-04-28 11:00:54 +01004342 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004343 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004344 const uint32_t dex_method_index_;
4345 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004346
4347 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4348 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004349};
4350
Vladimir Markofcb503c2016-05-18 12:48:17 +01004351class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004352 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004353 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004354 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004355 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004356 uint32_t dex_pc,
4357 uint32_t dex_method_index,
4358 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304359 : HInvoke(kInvokeUnresolved,
4360 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004361 number_of_arguments,
4362 0u /* number_of_other_inputs */,
4363 return_type,
4364 dex_pc,
4365 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004366 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004367 invoke_type) {
4368 }
4369
Artem Serovcced8ba2017-07-19 18:18:09 +01004370 bool IsClonable() const OVERRIDE { return true; }
4371
Calin Juravle175dc732015-08-25 15:42:32 +01004372 DECLARE_INSTRUCTION(InvokeUnresolved);
4373
Artem Serovcced8ba2017-07-19 18:18:09 +01004374 protected:
4375 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004376};
4377
Orion Hodsonac141392017-01-13 11:53:47 +00004378class HInvokePolymorphic FINAL : public HInvoke {
4379 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004380 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004381 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004382 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004383 uint32_t dex_pc,
4384 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304385 : HInvoke(kInvokePolymorphic,
4386 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004387 number_of_arguments,
4388 0u /* number_of_other_inputs */,
4389 return_type,
4390 dex_pc,
4391 dex_method_index,
4392 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304393 kVirtual) {
4394 }
Orion Hodsonac141392017-01-13 11:53:47 +00004395
Artem Serovcced8ba2017-07-19 18:18:09 +01004396 bool IsClonable() const OVERRIDE { return true; }
4397
Orion Hodsonac141392017-01-13 11:53:47 +00004398 DECLARE_INSTRUCTION(InvokePolymorphic);
4399
Artem Serovcced8ba2017-07-19 18:18:09 +01004400 protected:
4401 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004402};
4403
Vladimir Markofcb503c2016-05-18 12:48:17 +01004404class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004405 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004406 // Requirements of this method call regarding the class
4407 // initialization (clinit) check of its declaring class.
4408 enum class ClinitCheckRequirement {
4409 kNone, // Class already initialized.
4410 kExplicit, // Static call having explicit clinit check as last input.
4411 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004412 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004413 };
4414
Vladimir Marko58155012015-08-19 12:49:41 +00004415 // Determines how to load the target ArtMethod*.
4416 enum class MethodLoadKind {
4417 // Use a String init ArtMethod* loaded from Thread entrypoints.
4418 kStringInit,
4419
4420 // Use the method's own ArtMethod* loaded by the register allocator.
4421 kRecursive,
4422
Vladimir Marko65979462017-05-19 17:25:12 +01004423 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4424 // Used for boot image methods referenced by boot image code.
4425 kBootImageLinkTimePcRelative,
4426
Vladimir Marko58155012015-08-19 12:49:41 +00004427 // Use ArtMethod* at a known address, embed the direct address in the code.
4428 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4429 kDirectAddress,
4430
Vladimir Markob066d432018-01-03 13:14:37 +00004431 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4432 // Used for app->boot calls with relocatable image.
4433 kBootImageRelRo,
4434
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004435 // Load from an entry in the .bss section using a PC-relative load.
4436 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4437 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004438
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004439 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4440 // used when other kinds are unimplemented on a particular architecture.
4441 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004442 };
4443
4444 // Determines the location of the code pointer.
4445 enum class CodePtrLocation {
4446 // Recursive call, use local PC-relative call instruction.
4447 kCallSelf,
4448
Vladimir Marko58155012015-08-19 12:49:41 +00004449 // Use code pointer from the ArtMethod*.
4450 // Used when we don't know the target code. This is also the last-resort-kind used when
4451 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4452 kCallArtMethod,
4453 };
4454
4455 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004456 MethodLoadKind method_load_kind;
4457 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004458 // The method load data holds
4459 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4460 // Note that there are multiple string init methods, each having its own offset.
4461 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004462 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004463 };
4464
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004465 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004466 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004467 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004468 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004469 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004470 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004471 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004472 InvokeType invoke_type,
4473 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004474 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304475 : HInvoke(kInvokeStaticOrDirect,
4476 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004477 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004478 // There is potentially one extra argument for the HCurrentMethod node, and
4479 // potentially one other if the clinit check is explicit, and potentially
4480 // one other if the method is a string factory.
4481 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004482 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004483 return_type,
4484 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004485 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004486 resolved_method,
4487 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004488 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004489 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004490 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4491 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004492
Artem Serovcced8ba2017-07-19 18:18:09 +01004493 bool IsClonable() const OVERRIDE { return true; }
4494
Vladimir Markodc151b22015-10-15 18:02:30 +01004495 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004496 bool had_current_method_input = HasCurrentMethodInput();
4497 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4498
4499 // Using the current method is the default and once we find a better
4500 // method load kind, we should not go back to using the current method.
4501 DCHECK(had_current_method_input || !needs_current_method_input);
4502
4503 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004504 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4505 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004506 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004507 dispatch_info_ = dispatch_info;
4508 }
4509
Aart Bik6daebeb2017-04-03 14:35:41 -07004510 DispatchInfo GetDispatchInfo() const {
4511 return dispatch_info_;
4512 }
4513
Vladimir Markoc53c0792015-11-19 15:48:33 +00004514 void AddSpecialInput(HInstruction* input) {
4515 // We allow only one special input.
4516 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4517 DCHECK(InputCount() == GetSpecialInputIndex() ||
4518 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4519 InsertInputAt(GetSpecialInputIndex(), input);
4520 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004521
Vladimir Marko372f10e2016-05-17 16:30:10 +01004522 using HInstruction::GetInputRecords; // Keep the const version visible.
4523 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4524 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4525 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4526 DCHECK(!input_records.empty());
4527 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4528 HInstruction* last_input = input_records.back().GetInstruction();
4529 // Note: `last_input` may be null during arguments setup.
4530 if (last_input != nullptr) {
4531 // `last_input` is the last input of a static invoke marked as having
4532 // an explicit clinit check. It must either be:
4533 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4534 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4535 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4536 }
4537 }
4538 return input_records;
4539 }
4540
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004541 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004542 // We access the method via the dex cache so we can't do an implicit null check.
4543 // TODO: for intrinsics we can generate implicit null checks.
4544 return false;
4545 }
4546
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004547 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004548 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004549 }
4550
Vladimir Markoc53c0792015-11-19 15:48:33 +00004551 // Get the index of the special input, if any.
4552 //
David Brazdil6de19382016-01-08 17:37:10 +00004553 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4554 // method pointer; otherwise there may be one platform-specific special input,
4555 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004556 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004557 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004558
Vladimir Marko58155012015-08-19 12:49:41 +00004559 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4560 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4561 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004562 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004563 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004564 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004565 bool HasPcRelativeMethodLoadKind() const {
4566 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004567 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004568 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004569 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004570 bool HasCurrentMethodInput() const {
4571 // This function can be called only after the invoke has been fully initialized by the builder.
4572 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004573 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004574 return true;
4575 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004576 DCHECK(InputCount() == GetSpecialInputIndex() ||
4577 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004578 return false;
4579 }
4580 }
Vladimir Marko58155012015-08-19 12:49:41 +00004581
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004582 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004583 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004584 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004585 }
4586
4587 uint64_t GetMethodAddress() const {
4588 DCHECK(HasMethodAddress());
4589 return dispatch_info_.method_load_data;
4590 }
4591
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004592 const DexFile& GetDexFileForPcRelativeDexCache() const;
4593
Vladimir Markoa1de9182016-02-25 11:37:38 +00004594 ClinitCheckRequirement GetClinitCheckRequirement() const {
4595 return GetPackedField<ClinitCheckRequirementField>();
4596 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004597
Roland Levillain4c0eb422015-04-24 16:43:49 +01004598 // Is this instruction a call to a static method?
4599 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004600 return GetInvokeType() == kStatic;
4601 }
4602
4603 MethodReference GetTargetMethod() const {
4604 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004605 }
4606
Vladimir Markofbb184a2015-11-13 14:47:00 +00004607 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4608 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4609 // instruction; only relevant for static calls with explicit clinit check.
4610 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004611 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004612 size_t last_input_index = inputs_.size() - 1u;
4613 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004614 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004615 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004616 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004617 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004618 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004619 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004620 }
4621
4622 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004623 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004624 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004625 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004626 }
4627
4628 // Is this a call to a static method whose declaring class has an
4629 // implicit intialization check requirement?
4630 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004631 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004632 }
4633
Vladimir Markob554b5a2015-11-06 12:57:55 +00004634 // Does this method load kind need the current method as an input?
4635 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004636 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004637 }
4638
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004639 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004640
Artem Serovcced8ba2017-07-19 18:18:09 +01004641 protected:
4642 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4643
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004644 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004645 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004646 static constexpr size_t kFieldClinitCheckRequirementSize =
4647 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4648 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4649 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4650 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4651 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004652 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4653 kFieldClinitCheckRequirement,
4654 kFieldClinitCheckRequirementSize>;
4655
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004656 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004657 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004658 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004659};
Vladimir Markof64242a2015-12-01 14:58:23 +00004660std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004661std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004662
Vladimir Markofcb503c2016-05-18 12:48:17 +01004663class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004664 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004665 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004666 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004667 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004668 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004669 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004670 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004671 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304672 : HInvoke(kInvokeVirtual,
4673 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004674 number_of_arguments,
4675 0u,
4676 return_type,
4677 dex_pc,
4678 dex_method_index,
4679 resolved_method,
4680 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304681 vtable_index_(vtable_index) {
4682 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004683
Artem Serovcced8ba2017-07-19 18:18:09 +01004684 bool IsClonable() const OVERRIDE { return true; }
4685
Aart Bik71bf7b42016-11-16 10:17:46 -08004686 bool CanBeNull() const OVERRIDE {
4687 switch (GetIntrinsic()) {
4688 case Intrinsics::kThreadCurrentThread:
4689 case Intrinsics::kStringBufferAppend:
4690 case Intrinsics::kStringBufferToString:
4691 case Intrinsics::kStringBuilderAppend:
4692 case Intrinsics::kStringBuilderToString:
4693 return false;
4694 default:
4695 return HInvoke::CanBeNull();
4696 }
4697 }
4698
Calin Juravle641547a2015-04-21 22:08:51 +01004699 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004700 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004701 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004702 }
4703
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004704 uint32_t GetVTableIndex() const { return vtable_index_; }
4705
4706 DECLARE_INSTRUCTION(InvokeVirtual);
4707
Artem Serovcced8ba2017-07-19 18:18:09 +01004708 protected:
4709 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4710
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004711 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004712 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004713 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004714};
4715
Vladimir Markofcb503c2016-05-18 12:48:17 +01004716class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004717 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004718 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004719 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004720 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004721 uint32_t dex_pc,
4722 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004723 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004724 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304725 : HInvoke(kInvokeInterface,
4726 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004727 number_of_arguments,
4728 0u,
4729 return_type,
4730 dex_pc,
4731 dex_method_index,
4732 resolved_method,
4733 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304734 imt_index_(imt_index) {
4735 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004736
Artem Serovcced8ba2017-07-19 18:18:09 +01004737 bool IsClonable() const OVERRIDE { return true; }
4738
Calin Juravle641547a2015-04-21 22:08:51 +01004739 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004740 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004741 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004742 }
4743
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004744 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4745 // The assembly stub currently needs it.
4746 return true;
4747 }
4748
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004749 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004750
4751 DECLARE_INSTRUCTION(InvokeInterface);
4752
Artem Serovcced8ba2017-07-19 18:18:09 +01004753 protected:
4754 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4755
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004756 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004757 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004758 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004759};
4760
Vladimir Markofcb503c2016-05-18 12:48:17 +01004761class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004762 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004763 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304764 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004765 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004766 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004767
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004768 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004769
4770 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004771 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004772 }
4773 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004774 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004775 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004776 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4777 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4778 }
4779 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4780 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4781 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004782
Roland Levillain88cb1752014-10-20 16:36:47 +01004783 DECLARE_INSTRUCTION(Neg);
4784
Artem Serovcced8ba2017-07-19 18:18:09 +01004785 protected:
4786 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004787};
4788
Vladimir Markofcb503c2016-05-18 12:48:17 +01004789class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004790 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004791 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304792 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004793 SetRawInputAt(0, cls);
4794 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004795 }
4796
Artem Serovcced8ba2017-07-19 18:18:09 +01004797 bool IsClonable() const OVERRIDE { return true; }
4798
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004799 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004800 bool NeedsEnvironment() const OVERRIDE { return true; }
4801
Mingyao Yang0c365e62015-03-31 15:09:29 -07004802 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4803 bool CanThrow() const OVERRIDE { return true; }
4804
Calin Juravle10e244f2015-01-26 18:54:32 +00004805 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004806
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004807 HLoadClass* GetLoadClass() const {
4808 DCHECK(InputAt(0)->IsLoadClass());
4809 return InputAt(0)->AsLoadClass();
4810 }
4811
4812 HInstruction* GetLength() const {
4813 return InputAt(1);
4814 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004815
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004816 DECLARE_INSTRUCTION(NewArray);
4817
Artem Serovcced8ba2017-07-19 18:18:09 +01004818 protected:
4819 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004820};
4821
Vladimir Markofcb503c2016-05-18 12:48:17 +01004822class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004823 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004824 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004825 HInstruction* left,
4826 HInstruction* right,
4827 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304828 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4829 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004830
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004831 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004832
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004833 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004834
4835 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004836 return GetBlock()->GetGraph()->GetIntConstant(
4837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004838 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004839 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004840 return GetBlock()->GetGraph()->GetLongConstant(
4841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004842 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004843 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4844 return GetBlock()->GetGraph()->GetFloatConstant(
4845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4846 }
4847 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4848 return GetBlock()->GetGraph()->GetDoubleConstant(
4849 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4850 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004851
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004852 DECLARE_INSTRUCTION(Add);
4853
Artem Serovcced8ba2017-07-19 18:18:09 +01004854 protected:
4855 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004856};
4857
Vladimir Markofcb503c2016-05-18 12:48:17 +01004858class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004859 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004861 HInstruction* left,
4862 HInstruction* right,
4863 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304864 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4865 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004866
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004867 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004868
4869 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004870 return GetBlock()->GetGraph()->GetIntConstant(
4871 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004872 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004873 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004874 return GetBlock()->GetGraph()->GetLongConstant(
4875 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004876 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004877 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4878 return GetBlock()->GetGraph()->GetFloatConstant(
4879 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4880 }
4881 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4882 return GetBlock()->GetGraph()->GetDoubleConstant(
4883 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4884 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004885
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004886 DECLARE_INSTRUCTION(Sub);
4887
Artem Serovcced8ba2017-07-19 18:18:09 +01004888 protected:
4889 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004890};
4891
Vladimir Markofcb503c2016-05-18 12:48:17 +01004892class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004893 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004894 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004895 HInstruction* left,
4896 HInstruction* right,
4897 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304898 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4899 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004900
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004901 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004902
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004903 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004904
4905 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 return GetBlock()->GetGraph()->GetIntConstant(
4907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004908 }
4909 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004910 return GetBlock()->GetGraph()->GetLongConstant(
4911 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004912 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004913 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4914 return GetBlock()->GetGraph()->GetFloatConstant(
4915 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4916 }
4917 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4918 return GetBlock()->GetGraph()->GetDoubleConstant(
4919 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4920 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004921
4922 DECLARE_INSTRUCTION(Mul);
4923
Artem Serovcced8ba2017-07-19 18:18:09 +01004924 protected:
4925 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004926};
4927
Vladimir Markofcb503c2016-05-18 12:48:17 +01004928class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004929 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004930 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 HInstruction* left,
4932 HInstruction* right,
4933 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304934 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4935 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004936
Roland Levillain9867bc72015-08-05 10:21:34 +01004937 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004938 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004939 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004940 // Our graph structure ensures we never have 0 for `y` during
4941 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004942 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004943 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004944 return (y == -1) ? -x : x / y;
4945 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004946
Roland Levillain31dd3d62016-02-16 12:21:02 +00004947 template <typename T>
4948 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004949 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004950 return x / y;
4951 }
4952
Roland Levillain9867bc72015-08-05 10:21:34 +01004953 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004954 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004955 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004956 }
4957 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004958 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004959 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4960 }
4961 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4962 return GetBlock()->GetGraph()->GetFloatConstant(
4963 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4964 }
4965 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4966 return GetBlock()->GetGraph()->GetDoubleConstant(
4967 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004968 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004969
4970 DECLARE_INSTRUCTION(Div);
4971
Artem Serovcced8ba2017-07-19 18:18:09 +01004972 protected:
4973 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004974};
4975
Vladimir Markofcb503c2016-05-18 12:48:17 +01004976class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004977 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004978 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004979 HInstruction* left,
4980 HInstruction* right,
4981 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304982 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4983 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004984
Roland Levillain9867bc72015-08-05 10:21:34 +01004985 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004986 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004987 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004988 // Our graph structure ensures we never have 0 for `y` during
4989 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004990 DCHECK_NE(y, 0);
4991 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4992 return (y == -1) ? 0 : x % y;
4993 }
4994
Roland Levillain31dd3d62016-02-16 12:21:02 +00004995 template <typename T>
4996 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004997 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004998 return std::fmod(x, y);
4999 }
5000
Roland Levillain9867bc72015-08-05 10:21:34 +01005001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005002 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005003 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005004 }
5005 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005006 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005007 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005008 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005009 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
5010 return GetBlock()->GetGraph()->GetFloatConstant(
5011 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5012 }
5013 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
5014 return GetBlock()->GetGraph()->GetDoubleConstant(
5015 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5016 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005017
5018 DECLARE_INSTRUCTION(Rem);
5019
Artem Serovcced8ba2017-07-19 18:18:09 +01005020 protected:
5021 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005022};
5023
Aart Bik3dad3412018-02-28 12:01:46 -08005024class HAbs FINAL : public HUnaryOperation {
5025 public:
5026 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5027 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5028
5029 // Evaluation for integral values.
5030 template <typename T> static T ComputeIntegral(T x) {
5031 return x < 0 ? -x : x;
5032 }
5033
5034 // Evaluation for floating-point values.
5035 // Note, as a "quality of implementation", rather than pure "spec compliance",
5036 // we require that Math.abs() clears the sign bit (but changes nothing else)
5037 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5038 // http://b/30758343
5039 template <typename T, typename S> static T ComputeFP(T x) {
5040 S bits = bit_cast<S, T>(x);
5041 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5042 }
5043
5044 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5045 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5046 }
5047 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5048 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5049 }
5050 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5051 return GetBlock()->GetGraph()->GetFloatConstant(
5052 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5053 }
5054 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5055 return GetBlock()->GetGraph()->GetDoubleConstant(
5056 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5057 }
5058
5059 DECLARE_INSTRUCTION(Abs);
5060
5061 protected:
5062 DEFAULT_COPY_CONSTRUCTOR(Abs);
5063};
5064
Vladimir Markofcb503c2016-05-18 12:48:17 +01005065class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005066 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005067 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5068 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005069 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305070 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005071 SetRawInputAt(0, value);
5072 }
5073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005074 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06005075
Calin Juravled0d48522014-11-04 16:40:20 +00005076 bool CanBeMoved() const OVERRIDE { return true; }
5077
Vladimir Marko372f10e2016-05-17 16:30:10 +01005078 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005079 return true;
5080 }
5081
5082 bool NeedsEnvironment() const OVERRIDE { return true; }
5083 bool CanThrow() const OVERRIDE { return true; }
5084
Calin Juravled0d48522014-11-04 16:40:20 +00005085 DECLARE_INSTRUCTION(DivZeroCheck);
5086
Artem Serovcced8ba2017-07-19 18:18:09 +01005087 protected:
5088 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005089};
5090
Vladimir Markofcb503c2016-05-18 12:48:17 +01005091class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005092 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005093 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005094 HInstruction* value,
5095 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005096 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305097 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005098 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5099 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005100 }
5101
Roland Levillain5b5b9312016-03-22 14:57:31 +00005102 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005103 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005104 return value << (distance & max_shift_distance);
5105 }
5106
5107 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005108 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005109 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005110 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005111 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005112 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005113 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005114 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005115 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5116 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5117 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5118 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005119 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005120 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5121 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005122 LOG(FATAL) << DebugName() << " is not defined for float values";
5123 UNREACHABLE();
5124 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005125 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5126 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005127 LOG(FATAL) << DebugName() << " is not defined for double values";
5128 UNREACHABLE();
5129 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005130
5131 DECLARE_INSTRUCTION(Shl);
5132
Artem Serovcced8ba2017-07-19 18:18:09 +01005133 protected:
5134 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005135};
5136
Vladimir Markofcb503c2016-05-18 12:48:17 +01005137class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005138 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005139 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005140 HInstruction* value,
5141 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005142 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305143 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005144 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5145 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005146 }
5147
Roland Levillain5b5b9312016-03-22 14:57:31 +00005148 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005149 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005150 return value >> (distance & max_shift_distance);
5151 }
5152
5153 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005154 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005155 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005156 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005157 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005158 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005159 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005160 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005161 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5162 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5163 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5164 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005165 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005166 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5167 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005168 LOG(FATAL) << DebugName() << " is not defined for float values";
5169 UNREACHABLE();
5170 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005171 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5172 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005173 LOG(FATAL) << DebugName() << " is not defined for double values";
5174 UNREACHABLE();
5175 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005176
5177 DECLARE_INSTRUCTION(Shr);
5178
Artem Serovcced8ba2017-07-19 18:18:09 +01005179 protected:
5180 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005181};
5182
Vladimir Markofcb503c2016-05-18 12:48:17 +01005183class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005184 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005186 HInstruction* value,
5187 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005188 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305189 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005190 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5191 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005192 }
5193
Roland Levillain5b5b9312016-03-22 14:57:31 +00005194 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005195 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005196 typedef typename std::make_unsigned<T>::type V;
5197 V ux = static_cast<V>(value);
5198 return static_cast<T>(ux >> (distance & max_shift_distance));
5199 }
5200
5201 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005202 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005203 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005204 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005205 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005206 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005207 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005208 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005209 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5210 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5211 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5212 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005213 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005214 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5215 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005216 LOG(FATAL) << DebugName() << " is not defined for float values";
5217 UNREACHABLE();
5218 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005219 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5220 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005221 LOG(FATAL) << DebugName() << " is not defined for double values";
5222 UNREACHABLE();
5223 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005224
5225 DECLARE_INSTRUCTION(UShr);
5226
Artem Serovcced8ba2017-07-19 18:18:09 +01005227 protected:
5228 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005229};
5230
Vladimir Markofcb503c2016-05-18 12:48:17 +01005231class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005232 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005233 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005234 HInstruction* left,
5235 HInstruction* right,
5236 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305237 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5238 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005239
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005240 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005241
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005242 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005243
5244 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005245 return GetBlock()->GetGraph()->GetIntConstant(
5246 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005247 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005248 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005249 return GetBlock()->GetGraph()->GetLongConstant(
5250 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005251 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005252 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5253 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5254 LOG(FATAL) << DebugName() << " is not defined for float values";
5255 UNREACHABLE();
5256 }
5257 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5258 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5259 LOG(FATAL) << DebugName() << " is not defined for double values";
5260 UNREACHABLE();
5261 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005262
5263 DECLARE_INSTRUCTION(And);
5264
Artem Serovcced8ba2017-07-19 18:18:09 +01005265 protected:
5266 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005267};
5268
Vladimir Markofcb503c2016-05-18 12:48:17 +01005269class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005270 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005271 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005272 HInstruction* left,
5273 HInstruction* right,
5274 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305275 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5276 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005277
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005278 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005279
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005280 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005281
5282 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005283 return GetBlock()->GetGraph()->GetIntConstant(
5284 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005285 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005286 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005287 return GetBlock()->GetGraph()->GetLongConstant(
5288 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005289 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005290 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5291 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5292 LOG(FATAL) << DebugName() << " is not defined for float values";
5293 UNREACHABLE();
5294 }
5295 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5296 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5297 LOG(FATAL) << DebugName() << " is not defined for double values";
5298 UNREACHABLE();
5299 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005300
5301 DECLARE_INSTRUCTION(Or);
5302
Artem Serovcced8ba2017-07-19 18:18:09 +01005303 protected:
5304 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005305};
5306
Vladimir Markofcb503c2016-05-18 12:48:17 +01005307class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005308 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005309 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005310 HInstruction* left,
5311 HInstruction* right,
5312 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305313 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5314 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005315
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005316 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005317
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005318 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005319
5320 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005321 return GetBlock()->GetGraph()->GetIntConstant(
5322 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005323 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005324 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005325 return GetBlock()->GetGraph()->GetLongConstant(
5326 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005327 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005328 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5329 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5330 LOG(FATAL) << DebugName() << " is not defined for float values";
5331 UNREACHABLE();
5332 }
5333 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5334 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5335 LOG(FATAL) << DebugName() << " is not defined for double values";
5336 UNREACHABLE();
5337 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005338
5339 DECLARE_INSTRUCTION(Xor);
5340
Artem Serovcced8ba2017-07-19 18:18:09 +01005341 protected:
5342 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005343};
5344
Vladimir Markofcb503c2016-05-18 12:48:17 +01005345class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005346 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005347 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305348 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005349 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5350 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005351 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005352
Roland Levillain5b5b9312016-03-22 14:57:31 +00005353 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005354 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005355 typedef typename std::make_unsigned<T>::type V;
5356 V ux = static_cast<V>(value);
5357 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005358 return static_cast<T>(ux);
5359 } else {
5360 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005361 return static_cast<T>(ux >> (distance & max_shift_value)) |
5362 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005363 }
5364 }
5365
Roland Levillain5b5b9312016-03-22 14:57:31 +00005366 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005367 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005368 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005369 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005370 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005371 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005372 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005373 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005374 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5375 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5376 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5377 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005378 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005379 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5380 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005381 LOG(FATAL) << DebugName() << " is not defined for float values";
5382 UNREACHABLE();
5383 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005384 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5385 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005386 LOG(FATAL) << DebugName() << " is not defined for double values";
5387 UNREACHABLE();
5388 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005389
5390 DECLARE_INSTRUCTION(Ror);
5391
Artem Serovcced8ba2017-07-19 18:18:09 +01005392 protected:
5393 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005394};
5395
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005396// The value of a parameter in this method. Its location depends on
5397// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005398class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005399 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005400 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005401 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005402 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005403 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005404 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305405 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005406 dex_file_(dex_file),
5407 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005408 index_(index) {
5409 SetPackedFlag<kFlagIsThis>(is_this);
5410 SetPackedFlag<kFlagCanBeNull>(!is_this);
5411 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005412
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005413 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005414 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005415 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005416 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005417
Vladimir Markoa1de9182016-02-25 11:37:38 +00005418 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5419 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005420
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005421 DECLARE_INSTRUCTION(ParameterValue);
5422
Artem Serovcced8ba2017-07-19 18:18:09 +01005423 protected:
5424 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5425
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005426 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005427 // Whether or not the parameter value corresponds to 'this' argument.
5428 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5429 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5430 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5431 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5432 "Too many packed fields.");
5433
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005434 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005435 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005436 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005437 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005438 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005439};
5440
Vladimir Markofcb503c2016-05-18 12:48:17 +01005441class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005442 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005443 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305444 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5445 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005446
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005447 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005448 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005449 return true;
5450 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005451
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005452 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005453
5454 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005455 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005456 }
5457 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005458 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005459 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005460 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5461 LOG(FATAL) << DebugName() << " is not defined for float values";
5462 UNREACHABLE();
5463 }
5464 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5465 LOG(FATAL) << DebugName() << " is not defined for double values";
5466 UNREACHABLE();
5467 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005468
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005469 DECLARE_INSTRUCTION(Not);
5470
Artem Serovcced8ba2017-07-19 18:18:09 +01005471 protected:
5472 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005473};
5474
Vladimir Markofcb503c2016-05-18 12:48:17 +01005475class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005476 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005477 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305478 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5479 }
David Brazdil66d126e2015-04-03 16:02:44 +01005480
5481 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005482 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005483 return true;
5484 }
5485
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005486 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005487 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005488 return !x;
5489 }
5490
Roland Levillain9867bc72015-08-05 10:21:34 +01005491 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005492 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005493 }
5494 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5495 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005496 UNREACHABLE();
5497 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005498 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5499 LOG(FATAL) << DebugName() << " is not defined for float values";
5500 UNREACHABLE();
5501 }
5502 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5503 LOG(FATAL) << DebugName() << " is not defined for double values";
5504 UNREACHABLE();
5505 }
David Brazdil66d126e2015-04-03 16:02:44 +01005506
5507 DECLARE_INSTRUCTION(BooleanNot);
5508
Artem Serovcced8ba2017-07-19 18:18:09 +01005509 protected:
5510 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005511};
5512
Vladimir Markofcb503c2016-05-18 12:48:17 +01005513class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005514 public:
5515 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005516 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305517 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005518 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005519 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005520 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005521 }
5522
5523 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005524 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5525 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005526
5527 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005528 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5529 return true;
5530 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005531
Mark Mendelle82549b2015-05-06 10:55:34 -04005532 // Try to statically evaluate the conversion and return a HConstant
5533 // containing the result. If the input cannot be converted, return nullptr.
5534 HConstant* TryStaticEvaluation() const;
5535
Roland Levillaindff1f282014-11-05 14:15:05 +00005536 DECLARE_INSTRUCTION(TypeConversion);
5537
Artem Serovcced8ba2017-07-19 18:18:09 +01005538 protected:
5539 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005540};
5541
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005542static constexpr uint32_t kNoRegNumber = -1;
5543
Vladimir Markofcb503c2016-05-18 12:48:17 +01005544class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005545 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005546 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5547 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005548 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305549 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005550 SetRawInputAt(0, value);
5551 }
5552
Artem Serovcced8ba2017-07-19 18:18:09 +01005553 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005554 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005555 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005556 return true;
5557 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005558
Calin Juravle10e244f2015-01-26 18:54:32 +00005559 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005560
Calin Juravle10e244f2015-01-26 18:54:32 +00005561 bool CanThrow() const OVERRIDE { return true; }
5562
5563 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005564
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005565 DECLARE_INSTRUCTION(NullCheck);
5566
Artem Serovcced8ba2017-07-19 18:18:09 +01005567 protected:
5568 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005569};
5570
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005571// Embeds an ArtField and all the information required by the compiler. We cache
5572// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005573class FieldInfo : public ValueObject {
5574 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005575 FieldInfo(ArtField* field,
5576 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005577 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005578 bool is_volatile,
5579 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005580 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005581 const DexFile& dex_file)
5582 : field_(field),
5583 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005584 field_type_(field_type),
5585 is_volatile_(is_volatile),
5586 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005587 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005588 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005589
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005590 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005591 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005593 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005594 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005595 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005596 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005597
5598 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005599 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005600 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005602 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005603 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005604 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005605 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005606};
5607
Vladimir Markofcb503c2016-05-18 12:48:17 +01005608class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005609 public:
5610 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005611 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005612 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005613 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005614 bool is_volatile,
5615 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005616 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005617 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005618 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305619 : HExpression(kInstanceFieldGet,
5620 field_type,
5621 SideEffects::FieldReadOfType(field_type, is_volatile),
5622 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005623 field_info_(field,
5624 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005625 field_type,
5626 is_volatile,
5627 field_idx,
5628 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005629 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005630 SetRawInputAt(0, value);
5631 }
5632
Artem Serovcced8ba2017-07-19 18:18:09 +01005633 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005634 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005635
Vladimir Marko372f10e2016-05-17 16:30:10 +01005636 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5637 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005638 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005639 }
5640
Calin Juravle641547a2015-04-21 22:08:51 +01005641 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005642 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005643 }
5644
5645 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005646 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5647 }
5648
Calin Juravle52c48962014-12-16 17:02:57 +00005649 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005650 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005651 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005652 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005653
Vladimir Marko61b92282017-10-11 13:23:17 +01005654 void SetType(DataType::Type new_type) {
5655 DCHECK(DataType::IsIntegralType(GetType()));
5656 DCHECK(DataType::IsIntegralType(new_type));
5657 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5658 SetPackedField<TypeField>(new_type);
5659 }
5660
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005661 DECLARE_INSTRUCTION(InstanceFieldGet);
5662
Artem Serovcced8ba2017-07-19 18:18:09 +01005663 protected:
5664 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5665
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005666 private:
5667 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005668};
5669
Vladimir Markofcb503c2016-05-18 12:48:17 +01005670class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005671 public:
5672 HInstanceFieldSet(HInstruction* object,
5673 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005674 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005675 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005676 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005677 bool is_volatile,
5678 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005679 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005680 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005681 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305682 : HTemplateInstruction(kInstanceFieldSet,
5683 SideEffects::FieldWriteOfType(field_type, is_volatile),
5684 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005685 field_info_(field,
5686 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005687 field_type,
5688 is_volatile,
5689 field_idx,
5690 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005691 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005692 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005693 SetRawInputAt(0, object);
5694 SetRawInputAt(1, value);
5695 }
5696
Artem Serovcced8ba2017-07-19 18:18:09 +01005697 bool IsClonable() const OVERRIDE { return true; }
5698
Calin Juravle641547a2015-04-21 22:08:51 +01005699 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005700 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005701 }
5702
Calin Juravle52c48962014-12-16 17:02:57 +00005703 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005704 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005705 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005706 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005707 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005708 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5709 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005710
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005711 DECLARE_INSTRUCTION(InstanceFieldSet);
5712
Artem Serovcced8ba2017-07-19 18:18:09 +01005713 protected:
5714 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5715
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005716 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005717 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5718 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5719 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5720 "Too many packed fields.");
5721
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005722 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005723};
5724
Vladimir Markofcb503c2016-05-18 12:48:17 +01005725class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005726 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005727 HArrayGet(HInstruction* array,
5728 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005729 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005730 uint32_t dex_pc)
5731 : HArrayGet(array,
5732 index,
5733 type,
5734 SideEffects::ArrayReadOfType(type),
5735 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305736 /* is_string_char_at */ false) {
5737 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005738
5739 HArrayGet(HInstruction* array,
5740 HInstruction* index,
5741 DataType::Type type,
5742 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005743 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005744 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305745 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005746 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005747 SetRawInputAt(0, array);
5748 SetRawInputAt(1, index);
5749 }
5750
Artem Serovcced8ba2017-07-19 18:18:09 +01005751 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005752 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005753 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005754 return true;
5755 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005756 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005757 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005758 // Currently, unless the array is the result of NewArray, the array access is always
5759 // preceded by some form of null NullCheck necessary for the bounds check, usually
5760 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5761 // dynamic BCE. There are cases when these could be removed to produce better code.
5762 // If we ever add optimizations to do so we should allow an implicit check here
5763 // (as long as the address falls in the first page).
5764 //
5765 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5766 // a = cond ? new int[1] : null;
5767 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005768 return false;
5769 }
5770
David Brazdil4833f5a2015-12-16 10:37:39 +00005771 bool IsEquivalentOf(HArrayGet* other) const {
5772 bool result = (GetDexPc() == other->GetDexPc());
5773 if (kIsDebugBuild && result) {
5774 DCHECK_EQ(GetBlock(), other->GetBlock());
5775 DCHECK_EQ(GetArray(), other->GetArray());
5776 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005777 if (DataType::IsIntOrLongType(GetType())) {
5778 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005779 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005780 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5781 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005782 }
5783 }
5784 return result;
5785 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005786
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005787 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5788
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005789 HInstruction* GetArray() const { return InputAt(0); }
5790 HInstruction* GetIndex() const { return InputAt(1); }
5791
Vladimir Marko61b92282017-10-11 13:23:17 +01005792 void SetType(DataType::Type new_type) {
5793 DCHECK(DataType::IsIntegralType(GetType()));
5794 DCHECK(DataType::IsIntegralType(new_type));
5795 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5796 SetPackedField<TypeField>(new_type);
5797 }
5798
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005799 DECLARE_INSTRUCTION(ArrayGet);
5800
Artem Serovcced8ba2017-07-19 18:18:09 +01005801 protected:
5802 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5803
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005804 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005805 // We treat a String as an array, creating the HArrayGet from String.charAt()
5806 // intrinsic in the instruction simplifier. We can always determine whether
5807 // a particular HArrayGet is actually a String.charAt() by looking at the type
5808 // of the input but that requires holding the mutator lock, so we prefer to use
5809 // a flag, so that code generators don't need to do the locking.
5810 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5811 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5812 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5813 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005814};
5815
Vladimir Markofcb503c2016-05-18 12:48:17 +01005816class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005817 public:
5818 HArraySet(HInstruction* array,
5819 HInstruction* index,
5820 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005821 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005822 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005823 : HArraySet(array,
5824 index,
5825 value,
5826 expected_component_type,
5827 // Make a best guess for side effects now, may be refined during SSA building.
5828 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305829 dex_pc) {
5830 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005831
5832 HArraySet(HInstruction* array,
5833 HInstruction* index,
5834 HInstruction* value,
5835 DataType::Type expected_component_type,
5836 SideEffects side_effects,
5837 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305838 : HTemplateInstruction(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005839 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005841 SetPackedFlag<kFlagValueCanBeNull>(true);
5842 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005843 SetRawInputAt(0, array);
5844 SetRawInputAt(1, index);
5845 SetRawInputAt(2, value);
5846 }
5847
Artem Serovcced8ba2017-07-19 18:18:09 +01005848 bool IsClonable() const OVERRIDE { return true; }
5849
Calin Juravle77520bc2015-01-12 18:45:46 +00005850 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005851 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005852 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005853 }
5854
Mingyao Yang81014cb2015-06-02 03:16:27 -07005855 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005856 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005857
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005858 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005859 // TODO: Same as for ArrayGet.
5860 return false;
5861 }
5862
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005863 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005864 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005865 }
5866
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005867 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005868 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005869 }
5870
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005871 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005872 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005873 }
5874
Vladimir Markoa1de9182016-02-25 11:37:38 +00005875 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5876 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5877 bool StaticTypeOfArrayIsObjectArray() const {
5878 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5879 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005880
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005881 HInstruction* GetArray() const { return InputAt(0); }
5882 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005883 HInstruction* GetValue() const { return InputAt(2); }
5884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005885 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005886 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5887 }
5888
5889 static DataType::Type GetComponentType(DataType::Type value_type,
5890 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005891 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005892 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005893 // be correct, we also check what is the value type. If it is a floating
5894 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005896 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005897 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005898 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005899
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005900 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005901 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005902 }
5903
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005904 static SideEffects ComputeSideEffects(DataType::Type type) {
5905 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005906 }
5907
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5909 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5910 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005911 }
5912
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005913 DECLARE_INSTRUCTION(ArraySet);
5914
Artem Serovcced8ba2017-07-19 18:18:09 +01005915 protected:
5916 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5917
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005918 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005919 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5920 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005921 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005922 static constexpr size_t kFlagNeedsTypeCheck =
5923 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5924 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005925 // Cached information for the reference_type_info_ so that codegen
5926 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005927 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5928 static constexpr size_t kNumberOfArraySetPackedBits =
5929 kFlagStaticTypeOfArrayIsObjectArray + 1;
5930 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5931 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005932 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005933};
5934
Vladimir Markofcb503c2016-05-18 12:48:17 +01005935class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005936 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005937 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305938 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005939 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005940 // Note that arrays do not change length, so the instruction does not
5941 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005942 SetRawInputAt(0, array);
5943 }
5944
Artem Serovcced8ba2017-07-19 18:18:09 +01005945 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005946 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005947 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005948 return true;
5949 }
Calin Juravle641547a2015-04-21 22:08:51 +01005950 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5951 return obj == InputAt(0);
5952 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005953
Vladimir Markodce016e2016-04-28 13:10:02 +01005954 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5955
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005956 DECLARE_INSTRUCTION(ArrayLength);
5957
Artem Serovcced8ba2017-07-19 18:18:09 +01005958 protected:
5959 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
5960
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005961 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005962 // We treat a String as an array, creating the HArrayLength from String.length()
5963 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5964 // determine whether a particular HArrayLength is actually a String.length() by
5965 // looking at the type of the input but that requires holding the mutator lock, so
5966 // we prefer to use a flag, so that code generators don't need to do the locking.
5967 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5968 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5969 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5970 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005971};
5972
Vladimir Markofcb503c2016-05-18 12:48:17 +01005973class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005974 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005975 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5976 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005977 HBoundsCheck(HInstruction* index,
5978 HInstruction* length,
5979 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00005980 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305981 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005982 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00005983 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005984 SetRawInputAt(0, index);
5985 SetRawInputAt(1, length);
5986 }
5987
Artem Serovcced8ba2017-07-19 18:18:09 +01005988 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005989 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005990 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005991 return true;
5992 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005993
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005994 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005995
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005996 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005997
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005998 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005999
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006000 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001
6002 DECLARE_INSTRUCTION(BoundsCheck);
6003
Artem Serovcced8ba2017-07-19 18:18:09 +01006004 protected:
6005 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6006
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006007 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006008 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006009};
6010
Vladimir Markofcb503c2016-05-18 12:48:17 +01006011class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006012 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006013 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306014 : HTemplateInstruction(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
6015 slow_path_(nullptr) {
6016 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006017
Artem Serovcced8ba2017-07-19 18:18:09 +01006018 bool IsClonable() const OVERRIDE { return true; }
6019
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006020 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006021 return true;
6022 }
6023
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006024 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6025 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006026
6027 DECLARE_INSTRUCTION(SuspendCheck);
6028
Artem Serovcced8ba2017-07-19 18:18:09 +01006029 protected:
6030 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6031
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006032 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006033 // Only used for code generation, in order to share the same slow path between back edges
6034 // of a same loop.
6035 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006036};
6037
David Srbecky0cf44932015-12-09 14:09:59 +00006038// Pseudo-instruction which provides the native debugger with mapping information.
6039// It ensures that we can generate line number and local variables at this point.
6040class HNativeDebugInfo : public HTemplateInstruction<0> {
6041 public:
6042 explicit HNativeDebugInfo(uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306043 : HTemplateInstruction<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
6044 }
David Srbecky0cf44932015-12-09 14:09:59 +00006045
6046 bool NeedsEnvironment() const OVERRIDE {
6047 return true;
6048 }
6049
6050 DECLARE_INSTRUCTION(NativeDebugInfo);
6051
Artem Serovcced8ba2017-07-19 18:18:09 +01006052 protected:
6053 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006054};
6055
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006056/**
6057 * Instruction to load a Class object.
6058 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006059class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006060 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006061 // Determines how to load the Class.
6062 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006063 // We cannot load this class. See HSharpening::SharpenLoadClass.
6064 kInvalid = -1,
6065
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 // Use the Class* from the method's own ArtMethod*.
6067 kReferrersClass,
6068
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006069 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006070 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006071 kBootImageLinkTimePcRelative,
6072
6073 // Use a known boot image Class* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006074 // Used for boot image classes referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006075 kBootImageAddress,
6076
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006077 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6078 // Used for boot image classes referenced by apps in AOT-compiled code (PIC).
6079 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006080
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006081 // Load from an entry in the .bss section using a PC-relative load.
6082 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6083 kBssEntry,
6084
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006085 // Load from the root table associated with the JIT compiled method.
6086 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006087
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006088 // Load using a simple runtime call. This is the fall-back load kind when
6089 // the codegen is unable to use another appropriate kind.
6090 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006091
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006092 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006093 };
6094
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006095 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006096 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006097 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006098 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006099 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006100 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006101 bool needs_access_check)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306102 : HInstruction(kLoadClass, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006103 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006104 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006105 dex_file_(dex_file),
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006106 klass_(klass),
6107 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006108 // Referrers class should not need access check. We never inline unverified
6109 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006110 DCHECK(!is_referrers_class || !needs_access_check);
6111
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006112 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006113 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006114 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006115 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006117 }
6118
Artem Serovcced8ba2017-07-19 18:18:09 +01006119 bool IsClonable() const OVERRIDE { return true; }
6120
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006121 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006122
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006123 LoadKind GetLoadKind() const {
6124 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006125 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006126
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006127 bool HasPcRelativeLoadKind() const {
6128 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6129 GetLoadKind() == LoadKind::kBootImageRelRo ||
6130 GetLoadKind() == LoadKind::kBssEntry;
6131 }
6132
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006133 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006134
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006135 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006136
Andreas Gampea5b09a62016-11-17 15:21:22 -08006137 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006138
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006139 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006140
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006141 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006142 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006143 }
6144
Calin Juravle0ba218d2015-05-19 18:46:01 +01006145 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006146 // The entrypoint the code generator is going to call does not do
6147 // clinit of the class.
6148 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006149 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006150 }
6151
6152 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006153 return NeedsAccessCheck() ||
6154 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006155 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006156 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006157 }
6158
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006159 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006160 return NeedsAccessCheck() ||
6161 MustGenerateClinitCheck() ||
6162 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6163 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6164 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006165 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006166 GetLoadKind() == LoadKind::kBssEntry) &&
6167 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006168 }
6169
Calin Juravleacf735c2015-02-12 15:25:22 +00006170 ReferenceTypeInfo GetLoadedClassRTI() {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006171 return loaded_class_rti_;
Calin Juravleacf735c2015-02-12 15:25:22 +00006172 }
6173
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006174 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
6175 // Make sure we only set exact types (the loaded class should never be merged).
6176 DCHECK(rti.IsExact());
6177 loaded_class_rti_ = rti;
Calin Juravleacf735c2015-02-12 15:25:22 +00006178 }
6179
Andreas Gampea5b09a62016-11-17 15:21:22 -08006180 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006181 const DexFile& GetDexFile() const { return dex_file_; }
6182
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006183 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006184 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006185 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006186
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006187 static SideEffects SideEffectsForArchRuntimeCalls() {
6188 return SideEffects::CanTriggerGC();
6189 }
6190
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006191 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006192 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006193 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006194 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006195
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006196 void MarkInBootImage() {
6197 SetPackedFlag<kFlagIsInBootImage>(true);
6198 }
6199
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006200 void AddSpecialInput(HInstruction* special_input);
6201
6202 using HInstruction::GetInputRecords; // Keep the const version visible.
6203 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6204 return ArrayRef<HUserRecord<HInstruction*>>(
6205 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6206 }
6207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006208 DataType::Type GetType() const OVERRIDE {
6209 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006210 }
6211
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006212 Handle<mirror::Class> GetClass() const {
6213 return klass_;
6214 }
6215
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006216 DECLARE_INSTRUCTION(LoadClass);
6217
Artem Serovcced8ba2017-07-19 18:18:09 +01006218 protected:
6219 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6220
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006221 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006222 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006223 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006224 // Whether this instruction must generate the initialization check.
6225 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006226 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006227 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6228 static constexpr size_t kFieldLoadKindSize =
6229 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006230 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006231 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006232 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6233
6234 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006235 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006236 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006237 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006238 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006239 }
6240
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006241 void SetLoadKindInternal(LoadKind load_kind);
6242
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006243 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006244 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006245 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006246 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006247
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006248 // A type index and dex file where the class can be accessed. The dex file can be:
6249 // - The compiling method's dex file if the class is defined there too.
6250 // - The compiling method's dex file if the class is referenced there.
6251 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006252 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006253 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006254 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006255
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006256 Handle<mirror::Class> klass_;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006257
6258 ReferenceTypeInfo loaded_class_rti_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006259};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006260std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6261
6262// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006263inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6264 // The load kind should be determined before inserting the instruction to the graph.
6265 DCHECK(GetBlock() == nullptr);
6266 DCHECK(GetEnvironment() == nullptr);
6267 SetPackedField<LoadKindField>(load_kind);
6268 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6269 special_input_ = HUserRecord<HInstruction*>(nullptr);
6270 }
6271 if (!NeedsEnvironment()) {
6272 SetSideEffects(SideEffects::None());
6273 }
6274}
6275
6276// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006277inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006278 // The special input is used for PC-relative loads on some architectures,
6279 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006280 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006281 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006282 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006283 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006284 DCHECK(special_input_.GetInstruction() == nullptr);
6285 special_input_ = HUserRecord<HInstruction*>(special_input);
6286 special_input->AddUseAt(this, 0);
6287}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006288
Vladimir Marko372f10e2016-05-17 16:30:10 +01006289class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006290 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006291 // Determines how to load the String.
6292 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006293 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006294 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006295 kBootImageLinkTimePcRelative,
6296
6297 // Use a known boot image String* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006298 // Used for boot image strings referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006299 kBootImageAddress,
6300
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006301 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6302 // Used for boot image strings referenced by apps in AOT-compiled code (PIC).
6303 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006304
Vladimir Markoaad75c62016-10-03 08:46:48 +00006305 // Load from an entry in the .bss section using a PC-relative load.
6306 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6307 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006308
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006309 // Load from the root table associated with the JIT compiled method.
6310 kJitTableAddress,
6311
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006312 // Load using a simple runtime call. This is the fall-back load kind when
6313 // the codegen is unable to use another appropriate kind.
6314 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006315
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006316 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006317 };
6318
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006319 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006320 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006321 const DexFile& dex_file,
6322 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306323 : HInstruction(kLoadString, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006324 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006325 string_index_(string_index),
6326 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006327 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006328 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006329
Artem Serovcced8ba2017-07-19 18:18:09 +01006330 bool IsClonable() const OVERRIDE { return true; }
6331
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006332 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006333
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006334 LoadKind GetLoadKind() const {
6335 return GetPackedField<LoadKindField>();
6336 }
6337
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006338 bool HasPcRelativeLoadKind() const {
6339 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6340 GetLoadKind() == LoadKind::kBootImageRelRo ||
6341 GetLoadKind() == LoadKind::kBssEntry;
6342 }
6343
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006344 const DexFile& GetDexFile() const {
6345 return dex_file_;
6346 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006347
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006348 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006349 return string_index_;
6350 }
6351
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006352 Handle<mirror::String> GetString() const {
6353 return string_;
6354 }
6355
6356 void SetString(Handle<mirror::String> str) {
6357 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006358 }
6359
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006360 bool CanBeMoved() const OVERRIDE { return true; }
6361
Vladimir Marko372f10e2016-05-17 16:30:10 +01006362 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006363
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006364 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006365
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006366 // Will call the runtime if we need to load the string through
6367 // the dex cache and the string is not guaranteed to be there yet.
6368 bool NeedsEnvironment() const OVERRIDE {
6369 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006370 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006371 load_kind == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006372 load_kind == LoadKind::kBootImageRelRo ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006373 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006374 return false;
6375 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006376 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006377 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006378
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006379 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006380 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006381 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006382
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006383 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006384 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006385
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006386 static SideEffects SideEffectsForArchRuntimeCalls() {
6387 return SideEffects::CanTriggerGC();
6388 }
6389
Vladimir Marko372f10e2016-05-17 16:30:10 +01006390 void AddSpecialInput(HInstruction* special_input);
6391
6392 using HInstruction::GetInputRecords; // Keep the const version visible.
6393 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6394 return ArrayRef<HUserRecord<HInstruction*>>(
6395 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006396 }
6397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006398 DataType::Type GetType() const OVERRIDE {
6399 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006400 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006401
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006402 DECLARE_INSTRUCTION(LoadString);
6403
Artem Serovcced8ba2017-07-19 18:18:09 +01006404 protected:
6405 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6406
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006407 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006408 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006409 static constexpr size_t kFieldLoadKindSize =
6410 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6411 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006412 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006413 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006414
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006415 void SetLoadKindInternal(LoadKind load_kind);
6416
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006417 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006418 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006419 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006420 HUserRecord<HInstruction*> special_input_;
6421
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006422 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006423 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006424
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006425 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006426};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006427std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6428
6429// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006430inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6431 // The load kind should be determined before inserting the instruction to the graph.
6432 DCHECK(GetBlock() == nullptr);
6433 DCHECK(GetEnvironment() == nullptr);
6434 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6435 SetPackedField<LoadKindField>(load_kind);
6436 if (load_kind != LoadKind::kRuntimeCall) {
6437 special_input_ = HUserRecord<HInstruction*>(nullptr);
6438 }
6439 if (!NeedsEnvironment()) {
6440 SetSideEffects(SideEffects::None());
6441 }
6442}
6443
6444// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006445inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006446 // The special input is used for PC-relative loads on some architectures,
6447 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006448 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006449 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006450 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006451 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006452 // HLoadString::GetInputRecords() returns an empty array at this point,
6453 // so use the GetInputRecords() from the base class to set the input record.
6454 DCHECK(special_input_.GetInstruction() == nullptr);
6455 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006456 special_input->AddUseAt(this, 0);
6457}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006458
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006459/**
6460 * Performs an initialization check on its Class object input.
6461 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006462class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006463 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006464 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006465 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306466 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006467 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006468 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006469 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006470 SetRawInputAt(0, constant);
6471 }
6472
Artem Serovcced8ba2017-07-19 18:18:09 +01006473 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006474 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006475 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006476 return true;
6477 }
6478
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006479 bool NeedsEnvironment() const OVERRIDE {
6480 // May call runtime to initialize the class.
6481 return true;
6482 }
6483
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006484 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006485
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006486 HLoadClass* GetLoadClass() const {
6487 DCHECK(InputAt(0)->IsLoadClass());
6488 return InputAt(0)->AsLoadClass();
6489 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006490
6491 DECLARE_INSTRUCTION(ClinitCheck);
6492
Artem Serovcced8ba2017-07-19 18:18:09 +01006493
6494 protected:
6495 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006496};
6497
Vladimir Markofcb503c2016-05-18 12:48:17 +01006498class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006499 public:
6500 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006501 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006502 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006503 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006504 bool is_volatile,
6505 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006506 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006507 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006508 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306509 : HExpression(kStaticFieldGet,
6510 field_type,
6511 SideEffects::FieldReadOfType(field_type, is_volatile),
6512 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006513 field_info_(field,
6514 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006515 field_type,
6516 is_volatile,
6517 field_idx,
6518 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006519 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006520 SetRawInputAt(0, cls);
6521 }
6522
Calin Juravle52c48962014-12-16 17:02:57 +00006523
Artem Serovcced8ba2017-07-19 18:18:09 +01006524 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006525 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006526
Vladimir Marko372f10e2016-05-17 16:30:10 +01006527 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6528 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006529 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006530 }
6531
6532 size_t ComputeHashCode() const OVERRIDE {
6533 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6534 }
6535
Calin Juravle52c48962014-12-16 17:02:57 +00006536 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006537 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006538 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006539 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006540
Vladimir Marko61b92282017-10-11 13:23:17 +01006541 void SetType(DataType::Type new_type) {
6542 DCHECK(DataType::IsIntegralType(GetType()));
6543 DCHECK(DataType::IsIntegralType(new_type));
6544 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6545 SetPackedField<TypeField>(new_type);
6546 }
6547
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006548 DECLARE_INSTRUCTION(StaticFieldGet);
6549
Artem Serovcced8ba2017-07-19 18:18:09 +01006550 protected:
6551 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6552
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006553 private:
6554 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006555};
6556
Vladimir Markofcb503c2016-05-18 12:48:17 +01006557class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006558 public:
6559 HStaticFieldSet(HInstruction* cls,
6560 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006561 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006562 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006563 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006564 bool is_volatile,
6565 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006566 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006567 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006568 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306569 : HTemplateInstruction(kStaticFieldSet,
6570 SideEffects::FieldWriteOfType(field_type, is_volatile),
6571 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006572 field_info_(field,
6573 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006574 field_type,
6575 is_volatile,
6576 field_idx,
6577 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006578 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006579 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006580 SetRawInputAt(0, cls);
6581 SetRawInputAt(1, value);
6582 }
6583
Artem Serovcced8ba2017-07-19 18:18:09 +01006584 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006585 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006586 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006587 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006588 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006589
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006590 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006591 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6592 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006593
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006594 DECLARE_INSTRUCTION(StaticFieldSet);
6595
Artem Serovcced8ba2017-07-19 18:18:09 +01006596 protected:
6597 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6598
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006599 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006600 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6601 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6602 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6603 "Too many packed fields.");
6604
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006605 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006606};
6607
Vladimir Markofcb503c2016-05-18 12:48:17 +01006608class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006609 public:
6610 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006611 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006612 uint32_t field_index,
6613 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306614 : HExpression(kUnresolvedInstanceFieldGet,
6615 field_type,
6616 SideEffects::AllExceptGCDependency(),
6617 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006618 field_index_(field_index) {
6619 SetRawInputAt(0, obj);
6620 }
6621
Artem Serovcced8ba2017-07-19 18:18:09 +01006622 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006623 bool NeedsEnvironment() const OVERRIDE { return true; }
6624 bool CanThrow() const OVERRIDE { return true; }
6625
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006626 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006627 uint32_t GetFieldIndex() const { return field_index_; }
6628
6629 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6630
Artem Serovcced8ba2017-07-19 18:18:09 +01006631 protected:
6632 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6633
Calin Juravlee460d1d2015-09-29 04:52:17 +01006634 private:
6635 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006636};
6637
Vladimir Markofcb503c2016-05-18 12:48:17 +01006638class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006639 public:
6640 HUnresolvedInstanceFieldSet(HInstruction* obj,
6641 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006642 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006643 uint32_t field_index,
6644 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306645 : HTemplateInstruction(kUnresolvedInstanceFieldSet,
6646 SideEffects::AllExceptGCDependency(),
6647 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006648 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006649 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006651 SetRawInputAt(0, obj);
6652 SetRawInputAt(1, value);
6653 }
6654
Artem Serovcced8ba2017-07-19 18:18:09 +01006655 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006656 bool NeedsEnvironment() const OVERRIDE { return true; }
6657 bool CanThrow() const OVERRIDE { return true; }
6658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006659 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006660 uint32_t GetFieldIndex() const { return field_index_; }
6661
6662 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6663
Artem Serovcced8ba2017-07-19 18:18:09 +01006664 protected:
6665 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6666
Calin Juravlee460d1d2015-09-29 04:52:17 +01006667 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006668 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6669 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006670 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006671 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6672 kFieldFieldType + kFieldFieldTypeSize;
6673 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6674 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006675 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006676
Calin Juravlee460d1d2015-09-29 04:52:17 +01006677 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006678};
6679
Vladimir Markofcb503c2016-05-18 12:48:17 +01006680class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006681 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006682 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006683 uint32_t field_index,
6684 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306685 : HExpression(kUnresolvedStaticFieldGet,
6686 field_type,
6687 SideEffects::AllExceptGCDependency(),
6688 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006689 field_index_(field_index) {
6690 }
6691
Artem Serovcced8ba2017-07-19 18:18:09 +01006692 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006693 bool NeedsEnvironment() const OVERRIDE { return true; }
6694 bool CanThrow() const OVERRIDE { return true; }
6695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006696 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006697 uint32_t GetFieldIndex() const { return field_index_; }
6698
6699 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6700
Artem Serovcced8ba2017-07-19 18:18:09 +01006701 protected:
6702 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6703
Calin Juravlee460d1d2015-09-29 04:52:17 +01006704 private:
6705 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006706};
6707
Vladimir Markofcb503c2016-05-18 12:48:17 +01006708class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006709 public:
6710 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006711 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006712 uint32_t field_index,
6713 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306714 : HTemplateInstruction(kUnresolvedStaticFieldSet,
6715 SideEffects::AllExceptGCDependency(),
6716 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006717 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006718 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006719 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006720 SetRawInputAt(0, value);
6721 }
6722
Artem Serovcced8ba2017-07-19 18:18:09 +01006723 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006724 bool NeedsEnvironment() const OVERRIDE { return true; }
6725 bool CanThrow() const OVERRIDE { return true; }
6726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006728 uint32_t GetFieldIndex() const { return field_index_; }
6729
6730 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6731
Artem Serovcced8ba2017-07-19 18:18:09 +01006732 protected:
6733 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6734
Calin Juravlee460d1d2015-09-29 04:52:17 +01006735 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006736 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6737 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006738 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006739 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6740 kFieldFieldType + kFieldFieldTypeSize;
6741 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6742 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006743 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006744
Calin Juravlee460d1d2015-09-29 04:52:17 +01006745 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006746};
6747
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006748// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006749class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006750 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006751 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306752 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6753 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006754
David Brazdilbbd733e2015-08-18 17:48:17 +01006755 bool CanBeNull() const OVERRIDE { return false; }
6756
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006757 DECLARE_INSTRUCTION(LoadException);
6758
Artem Serovcced8ba2017-07-19 18:18:09 +01006759 protected:
6760 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006761};
6762
David Brazdilcb1c0552015-08-04 16:22:25 +01006763// Implicit part of move-exception which clears thread-local exception storage.
6764// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006765class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006766 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006767 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306768 : HTemplateInstruction(kClearException, SideEffects::AllWrites(), dex_pc) {
6769 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006770
6771 DECLARE_INSTRUCTION(ClearException);
6772
Artem Serovcced8ba2017-07-19 18:18:09 +01006773 protected:
6774 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006775};
6776
Vladimir Markofcb503c2016-05-18 12:48:17 +01006777class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006778 public:
6779 HThrow(HInstruction* exception, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306780 : HTemplateInstruction(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006781 SetRawInputAt(0, exception);
6782 }
6783
6784 bool IsControlFlow() const OVERRIDE { return true; }
6785
6786 bool NeedsEnvironment() const OVERRIDE { return true; }
6787
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006788 bool CanThrow() const OVERRIDE { return true; }
6789
Aart Bika8b8e9b2018-01-09 11:01:02 -08006790 bool AlwaysThrows() const OVERRIDE { return true; }
6791
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006792 DECLARE_INSTRUCTION(Throw);
6793
Artem Serovcced8ba2017-07-19 18:18:09 +01006794 protected:
6795 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006796};
6797
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006798/**
6799 * Implementation strategies for the code generator of a HInstanceOf
6800 * or `HCheckCast`.
6801 */
6802enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006803 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006804 kExactCheck, // Can do a single class compare.
6805 kClassHierarchyCheck, // Can just walk the super class chain.
6806 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6807 kInterfaceCheck, // No optimization yet when checking against an interface.
6808 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006809 kArrayCheck, // No optimization yet when checking against a generic array.
6810 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006811};
6812
Roland Levillain86503782016-02-11 19:07:30 +00006813std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6814
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006815class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006816 public:
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006817 HInstanceOf(HInstruction* object,
6818 HLoadClass* target_class,
6819 TypeCheckKind check_kind,
6820 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306821 : HExpression(kInstanceOf,
6822 DataType::Type::kBool,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006823 SideEffectsForArchRuntimeCalls(check_kind),
6824 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006825 SetPackedField<TypeCheckKindField>(check_kind);
6826 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006827 SetRawInputAt(0, object);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006828 SetRawInputAt(1, target_class);
Vladimir Marko87584542017-12-12 17:47:52 +00006829 }
6830
6831 HLoadClass* GetTargetClass() const {
6832 HInstruction* load_class = InputAt(1);
6833 DCHECK(load_class->IsLoadClass());
6834 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006835 }
6836
Artem Serovcced8ba2017-07-19 18:18:09 +01006837 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006838 bool CanBeMoved() const OVERRIDE { return true; }
6839
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006840 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6841 return true;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006842 }
6843
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00006844 bool NeedsEnvironment() const OVERRIDE {
6845 return CanCallRuntime(GetTypeCheckKind());
6846 }
6847
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006848 // Used only in code generation.
6849 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6850 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6851 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6852 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6853
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006854 static bool CanCallRuntime(TypeCheckKind check_kind) {
6855 // Mips currently does runtime calls for any other checks.
6856 return check_kind != TypeCheckKind::kExactCheck;
6857 }
6858
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006859 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006860 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006861 }
6862
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006863 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006864
Artem Serovcced8ba2017-07-19 18:18:09 +01006865 protected:
6866 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006867
6868 private:
6869 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6870 static constexpr size_t kFieldTypeCheckKindSize =
6871 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6872 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6873 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6874 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6875 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006876};
6877
Vladimir Markofcb503c2016-05-18 12:48:17 +01006878class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006879 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006880 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306881 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006882 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6883 SetPackedFlag<kFlagUpperCanBeNull>(true);
6884 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006885 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006886 SetRawInputAt(0, input);
6887 }
6888
Artem Serovcced8ba2017-07-19 18:18:09 +01006889 bool IsClonable() const OVERRIDE { return true; }
6890
David Brazdilf5552582015-12-27 13:36:12 +00006891 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006892 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006893 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006894 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006895
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006896 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006897 DCHECK(GetUpperCanBeNull() || !can_be_null);
6898 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006899 }
6900
Vladimir Markoa1de9182016-02-25 11:37:38 +00006901 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006902
Calin Juravleb1498f62015-02-16 13:13:29 +00006903 DECLARE_INSTRUCTION(BoundType);
6904
Artem Serovcced8ba2017-07-19 18:18:09 +01006905 protected:
6906 DEFAULT_COPY_CONSTRUCTOR(BoundType);
6907
Calin Juravleb1498f62015-02-16 13:13:29 +00006908 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006909 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6910 // is false then CanBeNull() cannot be true).
6911 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6912 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6913 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6914 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6915
Calin Juravleb1498f62015-02-16 13:13:29 +00006916 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006917 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6918 // It is used to bound the type in cases like:
6919 // if (x instanceof ClassX) {
6920 // // uper_bound_ will be ClassX
6921 // }
David Brazdilf5552582015-12-27 13:36:12 +00006922 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006923};
6924
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006925class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006926 public:
6927 HCheckCast(HInstruction* object,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006928 HLoadClass* target_class,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006929 TypeCheckKind check_kind,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006930 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306931 : HTemplateInstruction(kCheckCast, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006932 SetPackedField<TypeCheckKindField>(check_kind);
6933 SetPackedFlag<kFlagMustDoNullCheck>(true);
6934 SetRawInputAt(0, object);
6935 SetRawInputAt(1, target_class);
6936 }
6937
6938 HLoadClass* GetTargetClass() const {
6939 HInstruction* load_class = InputAt(1);
6940 DCHECK(load_class->IsLoadClass());
6941 return load_class->AsLoadClass();
6942 }
6943
6944 bool IsClonable() const OVERRIDE { return true; }
6945 bool CanBeMoved() const OVERRIDE { return true; }
6946
6947 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6948 return true;
6949 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006950
6951 bool NeedsEnvironment() const OVERRIDE {
6952 // Instruction may throw a CheckCastError.
6953 return true;
6954 }
6955
6956 bool CanThrow() const OVERRIDE { return true; }
6957
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006958 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6959 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6960 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6961 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6962
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006963 DECLARE_INSTRUCTION(CheckCast);
6964
Artem Serovcced8ba2017-07-19 18:18:09 +01006965 protected:
6966 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006967
6968 private:
6969 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6970 static constexpr size_t kFieldTypeCheckKindSize =
6971 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6972 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6973 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6974 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6975 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006976};
6977
Andreas Gampe26de38b2016-07-27 17:53:11 -07006978/**
6979 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6980 * @details We define the combined barrier types that are actually required
6981 * by the Java Memory Model, rather than using exactly the terminology from
6982 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6983 * primitives. Note that the JSR-133 cookbook generally does not deal with
6984 * store atomicity issues, and the recipes there are not always entirely sufficient.
6985 * The current recipe is as follows:
6986 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6987 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6988 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6989 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6990 * class has final fields.
6991 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6992 * store-to-memory instructions. Only generated together with non-temporal stores.
6993 */
6994enum MemBarrierKind {
6995 kAnyStore,
6996 kLoadAny,
6997 kStoreStore,
6998 kAnyAny,
6999 kNTStoreStore,
7000 kLastBarrierKind = kNTStoreStore
7001};
7002std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7003
Vladimir Markofcb503c2016-05-18 12:48:17 +01007004class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007005 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007006 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07007007 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307008 kMemoryBarrier,
7009 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7010 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007011 SetPackedField<BarrierKindField>(barrier_kind);
7012 }
Calin Juravle27df7582015-04-17 19:12:31 +01007013
Artem Serovcced8ba2017-07-19 18:18:09 +01007014 bool IsClonable() const OVERRIDE { return true; }
7015
Vladimir Markoa1de9182016-02-25 11:37:38 +00007016 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007017
7018 DECLARE_INSTRUCTION(MemoryBarrier);
7019
Artem Serovcced8ba2017-07-19 18:18:09 +01007020 protected:
7021 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7022
Calin Juravle27df7582015-04-17 19:12:31 +01007023 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007024 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7025 static constexpr size_t kFieldBarrierKindSize =
7026 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7027 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7028 kFieldBarrierKind + kFieldBarrierKindSize;
7029 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7030 "Too many packed fields.");
7031 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007032};
7033
Igor Murashkind01745e2017-04-05 16:40:31 -07007034// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7035// the specified object(s), with respect to any subsequent store that might "publish"
7036// (i.e. make visible) the specified object to another thread.
7037//
7038// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7039// for all final fields (that were set) at the end of the invoked constructor.
7040//
7041// The constructor fence models the freeze actions for the final fields of an object
7042// being constructed (semantically at the end of the constructor). Constructor fences
7043// have a per-object affinity; two separate objects being constructed get two separate
7044// constructor fences.
7045//
7046// (Note: that if calling a super-constructor or forwarding to another constructor,
7047// the freezes would happen at the end of *that* constructor being invoked).
7048//
7049// The memory model guarantees that when the object being constructed is "published" after
7050// constructor completion (i.e. escapes the current thread via a store), then any final field
7051// writes must be observable on other threads (once they observe that publication).
7052//
7053// Further, anything written before the freeze, and read by dereferencing through the final field,
7054// must also be visible (so final object field could itself have an object with non-final fields;
7055// yet the freeze must also extend to them).
7056//
7057// Constructor example:
7058//
7059// class HasFinal {
7060// final int field; Optimizing IR for <init>()V:
7061// HasFinal() {
7062// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7063// // freeze(this.field); HConstructorFence(this)
7064// } HReturn
7065// }
7066//
7067// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7068// already-initialized classes; in that case the allocation must act as a "default-initializer"
7069// of the object which effectively writes the class pointer "final field".
7070//
7071// For example, we can model default-initialiation as roughly the equivalent of the following:
7072//
7073// class Object {
7074// private final Class header;
7075// }
7076//
7077// Java code: Optimizing IR:
7078//
7079// T new_instance<T>() {
7080// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7081// obj.header = T.class; // header write is done by above call.
7082// // freeze(obj.header) HConstructorFence(obj)
7083// return (T)obj;
7084// }
7085//
7086// See also:
7087// * CompilerDriver::RequiresConstructorBarrier
7088// * QuasiAtomic::ThreadFenceForConstructor
7089//
7090class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7091 // A fence has variable inputs because the inputs can be removed
7092 // after prepare_for_register_allocation phase.
7093 // (TODO: In the future a fence could freeze multiple objects
7094 // after merging two fences together.)
7095 public:
7096 // `fence_object` is the reference that needs to be protected for correct publication.
7097 //
7098 // It makes sense in the following situations:
7099 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7100 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7101 //
7102 // After construction the `fence_object` becomes the 0th input.
7103 // This is not an input in a real sense, but just a convenient place to stash the information
7104 // about the associated object.
7105 HConstructorFence(HInstruction* fence_object,
7106 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007107 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007108 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7109 // constraints described in the class header. We claim that these SideEffects constraints
7110 // enforce a superset of the real constraints.
7111 //
7112 // The ordering described above is conservatively modeled with SideEffects as follows:
7113 //
7114 // * To prevent reordering of the publication stores:
7115 // ----> "Reads of objects" is the initial SideEffect.
7116 // * For every primitive final field store in the constructor:
7117 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7118 // * If there are any stores to reference final fields in the constructor:
7119 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7120 // that are reachable from `fence_object` also need to be prevented for reordering
7121 // (and we do not want to do alias analysis to figure out what those stores are).
7122 //
7123 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7124 // even more conservative approach than the one described above, and prevents all of the
7125 // above reordering without analyzing any of the instructions in the constructor.
7126 //
7127 // If in a later phase we discover that there are no writes to reference final fields,
7128 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307129 : HVariableInputSizeInstruction(kConstructorFence,
7130 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007131 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007132 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007133 /* number_of_inputs */ 1,
7134 kArenaAllocConstructorFenceInputs) {
7135 DCHECK(fence_object != nullptr);
7136 SetRawInputAt(0, fence_object);
7137 }
7138
7139 // The object associated with this constructor fence.
7140 //
7141 // (Note: This will be null after the prepare_for_register_allocation phase,
7142 // as all constructor fence inputs are removed there).
7143 HInstruction* GetFenceObject() const {
7144 return InputAt(0);
7145 }
7146
7147 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7148 // - Delete `fence_use` from `this`'s use list.
7149 // - Delete `this` from `fence_use`'s inputs list.
7150 // - If the `fence_use` is dead, remove it from the graph.
7151 //
7152 // A fence is considered dead once it no longer has any uses
7153 // and all of the inputs are dead.
7154 //
7155 // This must *not* be called during/after prepare_for_register_allocation,
7156 // because that removes all the inputs to the fences but the fence is actually
7157 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007158 //
7159 // Returns how many HConstructorFence instructions were removed from graph.
7160 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007161
Igor Murashkindd018df2017-08-09 10:38:31 -07007162 // Combine all inputs of `this` and `other` instruction and remove
7163 // `other` from the graph.
7164 //
7165 // Inputs are unique after the merge.
7166 //
7167 // Requirement: `this` must not be the same as `other.
7168 void Merge(HConstructorFence* other);
7169
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007170 // Check if this constructor fence is protecting
7171 // an HNewInstance or HNewArray that is also the immediate
7172 // predecessor of `this`.
7173 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007174 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7175 // to be one of the inputs of `this`.
7176 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007177 // Returns the associated HNewArray or HNewInstance,
7178 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007179 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007180
Igor Murashkind01745e2017-04-05 16:40:31 -07007181 DECLARE_INSTRUCTION(ConstructorFence);
7182
Artem Serovcced8ba2017-07-19 18:18:09 +01007183 protected:
7184 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007185};
7186
Vladimir Markofcb503c2016-05-18 12:48:17 +01007187class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007188 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007189 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007190 kEnter,
7191 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007192 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007193 };
7194
7195 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007196 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307197 kMonitorOperation,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007198 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7199 dex_pc) {
7200 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007201 SetRawInputAt(0, object);
7202 }
7203
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007204 // Instruction may go into runtime, so we need an environment.
7205 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007206
7207 bool CanThrow() const OVERRIDE {
7208 // Verifier guarantees that monitor-exit cannot throw.
7209 // This is important because it allows the HGraphBuilder to remove
7210 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7211 return IsEnter();
7212 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007213
Vladimir Markoa1de9182016-02-25 11:37:38 +00007214 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7215 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007216
7217 DECLARE_INSTRUCTION(MonitorOperation);
7218
Artem Serovcced8ba2017-07-19 18:18:09 +01007219 protected:
7220 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7221
Calin Juravle52c48962014-12-16 17:02:57 +00007222 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007223 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7224 static constexpr size_t kFieldOperationKindSize =
7225 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7226 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7227 kFieldOperationKind + kFieldOperationKindSize;
7228 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7229 "Too many packed fields.");
7230 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007231};
7232
Vladimir Markofcb503c2016-05-18 12:48:17 +01007233class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007234 public:
7235 HSelect(HInstruction* condition,
7236 HInstruction* true_value,
7237 HInstruction* false_value,
7238 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307239 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007240 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7241
7242 // First input must be `true_value` or `false_value` to allow codegens to
7243 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7244 // that architectures which implement HSelect as a conditional move also
7245 // will not need to invert the condition.
7246 SetRawInputAt(0, false_value);
7247 SetRawInputAt(1, true_value);
7248 SetRawInputAt(2, condition);
7249 }
7250
Artem Serovcced8ba2017-07-19 18:18:09 +01007251 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007252 HInstruction* GetFalseValue() const { return InputAt(0); }
7253 HInstruction* GetTrueValue() const { return InputAt(1); }
7254 HInstruction* GetCondition() const { return InputAt(2); }
7255
7256 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007257 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7258 return true;
7259 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007260
7261 bool CanBeNull() const OVERRIDE {
7262 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7263 }
7264
7265 DECLARE_INSTRUCTION(Select);
7266
Artem Serovcced8ba2017-07-19 18:18:09 +01007267 protected:
7268 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007269};
7270
Vladimir Markof9f64412015-09-02 14:05:49 +01007271class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007272 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007273 MoveOperands(Location source,
7274 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007275 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007276 HInstruction* instruction)
7277 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007278
7279 Location GetSource() const { return source_; }
7280 Location GetDestination() const { return destination_; }
7281
7282 void SetSource(Location value) { source_ = value; }
7283 void SetDestination(Location value) { destination_ = value; }
7284
7285 // The parallel move resolver marks moves as "in-progress" by clearing the
7286 // destination (but not the source).
7287 Location MarkPending() {
7288 DCHECK(!IsPending());
7289 Location dest = destination_;
7290 destination_ = Location::NoLocation();
7291 return dest;
7292 }
7293
7294 void ClearPending(Location dest) {
7295 DCHECK(IsPending());
7296 destination_ = dest;
7297 }
7298
7299 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007300 DCHECK(source_.IsValid() || destination_.IsInvalid());
7301 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007302 }
7303
7304 // True if this blocks a move from the given location.
7305 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007306 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007307 }
7308
7309 // A move is redundant if it's been eliminated, if its source and
7310 // destination are the same, or if its destination is unneeded.
7311 bool IsRedundant() const {
7312 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7313 }
7314
7315 // We clear both operands to indicate move that's been eliminated.
7316 void Eliminate() {
7317 source_ = destination_ = Location::NoLocation();
7318 }
7319
7320 bool IsEliminated() const {
7321 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7322 return source_.IsInvalid();
7323 }
7324
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007325 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007326
Nicolas Geoffray90218252015-04-15 11:56:51 +01007327 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007328 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007329 }
7330
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007331 HInstruction* GetInstruction() const { return instruction_; }
7332
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007333 private:
7334 Location source_;
7335 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007336 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007337 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007338 // The instruction this move is assocatied with. Null when this move is
7339 // for moving an input in the expected locations of user (including a phi user).
7340 // This is only used in debug mode, to ensure we do not connect interval siblings
7341 // in the same parallel move.
7342 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007343};
7344
Roland Levillainc9285912015-12-18 10:38:42 +00007345std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7346
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007347static constexpr size_t kDefaultNumberOfMoves = 4;
7348
Vladimir Markofcb503c2016-05-18 12:48:17 +01007349class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007350 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007351 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307352 : HTemplateInstruction(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007353 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007354 moves_.reserve(kDefaultNumberOfMoves);
7355 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007356
Nicolas Geoffray90218252015-04-15 11:56:51 +01007357 void AddMove(Location source,
7358 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007359 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007360 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007361 DCHECK(source.IsValid());
7362 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007363 if (kIsDebugBuild) {
7364 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007365 for (const MoveOperands& move : moves_) {
7366 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007367 // Special case the situation where the move is for the spill slot
7368 // of the instruction.
7369 if ((GetPrevious() == instruction)
7370 || ((GetPrevious() == nullptr)
7371 && instruction->IsPhi()
7372 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007373 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007374 << "Doing parallel moves for the same instruction.";
7375 } else {
7376 DCHECK(false) << "Doing parallel moves for the same instruction.";
7377 }
7378 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007379 }
7380 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007381 for (const MoveOperands& move : moves_) {
7382 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007383 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007384 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007385 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007386 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007387 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007388 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007389 }
7390
Vladimir Marko225b6462015-09-28 12:17:40 +01007391 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007392 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007393 }
7394
Vladimir Marko225b6462015-09-28 12:17:40 +01007395 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007396
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007397 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007398
Artem Serovcced8ba2017-07-19 18:18:09 +01007399 protected:
7400 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7401
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007402 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007403 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007404};
7405
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007406// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7407// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7408// never used across anything that can trigger GC.
7409// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7410// So we represent it by the type `DataType::Type::kInt`.
7411class HIntermediateAddress FINAL : public HExpression<2> {
7412 public:
7413 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307414 : HExpression(kIntermediateAddress,
7415 DataType::Type::kInt32,
7416 SideEffects::DependsOnGC(),
7417 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007418 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7419 DataType::Size(DataType::Type::kReference))
7420 << "kPrimInt and kPrimNot have different sizes.";
7421 SetRawInputAt(0, base_address);
7422 SetRawInputAt(1, offset);
7423 }
7424
Artem Serovcced8ba2017-07-19 18:18:09 +01007425 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007426 bool CanBeMoved() const OVERRIDE { return true; }
7427 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7428 return true;
7429 }
7430 bool IsActualObject() const OVERRIDE { return false; }
7431
7432 HInstruction* GetBaseAddress() const { return InputAt(0); }
7433 HInstruction* GetOffset() const { return InputAt(1); }
7434
7435 DECLARE_INSTRUCTION(IntermediateAddress);
7436
Artem Serovcced8ba2017-07-19 18:18:09 +01007437 protected:
7438 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007439};
7440
7441
Mark Mendell0616ae02015-04-17 12:49:27 -04007442} // namespace art
7443
Aart Bikf8f5a162017-02-06 15:35:29 -08007444#include "nodes_vector.h"
7445
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007446#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7447#include "nodes_shared.h"
7448#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007449#ifdef ART_ENABLE_CODEGEN_mips
7450#include "nodes_mips.h"
7451#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007452#ifdef ART_ENABLE_CODEGEN_x86
7453#include "nodes_x86.h"
7454#endif
7455
7456namespace art {
7457
Igor Murashkin6ef45672017-08-08 13:59:55 -07007458class OptimizingCompilerStats;
7459
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007460class HGraphVisitor : public ValueObject {
7461 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007462 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7463 : stats_(stats),
7464 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007465 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007466
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007467 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007468 virtual void VisitBasicBlock(HBasicBlock* block);
7469
Roland Levillain633021e2014-10-01 14:12:25 +01007470 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007471 void VisitInsertionOrder();
7472
Roland Levillain633021e2014-10-01 14:12:25 +01007473 // Visit the graph following dominator tree reverse post-order.
7474 void VisitReversePostOrder();
7475
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007476 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007477
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007478 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007479#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007480 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7481
7482 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7483
7484#undef DECLARE_VISIT_INSTRUCTION
7485
Igor Murashkin6ef45672017-08-08 13:59:55 -07007486 protected:
7487 OptimizingCompilerStats* stats_;
7488
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007489 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007490 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007491
7492 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7493};
7494
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007495class HGraphDelegateVisitor : public HGraphVisitor {
7496 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007497 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7498 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007499 virtual ~HGraphDelegateVisitor() {}
7500
7501 // Visit functions that delegate to to super class.
7502#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007503 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007504
7505 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7506
7507#undef DECLARE_VISIT_INSTRUCTION
7508
7509 private:
7510 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7511};
7512
Artem Serovcced8ba2017-07-19 18:18:09 +01007513// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7514// and remove the old instruction.
7515HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7516
7517// Create a clone for each clonable instructions/phis and replace the original with the clone.
7518//
7519// Used for testing individual instruction cloner.
7520class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7521 public:
7522 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007523 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007524
7525 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7526 if (instruction->IsClonable()) {
7527 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007528 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007529 }
7530 }
7531
Artem Serov7f4aff62017-06-21 17:02:18 +01007532 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007533
7534 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007535 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007536
7537 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7538};
7539
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007540// Iterator over the blocks that art part of the loop. Includes blocks part
7541// of an inner loop. The order in which the blocks are iterated is on their
7542// block id.
7543class HBlocksInLoopIterator : public ValueObject {
7544 public:
7545 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7546 : blocks_in_loop_(info.GetBlocks()),
7547 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7548 index_(0) {
7549 if (!blocks_in_loop_.IsBitSet(index_)) {
7550 Advance();
7551 }
7552 }
7553
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007554 bool Done() const { return index_ == blocks_.size(); }
7555 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007556 void Advance() {
7557 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007558 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007559 if (blocks_in_loop_.IsBitSet(index_)) {
7560 break;
7561 }
7562 }
7563 }
7564
7565 private:
7566 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007567 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007568 size_t index_;
7569
7570 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7571};
7572
Mingyao Yang3584bce2015-05-19 16:01:59 -07007573// Iterator over the blocks that art part of the loop. Includes blocks part
7574// of an inner loop. The order in which the blocks are iterated is reverse
7575// post order.
7576class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7577 public:
7578 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7579 : blocks_in_loop_(info.GetBlocks()),
7580 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7581 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007582 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007583 Advance();
7584 }
7585 }
7586
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007587 bool Done() const { return index_ == blocks_.size(); }
7588 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007589 void Advance() {
7590 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007591 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7592 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007593 break;
7594 }
7595 }
7596 }
7597
7598 private:
7599 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007600 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007601 size_t index_;
7602
7603 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7604};
7605
Aart Bikf3e61ee2017-04-12 17:09:20 -07007606// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007607inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007608 if (constant->IsIntConstant()) {
7609 return constant->AsIntConstant()->GetValue();
7610 } else if (constant->IsLongConstant()) {
7611 return constant->AsLongConstant()->GetValue();
7612 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007613 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007614 return 0;
7615 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007616}
7617
Aart Bikf3e61ee2017-04-12 17:09:20 -07007618// Returns true iff instruction is an integral constant (and sets value on success).
7619inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7620 if (instruction->IsIntConstant()) {
7621 *value = instruction->AsIntConstant()->GetValue();
7622 return true;
7623 } else if (instruction->IsLongConstant()) {
7624 *value = instruction->AsLongConstant()->GetValue();
7625 return true;
7626 } else if (instruction->IsNullConstant()) {
7627 *value = 0;
7628 return true;
7629 }
7630 return false;
7631}
7632
Aart Bik0148de42017-09-05 09:25:01 -07007633// Returns true iff instruction is the given integral constant.
7634inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7635 int64_t val = 0;
7636 return IsInt64AndGet(instruction, &val) && val == value;
7637}
7638
7639// Returns true iff instruction is a zero bit pattern.
7640inline bool IsZeroBitPattern(HInstruction* instruction) {
7641 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7642}
7643
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007644#define INSTRUCTION_TYPE_CHECK(type, super) \
7645 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7646 inline const H##type* HInstruction::As##type() const { \
7647 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7648 } \
7649 inline H##type* HInstruction::As##type() { \
7650 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7651 }
7652
7653 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7654#undef INSTRUCTION_TYPE_CHECK
7655
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007656// Create space in `blocks` for adding `number_of_new_blocks` entries
7657// starting at location `at`. Blocks after `at` are moved accordingly.
7658inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7659 size_t number_of_new_blocks,
7660 size_t after) {
7661 DCHECK_LT(after, blocks->size());
7662 size_t old_size = blocks->size();
7663 size_t new_size = old_size + number_of_new_blocks;
7664 blocks->resize(new_size);
7665 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7666}
7667
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007668/*
7669 * Hunt "under the hood" of array lengths (leading to array references),
7670 * null checks (also leading to array references), and new arrays
7671 * (leading to the actual length). This makes it more likely related
7672 * instructions become actually comparable.
7673 */
7674inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7675 while (instruction->IsArrayLength() ||
7676 instruction->IsNullCheck() ||
7677 instruction->IsNewArray()) {
7678 instruction = instruction->IsNewArray()
7679 ? instruction->AsNewArray()->GetLength()
7680 : instruction->InputAt(0);
7681 }
7682 return instruction;
7683}
7684
Artem Serov21c7e6f2017-07-27 16:04:42 +01007685void RemoveEnvironmentUses(HInstruction* instruction);
7686bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7687void ResetEnvironmentInputRecords(HInstruction* instruction);
7688
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007689} // namespace art
7690
7691#endif // ART_COMPILER_OPTIMIZING_NODES_H_