blob: 48fb611da21bf7bca845203b4bbe16cc6dde5a68 [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"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080029#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080030#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000031#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010032#include "base/transform_array_ref.h"
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +010033#include "art_method.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010034#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080036#include "dex/dex_file.h"
37#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080038#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070039#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000040#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000041#include "handle.h"
42#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010043#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010044#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000045#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010046#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010047#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010048#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049
50namespace art {
51
Vladimir Markoca6fff82017-10-03 14:49:14 +010052class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000053class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070055class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010056class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010058class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000060class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000063class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000064class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000065class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000066class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070067class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010068class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010069class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010070class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010071class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000072class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010073class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000074class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075
Mathieu Chartier736b5602015-09-02 14:54:11 -070076namespace mirror {
77class DexCache;
78} // namespace mirror
79
Nicolas Geoffray818f2102014-02-18 16:43:35 +000080static const int kDefaultNumberOfBlocks = 8;
81static const int kDefaultNumberOfSuccessors = 2;
82static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010083static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010084static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000086
Roland Levillain5b5b9312016-03-22 14:57:31 +000087// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
88static constexpr int32_t kMaxIntShiftDistance = 0x1f;
89// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
90static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000091
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010092static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070093static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010094
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010095static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
96
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060097static constexpr uint32_t kNoDexPc = -1;
98
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010099inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
100 // For the purposes of the compiler, the dex files must actually be the same object
101 // if we want to safely treat them as the same. This is especially important for JIT
102 // as custom class loaders can open the same underlying file (or memory) multiple
103 // times and provide different class resolution but no two class loaders should ever
104 // use the same DexFile object - doing so is an unsupported hack that can lead to
105 // all sorts of weird failures.
106 return &lhs == &rhs;
107}
108
Dave Allison20dfc792014-06-16 20:44:29 -0700109enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700110 // All types.
111 kCondEQ, // ==
112 kCondNE, // !=
113 // Signed integers and floating-point numbers.
114 kCondLT, // <
115 kCondLE, // <=
116 kCondGT, // >
117 kCondGE, // >=
118 // Unsigned integers.
119 kCondB, // <
120 kCondBE, // <=
121 kCondA, // >
122 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100123 // First and last aliases.
124 kCondFirst = kCondEQ,
125 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700126};
127
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000128enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000129 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000130 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000131 kAnalysisFailThrowCatchLoop,
132 kAnalysisFailAmbiguousArrayOp,
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100133 kAnalysisFailIrreducibleLoopAndStringInit,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000134 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000135};
136
Andreas Gampe9186ced2016-12-12 14:28:21 -0800137template <typename T>
138static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
139 return static_cast<typename std::make_unsigned<T>::type>(x);
140}
141
Vladimir Markof9f64412015-09-02 14:05:49 +0100142class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100143 public:
144 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
145
146 void AddInstruction(HInstruction* instruction);
147 void RemoveInstruction(HInstruction* instruction);
148
David Brazdilc3d743f2015-04-22 13:40:50 +0100149 // Insert `instruction` before/after an existing instruction `cursor`.
150 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
151 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
152
Roland Levillain6b469232014-09-25 10:10:38 +0100153 // Return true if this list contains `instruction`.
154 bool Contains(HInstruction* instruction) const;
155
Roland Levillainccc07a92014-09-16 14:48:16 +0100156 // Return true if `instruction1` is found before `instruction2` in
157 // this instruction list and false otherwise. Abort if none
158 // of these instructions is found.
159 bool FoundBefore(const HInstruction* instruction1,
160 const HInstruction* instruction2) const;
161
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000162 bool IsEmpty() const { return first_instruction_ == nullptr; }
163 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
164
165 // Update the block of all instructions to be `block`.
166 void SetBlockOfInstructions(HBasicBlock* block) const;
167
168 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000169 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000170 void Add(const HInstructionList& instruction_list);
171
David Brazdil2d7352b2015-04-20 14:52:42 +0100172 // Return the number of instructions in the list. This is an expensive operation.
173 size_t CountSize() const;
174
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100175 private:
176 HInstruction* first_instruction_;
177 HInstruction* last_instruction_;
178
179 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 friend class HGraph;
181 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000183 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100184 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100185
186 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
187};
188
David Brazdil4833f5a2015-12-16 10:37:39 +0000189class ReferenceTypeInfo : ValueObject {
190 public:
191 typedef Handle<mirror::Class> TypeHandle;
192
Vladimir Markoa1de9182016-02-25 11:37:38 +0000193 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
194
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700195 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100196 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
197 }
198
Vladimir Markoa1de9182016-02-25 11:37:38 +0000199 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 return ReferenceTypeInfo(type_handle, is_exact);
201 }
202
203 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
204
Vladimir Markof39745e2016-01-26 12:16:55 +0000205 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000206 return handle.GetReference() != nullptr;
207 }
208
Vladimir Marko456307a2016-04-19 14:12:13 +0000209 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000210 return IsValidHandle(type_handle_);
211 }
212
213 bool IsExact() const { return is_exact_; }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return GetTypeHandle()->IsObjectClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return GetTypeHandle()->IsStringClass();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsInterface();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsArrayClass();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 return GetTypeHandle()->IsPrimitiveArray();
243 }
244
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700245 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 DCHECK(IsValid());
247 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
248 }
249
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000251 DCHECK(IsValid());
252 if (!IsExact()) return false;
253 if (!IsArrayClass()) return false;
254 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
255 }
256
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700257 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000258 DCHECK(IsValid());
259 if (!IsExact()) return false;
260 if (!IsArrayClass()) return false;
261 if (!rti.IsArrayClass()) return false;
262 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
263 rti.GetTypeHandle()->GetComponentType());
264 }
265
266 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
267
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000269 DCHECK(IsValid());
270 DCHECK(rti.IsValid());
271 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
272 }
273
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000275 DCHECK(IsValid());
276 DCHECK(rti.IsValid());
277 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
278 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
279 }
280
281 // Returns true if the type information provide the same amount of details.
282 // Note that it does not mean that the instructions have the same actual type
283 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700284 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000285 if (!IsValid() && !rti.IsValid()) {
286 // Invalid types are equal.
287 return true;
288 }
289 if (!IsValid() || !rti.IsValid()) {
290 // One is valid, the other not.
291 return false;
292 }
293 return IsExact() == rti.IsExact()
294 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
295 }
296
297 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000298 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
299 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
300 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000301
302 // The class of the object.
303 TypeHandle type_handle_;
304 // Whether or not the type is exact or a superclass of the actual type.
305 // Whether or not we have any information about this type.
306 bool is_exact_;
307};
308
309std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
310
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100312class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000313 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100314 HGraph(ArenaAllocator* allocator,
315 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100316 const DexFile& dex_file,
317 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100319 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100320 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000321 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100322 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100323 : allocator_(allocator),
324 arena_stack_(arena_stack),
325 blocks_(allocator->Adapter(kArenaAllocBlockList)),
326 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
327 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700328 entry_block_(nullptr),
329 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100330 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100331 number_of_vregs_(0),
332 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000333 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400334 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000335 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700336 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800337 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000338 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000339 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000340 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100341 dex_file_(dex_file),
342 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100343 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100344 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800345 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700346 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000347 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100348 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
349 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
350 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
351 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000352 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100353 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000354 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700355 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100356 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100357 blocks_.reserve(kDefaultNumberOfBlocks);
358 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359
David Brazdilbadd8262016-02-02 16:28:56 +0000360 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700361 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000362
Vladimir Markoca6fff82017-10-03 14:49:14 +0100363 ArenaAllocator* GetAllocator() const { return allocator_; }
364 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100365 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
366
David Brazdil69ba7b72015-06-23 18:27:30 +0100367 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000368 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100369
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000370 HBasicBlock* GetEntryBlock() const { return entry_block_; }
371 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100372 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000373
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000374 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
375 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000376
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000377 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100378
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100379 void ComputeDominanceInformation();
380 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000381 void ClearLoopInformation();
382 void FindBackEdges(ArenaBitVector* visited);
383 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100384 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100385 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000386
David Brazdil4833f5a2015-12-16 10:37:39 +0000387 // Analyze all natural loops in this graph. Returns a code specifying that it
388 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000389 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000390 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100391
David Brazdilffee3d32015-07-06 11:48:53 +0100392 // Iterate over blocks to compute try block membership. Needs reverse post
393 // order and loop information.
394 void ComputeTryBlockInformation();
395
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000397 // Returns the instruction to replace the invoke expression or null if the
398 // invoke is for a void method. Note that the caller is responsible for replacing
399 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000400 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000401
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000402 // Update the loop and try membership of `block`, which was spawned from `reference`.
403 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
404 // should be the new back edge.
405 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
406 HBasicBlock* reference,
407 bool replace_if_back_edge);
408
Mingyao Yang3584bce2015-05-19 16:01:59 -0700409 // Need to add a couple of blocks to test if the loop body is entered and
410 // put deoptimization instructions, etc.
411 void TransformLoopHeaderForBCE(HBasicBlock* header);
412
Aart Bikf8f5a162017-02-06 15:35:29 -0800413 // Adds a new loop directly after the loop with the given header and exit.
414 // Returns the new preheader.
415 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
416 HBasicBlock* body,
417 HBasicBlock* exit);
418
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000419 // Removes `block` from the graph. Assumes `block` has been disconnected from
420 // other blocks and has no instructions or phis.
421 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000422
David Brazdilfc6a86a2015-06-26 10:33:45 +0000423 // Splits the edge between `block` and `successor` while preserving the
424 // indices in the predecessor/successor lists. If there are multiple edges
425 // between the blocks, the lowest indices are used.
426 // Returns the new block which is empty and has the same dex pc as `successor`.
427 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
428
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100429 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100430 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000431
432 // Transform a loop into a format with a single preheader.
433 //
434 // Each phi in the header should be split: original one in the header should only hold
435 // inputs reachable from the back edges and a single input from the preheader. The newly created
436 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
437 //
438 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
439 // that no longer have this property.
440 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
441
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100442 void SimplifyLoop(HBasicBlock* header);
443
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000444 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100445 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000446 return current_instruction_id_++;
447 }
448
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000449 int32_t GetCurrentInstructionId() const {
450 return current_instruction_id_;
451 }
452
453 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100454 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000455 current_instruction_id_ = id;
456 }
457
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100458 uint16_t GetMaximumNumberOfOutVRegs() const {
459 return maximum_number_of_out_vregs_;
460 }
461
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000462 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
463 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100464 }
465
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100466 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
467 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
468 }
469
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000470 void UpdateTemporariesVRegSlots(size_t slots) {
471 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100472 }
473
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000474 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100475 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000476 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100477 }
478
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100479 void SetNumberOfVRegs(uint16_t number_of_vregs) {
480 number_of_vregs_ = number_of_vregs;
481 }
482
483 uint16_t GetNumberOfVRegs() const {
484 return number_of_vregs_;
485 }
486
487 void SetNumberOfInVRegs(uint16_t value) {
488 number_of_in_vregs_ = value;
489 }
490
David Brazdildee58d62016-04-07 09:54:26 +0000491 uint16_t GetNumberOfInVRegs() const {
492 return number_of_in_vregs_;
493 }
494
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100495 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100496 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100497 return number_of_vregs_ - number_of_in_vregs_;
498 }
499
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100500 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100501 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100502 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100503
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100504 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
505 DCHECK(GetReversePostOrder()[0] == entry_block_);
506 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
507 }
508
509 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
510 return ReverseRange(GetReversePostOrder());
511 }
512
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100513 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100514 return linear_order_;
515 }
516
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100517 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
518 return ReverseRange(GetLinearOrder());
519 }
520
Mark Mendell1152c922015-04-24 17:06:35 -0400521 bool HasBoundsChecks() const {
522 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800523 }
524
Mark Mendell1152c922015-04-24 17:06:35 -0400525 void SetHasBoundsChecks(bool value) {
526 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800527 }
528
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000529 bool IsDebuggable() const { return debuggable_; }
530
David Brazdil8d5b8b22015-03-24 10:51:52 +0000531 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000532 // already, it is created and inserted into the graph. This method is only for
533 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100534 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000535
536 // TODO: This is problematic for the consistency of reference type propagation
537 // because it can be created anytime after the pass and thus it will be left
538 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000540
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600541 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
542 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000543 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600544 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
545 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000546 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600547 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
548 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000549 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600550 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
551 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000552 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000553
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100554 HCurrentMethod* GetCurrentMethod();
555
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100556 const DexFile& GetDexFile() const {
557 return dex_file_;
558 }
559
560 uint32_t GetMethodIdx() const {
561 return method_idx_;
562 }
563
Igor Murashkind01745e2017-04-05 16:40:31 -0700564 // Get the method name (without the signature), e.g. "<init>"
565 const char* GetMethodName() const;
566
567 // Get the pretty method name (class + name + optionally signature).
568 std::string PrettyMethod(bool with_signature = true) const;
569
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100570 InvokeType GetInvokeType() const {
571 return invoke_type_;
572 }
573
Mark Mendellc4701932015-04-10 13:18:51 -0400574 InstructionSet GetInstructionSet() const {
575 return instruction_set_;
576 }
577
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000578 bool IsCompilingOsr() const { return osr_; }
579
Mingyao Yang063fc772016-08-02 11:02:54 -0700580 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
581 return cha_single_implementation_list_;
582 }
583
584 void AddCHASingleImplementationDependency(ArtMethod* method) {
585 cha_single_implementation_list_.insert(method);
586 }
587
588 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800589 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700590 }
591
David Brazdil77a48ae2015-09-15 12:34:04 +0000592 bool HasTryCatch() const { return has_try_catch_; }
593 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100594
Aart Bikb13c65b2017-03-21 20:14:07 -0700595 bool HasSIMD() const { return has_simd_; }
596 void SetHasSIMD(bool value) { has_simd_ = value; }
597
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800598 bool HasLoops() const { return has_loops_; }
599 void SetHasLoops(bool value) { has_loops_ = value; }
600
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000601 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
602 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
603
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100604 ArtMethod* GetArtMethod() const { return art_method_; }
605 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
606
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100607 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500608 // The instruction has been inserted into the graph, either as a constant, or
609 // before cursor.
610 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
611
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000612 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
613
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800614 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
615 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
616 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
617
David Brazdil2d7352b2015-04-20 14:52:42 +0100618 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000619 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100620 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000621
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 template <class InstructionType, typename ValueType>
623 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600624 ArenaSafeMap<ValueType, InstructionType*>* cache,
625 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000626 // Try to find an existing constant of the given value.
627 InstructionType* constant = nullptr;
628 auto cached_constant = cache->find(value);
629 if (cached_constant != cache->end()) {
630 constant = cached_constant->second;
631 }
632
633 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100634 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000635 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100636 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000637 cache->Overwrite(value, constant);
638 InsertConstant(constant);
639 }
640 return constant;
641 }
642
David Brazdil8d5b8b22015-03-24 10:51:52 +0000643 void InsertConstant(HConstant* instruction);
644
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000645 // Cache a float constant into the graph. This method should only be
646 // called by the SsaBuilder when creating "equivalent" instructions.
647 void CacheFloatConstant(HFloatConstant* constant);
648
649 // See CacheFloatConstant comment.
650 void CacheDoubleConstant(HDoubleConstant* constant);
651
Vladimir Markoca6fff82017-10-03 14:49:14 +0100652 ArenaAllocator* const allocator_;
653 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000654
655 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000657
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100659 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000660
Aart Bik281c6812016-08-26 11:31:48 -0700661 // List of blocks to perform a linear order tree traversal. Unlike the reverse
662 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100663 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100664
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000665 HBasicBlock* entry_block_;
666 HBasicBlock* exit_block_;
667
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100668 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100669 uint16_t maximum_number_of_out_vregs_;
670
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100671 // The number of virtual registers in this method. Contains the parameters.
672 uint16_t number_of_vregs_;
673
674 // The number of virtual registers used by parameters of this method.
675 uint16_t number_of_in_vregs_;
676
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000677 // Number of vreg size slots that the temporaries use (used in baseline compiler).
678 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100679
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800680 // Flag whether there are bounds checks in the graph. We can skip
681 // BCE if it's false. It's only best effort to keep it up to date in
682 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400683 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800684
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800685 // Flag whether there are try/catch blocks in the graph. We will skip
686 // try/catch-related passes if it's false. It's only best effort to keep
687 // it up to date in the presence of code elimination so there might be
688 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000689 bool has_try_catch_;
690
Aart Bikb13c65b2017-03-21 20:14:07 -0700691 // Flag whether SIMD instructions appear in the graph. If true, the
692 // code generators may have to be more careful spilling the wider
693 // contents of SIMD registers.
694 bool has_simd_;
695
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800696 // Flag whether there are any loops in the graph. We can skip loop
697 // optimization if it's false. It's only best effort to keep it up
698 // to date in the presence of code elimination so there might be false
699 // positives.
700 bool has_loops_;
701
702 // Flag whether there are any irreducible loops in the graph. It's only
703 // best effort to keep it up to date in the presence of code elimination
704 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000705 bool has_irreducible_loops_;
706
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000707 // Indicates whether the graph should be compiled in a way that
708 // ensures full debuggability. If false, we can apply more
709 // aggressive optimizations that may limit the level of debugging.
710 const bool debuggable_;
711
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000712 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000713 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000714
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100715 // The dex file from which the method is from.
716 const DexFile& dex_file_;
717
718 // The method index in the dex file.
719 const uint32_t method_idx_;
720
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100721 // If inlined, this encodes how the callee is being invoked.
722 const InvokeType invoke_type_;
723
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100724 // Whether the graph has been transformed to SSA form. Only used
725 // in debug mode to ensure we are not using properties only valid
726 // for non-SSA form (like the number of temporaries).
727 bool in_ssa_form_;
728
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800729 // Number of CHA guards in the graph. Used to short-circuit the
730 // CHA guard optimization pass when there is no CHA guard left.
731 uint32_t number_of_cha_guards_;
732
Mathieu Chartiere401d142015-04-22 13:56:20 -0700733 const InstructionSet instruction_set_;
734
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000736 HNullConstant* cached_null_constant_;
737 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000738 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000739 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000740 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000741
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100742 HCurrentMethod* cached_current_method_;
743
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100744 // The ArtMethod this graph is for. Note that for AOT, it may be null,
745 // for example for methods whose declaring class could not be resolved
746 // (such as when the superclass could not be found).
747 ArtMethod* art_method_;
748
David Brazdil4833f5a2015-12-16 10:37:39 +0000749 // Keep the RTI of inexact Object to avoid having to pass stack handle
750 // collection pointer to passes which may create NullConstant.
751 ReferenceTypeInfo inexact_object_rti_;
752
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000753 // Whether we are compiling this graph for on stack replacement: this will
754 // make all loops seen as irreducible and emit special stack maps to mark
755 // compiled code entries which the interpreter can directly jump to.
756 const bool osr_;
757
Mingyao Yang063fc772016-08-02 11:02:54 -0700758 // List of methods that are assumed to have single implementation.
759 ArenaSet<ArtMethod*> cha_single_implementation_list_;
760
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000761 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100762 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000763 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000764 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000765 DISALLOW_COPY_AND_ASSIGN(HGraph);
766};
767
Vladimir Markof9f64412015-09-02 14:05:49 +0100768class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000769 public:
770 HLoopInformation(HBasicBlock* header, HGraph* graph)
771 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100772 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000773 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100774 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100775 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100776 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100777 blocks_(graph->GetAllocator(),
778 graph->GetBlocks().size(),
779 true,
780 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100781 back_edges_.reserve(kDefaultNumberOfBackEdges);
782 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100783
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000784 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100785 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000786
787 void Dump(std::ostream& os);
788
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100789 HBasicBlock* GetHeader() const {
790 return header_;
791 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000792
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000793 void SetHeader(HBasicBlock* block) {
794 header_ = block;
795 }
796
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100797 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
798 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
799 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
800
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000801 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100802 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000803 }
804
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100805 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100806 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 }
808
David Brazdil46e2a392015-03-16 17:31:52 +0000809 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100810 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100811 }
812
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000813 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100814 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000815 }
816
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100818
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100819 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100820 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100821 }
822
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100823 // Returns the lifetime position of the back edge that has the
824 // greatest lifetime position.
825 size_t GetLifetimeEnd() const;
826
827 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100828 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100829 }
830
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000831 // Finds blocks that are part of this loop.
832 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100833
Artem Serov7f4aff62017-06-21 17:02:18 +0100834 // Updates blocks population of the loop and all of its outer' ones recursively after the
835 // population of the inner loop is updated.
836 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
837
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100838 // Returns whether this loop information contains `block`.
839 // Note that this loop information *must* be populated before entering this function.
840 bool Contains(const HBasicBlock& block) const;
841
842 // Returns whether this loop information is an inner loop of `other`.
843 // Note that `other` *must* be populated before entering this function.
844 bool IsIn(const HLoopInformation& other) const;
845
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800846 // Returns true if instruction is not defined within this loop.
847 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700848
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 const ArenaBitVector& GetBlocks() const { return blocks_; }
850
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000851 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000852 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000853
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000854 void ClearAllBlocks() {
855 blocks_.ClearAllBits();
856 }
857
David Brazdil3f4a5222016-05-06 12:46:21 +0100858 bool HasBackEdgeNotDominatedByHeader() const;
859
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100860 bool IsPopulated() const {
861 return blocks_.GetHighestBitSet() != -1;
862 }
863
Anton Shaminf89381f2016-05-16 16:44:13 +0600864 bool DominatesAllBackEdges(HBasicBlock* block);
865
David Sehrc757dec2016-11-04 15:48:34 -0700866 bool HasExitEdge() const;
867
Artem Serov7f4aff62017-06-21 17:02:18 +0100868 // Resets back edge and blocks-in-loop data.
869 void ResetBasicBlockData() {
870 back_edges_.clear();
871 ClearAllBlocks();
872 }
873
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100875 // Internal recursive implementation of `Populate`.
876 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100877 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100878
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000879 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100880 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000881 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100882 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100883 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100884 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000885
886 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
887};
888
David Brazdilec16f792015-08-19 15:04:01 +0100889// Stores try/catch information for basic blocks.
890// Note that HGraph is constructed so that catch blocks cannot simultaneously
891// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100892class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100893 public:
894 // Try block information constructor.
895 explicit TryCatchInformation(const HTryBoundary& try_entry)
896 : try_entry_(&try_entry),
897 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000898 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100899 DCHECK(try_entry_ != nullptr);
900 }
901
902 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800903 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100904 : try_entry_(nullptr),
905 catch_dex_file_(&dex_file),
906 catch_type_index_(catch_type_index) {}
907
908 bool IsTryBlock() const { return try_entry_ != nullptr; }
909
910 const HTryBoundary& GetTryEntry() const {
911 DCHECK(IsTryBlock());
912 return *try_entry_;
913 }
914
915 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
916
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000917 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100918 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000919 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100920 }
921
Andreas Gampea5b09a62016-11-17 15:21:22 -0800922 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100923 DCHECK(IsCatchBlock());
924 return catch_type_index_;
925 }
926
927 const DexFile& GetCatchDexFile() const {
928 DCHECK(IsCatchBlock());
929 return *catch_dex_file_;
930 }
931
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000932 void SetInvalidTypeIndex() {
933 catch_type_index_ = dex::TypeIndex::Invalid();
934 }
935
David Brazdilec16f792015-08-19 15:04:01 +0100936 private:
937 // One of possibly several TryBoundary instructions entering the block's try.
938 // Only set for try blocks.
939 const HTryBoundary* try_entry_;
940
941 // Exception type information. Only set for catch blocks.
942 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000943 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100944};
945
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100946static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100947static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100948
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949// A block in a method. Contains the list of instructions represented
950// as a double linked list. Each block knows its predecessors and
951// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100952
Vladimir Markof9f64412015-09-02 14:05:49 +0100953class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000954 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700955 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000956 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100957 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
958 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000959 loop_information_(nullptr),
960 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100961 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100962 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100963 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100964 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000965 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000966 try_catch_information_(nullptr) {
967 predecessors_.reserve(kDefaultNumberOfPredecessors);
968 successors_.reserve(kDefaultNumberOfSuccessors);
969 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
970 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971
Vladimir Marko60584552015-09-03 13:35:12 +0000972 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100973 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000974 }
975
Vladimir Marko60584552015-09-03 13:35:12 +0000976 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100977 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000978 }
979
David Brazdild26a4112015-11-10 11:07:31 +0000980 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
981 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
982
Vladimir Marko60584552015-09-03 13:35:12 +0000983 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
984 return ContainsElement(successors_, block, start_from);
985 }
986
987 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100988 return dominated_blocks_;
989 }
990
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100991 bool IsEntryBlock() const {
992 return graph_->GetEntryBlock() == this;
993 }
994
995 bool IsExitBlock() const {
996 return graph_->GetExitBlock() == this;
997 }
998
David Brazdil46e2a392015-03-16 17:31:52 +0000999 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001000 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001001 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001002 bool IsSingleTryBoundary() const;
1003
1004 // Returns true if this block emits nothing but a jump.
1005 bool IsSingleJump() const {
1006 HLoopInformation* loop_info = GetLoopInformation();
1007 return (IsSingleGoto() || IsSingleTryBoundary())
1008 // Back edges generate a suspend check.
1009 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1010 }
David Brazdil46e2a392015-03-16 17:31:52 +00001011
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001012 void AddBackEdge(HBasicBlock* back_edge) {
1013 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001014 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001015 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001016 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001017 loop_information_->AddBackEdge(back_edge);
1018 }
1019
Artem Serov7f4aff62017-06-21 17:02:18 +01001020 // Registers a back edge; if the block was not a loop header before the call associates a newly
1021 // created loop info with it.
1022 //
1023 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1024 // info for all blocks during back edges recalculation.
1025 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1026 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1027 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1028 }
1029 loop_information_->AddBackEdge(back_edge);
1030 }
1031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001032 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001033 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001034
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001035 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001036 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001037 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039 HBasicBlock* GetDominator() const { return dominator_; }
1040 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001041 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1042
1043 void RemoveDominatedBlock(HBasicBlock* block) {
1044 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001045 }
Vladimir Marko60584552015-09-03 13:35:12 +00001046
1047 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1048 ReplaceElement(dominated_blocks_, existing, new_block);
1049 }
1050
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001051 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001052
1053 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001054 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001055 }
1056
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001057 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1058 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001059 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001060 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001061 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1062 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001063
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001064 HInstruction* GetFirstInstructionDisregardMoves() const;
1065
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001066 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001067 successors_.push_back(block);
1068 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069 }
1070
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001071 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1072 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001073 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001074 new_block->predecessors_.push_back(this);
1075 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001076 }
1077
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001078 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1079 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001080 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001081 new_block->successors_.push_back(this);
1082 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001083 }
1084
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001085 // Insert `this` between `predecessor` and `successor. This method
1086 // preserves the indicies, and will update the first edge found between
1087 // `predecessor` and `successor`.
1088 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1089 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001090 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001091 successor->predecessors_[predecessor_index] = this;
1092 predecessor->successors_[successor_index] = this;
1093 successors_.push_back(successor);
1094 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001095 }
1096
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001097 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001098 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001099 }
1100
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001101 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001102 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001103 }
1104
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001106 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001107 }
1108
1109 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001110 predecessors_.push_back(block);
1111 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001112 }
1113
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001114 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001115 DCHECK_EQ(predecessors_.size(), 2u);
1116 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001117 }
1118
David Brazdil769c9e52015-04-27 13:54:09 +01001119 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001120 DCHECK_EQ(successors_.size(), 2u);
1121 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001122 }
1123
David Brazdilfc6a86a2015-06-26 10:33:45 +00001124 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001125 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001126 }
1127
David Brazdilfc6a86a2015-06-26 10:33:45 +00001128 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001129 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001130 }
1131
David Brazdilfc6a86a2015-06-26 10:33:45 +00001132 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001133 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001134 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001135 }
1136
1137 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001138 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001139 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001140 }
1141
1142 // Returns whether the first occurrence of `predecessor` in the list of
1143 // predecessors is at index `idx`.
1144 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001145 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001146 return GetPredecessorIndexOf(predecessor) == idx;
1147 }
1148
David Brazdild7558da2015-09-22 13:04:14 +01001149 // Create a new block between this block and its predecessors. The new block
1150 // is added to the graph, all predecessor edges are relinked to it and an edge
1151 // is created to `this`. Returns the new empty block. Reverse post order or
1152 // loop and try/catch information are not updated.
1153 HBasicBlock* CreateImmediateDominator();
1154
David Brazdilfc6a86a2015-06-26 10:33:45 +00001155 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001156 // created, latter block. Note that this method will add the block to the
1157 // graph, create a Goto at the end of the former block and will create an edge
1158 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001159 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001160 HBasicBlock* SplitBefore(HInstruction* cursor);
1161
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001162 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001163 // created block. Note that this method just updates raw block information,
1164 // like predecessors, successors, dominators, and instruction list. It does not
1165 // update the graph, reverse post order, loop information, nor make sure the
1166 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001167 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1168
1169 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1170 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001171
1172 // Merge `other` at the end of `this`. Successors and dominated blocks of
1173 // `other` are changed to be successors and dominated blocks of `this`. Note
1174 // that this method does not update the graph, reverse post order, loop
1175 // information, nor make sure the blocks are consistent (for example ending
1176 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001177 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001178
1179 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1180 // of `this` are moved to `other`.
1181 // Note that this method does not update the graph, reverse post order, loop
1182 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001183 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001184 void ReplaceWith(HBasicBlock* other);
1185
Aart Bik6b69e0a2017-01-11 10:20:43 -08001186 // Merges the instructions of `other` at the end of `this`.
1187 void MergeInstructionsWith(HBasicBlock* other);
1188
David Brazdil2d7352b2015-04-20 14:52:42 +01001189 // Merge `other` at the end of `this`. This method updates loops, reverse post
1190 // order, links to predecessors, successors, dominators and deletes the block
1191 // from the graph. The two blocks must be successive, i.e. `this` the only
1192 // predecessor of `other` and vice versa.
1193 void MergeWith(HBasicBlock* other);
1194
1195 // Disconnects `this` from all its predecessors, successors and dominator,
1196 // removes it from all loops it is included in and eventually from the graph.
1197 // The block must not dominate any other block. Predecessors and successors
1198 // are safely updated.
1199 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001200
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001201 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001202 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001203 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001204 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001205 // Replace phi `initial` with `replacement` within this block.
1206 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001207 // Replace instruction `initial` with `replacement` within this block.
1208 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1209 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001210 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001211 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001212 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1213 // instruction list. With 'ensure_safety' set to true, it verifies that the
1214 // instruction is not in use and removes it from the use lists of its inputs.
1215 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1216 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001217 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001218
1219 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001220 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001221 }
1222
Roland Levillain6b879dd2014-09-22 17:13:44 +01001223 bool IsLoopPreHeaderFirstPredecessor() const {
1224 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001225 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001226 }
1227
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001228 bool IsFirstPredecessorBackEdge() const {
1229 DCHECK(IsLoopHeader());
1230 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1231 }
1232
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001233 HLoopInformation* GetLoopInformation() const {
1234 return loop_information_;
1235 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001236
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001237 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001238 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001239 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001240 void SetInLoop(HLoopInformation* info) {
1241 if (IsLoopHeader()) {
1242 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001243 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001244 loop_information_ = info;
1245 } else if (loop_information_->Contains(*info->GetHeader())) {
1246 // Block is currently part of an outer loop. Make it part of this inner loop.
1247 // Note that a non loop header having a loop information means this loop information
1248 // has already been populated
1249 loop_information_ = info;
1250 } else {
1251 // Block is part of an inner loop. Do not update the loop information.
1252 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1253 // at this point, because this method is being called while populating `info`.
1254 }
1255 }
1256
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001257 // Raw update of the loop information.
1258 void SetLoopInformation(HLoopInformation* info) {
1259 loop_information_ = info;
1260 }
1261
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001262 bool IsInLoop() const { return loop_information_ != nullptr; }
1263
David Brazdilec16f792015-08-19 15:04:01 +01001264 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1265
1266 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1267 try_catch_information_ = try_catch_information;
1268 }
1269
1270 bool IsTryBlock() const {
1271 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1272 }
1273
1274 bool IsCatchBlock() const {
1275 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1276 }
David Brazdilffee3d32015-07-06 11:48:53 +01001277
1278 // Returns the try entry that this block's successors should have. They will
1279 // be in the same try, unless the block ends in a try boundary. In that case,
1280 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001281 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001282
Aart Bik75ff2c92018-04-21 01:28:11 +00001283 bool HasThrowingInstructions() const;
1284
David Brazdila4b8c212015-05-07 09:59:30 +01001285 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001286 bool Dominates(HBasicBlock* block) const;
1287
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001288 size_t GetLifetimeStart() const { return lifetime_start_; }
1289 size_t GetLifetimeEnd() const { return lifetime_end_; }
1290
1291 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1292 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1293
David Brazdil8d5b8b22015-03-24 10:51:52 +00001294 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001295 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001296 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001297 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001298 bool HasSinglePhi() const;
1299
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001300 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001301 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001302 ArenaVector<HBasicBlock*> predecessors_;
1303 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001304 HInstructionList instructions_;
1305 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001306 HLoopInformation* loop_information_;
1307 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001308 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001309 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001310 // The dex program counter of the first instruction of this block.
1311 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001312 size_t lifetime_start_;
1313 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001314 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001315
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001316 friend class HGraph;
1317 friend class HInstruction;
1318
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001319 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1320};
1321
David Brazdilb2bd1c52015-03-25 11:17:37 +00001322// Iterates over the LoopInformation of all loops which contain 'block'
1323// from the innermost to the outermost.
1324class HLoopInformationOutwardIterator : public ValueObject {
1325 public:
1326 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1327 : current_(block.GetLoopInformation()) {}
1328
1329 bool Done() const { return current_ == nullptr; }
1330
1331 void Advance() {
1332 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001333 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001334 }
1335
1336 HLoopInformation* Current() const {
1337 DCHECK(!Done());
1338 return current_;
1339 }
1340
1341 private:
1342 HLoopInformation* current_;
1343
1344 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1345};
1346
Alexandre Ramesef20f712015-06-09 10:29:30 +01001347#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001348 M(Above, Condition) \
1349 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001350 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001351 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001352 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001353 M(ArrayGet, Instruction) \
1354 M(ArrayLength, Instruction) \
1355 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001356 M(Below, Condition) \
1357 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001358 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001359 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001360 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001361 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001362 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001363 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001364 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001366 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001367 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001368 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001369 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001370 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001371 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001372 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001373 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001374 M(Exit, Instruction) \
1375 M(FloatConstant, Constant) \
1376 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001377 M(GreaterThan, Condition) \
1378 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001379 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001380 M(InstanceFieldGet, Instruction) \
1381 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001382 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001383 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001384 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001385 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001386 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001387 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001388 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001389 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001390 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001391 M(LessThan, Condition) \
1392 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001393 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001394 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001395 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001396 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001397 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001398 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001399 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001400 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001401 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001402 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001403 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001404 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001405 M(Neg, UnaryOperation) \
1406 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001407 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001408 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001409 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001410 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001411 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001412 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001413 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001415 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001416 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001417 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001418 M(Return, Instruction) \
1419 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001420 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001421 M(Shl, BinaryOperation) \
1422 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001423 M(StaticFieldGet, Instruction) \
1424 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001425 M(UnresolvedInstanceFieldGet, Instruction) \
1426 M(UnresolvedInstanceFieldSet, Instruction) \
1427 M(UnresolvedStaticFieldGet, Instruction) \
1428 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001429 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001430 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001431 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001432 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001433 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001434 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001435 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001436 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001437 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001438 M(VecExtractScalar, VecUnaryOperation) \
1439 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001440 M(VecCnv, VecUnaryOperation) \
1441 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001442 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001443 M(VecNot, VecUnaryOperation) \
1444 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001445 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001446 M(VecSub, VecBinaryOperation) \
1447 M(VecMul, VecBinaryOperation) \
1448 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001449 M(VecMin, VecBinaryOperation) \
1450 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001451 M(VecAnd, VecBinaryOperation) \
1452 M(VecAndNot, VecBinaryOperation) \
1453 M(VecOr, VecBinaryOperation) \
1454 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001455 M(VecSaturationAdd, VecBinaryOperation) \
1456 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001457 M(VecShl, VecBinaryOperation) \
1458 M(VecShr, VecBinaryOperation) \
1459 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001460 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001461 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001462 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001463 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001464 M(VecLoad, VecMemoryOperation) \
1465 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001466
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001467/*
1468 * Instructions, shared across several (not all) architectures.
1469 */
1470#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1471#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1472#else
1473#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001474 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001475 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001476 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001477 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001478#endif
1479
Alexandre Ramesef20f712015-06-09 10:29:30 +01001480#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1481
1482#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1483
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001484#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001485#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001486#else
1487#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1488 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001489 M(MipsPackedSwitch, Instruction) \
1490 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001491#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001492
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001493#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1494
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001495#ifndef ART_ENABLE_CODEGEN_x86
1496#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1497#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001498#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1499 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001500 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001501 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001502 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001503#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001504
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301505#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1506#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
1507 M(X86AndNot, Instruction) \
1508 M(X86MaskOrResetLeastSetBit, Instruction)
1509#else
1510#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1511#endif
1512
Alexandre Ramesef20f712015-06-09 10:29:30 +01001513#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1514
1515#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1516 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001517 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001518 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1519 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001520 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001521 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001522 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301523 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1524 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001525
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001526#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1527 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001528 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001529 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001530 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001531 M(Invoke, Instruction) \
1532 M(VecOperation, Instruction) \
1533 M(VecUnaryOperation, VecOperation) \
1534 M(VecBinaryOperation, VecOperation) \
1535 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001536
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001537#define FOR_EACH_INSTRUCTION(M) \
1538 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1539 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1540
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001541#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001542FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1543#undef FORWARD_DECLARATION
1544
Vladimir Marko372f10e2016-05-17 16:30:10 +01001545#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001546 private: \
1547 H##type& operator=(const H##type&) = delete; \
1548 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001549 const char* DebugName() const override { return #type; } \
1550 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001551 DCHECK(IsClonable()); \
1552 return new (arena) H##type(*this->As##type()); \
1553 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001554 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001555
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001556#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001557 private: \
1558 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001559 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001560
Artem Serovcced8ba2017-07-19 18:18:09 +01001561#define DEFAULT_COPY_CONSTRUCTOR(type) \
1562 explicit H##type(const H##type& other) = default;
1563
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001564template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001565class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1566 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001567 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001568 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001569 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001570 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001571 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001572 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001573 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001574
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001575 private:
David Brazdiled596192015-01-23 10:39:45 +00001576 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001577 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001578
1579 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001580 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001581
Vladimir Marko46817b82016-03-29 12:21:58 +01001582 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001583
1584 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1585};
1586
David Brazdiled596192015-01-23 10:39:45 +00001587template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001588using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001589
David Brazdil1abb4192015-02-17 18:33:36 +00001590// This class is used by HEnvironment and HInstruction classes to record the
1591// instructions they use and pointers to the corresponding HUseListNodes kept
1592// by the used instructions.
1593template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001594class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001595 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001596 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1597 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001598
Vladimir Marko46817b82016-03-29 12:21:58 +01001599 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1600 : HUserRecord(old_record.instruction_, before_use_node) {}
1601 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1602 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001603 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001604 }
1605
1606 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001607 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1608 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001609
1610 private:
1611 // Instruction used by the user.
1612 HInstruction* instruction_;
1613
Vladimir Marko46817b82016-03-29 12:21:58 +01001614 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1615 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001616};
1617
Vladimir Markoe9004912016-06-16 16:50:52 +01001618// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1619// This is used for HInstruction::GetInputs() to return a container wrapper providing
1620// HInstruction* values even though the underlying container has HUserRecord<>s.
1621struct HInputExtractor {
1622 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1623 return record.GetInstruction();
1624 }
1625 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1626 return record.GetInstruction();
1627 }
1628};
1629
1630using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1631using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1632
Aart Bik854a02b2015-07-14 16:07:00 -07001633/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001635 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001636 * For write/read dependences on fields/arrays, the dependence analysis uses
1637 * type disambiguation (e.g. a float field write cannot modify the value of an
1638 * integer field read) and the access type (e.g. a reference array write cannot
1639 * modify the value of a reference field read [although it may modify the
1640 * reference fetch prior to reading the field, which is represented by its own
1641 * write/read dependence]). The analysis makes conservative points-to
1642 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1643 * the same, and any reference read depends on any reference read without
1644 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001645 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001646 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1647 * alive across the point where garbage collection might happen.
1648 *
1649 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1650 *
1651 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1652 * those instructions from the compiler perspective (between this instruction and the next one
1653 * in the IR).
1654 *
1655 * Note: Instructions which can cause GC only on a fatal slow path do not need
1656 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1657 * one. However the execution may return to compiled code if there is a catch block in the
1658 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1659 * set.
1660 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001661 * The internal representation uses 38-bit and is described in the table below.
1662 * The first line indicates the side effect, and for field/array accesses the
1663 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001664 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001665 * The two numbered lines below indicate the bit position in the bitfield (read
1666 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001667 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001668 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1669 * +-------------+---------+---------+--------------+---------+---------+
1670 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1671 * | 3 |333333322|222222221| 1 |111111110|000000000|
1672 * | 7 |654321098|765432109| 8 |765432109|876543210|
1673 *
1674 * Note that, to ease the implementation, 'changes' bits are least significant
1675 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001676 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001677class SideEffects : public ValueObject {
1678 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001679 SideEffects() : flags_(0) {}
1680
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001681 static SideEffects None() {
1682 return SideEffects(0);
1683 }
1684
1685 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001686 return SideEffects(kAllChangeBits | kAllDependOnBits);
1687 }
1688
1689 static SideEffects AllChanges() {
1690 return SideEffects(kAllChangeBits);
1691 }
1692
1693 static SideEffects AllDependencies() {
1694 return SideEffects(kAllDependOnBits);
1695 }
1696
1697 static SideEffects AllExceptGCDependency() {
1698 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1699 }
1700
1701 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001702 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001703 }
1704
Aart Bik34c3ba92015-07-20 14:08:59 -07001705 static SideEffects AllWrites() {
1706 return SideEffects(kAllWrites);
1707 }
1708
1709 static SideEffects AllReads() {
1710 return SideEffects(kAllReads);
1711 }
1712
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001713 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001714 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001715 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001716 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001717 }
1718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001719 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001720 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001721 }
1722
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001723 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001724 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001725 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001726 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001727 }
1728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001729 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001730 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001731 }
1732
Artem Serovd1aa7d02018-06-22 11:35:46 +01001733 // Returns whether GC might happen across this instruction from the compiler perspective so
1734 // the next instruction in the IR would see that.
1735 //
1736 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001737 static SideEffects CanTriggerGC() {
1738 return SideEffects(1ULL << kCanTriggerGCBit);
1739 }
1740
Artem Serovd1aa7d02018-06-22 11:35:46 +01001741 // Returns whether the instruction must not be alive across a GC point.
1742 //
1743 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001744 static SideEffects DependsOnGC() {
1745 return SideEffects(1ULL << kDependsOnGCBit);
1746 }
1747
Aart Bik854a02b2015-07-14 16:07:00 -07001748 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001749 SideEffects Union(SideEffects other) const {
1750 return SideEffects(flags_ | other.flags_);
1751 }
1752
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001753 SideEffects Exclusion(SideEffects other) const {
1754 return SideEffects(flags_ & ~other.flags_);
1755 }
1756
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001757 void Add(SideEffects other) {
1758 flags_ |= other.flags_;
1759 }
1760
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001761 bool Includes(SideEffects other) const {
1762 return (other.flags_ & flags_) == other.flags_;
1763 }
1764
1765 bool HasSideEffects() const {
1766 return (flags_ & kAllChangeBits);
1767 }
1768
1769 bool HasDependencies() const {
1770 return (flags_ & kAllDependOnBits);
1771 }
1772
1773 // Returns true if there are no side effects or dependencies.
1774 bool DoesNothing() const {
1775 return flags_ == 0;
1776 }
1777
Aart Bik854a02b2015-07-14 16:07:00 -07001778 // Returns true if something is written.
1779 bool DoesAnyWrite() const {
1780 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001781 }
1782
Aart Bik854a02b2015-07-14 16:07:00 -07001783 // Returns true if something is read.
1784 bool DoesAnyRead() const {
1785 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001786 }
1787
Aart Bik854a02b2015-07-14 16:07:00 -07001788 // Returns true if potentially everything is written and read
1789 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 bool DoesAllReadWrite() const {
1791 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1792 }
1793
Aart Bik854a02b2015-07-14 16:07:00 -07001794 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001795 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001796 }
1797
Roland Levillain0d5a2812015-11-13 10:07:31 +00001798 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001799 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001800 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1801 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001802 }
1803
1804 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001805 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001806 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001807 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001808 for (int s = kLastBit; s >= 0; s--) {
1809 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1810 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1811 // This is a bit for the GC side effect.
1812 if (current_bit_is_set) {
1813 flags += "GC";
1814 }
Aart Bik854a02b2015-07-14 16:07:00 -07001815 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001816 } else {
1817 // This is a bit for the array/field analysis.
1818 // The underscore character stands for the 'can trigger GC' bit.
1819 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1820 if (current_bit_is_set) {
1821 flags += kDebug[s];
1822 }
1823 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1824 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1825 flags += "|";
1826 }
1827 }
Aart Bik854a02b2015-07-14 16:07:00 -07001828 }
1829 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001830 }
1831
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001832 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001833
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001834 private:
1835 static constexpr int kFieldArrayAnalysisBits = 9;
1836
1837 static constexpr int kFieldWriteOffset = 0;
1838 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1839 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1840 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1841
1842 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1843
1844 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1845 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1846 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1847 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1848
1849 static constexpr int kLastBit = kDependsOnGCBit;
1850 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1851
1852 // Aliases.
1853
1854 static_assert(kChangeBits == kDependOnBits,
1855 "the 'change' bits should match the 'depend on' bits.");
1856
1857 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1858 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1859 static constexpr uint64_t kAllWrites =
1860 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1861 static constexpr uint64_t kAllReads =
1862 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001863
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001864 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001865 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001866 int shift;
1867 switch (type) {
1868 case DataType::Type::kReference: shift = 0; break;
1869 case DataType::Type::kBool: shift = 1; break;
1870 case DataType::Type::kInt8: shift = 2; break;
1871 case DataType::Type::kUint16: shift = 3; break;
1872 case DataType::Type::kInt16: shift = 4; break;
1873 case DataType::Type::kInt32: shift = 5; break;
1874 case DataType::Type::kInt64: shift = 6; break;
1875 case DataType::Type::kFloat32: shift = 7; break;
1876 case DataType::Type::kFloat64: shift = 8; break;
1877 default:
1878 LOG(FATAL) << "Unexpected data type " << type;
1879 UNREACHABLE();
1880 }
Aart Bik854a02b2015-07-14 16:07:00 -07001881 DCHECK_LE(kFieldWriteOffset, shift);
1882 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001883 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001884 }
1885
1886 // Private constructor on direct flags value.
1887 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1888
1889 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001890};
1891
David Brazdiled596192015-01-23 10:39:45 +00001892// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001893class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001894 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001895 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001896 size_t number_of_vregs,
1897 ArtMethod* method,
1898 uint32_t dex_pc,
1899 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001900 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1901 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001902 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001903 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001904 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001905 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001906 }
1907
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001908 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1909 const HEnvironment& to_copy,
1910 HInstruction* holder)
1911 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001912 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001913 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001914 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001915 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001916
Vladimir Markoec32f642017-06-02 10:51:55 +01001917 void AllocateLocations() {
1918 DCHECK(locations_.empty());
1919 locations_.resize(vregs_.size());
1920 }
1921
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001922 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001923 if (parent_ != nullptr) {
1924 parent_->SetAndCopyParentChain(allocator, parent);
1925 } else {
1926 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1927 parent_->CopyFrom(parent);
1928 if (parent->GetParent() != nullptr) {
1929 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1930 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001931 }
David Brazdiled596192015-01-23 10:39:45 +00001932 }
1933
Vladimir Marko69d310e2017-10-09 14:12:23 +01001934 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001935 void CopyFrom(HEnvironment* environment);
1936
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001937 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1938 // input to the loop phi instead. This is for inserting instructions that
1939 // require an environment (like HDeoptimization) in the loop pre-header.
1940 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001941
1942 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001943 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001944 }
1945
1946 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001947 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001948 }
1949
David Brazdil1abb4192015-02-17 18:33:36 +00001950 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001951
Artem Serovca210e32017-12-15 13:43:20 +00001952 // Replaces the input at the position 'index' with the replacement; the replacement and old
1953 // input instructions' env_uses_ lists are adjusted. The function works similar to
1954 // HInstruction::ReplaceInput.
1955 void ReplaceInput(HInstruction* replacement, size_t index);
1956
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001957 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001958
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001959 HEnvironment* GetParent() const { return parent_; }
1960
1961 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001962 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001963 }
1964
1965 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001966 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001967 }
1968
1969 uint32_t GetDexPc() const {
1970 return dex_pc_;
1971 }
1972
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001973 ArtMethod* GetMethod() const {
1974 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001975 }
1976
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001977 HInstruction* GetHolder() const {
1978 return holder_;
1979 }
1980
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001981
1982 bool IsFromInlinedInvoke() const {
1983 return GetParent() != nullptr;
1984 }
1985
David Brazdiled596192015-01-23 10:39:45 +00001986 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001987 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1988 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001989 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001990 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001991 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001992
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001993 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001994 HInstruction* const holder_;
1995
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001996 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001997
1998 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1999};
2000
Vladimir Markof9f64412015-09-02 14:05:49 +01002001class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002002 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302003#define DECLARE_KIND(type, super) k##type,
2004 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002005 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302006 kLastInstructionKind
2007 };
2008#undef DECLARE_KIND
2009
2010 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002011 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2012
2013 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002014 : previous_(nullptr),
2015 next_(nullptr),
2016 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002017 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002018 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002019 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002020 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002021 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002022 locations_(nullptr),
2023 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002024 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002025 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002026 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302027 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002028 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002029 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2030 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002031
Dave Allison20dfc792014-06-16 20:44:29 -07002032 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002033
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002034
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002035 HInstruction* GetNext() const { return next_; }
2036 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002037
Calin Juravle77520bc2015-01-12 18:45:46 +00002038 HInstruction* GetNextDisregardingMoves() const;
2039 HInstruction* GetPreviousDisregardingMoves() const;
2040
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002041 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002042 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002043 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002044 bool IsInBlock() const { return block_ != nullptr; }
2045 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002046 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2047 bool IsIrreducibleLoopHeaderPhi() const {
2048 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2049 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002050
Vladimir Marko372f10e2016-05-17 16:30:10 +01002051 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2052
2053 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2054 // One virtual method is enough, just const_cast<> and then re-add the const.
2055 return ArrayRef<const HUserRecord<HInstruction*>>(
2056 const_cast<HInstruction*>(this)->GetInputRecords());
2057 }
2058
Vladimir Markoe9004912016-06-16 16:50:52 +01002059 HInputsRef GetInputs() {
2060 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002061 }
2062
Vladimir Markoe9004912016-06-16 16:50:52 +01002063 HConstInputsRef GetInputs() const {
2064 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002065 }
2066
2067 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002068 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069
Aart Bik2767f4b2016-10-28 15:03:53 -07002070 bool HasInput(HInstruction* input) const {
2071 for (const HInstruction* i : GetInputs()) {
2072 if (i == input) {
2073 return true;
2074 }
2075 }
2076 return false;
2077 }
2078
Vladimir Marko372f10e2016-05-17 16:30:10 +01002079 void SetRawInputAt(size_t index, HInstruction* input) {
2080 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2081 }
2082
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002083 virtual void Accept(HGraphVisitor* visitor) = 0;
2084 virtual const char* DebugName() const = 0;
2085
Vladimir Markobd785672018-05-03 17:09:09 +01002086 DataType::Type GetType() const {
2087 return TypeField::Decode(GetPackedFields());
2088 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002089
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002090 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002091
2092 uint32_t GetDexPc() const { return dex_pc_; }
2093
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002094 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002095
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002096 // Can the instruction throw?
2097 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2098 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002099 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002100
2101 // Does the instruction always throw an exception unconditionally?
2102 virtual bool AlwaysThrows() const { return false; }
2103
David Brazdilec16f792015-08-19 15:04:01 +01002104 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002105
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002106 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002107 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002108
Calin Juravle10e244f2015-01-26 18:54:32 +00002109 // Does not apply for all instructions, but having this at top level greatly
2110 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002111 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002112 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002113 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002114 return true;
2115 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002116
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002117 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002118 return false;
2119 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002120
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002121 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2122 // with it. Otherwise return null.
2123 HNullCheck* GetImplicitNullCheck() const {
2124 // Find the first previous instruction which is not a move.
2125 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2126 if (first_prev_not_move != nullptr &&
2127 first_prev_not_move->IsNullCheck() &&
2128 first_prev_not_move->IsEmittedAtUseSite()) {
2129 return first_prev_not_move->AsNullCheck();
2130 }
2131 return nullptr;
2132 }
2133
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002134 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002135 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002136 }
2137
Calin Juravle2e768302015-07-28 14:41:11 +00002138 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002139
Calin Juravle61d544b2015-02-23 16:46:57 +00002140 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002141 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002142 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002143 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002144 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002145
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002146 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002147 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002148 // Note: fixup_end remains valid across push_front().
2149 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2150 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002151 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002152 uses_.push_front(*new_node);
2153 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002154 }
2155
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002156 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002157 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002158 // Note: env_fixup_end remains valid across push_front().
2159 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2160 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002161 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002162 env_uses_.push_front(*new_node);
2163 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002164 }
2165
David Brazdil1abb4192015-02-17 18:33:36 +00002166 void RemoveAsUserOfInput(size_t input) {
2167 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002168 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2169 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2170 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002171 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002172
Vladimir Marko372f10e2016-05-17 16:30:10 +01002173 void RemoveAsUserOfAllInputs() {
2174 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2175 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2176 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2177 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2178 }
2179 }
2180
David Brazdil1abb4192015-02-17 18:33:36 +00002181 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2182 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002183
Vladimir Marko46817b82016-03-29 12:21:58 +01002184 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2185 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2186 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002187 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002188 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002189 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002190
Aart Bikcc42be02016-10-20 16:14:16 -07002191 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002192 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002193 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002194 !CanThrow() &&
2195 !IsSuspendCheck() &&
2196 !IsControlFlow() &&
2197 !IsNativeDebugInfo() &&
2198 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002199 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002200 !IsMemoryBarrier() &&
2201 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002202 }
2203
Aart Bikcc42be02016-10-20 16:14:16 -07002204 bool IsDeadAndRemovable() const {
2205 return IsRemovable() && !HasUses();
2206 }
2207
Roland Levillain6c82d402014-10-13 16:10:27 +01002208 // Does this instruction strictly dominate `other_instruction`?
2209 // Returns false if this instruction and `other_instruction` are the same.
2210 // Aborts if this instruction and `other_instruction` are both phis.
2211 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002212
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002213 int GetId() const { return id_; }
2214 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002215
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002216 int GetSsaIndex() const { return ssa_index_; }
2217 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2218 bool HasSsaIndex() const { return ssa_index_ != -1; }
2219
2220 bool HasEnvironment() const { return environment_ != nullptr; }
2221 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002222 // Set the `environment_` field. Raw because this method does not
2223 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002224 void SetRawEnvironment(HEnvironment* environment) {
2225 DCHECK(environment_ == nullptr);
2226 DCHECK_EQ(environment->GetHolder(), this);
2227 environment_ = environment;
2228 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002229
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002230 void InsertRawEnvironment(HEnvironment* environment) {
2231 DCHECK(environment_ != nullptr);
2232 DCHECK_EQ(environment->GetHolder(), this);
2233 DCHECK(environment->GetParent() == nullptr);
2234 environment->parent_ = environment_;
2235 environment_ = environment;
2236 }
2237
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002238 void RemoveEnvironment();
2239
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002240 // Set the environment of this instruction, copying it from `environment`. While
2241 // copying, the uses lists are being updated.
2242 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002243 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002244 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002245 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002246 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002247 if (environment->GetParent() != nullptr) {
2248 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2249 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002250 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002251
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002252 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2253 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002254 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002255 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002256 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002257 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002258 if (environment->GetParent() != nullptr) {
2259 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2260 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002261 }
2262
Nicolas Geoffray39468442014-09-02 15:17:15 +01002263 // Returns the number of entries in the environment. Typically, that is the
2264 // number of dex registers in a method. It could be more in case of inlining.
2265 size_t EnvironmentSize() const;
2266
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002267 LocationSummary* GetLocations() const { return locations_; }
2268 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002269
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002270 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002271 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002272 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002273 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002274
Alexandre Rames188d4312015-04-09 18:30:21 +01002275 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2276 // uses of this instruction by `other` are *not* updated.
2277 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2278 ReplaceWith(other);
2279 other->ReplaceInput(this, use_index);
2280 }
2281
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002282 // Move `this` instruction before `cursor`
2283 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002284
Vladimir Markofb337ea2015-11-25 15:25:10 +00002285 // Move `this` before its first user and out of any loops. If there is no
2286 // out-of-loop user that dominates all other users, move the instruction
2287 // to the end of the out-of-loop common dominator of the user's blocks.
2288 //
2289 // This can be used only on non-throwing instructions with no side effects that
2290 // have at least one use but no environment uses.
2291 void MoveBeforeFirstUserAndOutOfLoops();
2292
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002293#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002294 bool Is##type() const;
2295
2296 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2297#undef INSTRUCTION_TYPE_CHECK
2298
2299#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002300 const H##type* As##type() const; \
2301 H##type* As##type();
2302
Vladimir Markoa90dd512018-05-04 15:04:45 +01002303 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2304#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002305
Artem Serovcced8ba2017-07-19 18:18:09 +01002306 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2307 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2308 // the instruction then the behaviour of this function is undefined.
2309 //
2310 // Note: It is semantically valid to create a clone of the instruction only until
2311 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2312 // copied.
2313 //
2314 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2315 // 'explicit HInstruction(const HInstruction& other)' for details.
2316 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2317 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2318 DebugName() << " " << GetId();
2319 UNREACHABLE();
2320 }
2321
2322 // Return whether instruction can be cloned (copied).
2323 virtual bool IsClonable() const { return false; }
2324
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002325 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002326 // TODO: this method is used by LICM and GVN with possibly different
2327 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002328 virtual bool CanBeMoved() const { return false; }
2329
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002330 // Returns whether any data encoded in the two instructions is equal.
2331 // This method does not look at the inputs. Both instructions must be
2332 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002333 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002334 return false;
2335 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002336
2337 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002338 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002339 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002340 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002341
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002342 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2343 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2344 // the virtual function because the __attribute__((__pure__)) doesn't really
2345 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302346 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002347
2348 virtual size_t ComputeHashCode() const {
2349 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002350 for (const HInstruction* input : GetInputs()) {
2351 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002352 }
2353 return result;
2354 }
2355
2356 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002357 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002358 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002359
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002360 size_t GetLifetimePosition() const { return lifetime_position_; }
2361 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2362 LiveInterval* GetLiveInterval() const { return live_interval_; }
2363 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2364 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2365
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002366 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2367
2368 // Returns whether the code generation of the instruction will require to have access
2369 // to the current method. Such instructions are:
2370 // (1): Instructions that require an environment, as calling the runtime requires
2371 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002372 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2373 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002374 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002375 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002376 }
2377
Vladimir Markodc151b22015-10-15 18:02:30 +01002378 // Returns whether the code generation of the instruction will require to have access
2379 // to the dex cache of the current method's declaring class via the current method.
2380 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002381
Mark Mendellc4701932015-04-10 13:18:51 -04002382 // Does this instruction have any use in an environment before
2383 // control flow hits 'other'?
2384 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2385
2386 // Remove all references to environment uses of this instruction.
2387 // The caller must ensure that this is safe to do.
2388 void RemoveEnvironmentUsers();
2389
Vladimir Markoa1de9182016-02-25 11:37:38 +00002390 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2391 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002392
David Brazdil1abb4192015-02-17 18:33:36 +00002393 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002394 // If set, the machine code for this instruction is assumed to be generated by
2395 // its users. Used by liveness analysis to compute use positions accordingly.
2396 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2397 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302398 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2399 static constexpr size_t kFieldInstructionKindSize =
2400 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002401 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302402 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002403 static constexpr size_t kFieldTypeSize =
2404 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2405 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002406 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2407
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302408 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2409 "Too many generic packed fields");
2410
Vladimir Markobd785672018-05-03 17:09:09 +01002411 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2412
Vladimir Marko372f10e2016-05-17 16:30:10 +01002413 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2414 return GetInputRecords()[i];
2415 }
2416
2417 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2418 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2419 input_records[index] = input;
2420 }
David Brazdil1abb4192015-02-17 18:33:36 +00002421
Vladimir Markoa1de9182016-02-25 11:37:38 +00002422 uint32_t GetPackedFields() const {
2423 return packed_fields_;
2424 }
2425
2426 template <size_t flag>
2427 bool GetPackedFlag() const {
2428 return (packed_fields_ & (1u << flag)) != 0u;
2429 }
2430
2431 template <size_t flag>
2432 void SetPackedFlag(bool value = true) {
2433 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2434 }
2435
2436 template <typename BitFieldType>
2437 typename BitFieldType::value_type GetPackedField() const {
2438 return BitFieldType::Decode(packed_fields_);
2439 }
2440
2441 template <typename BitFieldType>
2442 void SetPackedField(typename BitFieldType::value_type value) {
2443 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2444 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2445 }
2446
Artem Serovcced8ba2017-07-19 18:18:09 +01002447 // Copy construction for the instruction (used for Clone function).
2448 //
2449 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2450 // prepare_for_register_allocator are not copied (set to default values).
2451 //
2452 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2453 // fine for most of them. However for some of the instructions a custom copy constructor must be
2454 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2455 // for copying them).
2456 explicit HInstruction(const HInstruction& other)
2457 : previous_(nullptr),
2458 next_(nullptr),
2459 block_(nullptr),
2460 dex_pc_(other.dex_pc_),
2461 id_(-1),
2462 ssa_index_(-1),
2463 packed_fields_(other.packed_fields_),
2464 environment_(nullptr),
2465 locations_(nullptr),
2466 live_interval_(nullptr),
2467 lifetime_position_(kNoLifetime),
2468 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302469 reference_type_handle_(other.reference_type_handle_) {
2470 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002471
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002472 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302473 using InstructionKindField =
2474 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2475
Vladimir Marko46817b82016-03-29 12:21:58 +01002476 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2477 auto before_use_node = uses_.before_begin();
2478 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2479 HInstruction* user = use_node->GetUser();
2480 size_t input_index = use_node->GetIndex();
2481 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2482 before_use_node = use_node;
2483 }
2484 }
2485
2486 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2487 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2488 if (next != uses_.end()) {
2489 HInstruction* next_user = next->GetUser();
2490 size_t next_index = next->GetIndex();
2491 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2492 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2493 }
2494 }
2495
2496 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2497 auto before_env_use_node = env_uses_.before_begin();
2498 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2499 HEnvironment* user = env_use_node->GetUser();
2500 size_t input_index = env_use_node->GetIndex();
2501 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2502 before_env_use_node = env_use_node;
2503 }
2504 }
2505
2506 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2507 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2508 if (next != env_uses_.end()) {
2509 HEnvironment* next_user = next->GetUser();
2510 size_t next_index = next->GetIndex();
2511 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2512 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2513 }
2514 }
David Brazdil1abb4192015-02-17 18:33:36 +00002515
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002516 HInstruction* previous_;
2517 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002518 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002519 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002520
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002521 // An instruction gets an id when it is added to the graph.
2522 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002523 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002524 int id_;
2525
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002526 // When doing liveness analysis, instructions that have uses get an SSA index.
2527 int ssa_index_;
2528
Vladimir Markoa1de9182016-02-25 11:37:38 +00002529 // Packed fields.
2530 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002531
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002532 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002533 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002534
2535 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002536 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002537
Nicolas Geoffray39468442014-09-02 15:17:15 +01002538 // The environment associated with this instruction. Not null if the instruction
2539 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002540 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002541
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002542 // Set by the code generator.
2543 LocationSummary* locations_;
2544
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002545 // Set by the liveness analysis.
2546 LiveInterval* live_interval_;
2547
2548 // Set by the liveness analysis, this is the position in a linear
2549 // order of blocks where this instruction's live interval start.
2550 size_t lifetime_position_;
2551
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002552 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002553
Vladimir Markoa1de9182016-02-25 11:37:38 +00002554 // The reference handle part of the reference type info.
2555 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002556 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002557 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002558
David Brazdil1abb4192015-02-17 18:33:36 +00002559 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002560 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002561 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002562 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002563 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002564};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002565std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002566
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002567// Iterates over the instructions, while preserving the next instruction
2568// in case the current instruction gets removed from the list by the user
2569// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002570class HInstructionIterator : public ValueObject {
2571 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002572 explicit HInstructionIterator(const HInstructionList& instructions)
2573 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002574 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002575 }
2576
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002577 bool Done() const { return instruction_ == nullptr; }
2578 HInstruction* Current() const { return instruction_; }
2579 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002580 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002581 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002582 }
2583
2584 private:
2585 HInstruction* instruction_;
2586 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002587
2588 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002589};
2590
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002591// Iterates over the instructions without saving the next instruction,
2592// therefore handling changes in the graph potentially made by the user
2593// of this iterator.
2594class HInstructionIteratorHandleChanges : public ValueObject {
2595 public:
2596 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2597 : instruction_(instructions.first_instruction_) {
2598 }
2599
2600 bool Done() const { return instruction_ == nullptr; }
2601 HInstruction* Current() const { return instruction_; }
2602 void Advance() {
2603 instruction_ = instruction_->GetNext();
2604 }
2605
2606 private:
2607 HInstruction* instruction_;
2608
2609 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2610};
2611
2612
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002613class HBackwardInstructionIterator : public ValueObject {
2614 public:
2615 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2616 : instruction_(instructions.last_instruction_) {
2617 next_ = Done() ? nullptr : instruction_->GetPrevious();
2618 }
2619
2620 bool Done() const { return instruction_ == nullptr; }
2621 HInstruction* Current() const { return instruction_; }
2622 void Advance() {
2623 instruction_ = next_;
2624 next_ = Done() ? nullptr : instruction_->GetPrevious();
2625 }
2626
2627 private:
2628 HInstruction* instruction_;
2629 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002630
2631 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002632};
2633
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002634class HVariableInputSizeInstruction : public HInstruction {
2635 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002636 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002637 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002638 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2639 }
2640
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002641 void AddInput(HInstruction* input);
2642 void InsertInputAt(size_t index, HInstruction* input);
2643 void RemoveInputAt(size_t index);
2644
Igor Murashkind01745e2017-04-05 16:40:31 -07002645 // Removes all the inputs.
2646 // Also removes this instructions from each input's use list
2647 // (for non-environment uses only).
2648 void RemoveAllInputs();
2649
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002650 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302651 HVariableInputSizeInstruction(InstructionKind inst_kind,
2652 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002653 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002654 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002655 size_t number_of_inputs,
2656 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302657 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002658 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002659 HVariableInputSizeInstruction(InstructionKind inst_kind,
2660 DataType::Type type,
2661 SideEffects side_effects,
2662 uint32_t dex_pc,
2663 ArenaAllocator* allocator,
2664 size_t number_of_inputs,
2665 ArenaAllocKind kind)
2666 : HInstruction(inst_kind, type, side_effects, dex_pc),
2667 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002668
Artem Serovcced8ba2017-07-19 18:18:09 +01002669 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002670
Artem Serovcced8ba2017-07-19 18:18:09 +01002671 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002672};
2673
Vladimir Markof9f64412015-09-02 14:05:49 +01002674template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002675class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002676 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002677 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302678 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002679 HExpression<N>(InstructionKind kind,
2680 DataType::Type type,
2681 SideEffects side_effects,
2682 uint32_t dex_pc)
2683 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2684 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002685
Vladimir Marko372f10e2016-05-17 16:30:10 +01002686 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002687 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002688 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002689 }
2690
Artem Serovcced8ba2017-07-19 18:18:09 +01002691 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002692 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002693
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002694 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002695 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002696
2697 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002698};
2699
Vladimir Markobd785672018-05-03 17:09:09 +01002700// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002701template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002702class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002703 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002704 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002705
Vladimir Markobd785672018-05-03 17:09:09 +01002706 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002707
Vladimir Marko372f10e2016-05-17 16:30:10 +01002708 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002709 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002710 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002711 }
2712
Artem Serovcced8ba2017-07-19 18:18:09 +01002713 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002714 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002715
Vladimir Markof9f64412015-09-02 14:05:49 +01002716 private:
2717 friend class SsaBuilder;
2718};
2719
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002720// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2721// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002722class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002723 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002724 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002725 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302726 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002727
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002728 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002729
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002730 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002731
Artem Serovcced8ba2017-07-19 18:18:09 +01002732 protected:
2733 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002734};
2735
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002736// Represents dex's RETURN opcodes. A HReturn is a control flow
2737// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002738class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002739 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002741 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002742 SetRawInputAt(0, value);
2743 }
2744
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002745 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002746
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002747 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002748
Artem Serovcced8ba2017-07-19 18:18:09 +01002749 protected:
2750 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002751};
2752
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002753class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002754 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002755 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002756 uint32_t reg_number,
2757 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002758 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002759 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002760 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302761 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002762 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002763 SideEffects::None(),
2764 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002765 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002766 number_of_inputs,
2767 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002768 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002769 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002770 // Phis are constructed live and marked dead if conflicting or unused.
2771 // Individual steps of SsaBuilder should assume that if a phi has been
2772 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2773 SetPackedFlag<kFlagIsLive>(true);
2774 SetPackedFlag<kFlagCanBeNull>(true);
2775 }
2776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002777 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002778
David Brazdildee58d62016-04-07 09:54:26 +00002779 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002780 static DataType::Type ToPhiType(DataType::Type type) {
2781 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002782 }
2783
2784 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002787 // Make sure that only valid type changes occur. The following are allowed:
2788 // (1) int -> float/ref (primitive type propagation),
2789 // (2) long -> double (primitive type propagation).
2790 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2792 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2793 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002794 SetPackedField<TypeField>(new_type);
2795 }
2796
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002797 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002798 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2799
2800 uint32_t GetRegNumber() const { return reg_number_; }
2801
2802 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2803 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2804 bool IsDead() const { return !IsLive(); }
2805 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2806
Vladimir Markoe9004912016-06-16 16:50:52 +01002807 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002808 return other != nullptr
2809 && other->IsPhi()
2810 && other->AsPhi()->GetBlock() == GetBlock()
2811 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2812 }
2813
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002814 bool HasEquivalentPhi() const {
2815 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2816 return true;
2817 }
2818 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2819 return true;
2820 }
2821 return false;
2822 }
2823
David Brazdildee58d62016-04-07 09:54:26 +00002824 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2825 // An equivalent phi is a phi having the same dex register and type.
2826 // It assumes that phis with the same dex register are adjacent.
2827 HPhi* GetNextEquivalentPhiWithSameType() {
2828 HInstruction* next = GetNext();
2829 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2830 if (next->GetType() == GetType()) {
2831 return next->AsPhi();
2832 }
2833 next = next->GetNext();
2834 }
2835 return nullptr;
2836 }
2837
2838 DECLARE_INSTRUCTION(Phi);
2839
Artem Serovcced8ba2017-07-19 18:18:09 +01002840 protected:
2841 DEFAULT_COPY_CONSTRUCTOR(Phi);
2842
David Brazdildee58d62016-04-07 09:54:26 +00002843 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002844 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002845 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2846 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2847 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002848
David Brazdildee58d62016-04-07 09:54:26 +00002849 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002850};
2851
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002852// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002853// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002854// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002855class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002856 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302857 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002858 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302859 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002860
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002861 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002862
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002863 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002864
Artem Serovcced8ba2017-07-19 18:18:09 +01002865 protected:
2866 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002867};
2868
2869// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002870class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002871 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302872 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002873 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302874 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002876 bool IsClonable() const override { return true; }
2877 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002878
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002879 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002880 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002881 }
2882
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002883 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002884
Artem Serovcced8ba2017-07-19 18:18:09 +01002885 protected:
2886 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002887};
2888
Roland Levillain9867bc72015-08-05 10:21:34 +01002889class HConstant : public HExpression<0> {
2890 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302891 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2892 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2893 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002894
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002895 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002896
Roland Levillain1a653882016-03-18 18:05:57 +00002897 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002898 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002899 // Is this constant 0 in the arithmetic sense?
2900 virtual bool IsArithmeticZero() const { return false; }
2901 // Is this constant a 0-bit pattern?
2902 virtual bool IsZeroBitPattern() const { return false; }
2903 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002904 virtual bool IsOne() const { return false; }
2905
David Brazdil77a48ae2015-09-15 12:34:04 +00002906 virtual uint64_t GetValueAsUint64() const = 0;
2907
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002908 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002909
Artem Serovcced8ba2017-07-19 18:18:09 +01002910 protected:
2911 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002912};
2913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002914class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002915 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002916 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002917 return true;
2918 }
2919
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002920 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002922 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002923
Roland Levillain1a653882016-03-18 18:05:57 +00002924 // The null constant representation is a 0-bit pattern.
2925 virtual bool IsZeroBitPattern() const { return true; }
2926
Roland Levillain9867bc72015-08-05 10:21:34 +01002927 DECLARE_INSTRUCTION(NullConstant);
2928
Artem Serovcced8ba2017-07-19 18:18:09 +01002929 protected:
2930 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2931
Roland Levillain9867bc72015-08-05 10:21:34 +01002932 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302934 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2935 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002936
2937 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002938};
2939
2940// Constants of the type int. Those can be from Dex instructions, or
2941// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002942class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002943 public:
2944 int32_t GetValue() const { return value_; }
2945
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002946 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002947 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2948 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002950 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002951 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002952 return other->AsIntConstant()->value_ == value_;
2953 }
2954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002955 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002956
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002957 bool IsMinusOne() const override { return GetValue() == -1; }
2958 bool IsArithmeticZero() const override { return GetValue() == 0; }
2959 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2960 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002961
Roland Levillain1a653882016-03-18 18:05:57 +00002962 // Integer constants are used to encode Boolean values as well,
2963 // where 1 means true and 0 means false.
2964 bool IsTrue() const { return GetValue() == 1; }
2965 bool IsFalse() const { return GetValue() == 0; }
2966
Roland Levillain9867bc72015-08-05 10:21:34 +01002967 DECLARE_INSTRUCTION(IntConstant);
2968
Artem Serovcced8ba2017-07-19 18:18:09 +01002969 protected:
2970 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2971
Roland Levillain9867bc72015-08-05 10:21:34 +01002972 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002973 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302974 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2975 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002976 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302977 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2978 value_(value ? 1 : 0) {
2979 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002980
2981 const int32_t value_;
2982
2983 friend class HGraph;
2984 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2985 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002986};
2987
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002988class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002989 public:
2990 int64_t GetValue() const { return value_; }
2991
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002992 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002993
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002994 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002995 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002996 return other->AsLongConstant()->value_ == value_;
2997 }
2998
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002999 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003000
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003001 bool IsMinusOne() const override { return GetValue() == -1; }
3002 bool IsArithmeticZero() const override { return GetValue() == 0; }
3003 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3004 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003005
3006 DECLARE_INSTRUCTION(LongConstant);
3007
Artem Serovcced8ba2017-07-19 18:18:09 +01003008 protected:
3009 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3010
Roland Levillain9867bc72015-08-05 10:21:34 +01003011 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003012 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303013 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3014 value_(value) {
3015 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003016
3017 const int64_t value_;
3018
3019 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003020};
Dave Allison20dfc792014-06-16 20:44:29 -07003021
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003022class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023 public:
3024 float GetValue() const { return value_; }
3025
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003026 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3028 }
3029
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003030 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003031 DCHECK(other->IsFloatConstant()) << other->DebugName();
3032 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3033 }
3034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003035 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003037 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003038 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3039 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003040 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003041 return std::fpclassify(value_) == FP_ZERO;
3042 }
3043 bool IsArithmeticPositiveZero() const {
3044 return IsArithmeticZero() && !std::signbit(value_);
3045 }
3046 bool IsArithmeticNegativeZero() const {
3047 return IsArithmeticZero() && std::signbit(value_);
3048 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003049 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003050 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003051 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003052 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003053 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3054 }
3055 bool IsNaN() const {
3056 return std::isnan(value_);
3057 }
3058
3059 DECLARE_INSTRUCTION(FloatConstant);
3060
Artem Serovcced8ba2017-07-19 18:18:09 +01003061 protected:
3062 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3063
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064 private:
3065 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303066 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3067 value_(value) {
3068 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303070 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3071 value_(bit_cast<float, int32_t>(value)) {
3072 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003073
3074 const float value_;
3075
3076 // Only the SsaBuilder and HGraph can create floating-point constants.
3077 friend class SsaBuilder;
3078 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079};
3080
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003081class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003082 public:
3083 double GetValue() const { return value_; }
3084
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003085 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003086
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003087 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003088 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3089 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3090 }
3091
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003092 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003093
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003094 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003095 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3096 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003097 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003098 return std::fpclassify(value_) == FP_ZERO;
3099 }
3100 bool IsArithmeticPositiveZero() const {
3101 return IsArithmeticZero() && !std::signbit(value_);
3102 }
3103 bool IsArithmeticNegativeZero() const {
3104 return IsArithmeticZero() && std::signbit(value_);
3105 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003106 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003107 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003108 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003109 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3111 }
3112 bool IsNaN() const {
3113 return std::isnan(value_);
3114 }
3115
3116 DECLARE_INSTRUCTION(DoubleConstant);
3117
Artem Serovcced8ba2017-07-19 18:18:09 +01003118 protected:
3119 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3120
Roland Levillain31dd3d62016-02-16 12:21:02 +00003121 private:
3122 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303123 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3124 value_(value) {
3125 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003126 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303127 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3128 value_(bit_cast<double, int64_t>(value)) {
3129 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003130
3131 const double value_;
3132
3133 // Only the SsaBuilder and HGraph can create floating-point constants.
3134 friend class SsaBuilder;
3135 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003136};
3137
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003138// Conditional branch. A block ending with an HIf instruction must have
3139// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003140class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003141 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003142 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003143 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003144 SetRawInputAt(0, input);
3145 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003146
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003147 bool IsClonable() const override { return true; }
3148 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003149
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003150 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003151 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003152 }
3153
3154 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003155 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003156 }
3157
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003158 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003159
Artem Serovcced8ba2017-07-19 18:18:09 +01003160 protected:
3161 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003162};
3163
David Brazdilfc6a86a2015-06-26 10:33:45 +00003164
3165// Abstract instruction which marks the beginning and/or end of a try block and
3166// links it to the respective exception handlers. Behaves the same as a Goto in
3167// non-exceptional control flow.
3168// Normal-flow successor is stored at index zero, exception handlers under
3169// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003170class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003171 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003172 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003173 kEntry,
3174 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003175 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003176 };
3177
Artem Serovd1aa7d02018-06-22 11:35:46 +01003178 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3179 // across the catch block entering edges as GC might happen during throwing an exception.
3180 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3181 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003182 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003183 : HExpression(kTryBoundary,
3184 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3185 : SideEffects::None(),
3186 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003187 SetPackedField<BoundaryKindField>(kind);
3188 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003189
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003190 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003191
3192 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003193 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003194
David Brazdild26a4112015-11-10 11:07:31 +00003195 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3196 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3197 }
3198
David Brazdilfc6a86a2015-06-26 10:33:45 +00003199 // Returns whether `handler` is among its exception handlers (non-zero index
3200 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003201 bool HasExceptionHandler(const HBasicBlock& handler) const {
3202 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003203 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003204 }
3205
3206 // If not present already, adds `handler` to its block's list of exception
3207 // handlers.
3208 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003209 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003210 GetBlock()->AddSuccessor(handler);
3211 }
3212 }
3213
Vladimir Markoa1de9182016-02-25 11:37:38 +00003214 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3215 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003216
David Brazdilffee3d32015-07-06 11:48:53 +01003217 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3218
David Brazdilfc6a86a2015-06-26 10:33:45 +00003219 DECLARE_INSTRUCTION(TryBoundary);
3220
Artem Serovcced8ba2017-07-19 18:18:09 +01003221 protected:
3222 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3223
David Brazdilfc6a86a2015-06-26 10:33:45 +00003224 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003225 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3226 static constexpr size_t kFieldBoundaryKindSize =
3227 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3228 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3229 kFieldBoundaryKind + kFieldBoundaryKindSize;
3230 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3231 "Too many packed fields.");
3232 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003233};
3234
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003235// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003236class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003237 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003238 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3239 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003240 HDeoptimize(ArenaAllocator* allocator,
3241 HInstruction* cond,
3242 DeoptimizationKind kind,
3243 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003244 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303245 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003246 SideEffects::All(),
3247 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003248 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003249 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003250 kArenaAllocMisc) {
3251 SetPackedFlag<kFieldCanBeMoved>(false);
3252 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003253 SetRawInputAt(0, cond);
3254 }
3255
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003256 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003257
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003258 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3259 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3260 // instead of `guard`.
3261 // We set CanTriggerGC to prevent any intermediate address to be live
3262 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003263 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003264 HInstruction* cond,
3265 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003266 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003267 uint32_t dex_pc)
3268 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303269 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003270 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003271 SideEffects::CanTriggerGC(),
3272 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003273 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003274 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003275 kArenaAllocMisc) {
3276 SetPackedFlag<kFieldCanBeMoved>(true);
3277 SetPackedField<DeoptimizeKindField>(kind);
3278 SetRawInputAt(0, cond);
3279 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003280 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003282 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003284 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003285 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3286 }
3287
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003288 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003289
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003290 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003291
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003292 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003293
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003294 bool GuardsAnInput() const {
3295 return InputCount() == 2;
3296 }
3297
3298 HInstruction* GuardedInput() const {
3299 DCHECK(GuardsAnInput());
3300 return InputAt(1);
3301 }
3302
3303 void RemoveGuard() {
3304 RemoveInputAt(1);
3305 }
3306
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003307 DECLARE_INSTRUCTION(Deoptimize);
3308
Artem Serovcced8ba2017-07-19 18:18:09 +01003309 protected:
3310 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3311
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003312 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003313 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3314 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3315 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003316 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003317 static constexpr size_t kNumberOfDeoptimizePackedBits =
3318 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3319 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3320 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003321 using DeoptimizeKindField =
3322 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003323};
3324
Mingyao Yang063fc772016-08-02 11:02:54 -07003325// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3326// The compiled code checks this flag value in a guard before devirtualized call and
3327// if it's true, starts to do deoptimization.
3328// It has a 4-byte slot on stack.
3329// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003330class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003331 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003332 // CHA guards are only optimized in a separate pass and it has no side effects
3333 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003334 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303335 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003336 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303337 SideEffects::None(),
3338 dex_pc,
3339 allocator,
3340 0,
3341 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003342 }
3343
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003344 // We do all CHA guard elimination/motion in a single pass, after which there is no
3345 // further guard elimination/motion since a guard might have been used for justification
3346 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3347 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003348 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003349
3350 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3351
Artem Serovcced8ba2017-07-19 18:18:09 +01003352 protected:
3353 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003354};
3355
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003356// Represents the ArtMethod that was passed as a first argument to
3357// the method. It is used by instructions that depend on it, like
3358// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003359class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003360 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003361 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303362 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3363 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003364
3365 DECLARE_INSTRUCTION(CurrentMethod);
3366
Artem Serovcced8ba2017-07-19 18:18:09 +01003367 protected:
3368 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003369};
3370
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003371// Fetches an ArtMethod from the virtual table or the interface method table
3372// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003373class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003374 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003375 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003376 kVTable,
3377 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003378 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003379 };
3380 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003381 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003382 TableKind kind,
3383 size_t index,
3384 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303385 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003386 index_(index) {
3387 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003388 SetRawInputAt(0, cls);
3389 }
3390
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003391 bool IsClonable() const override { return true; }
3392 bool CanBeMoved() const override { return true; }
3393 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003394 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003395 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003396 }
3397
Vladimir Markoa1de9182016-02-25 11:37:38 +00003398 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003399 size_t GetIndex() const { return index_; }
3400
3401 DECLARE_INSTRUCTION(ClassTableGet);
3402
Artem Serovcced8ba2017-07-19 18:18:09 +01003403 protected:
3404 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3405
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003406 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003407 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003408 static constexpr size_t kFieldTableKindSize =
3409 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3410 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3411 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3412 "Too many packed fields.");
3413 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3414
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003415 // The index of the ArtMethod in the table.
3416 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003417};
3418
Mark Mendellfe57faa2015-09-18 09:26:15 -04003419// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3420// have one successor for each entry in the switch table, and the final successor
3421// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003422class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003423 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003424 HPackedSwitch(int32_t start_value,
3425 uint32_t num_entries,
3426 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003427 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003428 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003429 start_value_(start_value),
3430 num_entries_(num_entries) {
3431 SetRawInputAt(0, input);
3432 }
3433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003434 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003436 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003437
3438 int32_t GetStartValue() const { return start_value_; }
3439
Vladimir Marko211c2112015-09-24 16:52:33 +01003440 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003441
3442 HBasicBlock* GetDefaultBlock() const {
3443 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003444 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003445 }
3446 DECLARE_INSTRUCTION(PackedSwitch);
3447
Artem Serovcced8ba2017-07-19 18:18:09 +01003448 protected:
3449 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3450
Mark Mendellfe57faa2015-09-18 09:26:15 -04003451 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003452 const int32_t start_value_;
3453 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003454};
3455
Roland Levillain88cb1752014-10-20 16:36:47 +01003456class HUnaryOperation : public HExpression<1> {
3457 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303458 HUnaryOperation(InstructionKind kind,
3459 DataType::Type result_type,
3460 HInstruction* input,
3461 uint32_t dex_pc = kNoDexPc)
3462 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003463 SetRawInputAt(0, input);
3464 }
3465
Artem Serovcced8ba2017-07-19 18:18:09 +01003466 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003467 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003468
Roland Levillain88cb1752014-10-20 16:36:47 +01003469 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003470 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003471
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003472 bool CanBeMoved() const override { return true; }
3473 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003474 return true;
3475 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003476
Roland Levillain31dd3d62016-02-16 12:21:02 +00003477 // Try to statically evaluate `this` and return a HConstant
3478 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003479 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003480 HConstant* TryStaticEvaluation() const;
3481
3482 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003483 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3484 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003485 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3486 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003487
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003488 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003489
Artem Serovcced8ba2017-07-19 18:18:09 +01003490 protected:
3491 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003492};
3493
Dave Allison20dfc792014-06-16 20:44:29 -07003494class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003495 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303496 HBinaryOperation(InstructionKind kind,
3497 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003498 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003499 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003500 SideEffects side_effects = SideEffects::None(),
3501 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303502 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003503 SetRawInputAt(0, left);
3504 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003505 }
3506
Artem Serovcced8ba2017-07-19 18:18:09 +01003507 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003508 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003509
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003510 HInstruction* GetLeft() const { return InputAt(0); }
3511 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003513
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003514 virtual bool IsCommutative() const { return false; }
3515
3516 // Put constant on the right.
3517 // Returns whether order is changed.
3518 bool OrderInputsWithConstantOnTheRight() {
3519 HInstruction* left = InputAt(0);
3520 HInstruction* right = InputAt(1);
3521 if (left->IsConstant() && !right->IsConstant()) {
3522 ReplaceInput(right, 0);
3523 ReplaceInput(left, 1);
3524 return true;
3525 }
3526 return false;
3527 }
3528
3529 // Order inputs by instruction id, but favor constant on the right side.
3530 // This helps GVN for commutative ops.
3531 void OrderInputs() {
3532 DCHECK(IsCommutative());
3533 HInstruction* left = InputAt(0);
3534 HInstruction* right = InputAt(1);
3535 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3536 return;
3537 }
3538 if (OrderInputsWithConstantOnTheRight()) {
3539 return;
3540 }
3541 // Order according to instruction id.
3542 if (left->GetId() > right->GetId()) {
3543 ReplaceInput(right, 0);
3544 ReplaceInput(left, 1);
3545 }
3546 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003547
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003548 bool CanBeMoved() const override { return true; }
3549 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003550 return true;
3551 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003552
Roland Levillain31dd3d62016-02-16 12:21:02 +00003553 // Try to statically evaluate `this` and return a HConstant
3554 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003555 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003556 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003557
3558 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003559 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3560 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003561 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3562 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003563 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003564 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3565 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003566 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3567 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003568 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3569 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003570 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003571 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3572 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003573
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003574 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003575 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003576 HConstant* GetConstantRight() const;
3577
3578 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003579 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003580 HInstruction* GetLeastConstantLeft() const;
3581
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003582 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003583
Artem Serovcced8ba2017-07-19 18:18:09 +01003584 protected:
3585 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003586};
3587
Mark Mendellc4701932015-04-10 13:18:51 -04003588// The comparison bias applies for floating point operations and indicates how NaN
3589// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003590enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003591 kNoBias, // bias is not applicable (i.e. for long operation)
3592 kGtBias, // return 1 for NaN comparisons
3593 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003594 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003595};
3596
Roland Levillain31dd3d62016-02-16 12:21:02 +00003597std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3598
Dave Allison20dfc792014-06-16 20:44:29 -07003599class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003600 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303601 HCondition(InstructionKind kind,
3602 HInstruction* first,
3603 HInstruction* second,
3604 uint32_t dex_pc = kNoDexPc)
3605 : HBinaryOperation(kind,
3606 DataType::Type::kBool,
3607 first,
3608 second,
3609 SideEffects::None(),
3610 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003611 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3612 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003613
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003614 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003615 // `instruction`, and disregard moves in between.
3616 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003617
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003618 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003619
3620 virtual IfCondition GetCondition() const = 0;
3621
Mark Mendellc4701932015-04-10 13:18:51 -04003622 virtual IfCondition GetOppositeCondition() const = 0;
3623
Vladimir Markoa1de9182016-02-25 11:37:38 +00003624 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003625 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3626
Vladimir Markoa1de9182016-02-25 11:37:38 +00003627 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3628 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003629
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003630 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003631 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003632 }
3633
Roland Levillain4fa13f62015-07-06 18:11:54 +01003634 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003635 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003636 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003637 if (if_cond == kCondNE) {
3638 return true;
3639 } else if (if_cond == kCondEQ) {
3640 return false;
3641 }
3642 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003643 }
3644
3645 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003646 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003647 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003648 if (if_cond == kCondEQ) {
3649 return true;
3650 } else if (if_cond == kCondNE) {
3651 return false;
3652 }
3653 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003654 }
3655
Roland Levillain31dd3d62016-02-16 12:21:02 +00003656 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003657 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003658 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003659 static constexpr size_t kFieldComparisonBiasSize =
3660 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3661 static constexpr size_t kNumberOfConditionPackedBits =
3662 kFieldComparisonBias + kFieldComparisonBiasSize;
3663 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3664 using ComparisonBiasField =
3665 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3666
Roland Levillain31dd3d62016-02-16 12:21:02 +00003667 template <typename T>
3668 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3669
3670 template <typename T>
3671 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003672 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003673 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3674 // Handle the bias.
3675 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3676 }
3677
3678 // Return an integer constant containing the result of a condition evaluated at compile time.
3679 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3680 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3681 }
3682
Artem Serovcced8ba2017-07-19 18:18:09 +01003683 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003684};
3685
3686// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003687class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003688 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003689 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303690 : HCondition(kEqual, first, second, dex_pc) {
3691 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003692
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003693 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003694
Vladimir Marko9e23df52015-11-10 17:14:35 +00003695 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003696 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003697 return MakeConstantCondition(true, GetDexPc());
3698 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003699 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003700 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3701 }
3702 // In the following Evaluate methods, a HCompare instruction has
3703 // been merged into this HEqual instruction; evaluate it as
3704 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003705 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003706 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3707 GetDexPc());
3708 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003709 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003710 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3711 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003712 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003713 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003714 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003715
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003716 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003717
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003718 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003719 return kCondEQ;
3720 }
3721
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003722 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003723 return kCondNE;
3724 }
3725
Artem Serovcced8ba2017-07-19 18:18:09 +01003726 protected:
3727 DEFAULT_COPY_CONSTRUCTOR(Equal);
3728
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003729 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003730 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003731};
3732
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003733class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003734 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303735 HNotEqual(HInstruction* first, HInstruction* second,
3736 uint32_t dex_pc = kNoDexPc)
3737 : HCondition(kNotEqual, first, second, dex_pc) {
3738 }
Dave Allison20dfc792014-06-16 20:44:29 -07003739
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003740 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003741
Vladimir Marko9e23df52015-11-10 17:14:35 +00003742 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003743 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003744 return MakeConstantCondition(false, GetDexPc());
3745 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003746 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003747 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3748 }
3749 // In the following Evaluate methods, a HCompare instruction has
3750 // been merged into this HNotEqual instruction; evaluate it as
3751 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003752 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003753 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3754 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003755 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003756 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3757 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003758 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003759 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003760 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003761
Dave Allison20dfc792014-06-16 20:44:29 -07003762 DECLARE_INSTRUCTION(NotEqual);
3763
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003764 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003765 return kCondNE;
3766 }
3767
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003768 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003769 return kCondEQ;
3770 }
3771
Artem Serovcced8ba2017-07-19 18:18:09 +01003772 protected:
3773 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3774
Dave Allison20dfc792014-06-16 20:44:29 -07003775 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003776 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003777};
3778
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003779class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003780 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303781 HLessThan(HInstruction* first, HInstruction* second,
3782 uint32_t dex_pc = kNoDexPc)
3783 : HCondition(kLessThan, first, second, dex_pc) {
3784 }
Dave Allison20dfc792014-06-16 20:44:29 -07003785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003786 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003787 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003788 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003789 // In the following Evaluate methods, a HCompare instruction has
3790 // been merged into this HLessThan instruction; evaluate it as
3791 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003792 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003793 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3794 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003795 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003796 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3797 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003798 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003799 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003800 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003801
Dave Allison20dfc792014-06-16 20:44:29 -07003802 DECLARE_INSTRUCTION(LessThan);
3803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003804 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003805 return kCondLT;
3806 }
3807
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003808 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003809 return kCondGE;
3810 }
3811
Artem Serovcced8ba2017-07-19 18:18:09 +01003812 protected:
3813 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3814
Dave Allison20dfc792014-06-16 20:44:29 -07003815 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003816 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003817};
3818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003819class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003820 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303821 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3822 uint32_t dex_pc = kNoDexPc)
3823 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3824 }
Dave Allison20dfc792014-06-16 20:44:29 -07003825
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003826 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003827 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003828 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003829 // In the following Evaluate methods, a HCompare instruction has
3830 // been merged into this HLessThanOrEqual instruction; evaluate it as
3831 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003832 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003833 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3834 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003835 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003836 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3837 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003838 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003839 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003840 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003841
Dave Allison20dfc792014-06-16 20:44:29 -07003842 DECLARE_INSTRUCTION(LessThanOrEqual);
3843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003844 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003845 return kCondLE;
3846 }
3847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003848 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003849 return kCondGT;
3850 }
3851
Artem Serovcced8ba2017-07-19 18:18:09 +01003852 protected:
3853 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3854
Dave Allison20dfc792014-06-16 20:44:29 -07003855 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003856 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003857};
3858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003859class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003860 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003861 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303862 : HCondition(kGreaterThan, first, second, dex_pc) {
3863 }
Dave Allison20dfc792014-06-16 20:44:29 -07003864
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003865 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003866 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003867 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003868 // In the following Evaluate methods, a HCompare instruction has
3869 // been merged into this HGreaterThan instruction; evaluate it as
3870 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003871 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003872 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3873 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003874 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003875 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3876 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003877 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003878 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003879 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003880
Dave Allison20dfc792014-06-16 20:44:29 -07003881 DECLARE_INSTRUCTION(GreaterThan);
3882
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003883 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003884 return kCondGT;
3885 }
3886
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003887 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003888 return kCondLE;
3889 }
3890
Artem Serovcced8ba2017-07-19 18:18:09 +01003891 protected:
3892 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3893
Dave Allison20dfc792014-06-16 20:44:29 -07003894 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003895 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003896};
3897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003898class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003899 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003900 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303901 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3902 }
Dave Allison20dfc792014-06-16 20:44:29 -07003903
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003904 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003905 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003906 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003907 // In the following Evaluate methods, a HCompare instruction has
3908 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3909 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003910 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003911 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3912 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003913 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003914 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3915 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003916 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003917 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003918 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003919
Dave Allison20dfc792014-06-16 20:44:29 -07003920 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003922 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003923 return kCondGE;
3924 }
3925
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003926 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003927 return kCondLT;
3928 }
3929
Artem Serovcced8ba2017-07-19 18:18:09 +01003930 protected:
3931 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3932
Dave Allison20dfc792014-06-16 20:44:29 -07003933 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003934 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003935};
3936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003937class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003938 public:
3939 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303940 : HCondition(kBelow, first, second, dex_pc) {
3941 }
Aart Bike9f37602015-10-09 11:15:55 -07003942
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003943 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003944 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003945 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003946 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003947 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3948 }
3949 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003950 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003951 LOG(FATAL) << DebugName() << " is not defined for float values";
3952 UNREACHABLE();
3953 }
3954 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003955 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003956 LOG(FATAL) << DebugName() << " is not defined for double values";
3957 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003958 }
3959
3960 DECLARE_INSTRUCTION(Below);
3961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003962 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003963 return kCondB;
3964 }
3965
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003966 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003967 return kCondAE;
3968 }
3969
Artem Serovcced8ba2017-07-19 18:18:09 +01003970 protected:
3971 DEFAULT_COPY_CONSTRUCTOR(Below);
3972
Aart Bike9f37602015-10-09 11:15:55 -07003973 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003974 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003975 return MakeUnsigned(x) < MakeUnsigned(y);
3976 }
Aart Bike9f37602015-10-09 11:15:55 -07003977};
3978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003979class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003980 public:
3981 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303982 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3983 }
Aart Bike9f37602015-10-09 11:15:55 -07003984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003985 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003986 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003987 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003988 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003989 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3990 }
3991 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003992 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003993 LOG(FATAL) << DebugName() << " is not defined for float values";
3994 UNREACHABLE();
3995 }
3996 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003997 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003998 LOG(FATAL) << DebugName() << " is not defined for double values";
3999 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004000 }
4001
4002 DECLARE_INSTRUCTION(BelowOrEqual);
4003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004004 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004005 return kCondBE;
4006 }
4007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004008 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004009 return kCondA;
4010 }
4011
Artem Serovcced8ba2017-07-19 18:18:09 +01004012 protected:
4013 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4014
Aart Bike9f37602015-10-09 11:15:55 -07004015 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004016 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004017 return MakeUnsigned(x) <= MakeUnsigned(y);
4018 }
Aart Bike9f37602015-10-09 11:15:55 -07004019};
4020
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004021class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004022 public:
4023 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304024 : HCondition(kAbove, first, second, dex_pc) {
4025 }
Aart Bike9f37602015-10-09 11:15:55 -07004026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004027 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004028 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004029 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004030 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004031 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4032 }
4033 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004034 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004035 LOG(FATAL) << DebugName() << " is not defined for float values";
4036 UNREACHABLE();
4037 }
4038 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004039 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004040 LOG(FATAL) << DebugName() << " is not defined for double values";
4041 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004042 }
4043
4044 DECLARE_INSTRUCTION(Above);
4045
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004046 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004047 return kCondA;
4048 }
4049
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004050 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004051 return kCondBE;
4052 }
4053
Artem Serovcced8ba2017-07-19 18:18:09 +01004054 protected:
4055 DEFAULT_COPY_CONSTRUCTOR(Above);
4056
Aart Bike9f37602015-10-09 11:15:55 -07004057 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004058 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004059 return MakeUnsigned(x) > MakeUnsigned(y);
4060 }
Aart Bike9f37602015-10-09 11:15:55 -07004061};
4062
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004063class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004064 public:
4065 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304066 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4067 }
Aart Bike9f37602015-10-09 11:15:55 -07004068
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004069 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004070 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004071 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004072 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004073 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4074 }
4075 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004076 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004077 LOG(FATAL) << DebugName() << " is not defined for float values";
4078 UNREACHABLE();
4079 }
4080 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004081 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004082 LOG(FATAL) << DebugName() << " is not defined for double values";
4083 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004084 }
4085
4086 DECLARE_INSTRUCTION(AboveOrEqual);
4087
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004088 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004089 return kCondAE;
4090 }
4091
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004092 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004093 return kCondB;
4094 }
4095
Artem Serovcced8ba2017-07-19 18:18:09 +01004096 protected:
4097 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4098
Aart Bike9f37602015-10-09 11:15:55 -07004099 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004100 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004101 return MakeUnsigned(x) >= MakeUnsigned(y);
4102 }
Aart Bike9f37602015-10-09 11:15:55 -07004103};
Dave Allison20dfc792014-06-16 20:44:29 -07004104
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004105// Instruction to check how two inputs compare to each other.
4106// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004107class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004108 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004109 // Note that `comparison_type` is the type of comparison performed
4110 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004111 // HCompare instruction (which is always DataType::Type::kInt).
4112 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004113 HInstruction* first,
4114 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004115 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004116 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304117 : HBinaryOperation(kCompare,
4118 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004119 first,
4120 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004121 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004122 dex_pc) {
4123 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004124 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4125 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004126 }
4127
Roland Levillain9867bc72015-08-05 10:21:34 +01004128 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004129 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4130
4131 template <typename T>
4132 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004133 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004134 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4135 // Handle the bias.
4136 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4137 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004138
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004139 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004140 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4141 // reach this code path when processing a freshly built HIR
4142 // graph. However HCompare integer instructions can be synthesized
4143 // by the instruction simplifier to implement IntegerCompare and
4144 // IntegerSignum intrinsics, so we have to handle this case.
4145 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004146 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004147 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004148 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4149 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004150 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004151 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4152 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004153 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004154 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004155 }
4156
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004157 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004158 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004159 }
4160
Vladimir Markoa1de9182016-02-25 11:37:38 +00004161 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004162
Roland Levillain31dd3d62016-02-16 12:21:02 +00004163 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004164 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004165 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004166 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004167 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004168 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004169
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004170 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004171 // Comparisons do not require a runtime call in any back end.
4172 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004173 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004174
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004175 DECLARE_INSTRUCTION(Compare);
4176
Roland Levillain31dd3d62016-02-16 12:21:02 +00004177 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004178 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004179 static constexpr size_t kFieldComparisonBiasSize =
4180 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4181 static constexpr size_t kNumberOfComparePackedBits =
4182 kFieldComparisonBias + kFieldComparisonBiasSize;
4183 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4184 using ComparisonBiasField =
4185 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4186
Roland Levillain31dd3d62016-02-16 12:21:02 +00004187 // Return an integer constant containing the result of a comparison evaluated at compile time.
4188 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4189 DCHECK(value == -1 || value == 0 || value == 1) << value;
4190 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4191 }
4192
Artem Serovcced8ba2017-07-19 18:18:09 +01004193 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004194};
4195
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004196class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004197 public:
4198 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004199 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004200 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004201 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004202 bool finalizable,
4203 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304204 : HExpression(kNewInstance,
4205 DataType::Type::kReference,
4206 SideEffects::CanTriggerGC(),
4207 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004208 type_index_(type_index),
4209 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004210 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004211 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004212 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004213 }
4214
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004215 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004216
Andreas Gampea5b09a62016-11-17 15:21:22 -08004217 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004218 const DexFile& GetDexFile() const { return dex_file_; }
4219
4220 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004221 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004222
Mingyao Yang062157f2016-03-02 10:15:36 -08004223 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004224 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004225
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004226 bool NeedsChecks() const {
4227 return entrypoint_ == kQuickAllocObjectWithChecks;
4228 }
David Brazdil6de19382016-01-08 17:37:10 +00004229
Vladimir Markoa1de9182016-02-25 11:37:38 +00004230 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004231
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004232 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004233
4234 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4235
4236 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4237 entrypoint_ = entrypoint;
4238 }
4239
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004240 HLoadClass* GetLoadClass() const {
4241 HInstruction* input = InputAt(0);
4242 if (input->IsClinitCheck()) {
4243 input = input->InputAt(0);
4244 }
4245 DCHECK(input->IsLoadClass());
4246 return input->AsLoadClass();
4247 }
4248
David Brazdil6de19382016-01-08 17:37:10 +00004249 bool IsStringAlloc() const;
4250
4251 DECLARE_INSTRUCTION(NewInstance);
4252
Artem Serovcced8ba2017-07-19 18:18:09 +01004253 protected:
4254 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4255
David Brazdil6de19382016-01-08 17:37:10 +00004256 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004257 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004258 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4259 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4260 "Too many packed fields.");
4261
Andreas Gampea5b09a62016-11-17 15:21:22 -08004262 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004263 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004264 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004265};
4266
Agi Csaki05f20562015-08-19 14:58:14 -07004267enum IntrinsicNeedsEnvironmentOrCache {
4268 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4269 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004270};
4271
Aart Bik5d75afe2015-12-14 11:57:01 -08004272enum IntrinsicSideEffects {
4273 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4274 kReadSideEffects, // Intrinsic may read heap memory.
4275 kWriteSideEffects, // Intrinsic may write heap memory.
4276 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4277};
4278
4279enum IntrinsicExceptions {
4280 kNoThrow, // Intrinsic does not throw any exceptions.
4281 kCanThrow // Intrinsic may throw exceptions.
4282};
4283
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004284class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004285 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004286 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004287
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004288 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004289 SetRawInputAt(index, argument);
4290 }
4291
Roland Levillain3e3d7332015-04-28 11:00:54 +01004292 // Return the number of arguments. This number can be lower than
4293 // the number of inputs returned by InputCount(), as some invoke
4294 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4295 // inputs at the end of their list of inputs.
4296 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4297
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004298 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4299
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004300 InvokeType GetInvokeType() const {
4301 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004302 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004303
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004304 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004305 return intrinsic_;
4306 }
4307
Aart Bik5d75afe2015-12-14 11:57:01 -08004308 void SetIntrinsic(Intrinsics intrinsic,
4309 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4310 IntrinsicSideEffects side_effects,
4311 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004312
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004313 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004314 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004315 }
4316
Aart Bikff7d89c2016-11-07 08:49:28 -08004317 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4318
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004319 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004320
Aart Bika8b8e9b2018-01-09 11:01:02 -08004321 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004323 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004324
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004325 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004326
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004327 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004328 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4329 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004330
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004331 uint32_t* GetIntrinsicOptimizations() {
4332 return &intrinsic_optimizations_;
4333 }
4334
4335 const uint32_t* GetIntrinsicOptimizations() const {
4336 return &intrinsic_optimizations_;
4337 }
4338
4339 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4340
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004341 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004342 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004343
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004344 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004345
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004346 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004347 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4348 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004349 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004350 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004351 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4352 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004353 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004354 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004355
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304356 HInvoke(InstructionKind kind,
4357 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004358 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004359 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004360 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004361 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004362 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004363 ArtMethod* resolved_method,
4364 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004365 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304366 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004367 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004368 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4369 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004370 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004371 number_of_arguments + number_of_other_inputs,
4372 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004373 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004374 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004375 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004376 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004377 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004378 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004379 // Check mutator lock, constructors lack annotalysis support.
4380 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4381 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004382 }
4383
Artem Serovcced8ba2017-07-19 18:18:09 +01004384 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4385
Roland Levillain3e3d7332015-04-28 11:00:54 +01004386 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004387 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004388 const uint32_t dex_method_index_;
4389 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004390
4391 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4392 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004393};
4394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004395class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004396 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004397 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004398 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004399 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004400 uint32_t dex_pc,
4401 uint32_t dex_method_index,
4402 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304403 : HInvoke(kInvokeUnresolved,
4404 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004405 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004406 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004407 return_type,
4408 dex_pc,
4409 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004410 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004411 invoke_type) {
4412 }
4413
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004414 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004415
Calin Juravle175dc732015-08-25 15:42:32 +01004416 DECLARE_INSTRUCTION(InvokeUnresolved);
4417
Artem Serovcced8ba2017-07-19 18:18:09 +01004418 protected:
4419 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004420};
4421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004422class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004423 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004424 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004425 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004426 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004427 uint32_t dex_pc,
4428 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304429 : HInvoke(kInvokePolymorphic,
4430 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004431 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004432 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004433 return_type,
4434 dex_pc,
4435 dex_method_index,
4436 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304437 kVirtual) {
4438 }
Orion Hodsonac141392017-01-13 11:53:47 +00004439
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004440 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004441
Orion Hodsonac141392017-01-13 11:53:47 +00004442 DECLARE_INSTRUCTION(InvokePolymorphic);
4443
Artem Serovcced8ba2017-07-19 18:18:09 +01004444 protected:
4445 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004446};
4447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004448class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004449 public:
4450 HInvokeCustom(ArenaAllocator* allocator,
4451 uint32_t number_of_arguments,
4452 uint32_t call_site_index,
4453 DataType::Type return_type,
4454 uint32_t dex_pc)
4455 : HInvoke(kInvokeCustom,
4456 allocator,
4457 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004458 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004459 return_type,
4460 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004461 /* dex_method_index= */ dex::kDexNoIndex,
4462 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004463 kStatic),
4464 call_site_index_(call_site_index) {
4465 }
4466
4467 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4468
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004469 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004470
4471 DECLARE_INSTRUCTION(InvokeCustom);
4472
4473 protected:
4474 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4475
4476 private:
4477 uint32_t call_site_index_;
4478};
4479
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004480class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004481 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004482 // Requirements of this method call regarding the class
4483 // initialization (clinit) check of its declaring class.
4484 enum class ClinitCheckRequirement {
4485 kNone, // Class already initialized.
4486 kExplicit, // Static call having explicit clinit check as last input.
4487 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004488 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004489 };
4490
Vladimir Marko58155012015-08-19 12:49:41 +00004491 // Determines how to load the target ArtMethod*.
4492 enum class MethodLoadKind {
4493 // Use a String init ArtMethod* loaded from Thread entrypoints.
4494 kStringInit,
4495
4496 // Use the method's own ArtMethod* loaded by the register allocator.
4497 kRecursive,
4498
Vladimir Marko65979462017-05-19 17:25:12 +01004499 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4500 // Used for boot image methods referenced by boot image code.
4501 kBootImageLinkTimePcRelative,
4502
Vladimir Markob066d432018-01-03 13:14:37 +00004503 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4504 // Used for app->boot calls with relocatable image.
4505 kBootImageRelRo,
4506
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004507 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004508 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004509 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004510
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004511 // Use ArtMethod* at a known address, embed the direct address in the code.
4512 // Used for for JIT-compiled calls.
4513 kJitDirectAddress,
4514
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004515 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4516 // used when other kinds are unimplemented on a particular architecture.
4517 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004518 };
4519
4520 // Determines the location of the code pointer.
4521 enum class CodePtrLocation {
4522 // Recursive call, use local PC-relative call instruction.
4523 kCallSelf,
4524
Vladimir Marko58155012015-08-19 12:49:41 +00004525 // Use code pointer from the ArtMethod*.
4526 // Used when we don't know the target code. This is also the last-resort-kind used when
4527 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4528 kCallArtMethod,
4529 };
4530
4531 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004532 MethodLoadKind method_load_kind;
4533 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004534 // The method load data holds
4535 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4536 // Note that there are multiple string init methods, each having its own offset.
4537 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004538 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004539 };
4540
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004541 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004542 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004543 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004544 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004545 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004546 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004547 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004548 InvokeType invoke_type,
4549 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004550 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304551 : HInvoke(kInvokeStaticOrDirect,
4552 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004553 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004554 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004555 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004556 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004557 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004558 return_type,
4559 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004560 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004561 resolved_method,
4562 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004563 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004564 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004565 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4566 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004567
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004568 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004569
Vladimir Markodc151b22015-10-15 18:02:30 +01004570 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004571 bool had_current_method_input = HasCurrentMethodInput();
4572 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4573
4574 // Using the current method is the default and once we find a better
4575 // method load kind, we should not go back to using the current method.
4576 DCHECK(had_current_method_input || !needs_current_method_input);
4577
4578 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004579 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4580 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004581 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004582 dispatch_info_ = dispatch_info;
4583 }
4584
Aart Bik6daebeb2017-04-03 14:35:41 -07004585 DispatchInfo GetDispatchInfo() const {
4586 return dispatch_info_;
4587 }
4588
Vladimir Markoc53c0792015-11-19 15:48:33 +00004589 void AddSpecialInput(HInstruction* input) {
4590 // We allow only one special input.
4591 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4592 DCHECK(InputCount() == GetSpecialInputIndex() ||
4593 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4594 InsertInputAt(GetSpecialInputIndex(), input);
4595 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004596
Vladimir Marko372f10e2016-05-17 16:30:10 +01004597 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004598 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004599 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4600 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4601 DCHECK(!input_records.empty());
4602 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4603 HInstruction* last_input = input_records.back().GetInstruction();
4604 // Note: `last_input` may be null during arguments setup.
4605 if (last_input != nullptr) {
4606 // `last_input` is the last input of a static invoke marked as having
4607 // an explicit clinit check. It must either be:
4608 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4609 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4610 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4611 }
4612 }
4613 return input_records;
4614 }
4615
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004616 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004617 // We access the method via the dex cache so we can't do an implicit null check.
4618 // TODO: for intrinsics we can generate implicit null checks.
4619 return false;
4620 }
4621
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004622 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004623 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004624 }
4625
Vladimir Markoc53c0792015-11-19 15:48:33 +00004626 // Get the index of the special input, if any.
4627 //
David Brazdil6de19382016-01-08 17:37:10 +00004628 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4629 // method pointer; otherwise there may be one platform-specific special input,
4630 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004631 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004632 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004633
Vladimir Marko58155012015-08-19 12:49:41 +00004634 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4635 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4636 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004637 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004638 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004639 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004640 bool HasPcRelativeMethodLoadKind() const {
4641 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004642 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004643 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004644 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004645 bool HasCurrentMethodInput() const {
4646 // This function can be called only after the invoke has been fully initialized by the builder.
4647 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004648 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004649 return true;
4650 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004651 DCHECK(InputCount() == GetSpecialInputIndex() ||
4652 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004653 return false;
4654 }
4655 }
Vladimir Marko58155012015-08-19 12:49:41 +00004656
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004657 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004658 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004659 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004660 }
4661
4662 uint64_t GetMethodAddress() const {
4663 DCHECK(HasMethodAddress());
4664 return dispatch_info_.method_load_data;
4665 }
4666
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004667 const DexFile& GetDexFileForPcRelativeDexCache() const;
4668
Vladimir Markoa1de9182016-02-25 11:37:38 +00004669 ClinitCheckRequirement GetClinitCheckRequirement() const {
4670 return GetPackedField<ClinitCheckRequirementField>();
4671 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004672
Roland Levillain4c0eb422015-04-24 16:43:49 +01004673 // Is this instruction a call to a static method?
4674 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004675 return GetInvokeType() == kStatic;
4676 }
4677
4678 MethodReference GetTargetMethod() const {
4679 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004680 }
4681
Vladimir Markofbb184a2015-11-13 14:47:00 +00004682 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4683 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4684 // instruction; only relevant for static calls with explicit clinit check.
4685 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004686 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004687 size_t last_input_index = inputs_.size() - 1u;
4688 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004689 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004690 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004691 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004692 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004693 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004694 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004695 }
4696
4697 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004698 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004699 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004700 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004701 }
4702
4703 // Is this a call to a static method whose declaring class has an
4704 // implicit intialization check requirement?
4705 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004706 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004707 }
4708
Vladimir Markob554b5a2015-11-06 12:57:55 +00004709 // Does this method load kind need the current method as an input?
4710 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004711 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004712 }
4713
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004714 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004715
Artem Serovcced8ba2017-07-19 18:18:09 +01004716 protected:
4717 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4718
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004719 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004720 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004721 static constexpr size_t kFieldClinitCheckRequirementSize =
4722 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4723 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4724 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4725 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4726 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004727 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4728 kFieldClinitCheckRequirement,
4729 kFieldClinitCheckRequirementSize>;
4730
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004731 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004732 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004733 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004734};
Vladimir Markof64242a2015-12-01 14:58:23 +00004735std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004736std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004737
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004738class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004739 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004740 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004741 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004742 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004743 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004744 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004745 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004746 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304747 : HInvoke(kInvokeVirtual,
4748 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004749 number_of_arguments,
4750 0u,
4751 return_type,
4752 dex_pc,
4753 dex_method_index,
4754 resolved_method,
4755 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304756 vtable_index_(vtable_index) {
4757 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004759 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004760
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004761 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004762 switch (GetIntrinsic()) {
4763 case Intrinsics::kThreadCurrentThread:
4764 case Intrinsics::kStringBufferAppend:
4765 case Intrinsics::kStringBufferToString:
4766 case Intrinsics::kStringBuilderAppend:
4767 case Intrinsics::kStringBuilderToString:
4768 return false;
4769 default:
4770 return HInvoke::CanBeNull();
4771 }
4772 }
4773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004774 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004775 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004776 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004777 }
4778
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004779 uint32_t GetVTableIndex() const { return vtable_index_; }
4780
4781 DECLARE_INSTRUCTION(InvokeVirtual);
4782
Artem Serovcced8ba2017-07-19 18:18:09 +01004783 protected:
4784 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4785
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004786 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004787 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004788 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004789};
4790
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004791class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004792 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004793 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004794 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004796 uint32_t dex_pc,
4797 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004798 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004799 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304800 : HInvoke(kInvokeInterface,
4801 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004802 number_of_arguments,
4803 0u,
4804 return_type,
4805 dex_pc,
4806 dex_method_index,
4807 resolved_method,
4808 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304809 imt_index_(imt_index) {
4810 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004811
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004812 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004814 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004815 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004816 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004817 }
4818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004819 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004820 // The assembly stub currently needs it.
4821 return true;
4822 }
4823
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004824 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004825
4826 DECLARE_INSTRUCTION(InvokeInterface);
4827
Artem Serovcced8ba2017-07-19 18:18:09 +01004828 protected:
4829 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4830
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004831 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004832 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004833 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004834};
4835
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004836class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004837 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004838 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304839 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004840 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004841 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004842
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004843 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004845 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004847 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004848 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004849 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004850 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004851 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004852 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4853 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004854 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004855 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4856 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004857
Roland Levillain88cb1752014-10-20 16:36:47 +01004858 DECLARE_INSTRUCTION(Neg);
4859
Artem Serovcced8ba2017-07-19 18:18:09 +01004860 protected:
4861 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004862};
4863
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004864class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004865 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004866 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304867 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004868 SetRawInputAt(0, cls);
4869 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004870 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004871 }
4872
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004873 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004874
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004875 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004876 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004877
Mingyao Yang0c365e62015-03-31 15:09:29 -07004878 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004879 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004881 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004882
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004883 HLoadClass* GetLoadClass() const {
4884 DCHECK(InputAt(0)->IsLoadClass());
4885 return InputAt(0)->AsLoadClass();
4886 }
4887
4888 HInstruction* GetLength() const {
4889 return InputAt(1);
4890 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004891
Vladimir Markob5461632018-10-15 14:24:21 +01004892 size_t GetComponentSizeShift() {
4893 return GetPackedField<ComponentSizeShiftField>();
4894 }
4895
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004896 DECLARE_INSTRUCTION(NewArray);
4897
Artem Serovcced8ba2017-07-19 18:18:09 +01004898 protected:
4899 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004900
4901 private:
4902 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4903 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4904 static constexpr size_t kNumberOfNewArrayPackedBits =
4905 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4906 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4907 using ComponentSizeShiftField =
4908 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004909};
4910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004911class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004912 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004913 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004914 HInstruction* left,
4915 HInstruction* right,
4916 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304917 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4918 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004919
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004920 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004921
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004922 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004923
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004924 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004925 return GetBlock()->GetGraph()->GetIntConstant(
4926 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004927 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004928 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 return GetBlock()->GetGraph()->GetLongConstant(
4930 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004931 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004932 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004933 return GetBlock()->GetGraph()->GetFloatConstant(
4934 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4935 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004936 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004937 return GetBlock()->GetGraph()->GetDoubleConstant(
4938 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4939 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004940
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004941 DECLARE_INSTRUCTION(Add);
4942
Artem Serovcced8ba2017-07-19 18:18:09 +01004943 protected:
4944 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004945};
4946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004947class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004948 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004949 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004950 HInstruction* left,
4951 HInstruction* right,
4952 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304953 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4954 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004955
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004956 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004958 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004959 return GetBlock()->GetGraph()->GetIntConstant(
4960 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004961 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004962 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004963 return GetBlock()->GetGraph()->GetLongConstant(
4964 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004965 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004966 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004967 return GetBlock()->GetGraph()->GetFloatConstant(
4968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004971 return GetBlock()->GetGraph()->GetDoubleConstant(
4972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4973 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004974
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004975 DECLARE_INSTRUCTION(Sub);
4976
Artem Serovcced8ba2017-07-19 18:18:09 +01004977 protected:
4978 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004979};
4980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004981class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004982 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004983 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004984 HInstruction* left,
4985 HInstruction* right,
4986 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304987 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4988 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004990 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004991
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004992 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004993
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004994 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004995 return GetBlock()->GetGraph()->GetIntConstant(
4996 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004997 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004998 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004999 return GetBlock()->GetGraph()->GetLongConstant(
5000 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005001 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005002 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005003 return GetBlock()->GetGraph()->GetFloatConstant(
5004 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5005 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005006 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005007 return GetBlock()->GetGraph()->GetDoubleConstant(
5008 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5009 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005010
5011 DECLARE_INSTRUCTION(Mul);
5012
Artem Serovcced8ba2017-07-19 18:18:09 +01005013 protected:
5014 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005015};
5016
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005017class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005018 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005019 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005020 HInstruction* left,
5021 HInstruction* right,
5022 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305023 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5024 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005025
Roland Levillain9867bc72015-08-05 10:21:34 +01005026 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005027 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005028 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005029 // Our graph structure ensures we never have 0 for `y` during
5030 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005031 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005032 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005033 return (y == -1) ? -x : x / y;
5034 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005035
Roland Levillain31dd3d62016-02-16 12:21:02 +00005036 template <typename T>
5037 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005038 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005039 return x / y;
5040 }
5041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005042 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005043 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005044 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005045 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005046 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005047 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005048 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5049 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005050 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005051 return GetBlock()->GetGraph()->GetFloatConstant(
5052 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5053 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005054 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005055 return GetBlock()->GetGraph()->GetDoubleConstant(
5056 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005057 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005058
5059 DECLARE_INSTRUCTION(Div);
5060
Artem Serovcced8ba2017-07-19 18:18:09 +01005061 protected:
5062 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005063};
5064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005065class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005066 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005067 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005068 HInstruction* left,
5069 HInstruction* right,
5070 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305071 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5072 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005073
Roland Levillain9867bc72015-08-05 10:21:34 +01005074 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005075 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005076 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005077 // Our graph structure ensures we never have 0 for `y` during
5078 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005079 DCHECK_NE(y, 0);
5080 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5081 return (y == -1) ? 0 : x % y;
5082 }
5083
Roland Levillain31dd3d62016-02-16 12:21:02 +00005084 template <typename T>
5085 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005086 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005087 return std::fmod(x, y);
5088 }
5089
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005090 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005091 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005092 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005093 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005094 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005095 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005096 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005097 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005098 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005099 return GetBlock()->GetGraph()->GetFloatConstant(
5100 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5101 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005102 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005103 return GetBlock()->GetGraph()->GetDoubleConstant(
5104 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5105 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005106
5107 DECLARE_INSTRUCTION(Rem);
5108
Artem Serovcced8ba2017-07-19 18:18:09 +01005109 protected:
5110 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005111};
5112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005113class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005114 public:
5115 HMin(DataType::Type result_type,
5116 HInstruction* left,
5117 HInstruction* right,
5118 uint32_t dex_pc)
5119 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005121 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005122
5123 // Evaluation for integral values.
5124 template <typename T> static T ComputeIntegral(T x, T y) {
5125 return (x <= y) ? x : y;
5126 }
5127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005128 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005129 return GetBlock()->GetGraph()->GetIntConstant(
5130 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5131 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005132 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005133 return GetBlock()->GetGraph()->GetLongConstant(
5134 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5135 }
5136 // TODO: Evaluation for floating-point values.
5137 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005138 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005139 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005140 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005141
5142 DECLARE_INSTRUCTION(Min);
5143
5144 protected:
5145 DEFAULT_COPY_CONSTRUCTOR(Min);
5146};
5147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005148class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005149 public:
5150 HMax(DataType::Type result_type,
5151 HInstruction* left,
5152 HInstruction* right,
5153 uint32_t dex_pc)
5154 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5155
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005156 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005157
5158 // Evaluation for integral values.
5159 template <typename T> static T ComputeIntegral(T x, T y) {
5160 return (x >= y) ? x : y;
5161 }
5162
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005163 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005164 return GetBlock()->GetGraph()->GetIntConstant(
5165 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5166 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005167 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005168 return GetBlock()->GetGraph()->GetLongConstant(
5169 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5170 }
5171 // TODO: Evaluation for floating-point values.
5172 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005173 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005174 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005175 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005176
5177 DECLARE_INSTRUCTION(Max);
5178
5179 protected:
5180 DEFAULT_COPY_CONSTRUCTOR(Max);
5181};
5182
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005183class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005184 public:
5185 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5186 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5187
5188 // Evaluation for integral values.
5189 template <typename T> static T ComputeIntegral(T x) {
5190 return x < 0 ? -x : x;
5191 }
5192
5193 // Evaluation for floating-point values.
5194 // Note, as a "quality of implementation", rather than pure "spec compliance",
5195 // we require that Math.abs() clears the sign bit (but changes nothing else)
5196 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5197 // http://b/30758343
5198 template <typename T, typename S> static T ComputeFP(T x) {
5199 S bits = bit_cast<S, T>(x);
5200 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5201 }
5202
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005203 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005204 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5205 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005206 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005207 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5208 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005210 return GetBlock()->GetGraph()->GetFloatConstant(
5211 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5212 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005213 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005214 return GetBlock()->GetGraph()->GetDoubleConstant(
5215 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5216 }
5217
5218 DECLARE_INSTRUCTION(Abs);
5219
5220 protected:
5221 DEFAULT_COPY_CONSTRUCTOR(Abs);
5222};
5223
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005224class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005225 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005226 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005227 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5228 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005229 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005230 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005231 SetRawInputAt(0, value);
5232 }
5233
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005234 bool IsClonable() const override { return true; }
5235 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005237 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005238 return true;
5239 }
5240
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005241 bool NeedsEnvironment() const override { return true; }
5242 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005243
Calin Juravled0d48522014-11-04 16:40:20 +00005244 DECLARE_INSTRUCTION(DivZeroCheck);
5245
Artem Serovcced8ba2017-07-19 18:18:09 +01005246 protected:
5247 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005248};
5249
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005250class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005251 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005252 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005253 HInstruction* value,
5254 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005255 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305256 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005257 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5258 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005259 }
5260
Roland Levillain5b5b9312016-03-22 14:57:31 +00005261 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005262 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005263 return value << (distance & max_shift_distance);
5264 }
5265
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005266 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005267 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005268 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005269 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005270 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005271 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005272 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005273 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005274 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005275 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005276 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5277 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005278 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005279 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005280 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005281 LOG(FATAL) << DebugName() << " is not defined for float values";
5282 UNREACHABLE();
5283 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005284 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005285 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005286 LOG(FATAL) << DebugName() << " is not defined for double values";
5287 UNREACHABLE();
5288 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005289
5290 DECLARE_INSTRUCTION(Shl);
5291
Artem Serovcced8ba2017-07-19 18:18:09 +01005292 protected:
5293 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005294};
5295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005296class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005297 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005298 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005299 HInstruction* value,
5300 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005301 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305302 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005303 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5304 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005305 }
5306
Roland Levillain5b5b9312016-03-22 14:57:31 +00005307 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005308 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005309 return value >> (distance & max_shift_distance);
5310 }
5311
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005312 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005313 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005314 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005315 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005316 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005317 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005318 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005319 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005320 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005321 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005322 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5323 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005324 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005325 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005326 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005327 LOG(FATAL) << DebugName() << " is not defined for float values";
5328 UNREACHABLE();
5329 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005330 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005331 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005332 LOG(FATAL) << DebugName() << " is not defined for double values";
5333 UNREACHABLE();
5334 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005335
5336 DECLARE_INSTRUCTION(Shr);
5337
Artem Serovcced8ba2017-07-19 18:18:09 +01005338 protected:
5339 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005340};
5341
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005342class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005343 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005344 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005345 HInstruction* value,
5346 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005347 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305348 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
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()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005351 }
5352
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_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005355 typedef typename std::make_unsigned<T>::type V;
5356 V ux = static_cast<V>(value);
5357 return static_cast<T>(ux >> (distance & max_shift_distance));
5358 }
5359
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005360 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005361 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005362 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005363 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005364 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005365 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005366 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005367 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005368 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005369 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005370 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5371 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005372 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005373 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005374 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005375 LOG(FATAL) << DebugName() << " is not defined for float values";
5376 UNREACHABLE();
5377 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005378 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005379 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005380 LOG(FATAL) << DebugName() << " is not defined for double values";
5381 UNREACHABLE();
5382 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005383
5384 DECLARE_INSTRUCTION(UShr);
5385
Artem Serovcced8ba2017-07-19 18:18:09 +01005386 protected:
5387 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005388};
5389
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005390class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005391 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005393 HInstruction* left,
5394 HInstruction* right,
5395 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305396 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5397 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005398
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005399 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005400
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005401 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005403 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005404 return GetBlock()->GetGraph()->GetIntConstant(
5405 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005406 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005407 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005408 return GetBlock()->GetGraph()->GetLongConstant(
5409 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005410 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005411 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005412 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005413 LOG(FATAL) << DebugName() << " is not defined for float values";
5414 UNREACHABLE();
5415 }
5416 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005417 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005418 LOG(FATAL) << DebugName() << " is not defined for double values";
5419 UNREACHABLE();
5420 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005421
5422 DECLARE_INSTRUCTION(And);
5423
Artem Serovcced8ba2017-07-19 18:18:09 +01005424 protected:
5425 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005426};
5427
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005428class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005429 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005430 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005431 HInstruction* left,
5432 HInstruction* right,
5433 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305434 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5435 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005436
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005437 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005438
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005439 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005440
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005441 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005442 return GetBlock()->GetGraph()->GetIntConstant(
5443 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005444 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005445 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005446 return GetBlock()->GetGraph()->GetLongConstant(
5447 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005448 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005449 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005450 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005451 LOG(FATAL) << DebugName() << " is not defined for float values";
5452 UNREACHABLE();
5453 }
5454 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005455 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005456 LOG(FATAL) << DebugName() << " is not defined for double values";
5457 UNREACHABLE();
5458 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005459
5460 DECLARE_INSTRUCTION(Or);
5461
Artem Serovcced8ba2017-07-19 18:18:09 +01005462 protected:
5463 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005464};
5465
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005466class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005467 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005468 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005469 HInstruction* left,
5470 HInstruction* right,
5471 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305472 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5473 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005475 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005476
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005477 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005480 return GetBlock()->GetGraph()->GetIntConstant(
5481 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005482 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005483 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005484 return GetBlock()->GetGraph()->GetLongConstant(
5485 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005486 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005487 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005488 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005489 LOG(FATAL) << DebugName() << " is not defined for float values";
5490 UNREACHABLE();
5491 }
5492 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005493 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005494 LOG(FATAL) << DebugName() << " is not defined for double values";
5495 UNREACHABLE();
5496 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005497
5498 DECLARE_INSTRUCTION(Xor);
5499
Artem Serovcced8ba2017-07-19 18:18:09 +01005500 protected:
5501 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005502};
5503
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005504class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005505 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005506 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305507 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005508 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5509 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005510 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005511
Roland Levillain5b5b9312016-03-22 14:57:31 +00005512 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005513 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005514 typedef typename std::make_unsigned<T>::type V;
5515 V ux = static_cast<V>(value);
5516 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005517 return static_cast<T>(ux);
5518 } else {
5519 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005520 return static_cast<T>(ux >> (distance & max_shift_value)) |
5521 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005522 }
5523 }
5524
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005525 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005526 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005527 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005528 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005529 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005530 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005531 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005532 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005533 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005534 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005535 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5536 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005537 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005538 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005539 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005540 LOG(FATAL) << DebugName() << " is not defined for float values";
5541 UNREACHABLE();
5542 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005543 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005544 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005545 LOG(FATAL) << DebugName() << " is not defined for double values";
5546 UNREACHABLE();
5547 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005548
5549 DECLARE_INSTRUCTION(Ror);
5550
Artem Serovcced8ba2017-07-19 18:18:09 +01005551 protected:
5552 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005553};
5554
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005555// The value of a parameter in this method. Its location depends on
5556// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005557class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005558 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005559 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005560 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005561 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005562 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005563 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305564 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005565 dex_file_(dex_file),
5566 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005567 index_(index) {
5568 SetPackedFlag<kFlagIsThis>(is_this);
5569 SetPackedFlag<kFlagCanBeNull>(!is_this);
5570 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005571
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005572 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005573 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005574 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005575 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005576
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005577 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005578 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005579
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005580 DECLARE_INSTRUCTION(ParameterValue);
5581
Artem Serovcced8ba2017-07-19 18:18:09 +01005582 protected:
5583 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5584
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005585 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005586 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005587 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005588 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5589 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5590 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5591 "Too many packed fields.");
5592
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005593 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005594 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005595 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005596 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005597 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005598};
5599
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005600class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005601 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005602 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305603 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5604 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005605
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005606 bool CanBeMoved() const override { return true; }
5607 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005608 return true;
5609 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005610
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005611 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005612
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005613 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005614 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005615 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005616 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005617 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005618 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005619 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005620 LOG(FATAL) << DebugName() << " is not defined for float values";
5621 UNREACHABLE();
5622 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005623 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005624 LOG(FATAL) << DebugName() << " is not defined for double values";
5625 UNREACHABLE();
5626 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005627
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005628 DECLARE_INSTRUCTION(Not);
5629
Artem Serovcced8ba2017-07-19 18:18:09 +01005630 protected:
5631 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005632};
5633
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005634class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005635 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005636 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305637 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5638 }
David Brazdil66d126e2015-04-03 16:02:44 +01005639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005640 bool CanBeMoved() const override { return true; }
5641 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005642 return true;
5643 }
5644
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005645 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005646 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005647 return !x;
5648 }
5649
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005650 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005651 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005652 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005653 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005654 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005655 UNREACHABLE();
5656 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005657 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005658 LOG(FATAL) << DebugName() << " is not defined for float values";
5659 UNREACHABLE();
5660 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005661 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005662 LOG(FATAL) << DebugName() << " is not defined for double values";
5663 UNREACHABLE();
5664 }
David Brazdil66d126e2015-04-03 16:02:44 +01005665
5666 DECLARE_INSTRUCTION(BooleanNot);
5667
Artem Serovcced8ba2017-07-19 18:18:09 +01005668 protected:
5669 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005670};
5671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005672class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005673 public:
5674 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005675 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305676 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005677 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005678 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005679 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005680 }
5681
5682 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005683 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5684 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005685
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005686 bool IsClonable() const override { return true; }
5687 bool CanBeMoved() const override { return true; }
5688 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005689 return true;
5690 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005691 // Return whether the conversion is implicit. This includes conversion to the same type.
5692 bool IsImplicitConversion() const {
5693 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5694 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005695
Mark Mendelle82549b2015-05-06 10:55:34 -04005696 // Try to statically evaluate the conversion and return a HConstant
5697 // containing the result. If the input cannot be converted, return nullptr.
5698 HConstant* TryStaticEvaluation() const;
5699
Roland Levillaindff1f282014-11-05 14:15:05 +00005700 DECLARE_INSTRUCTION(TypeConversion);
5701
Artem Serovcced8ba2017-07-19 18:18:09 +01005702 protected:
5703 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005704};
5705
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005706static constexpr uint32_t kNoRegNumber = -1;
5707
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005708class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005709 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005710 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005711 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5712 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005713 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005714 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005715 SetRawInputAt(0, value);
5716 }
5717
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005718 bool IsClonable() const override { return true; }
5719 bool CanBeMoved() const override { return true; }
5720 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005721 return true;
5722 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005724 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005725
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005726 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005727
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005728 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005729
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005730 DECLARE_INSTRUCTION(NullCheck);
5731
Artem Serovcced8ba2017-07-19 18:18:09 +01005732 protected:
5733 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005734};
5735
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005736// Embeds an ArtField and all the information required by the compiler. We cache
5737// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005738class FieldInfo : public ValueObject {
5739 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005740 FieldInfo(ArtField* field,
5741 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005742 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005743 bool is_volatile,
5744 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005745 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005746 const DexFile& dex_file)
5747 : field_(field),
5748 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005749 field_type_(field_type),
5750 is_volatile_(is_volatile),
5751 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005752 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005753 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005754
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005755 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005756 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005757 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005758 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005759 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005760 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005761 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005762
5763 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005764 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005765 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005766 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005767 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005768 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005769 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005770 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005771};
5772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005773class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005774 public:
5775 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005776 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005777 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005778 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005779 bool is_volatile,
5780 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005781 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005782 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005783 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305784 : HExpression(kInstanceFieldGet,
5785 field_type,
5786 SideEffects::FieldReadOfType(field_type, is_volatile),
5787 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005788 field_info_(field,
5789 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005790 field_type,
5791 is_volatile,
5792 field_idx,
5793 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005794 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005795 SetRawInputAt(0, value);
5796 }
5797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005798 bool IsClonable() const override { return true; }
5799 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005800
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005801 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005802 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005803 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005804 }
5805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005806 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005807 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005808 }
5809
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005810 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005811 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5812 }
5813
Calin Juravle52c48962014-12-16 17:02:57 +00005814 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005815 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005816 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005817 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005818
Vladimir Marko61b92282017-10-11 13:23:17 +01005819 void SetType(DataType::Type new_type) {
5820 DCHECK(DataType::IsIntegralType(GetType()));
5821 DCHECK(DataType::IsIntegralType(new_type));
5822 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5823 SetPackedField<TypeField>(new_type);
5824 }
5825
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005826 DECLARE_INSTRUCTION(InstanceFieldGet);
5827
Artem Serovcced8ba2017-07-19 18:18:09 +01005828 protected:
5829 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5830
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005831 private:
5832 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005833};
5834
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005835class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005836 public:
5837 HInstanceFieldSet(HInstruction* object,
5838 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005839 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005841 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005842 bool is_volatile,
5843 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005844 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005845 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005846 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005847 : HExpression(kInstanceFieldSet,
5848 SideEffects::FieldWriteOfType(field_type, is_volatile),
5849 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005850 field_info_(field,
5851 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005852 field_type,
5853 is_volatile,
5854 field_idx,
5855 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005856 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005857 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005858 SetRawInputAt(0, object);
5859 SetRawInputAt(1, value);
5860 }
5861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005862 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005863
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005864 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005865 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005866 }
5867
Calin Juravle52c48962014-12-16 17:02:57 +00005868 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005869 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005870 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005871 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005872 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005873 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5874 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005875
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005876 DECLARE_INSTRUCTION(InstanceFieldSet);
5877
Artem Serovcced8ba2017-07-19 18:18:09 +01005878 protected:
5879 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5880
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005881 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005882 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5883 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5884 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5885 "Too many packed fields.");
5886
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005887 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005888};
5889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005890class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005891 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005892 HArrayGet(HInstruction* array,
5893 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005895 uint32_t dex_pc)
5896 : HArrayGet(array,
5897 index,
5898 type,
5899 SideEffects::ArrayReadOfType(type),
5900 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005901 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305902 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005903
5904 HArrayGet(HInstruction* array,
5905 HInstruction* index,
5906 DataType::Type type,
5907 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005908 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005909 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305910 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005911 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005912 SetRawInputAt(0, array);
5913 SetRawInputAt(1, index);
5914 }
5915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005916 bool IsClonable() const override { return true; }
5917 bool CanBeMoved() const override { return true; }
5918 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005919 return true;
5920 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005921 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005922 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005923 // Currently, unless the array is the result of NewArray, the array access is always
5924 // preceded by some form of null NullCheck necessary for the bounds check, usually
5925 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5926 // dynamic BCE. There are cases when these could be removed to produce better code.
5927 // If we ever add optimizations to do so we should allow an implicit check here
5928 // (as long as the address falls in the first page).
5929 //
5930 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5931 // a = cond ? new int[1] : null;
5932 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005933 return false;
5934 }
5935
David Brazdil4833f5a2015-12-16 10:37:39 +00005936 bool IsEquivalentOf(HArrayGet* other) const {
5937 bool result = (GetDexPc() == other->GetDexPc());
5938 if (kIsDebugBuild && result) {
5939 DCHECK_EQ(GetBlock(), other->GetBlock());
5940 DCHECK_EQ(GetArray(), other->GetArray());
5941 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005942 if (DataType::IsIntOrLongType(GetType())) {
5943 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005944 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005945 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5946 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005947 }
5948 }
5949 return result;
5950 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005951
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005952 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5953
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005954 HInstruction* GetArray() const { return InputAt(0); }
5955 HInstruction* GetIndex() const { return InputAt(1); }
5956
Vladimir Marko61b92282017-10-11 13:23:17 +01005957 void SetType(DataType::Type new_type) {
5958 DCHECK(DataType::IsIntegralType(GetType()));
5959 DCHECK(DataType::IsIntegralType(new_type));
5960 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5961 SetPackedField<TypeField>(new_type);
5962 }
5963
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005964 DECLARE_INSTRUCTION(ArrayGet);
5965
Artem Serovcced8ba2017-07-19 18:18:09 +01005966 protected:
5967 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5968
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005969 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005970 // We treat a String as an array, creating the HArrayGet from String.charAt()
5971 // intrinsic in the instruction simplifier. We can always determine whether
5972 // a particular HArrayGet is actually a String.charAt() by looking at the type
5973 // of the input but that requires holding the mutator lock, so we prefer to use
5974 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005975 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005976 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5977 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5978 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005979};
5980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005981class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005982 public:
5983 HArraySet(HInstruction* array,
5984 HInstruction* index,
5985 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005986 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005987 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005988 : HArraySet(array,
5989 index,
5990 value,
5991 expected_component_type,
5992 // Make a best guess for side effects now, may be refined during SSA building.
5993 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305994 dex_pc) {
5995 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005996
5997 HArraySet(HInstruction* array,
5998 HInstruction* index,
5999 HInstruction* value,
6000 DataType::Type expected_component_type,
6001 SideEffects side_effects,
6002 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006003 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006005 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006006 SetPackedFlag<kFlagValueCanBeNull>(true);
6007 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006008 SetRawInputAt(0, array);
6009 SetRawInputAt(1, index);
6010 SetRawInputAt(2, value);
6011 }
6012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006013 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006015 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006016 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006017 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006018 }
6019
Mingyao Yang81014cb2015-06-02 03:16:27 -07006020 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006021 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006023 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006024 // TODO: Same as for ArrayGet.
6025 return false;
6026 }
6027
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006028 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006029 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006030 }
6031
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006032 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006033 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006034 }
6035
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006036 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006037 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006038 }
6039
Vladimir Markoa1de9182016-02-25 11:37:38 +00006040 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6041 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6042 bool StaticTypeOfArrayIsObjectArray() const {
6043 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6044 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006045
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006046 HInstruction* GetArray() const { return InputAt(0); }
6047 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006048 HInstruction* GetValue() const { return InputAt(2); }
6049
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006050 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006051 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6052 }
6053
6054 static DataType::Type GetComponentType(DataType::Type value_type,
6055 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006056 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006057 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006058 // be correct, we also check what is the value type. If it is a floating
6059 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006060 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006061 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006062 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006063 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006064
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006065 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006066 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006067 }
6068
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006069 static SideEffects ComputeSideEffects(DataType::Type type) {
6070 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006071 }
6072
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006073 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6074 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6075 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006076 }
6077
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006078 DECLARE_INSTRUCTION(ArraySet);
6079
Artem Serovcced8ba2017-07-19 18:18:09 +01006080 protected:
6081 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6082
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006083 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006084 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6085 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006086 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006087 static constexpr size_t kFlagNeedsTypeCheck =
6088 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6089 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006090 // Cached information for the reference_type_info_ so that codegen
6091 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006092 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6093 static constexpr size_t kNumberOfArraySetPackedBits =
6094 kFlagStaticTypeOfArrayIsObjectArray + 1;
6095 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6096 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006097 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006098};
6099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006100class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006101 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006102 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306103 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006104 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006105 // Note that arrays do not change length, so the instruction does not
6106 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006107 SetRawInputAt(0, array);
6108 }
6109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006110 bool IsClonable() const override { return true; }
6111 bool CanBeMoved() const override { return true; }
6112 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006113 return true;
6114 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006115 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006116 return obj == InputAt(0);
6117 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006118
Vladimir Markodce016e2016-04-28 13:10:02 +01006119 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6120
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006121 DECLARE_INSTRUCTION(ArrayLength);
6122
Artem Serovcced8ba2017-07-19 18:18:09 +01006123 protected:
6124 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6125
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006126 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006127 // We treat a String as an array, creating the HArrayLength from String.length()
6128 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6129 // determine whether a particular HArrayLength is actually a String.length() by
6130 // looking at the type of the input but that requires holding the mutator lock, so
6131 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006132 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006133 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6134 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6135 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006136};
6137
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006138class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006139 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006140 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006141 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6142 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006143 HBoundsCheck(HInstruction* index,
6144 HInstruction* length,
6145 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006146 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006147 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006148 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006149 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006150 SetRawInputAt(0, index);
6151 SetRawInputAt(1, length);
6152 }
6153
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006154 bool IsClonable() const override { return true; }
6155 bool CanBeMoved() const override { return true; }
6156 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006157 return true;
6158 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006159
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006160 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006162 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006163
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006164 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006165
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006166 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006167
6168 DECLARE_INSTRUCTION(BoundsCheck);
6169
Artem Serovcced8ba2017-07-19 18:18:09 +01006170 protected:
6171 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6172
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006173 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006174 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006175 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6176 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6177 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006178};
6179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006180class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006181 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006182 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006183 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306184 slow_path_(nullptr) {
6185 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006187 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006188
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006189 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006190 return true;
6191 }
6192
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006193 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6194 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006195
6196 DECLARE_INSTRUCTION(SuspendCheck);
6197
Artem Serovcced8ba2017-07-19 18:18:09 +01006198 protected:
6199 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6200
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006201 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006202 // Only used for code generation, in order to share the same slow path between back edges
6203 // of a same loop.
6204 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006205};
6206
David Srbecky0cf44932015-12-09 14:09:59 +00006207// Pseudo-instruction which provides the native debugger with mapping information.
6208// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006209class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006210 public:
6211 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006212 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306213 }
David Srbecky0cf44932015-12-09 14:09:59 +00006214
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006215 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006216 return true;
6217 }
6218
6219 DECLARE_INSTRUCTION(NativeDebugInfo);
6220
Artem Serovcced8ba2017-07-19 18:18:09 +01006221 protected:
6222 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006223};
6224
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006225/**
6226 * Instruction to load a Class object.
6227 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006228class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006229 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006230 // Determines how to load the Class.
6231 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006232 // We cannot load this class. See HSharpening::SharpenLoadClass.
6233 kInvalid = -1,
6234
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006235 // Use the Class* from the method's own ArtMethod*.
6236 kReferrersClass,
6237
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006238 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006239 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006240 kBootImageLinkTimePcRelative,
6241
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006242 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006243 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006244 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006245
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006246 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006247 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006248 kBssEntry,
6249
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006250 // Use a known boot image Class* address, embedded in the code by the codegen.
6251 // Used for boot image classes referenced by apps in JIT-compiled code.
6252 kJitBootImageAddress,
6253
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006254 // Load from the root table associated with the JIT compiled method.
6255 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006256
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006257 // Load using a simple runtime call. This is the fall-back load kind when
6258 // the codegen is unable to use another appropriate kind.
6259 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006260
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006261 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006262 };
6263
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006264 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006265 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006266 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006267 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006268 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006269 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006270 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006271 : HInstruction(kLoadClass,
6272 DataType::Type::kReference,
6273 SideEffectsForArchRuntimeCalls(),
6274 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006275 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006276 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006277 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006278 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006279 // Referrers class should not need access check. We never inline unverified
6280 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006281 DCHECK(!is_referrers_class || !needs_access_check);
6282
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006283 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006284 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006285 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006286 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006287 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006288 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006289 }
6290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006291 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006292
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006293 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006294
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006295 LoadKind GetLoadKind() const {
6296 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006297 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006298
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006299 bool HasPcRelativeLoadKind() const {
6300 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6301 GetLoadKind() == LoadKind::kBootImageRelRo ||
6302 GetLoadKind() == LoadKind::kBssEntry;
6303 }
6304
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006305 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006306
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006307 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006309 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006311 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006313 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006314 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006315 }
6316
Calin Juravle0ba218d2015-05-19 18:46:01 +01006317 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006318 // The entrypoint the code generator is going to call does not do
6319 // clinit of the class.
6320 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006321 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006322 }
6323
6324 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006325 return NeedsAccessCheck() ||
6326 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006327 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006328 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006329 }
6330
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006331 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006332 return NeedsAccessCheck() ||
6333 MustGenerateClinitCheck() ||
6334 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006335 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006336 GetLoadKind() == LoadKind::kBssEntry) &&
6337 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006338 }
6339
Calin Juravleacf735c2015-02-12 15:25:22 +00006340 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006341 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6342 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006343 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006344 } else {
6345 return ReferenceTypeInfo::CreateInvalid();
6346 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006347 }
6348
Vladimir Marko175e7862018-03-27 09:03:13 +00006349 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6350 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6351 DCHECK(klass_ != nullptr);
6352 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006353 }
6354
Andreas Gampea5b09a62016-11-17 15:21:22 -08006355 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006356 const DexFile& GetDexFile() const { return dex_file_; }
6357
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006358 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006359 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006360 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006361
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006362 static SideEffects SideEffectsForArchRuntimeCalls() {
6363 return SideEffects::CanTriggerGC();
6364 }
6365
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006366 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006367 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006368 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006369 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006370
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006371 bool MustResolveTypeOnSlowPath() const {
6372 // Check that this instruction has a slow path.
6373 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6374 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6375 return GetLoadKind() == LoadKind::kBssEntry;
6376 }
6377
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006378 void MarkInBootImage() {
6379 SetPackedFlag<kFlagIsInBootImage>(true);
6380 }
6381
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006382 void AddSpecialInput(HInstruction* special_input);
6383
6384 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006385 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006386 return ArrayRef<HUserRecord<HInstruction*>>(
6387 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6388 }
6389
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006390 Handle<mirror::Class> GetClass() const {
6391 return klass_;
6392 }
6393
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006394 DECLARE_INSTRUCTION(LoadClass);
6395
Artem Serovcced8ba2017-07-19 18:18:09 +01006396 protected:
6397 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6398
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006399 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006400 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006401 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006402 // Whether this instruction must generate the initialization check.
6403 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006404 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006405 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6406 static constexpr size_t kFieldLoadKindSize =
6407 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006408 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6409 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006411 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6412
6413 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006414 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006415 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006416 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006417 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006418 }
6419
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006420 void SetLoadKindInternal(LoadKind load_kind);
6421
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006422 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006423 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006424 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006425 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006426
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006427 // A type index and dex file where the class can be accessed. The dex file can be:
6428 // - The compiling method's dex file if the class is defined there too.
6429 // - The compiling method's dex file if the class is referenced there.
6430 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006431 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006432 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006433 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006434
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006435 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006436};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006437std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6438
6439// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006440inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6441 // The load kind should be determined before inserting the instruction to the graph.
6442 DCHECK(GetBlock() == nullptr);
6443 DCHECK(GetEnvironment() == nullptr);
6444 SetPackedField<LoadKindField>(load_kind);
6445 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6446 special_input_ = HUserRecord<HInstruction*>(nullptr);
6447 }
6448 if (!NeedsEnvironment()) {
6449 SetSideEffects(SideEffects::None());
6450 }
6451}
6452
6453// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006454inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006455 // The special input is used for PC-relative loads on some architectures,
6456 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006457 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006458 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006459 GetLoadKind() == LoadKind::kBssEntry ||
6460 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006461 DCHECK(special_input_.GetInstruction() == nullptr);
6462 special_input_ = HUserRecord<HInstruction*>(special_input);
6463 special_input->AddUseAt(this, 0);
6464}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006465
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006466class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006467 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006468 // Determines how to load the String.
6469 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006470 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006471 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006472 kBootImageLinkTimePcRelative,
6473
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006474 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006475 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006476 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006477
Vladimir Markoaad75c62016-10-03 08:46:48 +00006478 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006479 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006480 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006481
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006482 // Use a known boot image String* address, embedded in the code by the codegen.
6483 // Used for boot image strings referenced by apps in JIT-compiled code.
6484 kJitBootImageAddress,
6485
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006486 // Load from the root table associated with the JIT compiled method.
6487 kJitTableAddress,
6488
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006489 // Load using a simple runtime call. This is the fall-back load kind when
6490 // the codegen is unable to use another appropriate kind.
6491 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006492
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006493 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006494 };
6495
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006496 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006497 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006498 const DexFile& dex_file,
6499 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006500 : HInstruction(kLoadString,
6501 DataType::Type::kReference,
6502 SideEffectsForArchRuntimeCalls(),
6503 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006504 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006505 string_index_(string_index),
6506 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006507 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006508 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006509
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006510 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006511
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006512 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006513
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006514 LoadKind GetLoadKind() const {
6515 return GetPackedField<LoadKindField>();
6516 }
6517
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006518 bool HasPcRelativeLoadKind() const {
6519 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6520 GetLoadKind() == LoadKind::kBootImageRelRo ||
6521 GetLoadKind() == LoadKind::kBssEntry;
6522 }
6523
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006524 const DexFile& GetDexFile() const {
6525 return dex_file_;
6526 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006527
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006528 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006529 return string_index_;
6530 }
6531
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006532 Handle<mirror::String> GetString() const {
6533 return string_;
6534 }
6535
6536 void SetString(Handle<mirror::String> str) {
6537 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006538 }
6539
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006540 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006541
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006542 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006543
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006544 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006545
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006546 // Will call the runtime if we need to load the string through
6547 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006548 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006549 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006550 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006551 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006552 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006553 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006554 return false;
6555 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006556 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006557 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006558
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006559 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006560 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006561 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006563 bool CanBeNull() const override { return false; }
6564 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006565
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006566 static SideEffects SideEffectsForArchRuntimeCalls() {
6567 return SideEffects::CanTriggerGC();
6568 }
6569
Vladimir Marko372f10e2016-05-17 16:30:10 +01006570 void AddSpecialInput(HInstruction* special_input);
6571
6572 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006573 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006574 return ArrayRef<HUserRecord<HInstruction*>>(
6575 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006576 }
6577
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006578 DECLARE_INSTRUCTION(LoadString);
6579
Artem Serovcced8ba2017-07-19 18:18:09 +01006580 protected:
6581 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6582
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006583 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006584 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006585 static constexpr size_t kFieldLoadKindSize =
6586 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6587 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006588 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006589 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006590
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006591 void SetLoadKindInternal(LoadKind load_kind);
6592
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006593 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006594 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006595 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006596 HUserRecord<HInstruction*> special_input_;
6597
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006598 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006599 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006600
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006601 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006602};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006603std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6604
6605// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006606inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6607 // The load kind should be determined before inserting the instruction to the graph.
6608 DCHECK(GetBlock() == nullptr);
6609 DCHECK(GetEnvironment() == nullptr);
6610 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6611 SetPackedField<LoadKindField>(load_kind);
6612 if (load_kind != LoadKind::kRuntimeCall) {
6613 special_input_ = HUserRecord<HInstruction*>(nullptr);
6614 }
6615 if (!NeedsEnvironment()) {
6616 SetSideEffects(SideEffects::None());
6617 }
6618}
6619
6620// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006621inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006622 // The special input is used for PC-relative loads on some architectures,
6623 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006624 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006625 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006626 GetLoadKind() == LoadKind::kBssEntry ||
6627 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006628 // HLoadString::GetInputRecords() returns an empty array at this point,
6629 // so use the GetInputRecords() from the base class to set the input record.
6630 DCHECK(special_input_.GetInstruction() == nullptr);
6631 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006632 special_input->AddUseAt(this, 0);
6633}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006634
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006635class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006636 public:
6637 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006638 uint16_t method_handle_idx,
6639 const DexFile& dex_file,
6640 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006641 : HInstruction(kLoadMethodHandle,
6642 DataType::Type::kReference,
6643 SideEffectsForArchRuntimeCalls(),
6644 dex_pc),
6645 special_input_(HUserRecord<HInstruction*>(current_method)),
6646 method_handle_idx_(method_handle_idx),
6647 dex_file_(dex_file) {
6648 }
6649
6650 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006651 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006652 return ArrayRef<HUserRecord<HInstruction*>>(
6653 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6654 }
6655
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006656 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006657
6658 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6659
6660 const DexFile& GetDexFile() const { return dex_file_; }
6661
6662 static SideEffects SideEffectsForArchRuntimeCalls() {
6663 return SideEffects::CanTriggerGC();
6664 }
6665
6666 DECLARE_INSTRUCTION(LoadMethodHandle);
6667
6668 protected:
6669 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6670
6671 private:
6672 // The special input is the HCurrentMethod for kRuntimeCall.
6673 HUserRecord<HInstruction*> special_input_;
6674
6675 const uint16_t method_handle_idx_;
6676 const DexFile& dex_file_;
6677};
6678
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006679class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006680 public:
6681 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006682 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006683 const DexFile& dex_file,
6684 uint32_t dex_pc)
6685 : HInstruction(kLoadMethodType,
6686 DataType::Type::kReference,
6687 SideEffectsForArchRuntimeCalls(),
6688 dex_pc),
6689 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006690 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006691 dex_file_(dex_file) {
6692 }
6693
6694 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006695 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006696 return ArrayRef<HUserRecord<HInstruction*>>(
6697 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6698 }
6699
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006700 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006701
Orion Hodson06d10a72018-05-14 08:53:38 +01006702 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006703
6704 const DexFile& GetDexFile() const { return dex_file_; }
6705
6706 static SideEffects SideEffectsForArchRuntimeCalls() {
6707 return SideEffects::CanTriggerGC();
6708 }
6709
6710 DECLARE_INSTRUCTION(LoadMethodType);
6711
6712 protected:
6713 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6714
6715 private:
6716 // The special input is the HCurrentMethod for kRuntimeCall.
6717 HUserRecord<HInstruction*> special_input_;
6718
Orion Hodson06d10a72018-05-14 08:53:38 +01006719 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006720 const DexFile& dex_file_;
6721};
6722
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006723/**
6724 * Performs an initialization check on its Class object input.
6725 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006726class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006727 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006728 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006729 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306730 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006731 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006732 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006733 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006734 SetRawInputAt(0, constant);
6735 }
Artem Serova6e26142018-06-19 14:55:17 +01006736 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006737 bool CanBeMoved() const override { return true; }
6738 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006739 return true;
6740 }
6741
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006742 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006743 // May call runtime to initialize the class.
6744 return true;
6745 }
6746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006747 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006748
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006749 HLoadClass* GetLoadClass() const {
6750 DCHECK(InputAt(0)->IsLoadClass());
6751 return InputAt(0)->AsLoadClass();
6752 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006753
6754 DECLARE_INSTRUCTION(ClinitCheck);
6755
Artem Serovcced8ba2017-07-19 18:18:09 +01006756
6757 protected:
6758 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006759};
6760
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006761class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006762 public:
6763 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006764 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006765 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006766 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006767 bool is_volatile,
6768 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006769 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006770 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006771 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306772 : HExpression(kStaticFieldGet,
6773 field_type,
6774 SideEffects::FieldReadOfType(field_type, is_volatile),
6775 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006776 field_info_(field,
6777 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006778 field_type,
6779 is_volatile,
6780 field_idx,
6781 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006782 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006783 SetRawInputAt(0, cls);
6784 }
6785
Calin Juravle52c48962014-12-16 17:02:57 +00006786
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006787 bool IsClonable() const override { return true; }
6788 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006789
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006790 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006791 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006792 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006793 }
6794
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006795 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006796 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6797 }
6798
Calin Juravle52c48962014-12-16 17:02:57 +00006799 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006800 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006801 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006802 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006803
Vladimir Marko61b92282017-10-11 13:23:17 +01006804 void SetType(DataType::Type new_type) {
6805 DCHECK(DataType::IsIntegralType(GetType()));
6806 DCHECK(DataType::IsIntegralType(new_type));
6807 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6808 SetPackedField<TypeField>(new_type);
6809 }
6810
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006811 DECLARE_INSTRUCTION(StaticFieldGet);
6812
Artem Serovcced8ba2017-07-19 18:18:09 +01006813 protected:
6814 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6815
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006816 private:
6817 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006818};
6819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006820class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006821 public:
6822 HStaticFieldSet(HInstruction* cls,
6823 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006824 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006825 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006826 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006827 bool is_volatile,
6828 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006829 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006830 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006831 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006832 : HExpression(kStaticFieldSet,
6833 SideEffects::FieldWriteOfType(field_type, is_volatile),
6834 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006835 field_info_(field,
6836 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006837 field_type,
6838 is_volatile,
6839 field_idx,
6840 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006841 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006842 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006843 SetRawInputAt(0, cls);
6844 SetRawInputAt(1, value);
6845 }
6846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006847 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006848 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006849 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006850 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006851 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006852
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006853 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006854 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6855 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006856
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006857 DECLARE_INSTRUCTION(StaticFieldSet);
6858
Artem Serovcced8ba2017-07-19 18:18:09 +01006859 protected:
6860 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6861
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006862 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006863 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6864 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6865 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6866 "Too many packed fields.");
6867
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006868 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006869};
6870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006871class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006872 public:
6873 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006874 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006875 uint32_t field_index,
6876 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306877 : HExpression(kUnresolvedInstanceFieldGet,
6878 field_type,
6879 SideEffects::AllExceptGCDependency(),
6880 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006881 field_index_(field_index) {
6882 SetRawInputAt(0, obj);
6883 }
6884
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006885 bool IsClonable() const override { return true; }
6886 bool NeedsEnvironment() const override { return true; }
6887 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006888
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006889 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006890 uint32_t GetFieldIndex() const { return field_index_; }
6891
6892 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6893
Artem Serovcced8ba2017-07-19 18:18:09 +01006894 protected:
6895 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6896
Calin Juravlee460d1d2015-09-29 04:52:17 +01006897 private:
6898 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006899};
6900
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006901class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006902 public:
6903 HUnresolvedInstanceFieldSet(HInstruction* obj,
6904 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006905 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006906 uint32_t field_index,
6907 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006908 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006909 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006910 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006911 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006912 SetRawInputAt(0, obj);
6913 SetRawInputAt(1, value);
6914 }
6915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006916 bool IsClonable() const override { return true; }
6917 bool NeedsEnvironment() const override { return true; }
6918 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006919
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006920 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006921 uint32_t GetFieldIndex() const { return field_index_; }
6922
6923 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6924
Artem Serovcced8ba2017-07-19 18:18:09 +01006925 protected:
6926 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6927
Calin Juravlee460d1d2015-09-29 04:52:17 +01006928 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006929 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6930 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006931 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006932 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6933 kFieldFieldType + kFieldFieldTypeSize;
6934 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6935 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006936 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006937
Calin Juravlee460d1d2015-09-29 04:52:17 +01006938 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006939};
6940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006941class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006942 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006943 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006944 uint32_t field_index,
6945 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306946 : HExpression(kUnresolvedStaticFieldGet,
6947 field_type,
6948 SideEffects::AllExceptGCDependency(),
6949 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006950 field_index_(field_index) {
6951 }
6952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006953 bool IsClonable() const override { return true; }
6954 bool NeedsEnvironment() const override { return true; }
6955 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006957 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006958 uint32_t GetFieldIndex() const { return field_index_; }
6959
6960 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6961
Artem Serovcced8ba2017-07-19 18:18:09 +01006962 protected:
6963 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6964
Calin Juravlee460d1d2015-09-29 04:52:17 +01006965 private:
6966 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006967};
6968
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006969class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006970 public:
6971 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006972 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006973 uint32_t field_index,
6974 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006975 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006976 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006977 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006978 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006979 SetRawInputAt(0, value);
6980 }
6981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006982 bool IsClonable() const override { return true; }
6983 bool NeedsEnvironment() const override { return true; }
6984 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006986 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006987 uint32_t GetFieldIndex() const { return field_index_; }
6988
6989 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6990
Artem Serovcced8ba2017-07-19 18:18:09 +01006991 protected:
6992 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6993
Calin Juravlee460d1d2015-09-29 04:52:17 +01006994 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006995 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6996 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006997 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006998 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6999 kFieldFieldType + kFieldFieldTypeSize;
7000 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7001 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007002 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007003
Calin Juravlee460d1d2015-09-29 04:52:17 +01007004 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007005};
7006
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007007// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007008class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007009 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007010 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307011 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7012 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007014 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007015
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007016 DECLARE_INSTRUCTION(LoadException);
7017
Artem Serovcced8ba2017-07-19 18:18:09 +01007018 protected:
7019 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007020};
7021
David Brazdilcb1c0552015-08-04 16:22:25 +01007022// Implicit part of move-exception which clears thread-local exception storage.
7023// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007024class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007025 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007026 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007027 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307028 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007029
7030 DECLARE_INSTRUCTION(ClearException);
7031
Artem Serovcced8ba2017-07-19 18:18:09 +01007032 protected:
7033 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007034};
7035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007036class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007037 public:
7038 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007039 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007040 SetRawInputAt(0, exception);
7041 }
7042
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007043 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007044
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007045 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007047 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007049 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007050
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007051 DECLARE_INSTRUCTION(Throw);
7052
Artem Serovcced8ba2017-07-19 18:18:09 +01007053 protected:
7054 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007055};
7056
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007057/**
7058 * Implementation strategies for the code generator of a HInstanceOf
7059 * or `HCheckCast`.
7060 */
7061enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007062 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007063 kExactCheck, // Can do a single class compare.
7064 kClassHierarchyCheck, // Can just walk the super class chain.
7065 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7066 kInterfaceCheck, // No optimization yet when checking against an interface.
7067 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007068 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007069 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007070 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007071};
7072
Roland Levillain86503782016-02-11 19:07:30 +00007073std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7074
Vladimir Marko175e7862018-03-27 09:03:13 +00007075// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7076// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7077class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007078 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007079 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007080 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007081 HInstruction* object,
7082 HInstruction* target_class_or_null,
7083 TypeCheckKind check_kind,
7084 Handle<mirror::Class> klass,
7085 uint32_t dex_pc,
7086 ArenaAllocator* allocator,
7087 HIntConstant* bitstring_path_to_root,
7088 HIntConstant* bitstring_mask,
7089 SideEffects side_effects)
7090 : HVariableInputSizeInstruction(
7091 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007092 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007093 side_effects,
7094 dex_pc,
7095 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007096 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007097 kArenaAllocTypeCheckInputs),
7098 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007099 SetPackedField<TypeCheckKindField>(check_kind);
7100 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007101 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007102 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007103 SetRawInputAt(1, target_class_or_null);
7104 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7105 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7106 if (check_kind == TypeCheckKind::kBitstringCheck) {
7107 DCHECK(target_class_or_null->IsNullConstant());
7108 SetRawInputAt(2, bitstring_path_to_root);
7109 SetRawInputAt(3, bitstring_mask);
7110 } else {
7111 DCHECK(target_class_or_null->IsLoadClass());
7112 }
Vladimir Marko87584542017-12-12 17:47:52 +00007113 }
7114
7115 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007116 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007117 HInstruction* load_class = InputAt(1);
7118 DCHECK(load_class->IsLoadClass());
7119 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007120 }
7121
Vladimir Marko175e7862018-03-27 09:03:13 +00007122 uint32_t GetBitstringPathToRoot() const {
7123 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7124 HInstruction* path_to_root = InputAt(2);
7125 DCHECK(path_to_root->IsIntConstant());
7126 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7127 }
7128
7129 uint32_t GetBitstringMask() const {
7130 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7131 HInstruction* mask = InputAt(3);
7132 DCHECK(mask->IsIntConstant());
7133 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7134 }
7135
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007136 bool IsClonable() const override { return true; }
7137 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007138
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007139 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007140 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7141 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007142 }
7143
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007144 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7145 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7146 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7147 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7148
Vladimir Marko175e7862018-03-27 09:03:13 +00007149 ReferenceTypeInfo GetTargetClassRTI() {
7150 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7151 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007152 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007153 } else {
7154 return ReferenceTypeInfo::CreateInvalid();
7155 }
7156 }
7157
7158 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7159 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7160 DCHECK(klass_ != nullptr);
7161 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7162 }
7163
7164 Handle<mirror::Class> GetClass() const {
7165 return klass_;
7166 }
7167
7168 protected:
7169 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7170
7171 private:
7172 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7173 static constexpr size_t kFieldTypeCheckKindSize =
7174 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7175 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7176 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7177 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7178 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7179 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7180
7181 Handle<mirror::Class> klass_;
7182};
7183
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007184class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007185 public:
7186 HInstanceOf(HInstruction* object,
7187 HInstruction* target_class_or_null,
7188 TypeCheckKind check_kind,
7189 Handle<mirror::Class> klass,
7190 uint32_t dex_pc,
7191 ArenaAllocator* allocator,
7192 HIntConstant* bitstring_path_to_root,
7193 HIntConstant* bitstring_mask)
7194 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007195 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007196 object,
7197 target_class_or_null,
7198 check_kind,
7199 klass,
7200 dex_pc,
7201 allocator,
7202 bitstring_path_to_root,
7203 bitstring_mask,
7204 SideEffectsForArchRuntimeCalls(check_kind)) {}
7205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007206 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007207
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007208 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007209 return CanCallRuntime(GetTypeCheckKind());
7210 }
7211
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007212 static bool CanCallRuntime(TypeCheckKind check_kind) {
7213 // Mips currently does runtime calls for any other checks.
7214 return check_kind != TypeCheckKind::kExactCheck;
7215 }
7216
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007217 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007218 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007219 }
7220
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007221 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007222
Artem Serovcced8ba2017-07-19 18:18:09 +01007223 protected:
7224 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007225};
7226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007227class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007228 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007229 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307230 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007231 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7232 SetPackedFlag<kFlagUpperCanBeNull>(true);
7233 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007234 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007235 SetRawInputAt(0, input);
7236 }
7237
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007238 bool InstructionDataEquals(const HInstruction* other) const override;
7239 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007240
David Brazdilf5552582015-12-27 13:36:12 +00007241 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007242 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007243 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007244 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007245
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007246 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007247 DCHECK(GetUpperCanBeNull() || !can_be_null);
7248 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007249 }
7250
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007251 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007252
Calin Juravleb1498f62015-02-16 13:13:29 +00007253 DECLARE_INSTRUCTION(BoundType);
7254
Artem Serovcced8ba2017-07-19 18:18:09 +01007255 protected:
7256 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7257
Calin Juravleb1498f62015-02-16 13:13:29 +00007258 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007259 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7260 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007261 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007262 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7263 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7264 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7265
Calin Juravleb1498f62015-02-16 13:13:29 +00007266 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007267 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7268 // It is used to bound the type in cases like:
7269 // if (x instanceof ClassX) {
7270 // // uper_bound_ will be ClassX
7271 // }
David Brazdilf5552582015-12-27 13:36:12 +00007272 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007273};
7274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007275class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007276 public:
7277 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007278 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007279 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007280 Handle<mirror::Class> klass,
7281 uint32_t dex_pc,
7282 ArenaAllocator* allocator,
7283 HIntConstant* bitstring_path_to_root,
7284 HIntConstant* bitstring_mask)
7285 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007286 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007287 object,
7288 target_class_or_null,
7289 check_kind,
7290 klass,
7291 dex_pc,
7292 allocator,
7293 bitstring_path_to_root,
7294 bitstring_mask,
7295 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007297 bool IsClonable() const override { return true; }
7298 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007299 // Instruction may throw a CheckCastError.
7300 return true;
7301 }
7302
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007303 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007304
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007305 DECLARE_INSTRUCTION(CheckCast);
7306
Artem Serovcced8ba2017-07-19 18:18:09 +01007307 protected:
7308 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007309};
7310
Andreas Gampe26de38b2016-07-27 17:53:11 -07007311/**
7312 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7313 * @details We define the combined barrier types that are actually required
7314 * by the Java Memory Model, rather than using exactly the terminology from
7315 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7316 * primitives. Note that the JSR-133 cookbook generally does not deal with
7317 * store atomicity issues, and the recipes there are not always entirely sufficient.
7318 * The current recipe is as follows:
7319 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7320 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7321 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7322 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7323 * class has final fields.
7324 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7325 * store-to-memory instructions. Only generated together with non-temporal stores.
7326 */
7327enum MemBarrierKind {
7328 kAnyStore,
7329 kLoadAny,
7330 kStoreStore,
7331 kAnyAny,
7332 kNTStoreStore,
7333 kLastBarrierKind = kNTStoreStore
7334};
7335std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7336
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007337class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007339 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007340 : HExpression(kMemoryBarrier,
7341 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7342 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007343 SetPackedField<BarrierKindField>(barrier_kind);
7344 }
Calin Juravle27df7582015-04-17 19:12:31 +01007345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007346 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007347
Vladimir Markoa1de9182016-02-25 11:37:38 +00007348 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007349
7350 DECLARE_INSTRUCTION(MemoryBarrier);
7351
Artem Serovcced8ba2017-07-19 18:18:09 +01007352 protected:
7353 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7354
Calin Juravle27df7582015-04-17 19:12:31 +01007355 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007356 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7357 static constexpr size_t kFieldBarrierKindSize =
7358 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7359 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7360 kFieldBarrierKind + kFieldBarrierKindSize;
7361 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7362 "Too many packed fields.");
7363 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007364};
7365
Igor Murashkind01745e2017-04-05 16:40:31 -07007366// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7367// the specified object(s), with respect to any subsequent store that might "publish"
7368// (i.e. make visible) the specified object to another thread.
7369//
7370// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7371// for all final fields (that were set) at the end of the invoked constructor.
7372//
7373// The constructor fence models the freeze actions for the final fields of an object
7374// being constructed (semantically at the end of the constructor). Constructor fences
7375// have a per-object affinity; two separate objects being constructed get two separate
7376// constructor fences.
7377//
7378// (Note: that if calling a super-constructor or forwarding to another constructor,
7379// the freezes would happen at the end of *that* constructor being invoked).
7380//
7381// The memory model guarantees that when the object being constructed is "published" after
7382// constructor completion (i.e. escapes the current thread via a store), then any final field
7383// writes must be observable on other threads (once they observe that publication).
7384//
7385// Further, anything written before the freeze, and read by dereferencing through the final field,
7386// must also be visible (so final object field could itself have an object with non-final fields;
7387// yet the freeze must also extend to them).
7388//
7389// Constructor example:
7390//
7391// class HasFinal {
7392// final int field; Optimizing IR for <init>()V:
7393// HasFinal() {
7394// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7395// // freeze(this.field); HConstructorFence(this)
7396// } HReturn
7397// }
7398//
7399// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7400// already-initialized classes; in that case the allocation must act as a "default-initializer"
7401// of the object which effectively writes the class pointer "final field".
7402//
7403// For example, we can model default-initialiation as roughly the equivalent of the following:
7404//
7405// class Object {
7406// private final Class header;
7407// }
7408//
7409// Java code: Optimizing IR:
7410//
7411// T new_instance<T>() {
7412// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7413// obj.header = T.class; // header write is done by above call.
7414// // freeze(obj.header) HConstructorFence(obj)
7415// return (T)obj;
7416// }
7417//
7418// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007419// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007420// * QuasiAtomic::ThreadFenceForConstructor
7421//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007422class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007423 // A fence has variable inputs because the inputs can be removed
7424 // after prepare_for_register_allocation phase.
7425 // (TODO: In the future a fence could freeze multiple objects
7426 // after merging two fences together.)
7427 public:
7428 // `fence_object` is the reference that needs to be protected for correct publication.
7429 //
7430 // It makes sense in the following situations:
7431 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7432 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7433 //
7434 // After construction the `fence_object` becomes the 0th input.
7435 // This is not an input in a real sense, but just a convenient place to stash the information
7436 // about the associated object.
7437 HConstructorFence(HInstruction* fence_object,
7438 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007439 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007440 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7441 // constraints described in the class header. We claim that these SideEffects constraints
7442 // enforce a superset of the real constraints.
7443 //
7444 // The ordering described above is conservatively modeled with SideEffects as follows:
7445 //
7446 // * To prevent reordering of the publication stores:
7447 // ----> "Reads of objects" is the initial SideEffect.
7448 // * For every primitive final field store in the constructor:
7449 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7450 // * If there are any stores to reference final fields in the constructor:
7451 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7452 // that are reachable from `fence_object` also need to be prevented for reordering
7453 // (and we do not want to do alias analysis to figure out what those stores are).
7454 //
7455 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7456 // even more conservative approach than the one described above, and prevents all of the
7457 // above reordering without analyzing any of the instructions in the constructor.
7458 //
7459 // If in a later phase we discover that there are no writes to reference final fields,
7460 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307461 : HVariableInputSizeInstruction(kConstructorFence,
7462 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007463 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007464 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007465 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007466 kArenaAllocConstructorFenceInputs) {
7467 DCHECK(fence_object != nullptr);
7468 SetRawInputAt(0, fence_object);
7469 }
7470
7471 // The object associated with this constructor fence.
7472 //
7473 // (Note: This will be null after the prepare_for_register_allocation phase,
7474 // as all constructor fence inputs are removed there).
7475 HInstruction* GetFenceObject() const {
7476 return InputAt(0);
7477 }
7478
7479 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7480 // - Delete `fence_use` from `this`'s use list.
7481 // - Delete `this` from `fence_use`'s inputs list.
7482 // - If the `fence_use` is dead, remove it from the graph.
7483 //
7484 // A fence is considered dead once it no longer has any uses
7485 // and all of the inputs are dead.
7486 //
7487 // This must *not* be called during/after prepare_for_register_allocation,
7488 // because that removes all the inputs to the fences but the fence is actually
7489 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007490 //
7491 // Returns how many HConstructorFence instructions were removed from graph.
7492 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007493
Igor Murashkindd018df2017-08-09 10:38:31 -07007494 // Combine all inputs of `this` and `other` instruction and remove
7495 // `other` from the graph.
7496 //
7497 // Inputs are unique after the merge.
7498 //
7499 // Requirement: `this` must not be the same as `other.
7500 void Merge(HConstructorFence* other);
7501
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007502 // Check if this constructor fence is protecting
7503 // an HNewInstance or HNewArray that is also the immediate
7504 // predecessor of `this`.
7505 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007506 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7507 // to be one of the inputs of `this`.
7508 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007509 // Returns the associated HNewArray or HNewInstance,
7510 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007511 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007512
Igor Murashkind01745e2017-04-05 16:40:31 -07007513 DECLARE_INSTRUCTION(ConstructorFence);
7514
Artem Serovcced8ba2017-07-19 18:18:09 +01007515 protected:
7516 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007517};
7518
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007519class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007520 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007521 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007522 kEnter,
7523 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007524 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007525 };
7526
7527 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007528 : HExpression(kMonitorOperation,
7529 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7530 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007531 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007532 SetRawInputAt(0, object);
7533 }
7534
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007535 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007536 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007537
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007538 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007539 // Verifier guarantees that monitor-exit cannot throw.
7540 // This is important because it allows the HGraphBuilder to remove
7541 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7542 return IsEnter();
7543 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007544
Vladimir Markoa1de9182016-02-25 11:37:38 +00007545 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7546 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007547
7548 DECLARE_INSTRUCTION(MonitorOperation);
7549
Artem Serovcced8ba2017-07-19 18:18:09 +01007550 protected:
7551 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7552
Calin Juravle52c48962014-12-16 17:02:57 +00007553 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007554 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7555 static constexpr size_t kFieldOperationKindSize =
7556 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7557 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7558 kFieldOperationKind + kFieldOperationKindSize;
7559 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7560 "Too many packed fields.");
7561 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007562};
7563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007564class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007565 public:
7566 HSelect(HInstruction* condition,
7567 HInstruction* true_value,
7568 HInstruction* false_value,
7569 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307570 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007571 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7572
7573 // First input must be `true_value` or `false_value` to allow codegens to
7574 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7575 // that architectures which implement HSelect as a conditional move also
7576 // will not need to invert the condition.
7577 SetRawInputAt(0, false_value);
7578 SetRawInputAt(1, true_value);
7579 SetRawInputAt(2, condition);
7580 }
7581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007582 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007583 HInstruction* GetFalseValue() const { return InputAt(0); }
7584 HInstruction* GetTrueValue() const { return InputAt(1); }
7585 HInstruction* GetCondition() const { return InputAt(2); }
7586
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007587 bool CanBeMoved() const override { return true; }
7588 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007589 return true;
7590 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007591
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007592 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007593 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7594 }
7595
7596 DECLARE_INSTRUCTION(Select);
7597
Artem Serovcced8ba2017-07-19 18:18:09 +01007598 protected:
7599 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007600};
7601
Vladimir Markof9f64412015-09-02 14:05:49 +01007602class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007603 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007604 MoveOperands(Location source,
7605 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007606 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007607 HInstruction* instruction)
7608 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007609
7610 Location GetSource() const { return source_; }
7611 Location GetDestination() const { return destination_; }
7612
7613 void SetSource(Location value) { source_ = value; }
7614 void SetDestination(Location value) { destination_ = value; }
7615
7616 // The parallel move resolver marks moves as "in-progress" by clearing the
7617 // destination (but not the source).
7618 Location MarkPending() {
7619 DCHECK(!IsPending());
7620 Location dest = destination_;
7621 destination_ = Location::NoLocation();
7622 return dest;
7623 }
7624
7625 void ClearPending(Location dest) {
7626 DCHECK(IsPending());
7627 destination_ = dest;
7628 }
7629
7630 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007631 DCHECK(source_.IsValid() || destination_.IsInvalid());
7632 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007633 }
7634
7635 // True if this blocks a move from the given location.
7636 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007637 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007638 }
7639
7640 // A move is redundant if it's been eliminated, if its source and
7641 // destination are the same, or if its destination is unneeded.
7642 bool IsRedundant() const {
7643 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7644 }
7645
7646 // We clear both operands to indicate move that's been eliminated.
7647 void Eliminate() {
7648 source_ = destination_ = Location::NoLocation();
7649 }
7650
7651 bool IsEliminated() const {
7652 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7653 return source_.IsInvalid();
7654 }
7655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007656 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007657
Nicolas Geoffray90218252015-04-15 11:56:51 +01007658 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007659 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007660 }
7661
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007662 HInstruction* GetInstruction() const { return instruction_; }
7663
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007664 private:
7665 Location source_;
7666 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007667 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007668 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007669 // The instruction this move is assocatied with. Null when this move is
7670 // for moving an input in the expected locations of user (including a phi user).
7671 // This is only used in debug mode, to ensure we do not connect interval siblings
7672 // in the same parallel move.
7673 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007674};
7675
Roland Levillainc9285912015-12-18 10:38:42 +00007676std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7677
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007678static constexpr size_t kDefaultNumberOfMoves = 4;
7679
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007680class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007681 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007682 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007683 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007684 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007685 moves_.reserve(kDefaultNumberOfMoves);
7686 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007687
Nicolas Geoffray90218252015-04-15 11:56:51 +01007688 void AddMove(Location source,
7689 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007690 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007691 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007692 DCHECK(source.IsValid());
7693 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007694 if (kIsDebugBuild) {
7695 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007696 for (const MoveOperands& move : moves_) {
7697 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007698 // Special case the situation where the move is for the spill slot
7699 // of the instruction.
7700 if ((GetPrevious() == instruction)
7701 || ((GetPrevious() == nullptr)
7702 && instruction->IsPhi()
7703 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007704 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007705 << "Doing parallel moves for the same instruction.";
7706 } else {
7707 DCHECK(false) << "Doing parallel moves for the same instruction.";
7708 }
7709 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007710 }
7711 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007712 for (const MoveOperands& move : moves_) {
7713 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007714 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007715 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007716 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007717 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007718 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007719 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007720 }
7721
Vladimir Marko225b6462015-09-28 12:17:40 +01007722 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007723 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007724 }
7725
Vladimir Marko225b6462015-09-28 12:17:40 +01007726 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007727
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007728 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007729
Artem Serovcced8ba2017-07-19 18:18:09 +01007730 protected:
7731 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7732
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007733 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007734 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007735};
7736
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007737// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7738// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7739// never used across anything that can trigger GC.
7740// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7741// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007742class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007743 public:
7744 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307745 : HExpression(kIntermediateAddress,
7746 DataType::Type::kInt32,
7747 SideEffects::DependsOnGC(),
7748 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007749 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7750 DataType::Size(DataType::Type::kReference))
7751 << "kPrimInt and kPrimNot have different sizes.";
7752 SetRawInputAt(0, base_address);
7753 SetRawInputAt(1, offset);
7754 }
7755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007756 bool IsClonable() const override { return true; }
7757 bool CanBeMoved() const override { return true; }
7758 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007759 return true;
7760 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007761 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007762
7763 HInstruction* GetBaseAddress() const { return InputAt(0); }
7764 HInstruction* GetOffset() const { return InputAt(1); }
7765
7766 DECLARE_INSTRUCTION(IntermediateAddress);
7767
Artem Serovcced8ba2017-07-19 18:18:09 +01007768 protected:
7769 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007770};
7771
7772
Mark Mendell0616ae02015-04-17 12:49:27 -04007773} // namespace art
7774
Aart Bikf8f5a162017-02-06 15:35:29 -08007775#include "nodes_vector.h"
7776
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007777#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7778#include "nodes_shared.h"
7779#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007780#ifdef ART_ENABLE_CODEGEN_mips
7781#include "nodes_mips.h"
7782#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307783#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007784#include "nodes_x86.h"
7785#endif
7786
7787namespace art {
7788
Igor Murashkin6ef45672017-08-08 13:59:55 -07007789class OptimizingCompilerStats;
7790
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007791class HGraphVisitor : public ValueObject {
7792 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007793 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7794 : stats_(stats),
7795 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007796 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007797
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007798 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007799 virtual void VisitBasicBlock(HBasicBlock* block);
7800
Roland Levillain633021e2014-10-01 14:12:25 +01007801 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007802 void VisitInsertionOrder();
7803
Roland Levillain633021e2014-10-01 14:12:25 +01007804 // Visit the graph following dominator tree reverse post-order.
7805 void VisitReversePostOrder();
7806
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007807 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007808
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007809 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007810#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007811 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7812
7813 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7814
7815#undef DECLARE_VISIT_INSTRUCTION
7816
Igor Murashkin6ef45672017-08-08 13:59:55 -07007817 protected:
7818 OptimizingCompilerStats* stats_;
7819
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007820 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007821 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007822
7823 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7824};
7825
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007826class HGraphDelegateVisitor : public HGraphVisitor {
7827 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007828 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7829 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007830 virtual ~HGraphDelegateVisitor() {}
7831
7832 // Visit functions that delegate to to super class.
7833#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007834 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007835
7836 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7837
7838#undef DECLARE_VISIT_INSTRUCTION
7839
7840 private:
7841 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7842};
7843
Artem Serovcced8ba2017-07-19 18:18:09 +01007844// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7845// and remove the old instruction.
7846HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7847
7848// Create a clone for each clonable instructions/phis and replace the original with the clone.
7849//
7850// Used for testing individual instruction cloner.
7851class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7852 public:
7853 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007854 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007855
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007856 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007857 if (instruction->IsClonable()) {
7858 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007859 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007860 }
7861 }
7862
Artem Serov7f4aff62017-06-21 17:02:18 +01007863 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007864
7865 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007866 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007867
7868 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7869};
7870
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007871// Iterator over the blocks that art part of the loop. Includes blocks part
7872// of an inner loop. The order in which the blocks are iterated is on their
7873// block id.
7874class HBlocksInLoopIterator : public ValueObject {
7875 public:
7876 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7877 : blocks_in_loop_(info.GetBlocks()),
7878 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7879 index_(0) {
7880 if (!blocks_in_loop_.IsBitSet(index_)) {
7881 Advance();
7882 }
7883 }
7884
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007885 bool Done() const { return index_ == blocks_.size(); }
7886 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007887 void Advance() {
7888 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007889 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007890 if (blocks_in_loop_.IsBitSet(index_)) {
7891 break;
7892 }
7893 }
7894 }
7895
7896 private:
7897 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007898 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007899 size_t index_;
7900
7901 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7902};
7903
Mingyao Yang3584bce2015-05-19 16:01:59 -07007904// Iterator over the blocks that art part of the loop. Includes blocks part
7905// of an inner loop. The order in which the blocks are iterated is reverse
7906// post order.
7907class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7908 public:
7909 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7910 : blocks_in_loop_(info.GetBlocks()),
7911 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7912 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007913 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007914 Advance();
7915 }
7916 }
7917
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007918 bool Done() const { return index_ == blocks_.size(); }
7919 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007920 void Advance() {
7921 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007922 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7923 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007924 break;
7925 }
7926 }
7927 }
7928
7929 private:
7930 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007931 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007932 size_t index_;
7933
7934 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7935};
7936
Aart Bikf3e61ee2017-04-12 17:09:20 -07007937// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007938inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007939 if (constant->IsIntConstant()) {
7940 return constant->AsIntConstant()->GetValue();
7941 } else if (constant->IsLongConstant()) {
7942 return constant->AsLongConstant()->GetValue();
7943 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007944 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007945 return 0;
7946 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007947}
7948
Aart Bikf3e61ee2017-04-12 17:09:20 -07007949// Returns true iff instruction is an integral constant (and sets value on success).
7950inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7951 if (instruction->IsIntConstant()) {
7952 *value = instruction->AsIntConstant()->GetValue();
7953 return true;
7954 } else if (instruction->IsLongConstant()) {
7955 *value = instruction->AsLongConstant()->GetValue();
7956 return true;
7957 } else if (instruction->IsNullConstant()) {
7958 *value = 0;
7959 return true;
7960 }
7961 return false;
7962}
7963
Aart Bik0148de42017-09-05 09:25:01 -07007964// Returns true iff instruction is the given integral constant.
7965inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7966 int64_t val = 0;
7967 return IsInt64AndGet(instruction, &val) && val == value;
7968}
7969
7970// Returns true iff instruction is a zero bit pattern.
7971inline bool IsZeroBitPattern(HInstruction* instruction) {
7972 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7973}
7974
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007975// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007976#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007977 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7978 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7979#undef INSTRUCTION_TYPE_CHECK
7980
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007981// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007982#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7983 std::is_base_of<BaseType, H##type>::value,
7984#define INSTRUCTION_TYPE_CHECK(type, super) \
7985 inline bool HInstruction::Is##type() const { \
7986 DCHECK_LT(GetKind(), kLastInstructionKind); \
7987 using BaseType = H##type; \
7988 static constexpr bool results[] = { \
7989 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7990 }; \
7991 return results[static_cast<size_t>(GetKind())]; \
7992 }
7993
7994 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7995#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007996#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007997
7998#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007999 inline const H##type* HInstruction::As##type() const { \
8000 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8001 } \
8002 inline H##type* HInstruction::As##type() { \
8003 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8004 }
8005
Vladimir Markoa90dd512018-05-04 15:04:45 +01008006 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8007#undef INSTRUCTION_TYPE_CAST
8008
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008009
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008010// Create space in `blocks` for adding `number_of_new_blocks` entries
8011// starting at location `at`. Blocks after `at` are moved accordingly.
8012inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8013 size_t number_of_new_blocks,
8014 size_t after) {
8015 DCHECK_LT(after, blocks->size());
8016 size_t old_size = blocks->size();
8017 size_t new_size = old_size + number_of_new_blocks;
8018 blocks->resize(new_size);
8019 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8020}
8021
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008022/*
8023 * Hunt "under the hood" of array lengths (leading to array references),
8024 * null checks (also leading to array references), and new arrays
8025 * (leading to the actual length). This makes it more likely related
8026 * instructions become actually comparable.
8027 */
8028inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8029 while (instruction->IsArrayLength() ||
8030 instruction->IsNullCheck() ||
8031 instruction->IsNewArray()) {
8032 instruction = instruction->IsNewArray()
8033 ? instruction->AsNewArray()->GetLength()
8034 : instruction->InputAt(0);
8035 }
8036 return instruction;
8037}
8038
Artem Serov21c7e6f2017-07-27 16:04:42 +01008039void RemoveEnvironmentUses(HInstruction* instruction);
8040bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8041void ResetEnvironmentInputRecords(HInstruction* instruction);
8042
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008043} // namespace art
8044
8045#endif // ART_COMPILER_OPTIMIZING_NODES_H_