blob: 686a2deb0b4f41dba1c79c38044998fec7b95637 [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),
898 catch_type_index_(DexFile::kDexNoIndex16) {
899 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
917 bool IsCatchAllTypeIndex() const {
918 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800919 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
932 private:
933 // One of possibly several TryBoundary instructions entering the block's try.
934 // Only set for try blocks.
935 const HTryBoundary* try_entry_;
936
937 // Exception type information. Only set for catch blocks.
938 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800939 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100940};
941
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100942static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100943static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100944
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000945// A block in a method. Contains the list of instructions represented
946// as a double linked list. Each block knows its predecessors and
947// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100948
Vladimir Markof9f64412015-09-02 14:05:49 +0100949class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000950 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700951 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100953 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
954 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000955 loop_information_(nullptr),
956 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100957 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100958 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100959 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100960 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000961 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000962 try_catch_information_(nullptr) {
963 predecessors_.reserve(kDefaultNumberOfPredecessors);
964 successors_.reserve(kDefaultNumberOfSuccessors);
965 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
966 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967
Vladimir Marko60584552015-09-03 13:35:12 +0000968 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 }
971
Vladimir Marko60584552015-09-03 13:35:12 +0000972 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100973 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000974 }
975
David Brazdild26a4112015-11-10 11:07:31 +0000976 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
977 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
978
Vladimir Marko60584552015-09-03 13:35:12 +0000979 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
980 return ContainsElement(successors_, block, start_from);
981 }
982
983 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100984 return dominated_blocks_;
985 }
986
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100987 bool IsEntryBlock() const {
988 return graph_->GetEntryBlock() == this;
989 }
990
991 bool IsExitBlock() const {
992 return graph_->GetExitBlock() == this;
993 }
994
David Brazdil46e2a392015-03-16 17:31:52 +0000995 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200996 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700997 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000998 bool IsSingleTryBoundary() const;
999
1000 // Returns true if this block emits nothing but a jump.
1001 bool IsSingleJump() const {
1002 HLoopInformation* loop_info = GetLoopInformation();
1003 return (IsSingleGoto() || IsSingleTryBoundary())
1004 // Back edges generate a suspend check.
1005 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1006 }
David Brazdil46e2a392015-03-16 17:31:52 +00001007
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001008 void AddBackEdge(HBasicBlock* back_edge) {
1009 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001010 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001011 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001012 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001013 loop_information_->AddBackEdge(back_edge);
1014 }
1015
Artem Serov7f4aff62017-06-21 17:02:18 +01001016 // Registers a back edge; if the block was not a loop header before the call associates a newly
1017 // created loop info with it.
1018 //
1019 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1020 // info for all blocks during back edges recalculation.
1021 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1022 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1023 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1024 }
1025 loop_information_->AddBackEdge(back_edge);
1026 }
1027
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001028 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001031 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001032 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001033 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001034
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001035 HBasicBlock* GetDominator() const { return dominator_; }
1036 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001037 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1038
1039 void RemoveDominatedBlock(HBasicBlock* block) {
1040 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001041 }
Vladimir Marko60584552015-09-03 13:35:12 +00001042
1043 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1044 ReplaceElement(dominated_blocks_, existing, new_block);
1045 }
1046
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001047 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001048
1049 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001050 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001051 }
1052
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001053 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1054 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001055 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001056 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001057 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1058 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001060 HInstruction* GetFirstInstructionDisregardMoves() const;
1061
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001063 successors_.push_back(block);
1064 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065 }
1066
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001067 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1068 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001069 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001070 new_block->predecessors_.push_back(this);
1071 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001072 }
1073
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001074 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1075 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001076 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001077 new_block->successors_.push_back(this);
1078 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001079 }
1080
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001081 // Insert `this` between `predecessor` and `successor. This method
1082 // preserves the indicies, and will update the first edge found between
1083 // `predecessor` and `successor`.
1084 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1085 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001086 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001087 successor->predecessors_[predecessor_index] = this;
1088 predecessor->successors_[successor_index] = this;
1089 successors_.push_back(successor);
1090 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001091 }
1092
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001093 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001094 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001095 }
1096
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001097 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001098 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001099 }
1100
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001101 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001102 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001103 }
1104
1105 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001106 predecessors_.push_back(block);
1107 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001108 }
1109
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001110 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001111 DCHECK_EQ(predecessors_.size(), 2u);
1112 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001113 }
1114
David Brazdil769c9e52015-04-27 13:54:09 +01001115 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001116 DCHECK_EQ(successors_.size(), 2u);
1117 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001118 }
1119
David Brazdilfc6a86a2015-06-26 10:33:45 +00001120 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001121 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001122 }
1123
David Brazdilfc6a86a2015-06-26 10:33:45 +00001124 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001125 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001126 }
1127
David Brazdilfc6a86a2015-06-26 10:33:45 +00001128 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001129 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001130 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001131 }
1132
1133 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001134 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001135 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001136 }
1137
1138 // Returns whether the first occurrence of `predecessor` in the list of
1139 // predecessors is at index `idx`.
1140 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001141 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001142 return GetPredecessorIndexOf(predecessor) == idx;
1143 }
1144
David Brazdild7558da2015-09-22 13:04:14 +01001145 // Create a new block between this block and its predecessors. The new block
1146 // is added to the graph, all predecessor edges are relinked to it and an edge
1147 // is created to `this`. Returns the new empty block. Reverse post order or
1148 // loop and try/catch information are not updated.
1149 HBasicBlock* CreateImmediateDominator();
1150
David Brazdilfc6a86a2015-06-26 10:33:45 +00001151 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001152 // created, latter block. Note that this method will add the block to the
1153 // graph, create a Goto at the end of the former block and will create an edge
1154 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001155 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001156 HBasicBlock* SplitBefore(HInstruction* cursor);
1157
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001158 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001159 // created block. Note that this method just updates raw block information,
1160 // like predecessors, successors, dominators, and instruction list. It does not
1161 // update the graph, reverse post order, loop information, nor make sure the
1162 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001163 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1164
1165 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1166 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001167
1168 // Merge `other` at the end of `this`. Successors and dominated blocks of
1169 // `other` are changed to be successors and dominated blocks of `this`. Note
1170 // that this method does not update the graph, reverse post order, loop
1171 // information, nor make sure the blocks are consistent (for example ending
1172 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001173 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001174
1175 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1176 // of `this` are moved to `other`.
1177 // Note that this method does not update the graph, reverse post order, loop
1178 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001179 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001180 void ReplaceWith(HBasicBlock* other);
1181
Aart Bik6b69e0a2017-01-11 10:20:43 -08001182 // Merges the instructions of `other` at the end of `this`.
1183 void MergeInstructionsWith(HBasicBlock* other);
1184
David Brazdil2d7352b2015-04-20 14:52:42 +01001185 // Merge `other` at the end of `this`. This method updates loops, reverse post
1186 // order, links to predecessors, successors, dominators and deletes the block
1187 // from the graph. The two blocks must be successive, i.e. `this` the only
1188 // predecessor of `other` and vice versa.
1189 void MergeWith(HBasicBlock* other);
1190
1191 // Disconnects `this` from all its predecessors, successors and dominator,
1192 // removes it from all loops it is included in and eventually from the graph.
1193 // The block must not dominate any other block. Predecessors and successors
1194 // are safely updated.
1195 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001196
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001197 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001198 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001199 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001200 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001201 // Replace phi `initial` with `replacement` within this block.
1202 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001203 // Replace instruction `initial` with `replacement` within this block.
1204 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1205 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001206 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001207 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001208 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1209 // instruction list. With 'ensure_safety' set to true, it verifies that the
1210 // instruction is not in use and removes it from the use lists of its inputs.
1211 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1212 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001213 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214
1215 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001216 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001217 }
1218
Roland Levillain6b879dd2014-09-22 17:13:44 +01001219 bool IsLoopPreHeaderFirstPredecessor() const {
1220 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001221 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001222 }
1223
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001224 bool IsFirstPredecessorBackEdge() const {
1225 DCHECK(IsLoopHeader());
1226 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1227 }
1228
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001229 HLoopInformation* GetLoopInformation() const {
1230 return loop_information_;
1231 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001232
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001233 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001234 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001235 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001236 void SetInLoop(HLoopInformation* info) {
1237 if (IsLoopHeader()) {
1238 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001239 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001240 loop_information_ = info;
1241 } else if (loop_information_->Contains(*info->GetHeader())) {
1242 // Block is currently part of an outer loop. Make it part of this inner loop.
1243 // Note that a non loop header having a loop information means this loop information
1244 // has already been populated
1245 loop_information_ = info;
1246 } else {
1247 // Block is part of an inner loop. Do not update the loop information.
1248 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1249 // at this point, because this method is being called while populating `info`.
1250 }
1251 }
1252
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001253 // Raw update of the loop information.
1254 void SetLoopInformation(HLoopInformation* info) {
1255 loop_information_ = info;
1256 }
1257
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001258 bool IsInLoop() const { return loop_information_ != nullptr; }
1259
David Brazdilec16f792015-08-19 15:04:01 +01001260 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1261
1262 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1263 try_catch_information_ = try_catch_information;
1264 }
1265
1266 bool IsTryBlock() const {
1267 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1268 }
1269
1270 bool IsCatchBlock() const {
1271 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1272 }
David Brazdilffee3d32015-07-06 11:48:53 +01001273
1274 // Returns the try entry that this block's successors should have. They will
1275 // be in the same try, unless the block ends in a try boundary. In that case,
1276 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001277 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001278
Aart Bik75ff2c92018-04-21 01:28:11 +00001279 bool HasThrowingInstructions() const;
1280
David Brazdila4b8c212015-05-07 09:59:30 +01001281 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001282 bool Dominates(HBasicBlock* block) const;
1283
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001284 size_t GetLifetimeStart() const { return lifetime_start_; }
1285 size_t GetLifetimeEnd() const { return lifetime_end_; }
1286
1287 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1288 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1289
David Brazdil8d5b8b22015-03-24 10:51:52 +00001290 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001291 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001292 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001293 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001294 bool HasSinglePhi() const;
1295
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001296 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001297 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001298 ArenaVector<HBasicBlock*> predecessors_;
1299 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001300 HInstructionList instructions_;
1301 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001302 HLoopInformation* loop_information_;
1303 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001304 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001305 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001306 // The dex program counter of the first instruction of this block.
1307 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001308 size_t lifetime_start_;
1309 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001310 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001311
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001312 friend class HGraph;
1313 friend class HInstruction;
1314
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001315 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1316};
1317
David Brazdilb2bd1c52015-03-25 11:17:37 +00001318// Iterates over the LoopInformation of all loops which contain 'block'
1319// from the innermost to the outermost.
1320class HLoopInformationOutwardIterator : public ValueObject {
1321 public:
1322 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1323 : current_(block.GetLoopInformation()) {}
1324
1325 bool Done() const { return current_ == nullptr; }
1326
1327 void Advance() {
1328 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001329 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001330 }
1331
1332 HLoopInformation* Current() const {
1333 DCHECK(!Done());
1334 return current_;
1335 }
1336
1337 private:
1338 HLoopInformation* current_;
1339
1340 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1341};
1342
Alexandre Ramesef20f712015-06-09 10:29:30 +01001343#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001344 M(Above, Condition) \
1345 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001346 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001347 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001348 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001349 M(ArrayGet, Instruction) \
1350 M(ArrayLength, Instruction) \
1351 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001352 M(Below, Condition) \
1353 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001354 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001355 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001356 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001357 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001358 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001359 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001360 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001361 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001362 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001363 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001364 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001365 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001366 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001367 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001368 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001370 M(Exit, Instruction) \
1371 M(FloatConstant, Constant) \
1372 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001373 M(GreaterThan, Condition) \
1374 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001375 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001376 M(InstanceFieldGet, Instruction) \
1377 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001378 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001379 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001380 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001381 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001382 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001383 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001384 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001385 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001386 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001387 M(LessThan, Condition) \
1388 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001389 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001390 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001391 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001392 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001393 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001394 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001395 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001396 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001397 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001398 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001400 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001401 M(Neg, UnaryOperation) \
1402 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001403 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001404 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001405 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001406 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001407 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001408 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001409 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001410 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001411 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001412 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001413 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 M(Return, Instruction) \
1415 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001416 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001417 M(Shl, BinaryOperation) \
1418 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001419 M(StaticFieldGet, Instruction) \
1420 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001421 M(UnresolvedInstanceFieldGet, Instruction) \
1422 M(UnresolvedInstanceFieldSet, Instruction) \
1423 M(UnresolvedStaticFieldGet, Instruction) \
1424 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001425 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001426 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001427 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001428 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001429 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001430 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001431 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001432 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001433 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001434 M(VecExtractScalar, VecUnaryOperation) \
1435 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001436 M(VecCnv, VecUnaryOperation) \
1437 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001438 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001439 M(VecNot, VecUnaryOperation) \
1440 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001441 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001442 M(VecSub, VecBinaryOperation) \
1443 M(VecMul, VecBinaryOperation) \
1444 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001445 M(VecMin, VecBinaryOperation) \
1446 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001447 M(VecAnd, VecBinaryOperation) \
1448 M(VecAndNot, VecBinaryOperation) \
1449 M(VecOr, VecBinaryOperation) \
1450 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001451 M(VecSaturationAdd, VecBinaryOperation) \
1452 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001453 M(VecShl, VecBinaryOperation) \
1454 M(VecShr, VecBinaryOperation) \
1455 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001456 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001457 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001458 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001459 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001460 M(VecLoad, VecMemoryOperation) \
1461 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001462
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001463/*
1464 * Instructions, shared across several (not all) architectures.
1465 */
1466#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1467#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1468#else
1469#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001470 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001471 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001472 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001473 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001474#endif
1475
Alexandre Ramesef20f712015-06-09 10:29:30 +01001476#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1477
1478#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1479
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001480#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001481#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001482#else
1483#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1484 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001485 M(MipsPackedSwitch, Instruction) \
1486 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001487#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001488
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001489#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1490
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001491#ifndef ART_ENABLE_CODEGEN_x86
1492#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1493#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001494#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1495 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001496 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001497 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001498 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001499#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001500
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301501#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1502#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
1503 M(X86AndNot, Instruction) \
1504 M(X86MaskOrResetLeastSetBit, Instruction)
1505#else
1506#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1507#endif
1508
Alexandre Ramesef20f712015-06-09 10:29:30 +01001509#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1510
1511#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1512 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001513 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001514 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1515 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001516 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001517 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001518 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301519 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1520 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001521
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001522#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1523 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001524 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001525 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001526 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001527 M(Invoke, Instruction) \
1528 M(VecOperation, Instruction) \
1529 M(VecUnaryOperation, VecOperation) \
1530 M(VecBinaryOperation, VecOperation) \
1531 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001532
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001533#define FOR_EACH_INSTRUCTION(M) \
1534 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1535 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1536
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001537#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001538FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1539#undef FORWARD_DECLARATION
1540
Vladimir Marko372f10e2016-05-17 16:30:10 +01001541#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001542 private: \
1543 H##type& operator=(const H##type&) = delete; \
1544 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001545 const char* DebugName() const override { return #type; } \
1546 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001547 DCHECK(IsClonable()); \
1548 return new (arena) H##type(*this->As##type()); \
1549 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001550 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001551
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001552#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001553 private: \
1554 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001555 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001556
Artem Serovcced8ba2017-07-19 18:18:09 +01001557#define DEFAULT_COPY_CONSTRUCTOR(type) \
1558 explicit H##type(const H##type& other) = default;
1559
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001560template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001561class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1562 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001563 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001564 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001565 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001566 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001567 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001568 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001569 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001570
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001571 private:
David Brazdiled596192015-01-23 10:39:45 +00001572 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001573 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001574
1575 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001576 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001577
Vladimir Marko46817b82016-03-29 12:21:58 +01001578 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001579
1580 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1581};
1582
David Brazdiled596192015-01-23 10:39:45 +00001583template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001584using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001585
David Brazdil1abb4192015-02-17 18:33:36 +00001586// This class is used by HEnvironment and HInstruction classes to record the
1587// instructions they use and pointers to the corresponding HUseListNodes kept
1588// by the used instructions.
1589template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001590class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001591 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001592 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1593 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001594
Vladimir Marko46817b82016-03-29 12:21:58 +01001595 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1596 : HUserRecord(old_record.instruction_, before_use_node) {}
1597 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1598 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001599 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001600 }
1601
1602 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001603 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1604 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001605
1606 private:
1607 // Instruction used by the user.
1608 HInstruction* instruction_;
1609
Vladimir Marko46817b82016-03-29 12:21:58 +01001610 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1611 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001612};
1613
Vladimir Markoe9004912016-06-16 16:50:52 +01001614// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1615// This is used for HInstruction::GetInputs() to return a container wrapper providing
1616// HInstruction* values even though the underlying container has HUserRecord<>s.
1617struct HInputExtractor {
1618 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1619 return record.GetInstruction();
1620 }
1621 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1622 return record.GetInstruction();
1623 }
1624};
1625
1626using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1627using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1628
Aart Bik854a02b2015-07-14 16:07:00 -07001629/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001631 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 * For write/read dependences on fields/arrays, the dependence analysis uses
1633 * type disambiguation (e.g. a float field write cannot modify the value of an
1634 * integer field read) and the access type (e.g. a reference array write cannot
1635 * modify the value of a reference field read [although it may modify the
1636 * reference fetch prior to reading the field, which is represented by its own
1637 * write/read dependence]). The analysis makes conservative points-to
1638 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1639 * the same, and any reference read depends on any reference read without
1640 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001641 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001642 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1643 * alive across the point where garbage collection might happen.
1644 *
1645 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1646 *
1647 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1648 * those instructions from the compiler perspective (between this instruction and the next one
1649 * in the IR).
1650 *
1651 * Note: Instructions which can cause GC only on a fatal slow path do not need
1652 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1653 * one. However the execution may return to compiled code if there is a catch block in the
1654 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1655 * set.
1656 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001657 * The internal representation uses 38-bit and is described in the table below.
1658 * The first line indicates the side effect, and for field/array accesses the
1659 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001660 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001661 * The two numbered lines below indicate the bit position in the bitfield (read
1662 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001663 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001664 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1665 * +-------------+---------+---------+--------------+---------+---------+
1666 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1667 * | 3 |333333322|222222221| 1 |111111110|000000000|
1668 * | 7 |654321098|765432109| 8 |765432109|876543210|
1669 *
1670 * Note that, to ease the implementation, 'changes' bits are least significant
1671 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001672 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001673class SideEffects : public ValueObject {
1674 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001675 SideEffects() : flags_(0) {}
1676
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001677 static SideEffects None() {
1678 return SideEffects(0);
1679 }
1680
1681 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001682 return SideEffects(kAllChangeBits | kAllDependOnBits);
1683 }
1684
1685 static SideEffects AllChanges() {
1686 return SideEffects(kAllChangeBits);
1687 }
1688
1689 static SideEffects AllDependencies() {
1690 return SideEffects(kAllDependOnBits);
1691 }
1692
1693 static SideEffects AllExceptGCDependency() {
1694 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1695 }
1696
1697 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001698 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001699 }
1700
Aart Bik34c3ba92015-07-20 14:08:59 -07001701 static SideEffects AllWrites() {
1702 return SideEffects(kAllWrites);
1703 }
1704
1705 static SideEffects AllReads() {
1706 return SideEffects(kAllReads);
1707 }
1708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001709 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001710 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001711 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001712 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001713 }
1714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001715 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001716 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001717 }
1718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001719 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001720 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001722 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001723 }
1724
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001725 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001726 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001727 }
1728
Artem Serovd1aa7d02018-06-22 11:35:46 +01001729 // Returns whether GC might happen across this instruction from the compiler perspective so
1730 // the next instruction in the IR would see that.
1731 //
1732 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001733 static SideEffects CanTriggerGC() {
1734 return SideEffects(1ULL << kCanTriggerGCBit);
1735 }
1736
Artem Serovd1aa7d02018-06-22 11:35:46 +01001737 // Returns whether the instruction must not be alive across a GC point.
1738 //
1739 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001740 static SideEffects DependsOnGC() {
1741 return SideEffects(1ULL << kDependsOnGCBit);
1742 }
1743
Aart Bik854a02b2015-07-14 16:07:00 -07001744 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001745 SideEffects Union(SideEffects other) const {
1746 return SideEffects(flags_ | other.flags_);
1747 }
1748
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001749 SideEffects Exclusion(SideEffects other) const {
1750 return SideEffects(flags_ & ~other.flags_);
1751 }
1752
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001753 void Add(SideEffects other) {
1754 flags_ |= other.flags_;
1755 }
1756
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001757 bool Includes(SideEffects other) const {
1758 return (other.flags_ & flags_) == other.flags_;
1759 }
1760
1761 bool HasSideEffects() const {
1762 return (flags_ & kAllChangeBits);
1763 }
1764
1765 bool HasDependencies() const {
1766 return (flags_ & kAllDependOnBits);
1767 }
1768
1769 // Returns true if there are no side effects or dependencies.
1770 bool DoesNothing() const {
1771 return flags_ == 0;
1772 }
1773
Aart Bik854a02b2015-07-14 16:07:00 -07001774 // Returns true if something is written.
1775 bool DoesAnyWrite() const {
1776 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001777 }
1778
Aart Bik854a02b2015-07-14 16:07:00 -07001779 // Returns true if something is read.
1780 bool DoesAnyRead() const {
1781 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001782 }
1783
Aart Bik854a02b2015-07-14 16:07:00 -07001784 // Returns true if potentially everything is written and read
1785 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001786 bool DoesAllReadWrite() const {
1787 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1788 }
1789
Aart Bik854a02b2015-07-14 16:07:00 -07001790 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001792 }
1793
Roland Levillain0d5a2812015-11-13 10:07:31 +00001794 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001795 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001796 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1797 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001798 }
1799
1800 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001801 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001802 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001803 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001804 for (int s = kLastBit; s >= 0; s--) {
1805 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1806 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1807 // This is a bit for the GC side effect.
1808 if (current_bit_is_set) {
1809 flags += "GC";
1810 }
Aart Bik854a02b2015-07-14 16:07:00 -07001811 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001812 } else {
1813 // This is a bit for the array/field analysis.
1814 // The underscore character stands for the 'can trigger GC' bit.
1815 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1816 if (current_bit_is_set) {
1817 flags += kDebug[s];
1818 }
1819 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1820 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1821 flags += "|";
1822 }
1823 }
Aart Bik854a02b2015-07-14 16:07:00 -07001824 }
1825 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001826 }
1827
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001828 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001829
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001830 private:
1831 static constexpr int kFieldArrayAnalysisBits = 9;
1832
1833 static constexpr int kFieldWriteOffset = 0;
1834 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1835 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1836 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1837
1838 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1839
1840 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1841 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1842 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1843 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1844
1845 static constexpr int kLastBit = kDependsOnGCBit;
1846 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1847
1848 // Aliases.
1849
1850 static_assert(kChangeBits == kDependOnBits,
1851 "the 'change' bits should match the 'depend on' bits.");
1852
1853 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1854 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1855 static constexpr uint64_t kAllWrites =
1856 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1857 static constexpr uint64_t kAllReads =
1858 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001859
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001860 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001861 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001862 int shift;
1863 switch (type) {
1864 case DataType::Type::kReference: shift = 0; break;
1865 case DataType::Type::kBool: shift = 1; break;
1866 case DataType::Type::kInt8: shift = 2; break;
1867 case DataType::Type::kUint16: shift = 3; break;
1868 case DataType::Type::kInt16: shift = 4; break;
1869 case DataType::Type::kInt32: shift = 5; break;
1870 case DataType::Type::kInt64: shift = 6; break;
1871 case DataType::Type::kFloat32: shift = 7; break;
1872 case DataType::Type::kFloat64: shift = 8; break;
1873 default:
1874 LOG(FATAL) << "Unexpected data type " << type;
1875 UNREACHABLE();
1876 }
Aart Bik854a02b2015-07-14 16:07:00 -07001877 DCHECK_LE(kFieldWriteOffset, shift);
1878 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001879 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001880 }
1881
1882 // Private constructor on direct flags value.
1883 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1884
1885 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001886};
1887
David Brazdiled596192015-01-23 10:39:45 +00001888// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001889class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001890 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001891 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001892 size_t number_of_vregs,
1893 ArtMethod* method,
1894 uint32_t dex_pc,
1895 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001896 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1897 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001898 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001899 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001900 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001901 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001902 }
1903
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001904 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1905 const HEnvironment& to_copy,
1906 HInstruction* holder)
1907 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001908 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001909 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001910 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001911 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001912
Vladimir Markoec32f642017-06-02 10:51:55 +01001913 void AllocateLocations() {
1914 DCHECK(locations_.empty());
1915 locations_.resize(vregs_.size());
1916 }
1917
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001919 if (parent_ != nullptr) {
1920 parent_->SetAndCopyParentChain(allocator, parent);
1921 } else {
1922 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1923 parent_->CopyFrom(parent);
1924 if (parent->GetParent() != nullptr) {
1925 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1926 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001927 }
David Brazdiled596192015-01-23 10:39:45 +00001928 }
1929
Vladimir Marko69d310e2017-10-09 14:12:23 +01001930 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001931 void CopyFrom(HEnvironment* environment);
1932
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001933 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1934 // input to the loop phi instead. This is for inserting instructions that
1935 // require an environment (like HDeoptimization) in the loop pre-header.
1936 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001937
1938 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001939 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001940 }
1941
1942 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001943 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001944 }
1945
David Brazdil1abb4192015-02-17 18:33:36 +00001946 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001947
Artem Serovca210e32017-12-15 13:43:20 +00001948 // Replaces the input at the position 'index' with the replacement; the replacement and old
1949 // input instructions' env_uses_ lists are adjusted. The function works similar to
1950 // HInstruction::ReplaceInput.
1951 void ReplaceInput(HInstruction* replacement, size_t index);
1952
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001953 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001954
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001955 HEnvironment* GetParent() const { return parent_; }
1956
1957 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001958 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001959 }
1960
1961 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001962 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001963 }
1964
1965 uint32_t GetDexPc() const {
1966 return dex_pc_;
1967 }
1968
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001969 ArtMethod* GetMethod() const {
1970 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001971 }
1972
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001973 HInstruction* GetHolder() const {
1974 return holder_;
1975 }
1976
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001977
1978 bool IsFromInlinedInvoke() const {
1979 return GetParent() != nullptr;
1980 }
1981
David Brazdiled596192015-01-23 10:39:45 +00001982 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001983 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1984 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001985 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001986 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001987 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001988
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001989 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001990 HInstruction* const holder_;
1991
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001992 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001993
1994 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1995};
1996
Vladimir Markof9f64412015-09-02 14:05:49 +01001997class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001998 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301999#define DECLARE_KIND(type, super) k##type,
2000 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002001 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302002 kLastInstructionKind
2003 };
2004#undef DECLARE_KIND
2005
2006 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002007 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2008
2009 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002010 : previous_(nullptr),
2011 next_(nullptr),
2012 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002013 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002014 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002015 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002016 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002018 locations_(nullptr),
2019 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002020 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002021 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002022 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302023 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002024 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002025 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2026 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002027
Dave Allison20dfc792014-06-16 20:44:29 -07002028 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002029
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002030
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002031 HInstruction* GetNext() const { return next_; }
2032 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002033
Calin Juravle77520bc2015-01-12 18:45:46 +00002034 HInstruction* GetNextDisregardingMoves() const;
2035 HInstruction* GetPreviousDisregardingMoves() const;
2036
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002037 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002038 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002039 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002040 bool IsInBlock() const { return block_ != nullptr; }
2041 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002042 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2043 bool IsIrreducibleLoopHeaderPhi() const {
2044 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2045 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002046
Vladimir Marko372f10e2016-05-17 16:30:10 +01002047 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2048
2049 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2050 // One virtual method is enough, just const_cast<> and then re-add the const.
2051 return ArrayRef<const HUserRecord<HInstruction*>>(
2052 const_cast<HInstruction*>(this)->GetInputRecords());
2053 }
2054
Vladimir Markoe9004912016-06-16 16:50:52 +01002055 HInputsRef GetInputs() {
2056 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002057 }
2058
Vladimir Markoe9004912016-06-16 16:50:52 +01002059 HConstInputsRef GetInputs() const {
2060 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002061 }
2062
2063 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002064 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002065
Aart Bik2767f4b2016-10-28 15:03:53 -07002066 bool HasInput(HInstruction* input) const {
2067 for (const HInstruction* i : GetInputs()) {
2068 if (i == input) {
2069 return true;
2070 }
2071 }
2072 return false;
2073 }
2074
Vladimir Marko372f10e2016-05-17 16:30:10 +01002075 void SetRawInputAt(size_t index, HInstruction* input) {
2076 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2077 }
2078
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002079 virtual void Accept(HGraphVisitor* visitor) = 0;
2080 virtual const char* DebugName() const = 0;
2081
Vladimir Markobd785672018-05-03 17:09:09 +01002082 DataType::Type GetType() const {
2083 return TypeField::Decode(GetPackedFields());
2084 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002085
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002086 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002087
2088 uint32_t GetDexPc() const { return dex_pc_; }
2089
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002090 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002091
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002092 // Can the instruction throw?
2093 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2094 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002095 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002096
2097 // Does the instruction always throw an exception unconditionally?
2098 virtual bool AlwaysThrows() const { return false; }
2099
David Brazdilec16f792015-08-19 15:04:01 +01002100 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002101
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002102 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002103 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104
Calin Juravle10e244f2015-01-26 18:54:32 +00002105 // Does not apply for all instructions, but having this at top level greatly
2106 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002107 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002108 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002110 return true;
2111 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002112
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002113 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002114 return false;
2115 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002116
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002117 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2118 // with it. Otherwise return null.
2119 HNullCheck* GetImplicitNullCheck() const {
2120 // Find the first previous instruction which is not a move.
2121 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2122 if (first_prev_not_move != nullptr &&
2123 first_prev_not_move->IsNullCheck() &&
2124 first_prev_not_move->IsEmittedAtUseSite()) {
2125 return first_prev_not_move->AsNullCheck();
2126 }
2127 return nullptr;
2128 }
2129
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002130 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002131 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002132 }
2133
Calin Juravle2e768302015-07-28 14:41:11 +00002134 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002135
Calin Juravle61d544b2015-02-23 16:46:57 +00002136 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002137 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002138 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002139 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002140 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002141
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002142 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002143 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002144 // Note: fixup_end remains valid across push_front().
2145 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2146 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002147 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002148 uses_.push_front(*new_node);
2149 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002150 }
2151
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002152 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002153 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002154 // Note: env_fixup_end remains valid across push_front().
2155 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2156 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002157 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002158 env_uses_.push_front(*new_node);
2159 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002160 }
2161
David Brazdil1abb4192015-02-17 18:33:36 +00002162 void RemoveAsUserOfInput(size_t input) {
2163 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002164 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2165 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2166 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002167 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168
Vladimir Marko372f10e2016-05-17 16:30:10 +01002169 void RemoveAsUserOfAllInputs() {
2170 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2171 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2172 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2173 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2174 }
2175 }
2176
David Brazdil1abb4192015-02-17 18:33:36 +00002177 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2178 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002179
Vladimir Marko46817b82016-03-29 12:21:58 +01002180 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2181 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2182 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002183 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002184 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002185 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002186
Aart Bikcc42be02016-10-20 16:14:16 -07002187 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002188 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002189 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002190 !CanThrow() &&
2191 !IsSuspendCheck() &&
2192 !IsControlFlow() &&
2193 !IsNativeDebugInfo() &&
2194 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002195 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002196 !IsMemoryBarrier() &&
2197 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002198 }
2199
Aart Bikcc42be02016-10-20 16:14:16 -07002200 bool IsDeadAndRemovable() const {
2201 return IsRemovable() && !HasUses();
2202 }
2203
Roland Levillain6c82d402014-10-13 16:10:27 +01002204 // Does this instruction strictly dominate `other_instruction`?
2205 // Returns false if this instruction and `other_instruction` are the same.
2206 // Aborts if this instruction and `other_instruction` are both phis.
2207 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002208
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002209 int GetId() const { return id_; }
2210 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002211
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002212 int GetSsaIndex() const { return ssa_index_; }
2213 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2214 bool HasSsaIndex() const { return ssa_index_ != -1; }
2215
2216 bool HasEnvironment() const { return environment_ != nullptr; }
2217 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002218 // Set the `environment_` field. Raw because this method does not
2219 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002220 void SetRawEnvironment(HEnvironment* environment) {
2221 DCHECK(environment_ == nullptr);
2222 DCHECK_EQ(environment->GetHolder(), this);
2223 environment_ = environment;
2224 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002225
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002226 void InsertRawEnvironment(HEnvironment* environment) {
2227 DCHECK(environment_ != nullptr);
2228 DCHECK_EQ(environment->GetHolder(), this);
2229 DCHECK(environment->GetParent() == nullptr);
2230 environment->parent_ = environment_;
2231 environment_ = environment;
2232 }
2233
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002234 void RemoveEnvironment();
2235
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002236 // Set the environment of this instruction, copying it from `environment`. While
2237 // copying, the uses lists are being updated.
2238 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002239 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002240 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002241 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002242 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002243 if (environment->GetParent() != nullptr) {
2244 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2245 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002246 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002247
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002248 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2249 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002250 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002251 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002252 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002253 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002254 if (environment->GetParent() != nullptr) {
2255 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2256 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002257 }
2258
Nicolas Geoffray39468442014-09-02 15:17:15 +01002259 // Returns the number of entries in the environment. Typically, that is the
2260 // number of dex registers in a method. It could be more in case of inlining.
2261 size_t EnvironmentSize() const;
2262
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002263 LocationSummary* GetLocations() const { return locations_; }
2264 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002265
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002266 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002267 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002268 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002269 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002270
Alexandre Rames188d4312015-04-09 18:30:21 +01002271 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2272 // uses of this instruction by `other` are *not* updated.
2273 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2274 ReplaceWith(other);
2275 other->ReplaceInput(this, use_index);
2276 }
2277
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002278 // Move `this` instruction before `cursor`
2279 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002280
Vladimir Markofb337ea2015-11-25 15:25:10 +00002281 // Move `this` before its first user and out of any loops. If there is no
2282 // out-of-loop user that dominates all other users, move the instruction
2283 // to the end of the out-of-loop common dominator of the user's blocks.
2284 //
2285 // This can be used only on non-throwing instructions with no side effects that
2286 // have at least one use but no environment uses.
2287 void MoveBeforeFirstUserAndOutOfLoops();
2288
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002289#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002290 bool Is##type() const;
2291
2292 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2293#undef INSTRUCTION_TYPE_CHECK
2294
2295#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002296 const H##type* As##type() const; \
2297 H##type* As##type();
2298
Vladimir Markoa90dd512018-05-04 15:04:45 +01002299 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2300#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002301
Artem Serovcced8ba2017-07-19 18:18:09 +01002302 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2303 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2304 // the instruction then the behaviour of this function is undefined.
2305 //
2306 // Note: It is semantically valid to create a clone of the instruction only until
2307 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2308 // copied.
2309 //
2310 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2311 // 'explicit HInstruction(const HInstruction& other)' for details.
2312 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2313 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2314 DebugName() << " " << GetId();
2315 UNREACHABLE();
2316 }
2317
2318 // Return whether instruction can be cloned (copied).
2319 virtual bool IsClonable() const { return false; }
2320
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002321 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002322 // TODO: this method is used by LICM and GVN with possibly different
2323 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002324 virtual bool CanBeMoved() const { return false; }
2325
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002326 // Returns whether any data encoded in the two instructions is equal.
2327 // This method does not look at the inputs. Both instructions must be
2328 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002329 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002330 return false;
2331 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002332
2333 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002334 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002335 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002336 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002337
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002338 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2339 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2340 // the virtual function because the __attribute__((__pure__)) doesn't really
2341 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302342 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002343
2344 virtual size_t ComputeHashCode() const {
2345 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002346 for (const HInstruction* input : GetInputs()) {
2347 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002348 }
2349 return result;
2350 }
2351
2352 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002353 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002354 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002355
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002356 size_t GetLifetimePosition() const { return lifetime_position_; }
2357 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2358 LiveInterval* GetLiveInterval() const { return live_interval_; }
2359 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2360 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2361
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002362 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2363
2364 // Returns whether the code generation of the instruction will require to have access
2365 // to the current method. Such instructions are:
2366 // (1): Instructions that require an environment, as calling the runtime requires
2367 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002368 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2369 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002370 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002371 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002372 }
2373
Vladimir Markodc151b22015-10-15 18:02:30 +01002374 // Returns whether the code generation of the instruction will require to have access
2375 // to the dex cache of the current method's declaring class via the current method.
2376 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002377
Mark Mendellc4701932015-04-10 13:18:51 -04002378 // Does this instruction have any use in an environment before
2379 // control flow hits 'other'?
2380 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2381
2382 // Remove all references to environment uses of this instruction.
2383 // The caller must ensure that this is safe to do.
2384 void RemoveEnvironmentUsers();
2385
Vladimir Markoa1de9182016-02-25 11:37:38 +00002386 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2387 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002388
David Brazdil1abb4192015-02-17 18:33:36 +00002389 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002390 // If set, the machine code for this instruction is assumed to be generated by
2391 // its users. Used by liveness analysis to compute use positions accordingly.
2392 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2393 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302394 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2395 static constexpr size_t kFieldInstructionKindSize =
2396 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002397 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302398 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002399 static constexpr size_t kFieldTypeSize =
2400 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2401 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002402 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2403
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302404 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2405 "Too many generic packed fields");
2406
Vladimir Markobd785672018-05-03 17:09:09 +01002407 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2408
Vladimir Marko372f10e2016-05-17 16:30:10 +01002409 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2410 return GetInputRecords()[i];
2411 }
2412
2413 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2414 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2415 input_records[index] = input;
2416 }
David Brazdil1abb4192015-02-17 18:33:36 +00002417
Vladimir Markoa1de9182016-02-25 11:37:38 +00002418 uint32_t GetPackedFields() const {
2419 return packed_fields_;
2420 }
2421
2422 template <size_t flag>
2423 bool GetPackedFlag() const {
2424 return (packed_fields_ & (1u << flag)) != 0u;
2425 }
2426
2427 template <size_t flag>
2428 void SetPackedFlag(bool value = true) {
2429 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2430 }
2431
2432 template <typename BitFieldType>
2433 typename BitFieldType::value_type GetPackedField() const {
2434 return BitFieldType::Decode(packed_fields_);
2435 }
2436
2437 template <typename BitFieldType>
2438 void SetPackedField(typename BitFieldType::value_type value) {
2439 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2440 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2441 }
2442
Artem Serovcced8ba2017-07-19 18:18:09 +01002443 // Copy construction for the instruction (used for Clone function).
2444 //
2445 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2446 // prepare_for_register_allocator are not copied (set to default values).
2447 //
2448 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2449 // fine for most of them. However for some of the instructions a custom copy constructor must be
2450 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2451 // for copying them).
2452 explicit HInstruction(const HInstruction& other)
2453 : previous_(nullptr),
2454 next_(nullptr),
2455 block_(nullptr),
2456 dex_pc_(other.dex_pc_),
2457 id_(-1),
2458 ssa_index_(-1),
2459 packed_fields_(other.packed_fields_),
2460 environment_(nullptr),
2461 locations_(nullptr),
2462 live_interval_(nullptr),
2463 lifetime_position_(kNoLifetime),
2464 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302465 reference_type_handle_(other.reference_type_handle_) {
2466 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002467
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302469 using InstructionKindField =
2470 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2471
Vladimir Marko46817b82016-03-29 12:21:58 +01002472 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2473 auto before_use_node = uses_.before_begin();
2474 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2475 HInstruction* user = use_node->GetUser();
2476 size_t input_index = use_node->GetIndex();
2477 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2478 before_use_node = use_node;
2479 }
2480 }
2481
2482 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2483 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2484 if (next != uses_.end()) {
2485 HInstruction* next_user = next->GetUser();
2486 size_t next_index = next->GetIndex();
2487 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2488 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2489 }
2490 }
2491
2492 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2493 auto before_env_use_node = env_uses_.before_begin();
2494 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2495 HEnvironment* user = env_use_node->GetUser();
2496 size_t input_index = env_use_node->GetIndex();
2497 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2498 before_env_use_node = env_use_node;
2499 }
2500 }
2501
2502 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2503 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2504 if (next != env_uses_.end()) {
2505 HEnvironment* next_user = next->GetUser();
2506 size_t next_index = next->GetIndex();
2507 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2508 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2509 }
2510 }
David Brazdil1abb4192015-02-17 18:33:36 +00002511
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512 HInstruction* previous_;
2513 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002514 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002515 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002516
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002517 // An instruction gets an id when it is added to the graph.
2518 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002519 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002520 int id_;
2521
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002522 // When doing liveness analysis, instructions that have uses get an SSA index.
2523 int ssa_index_;
2524
Vladimir Markoa1de9182016-02-25 11:37:38 +00002525 // Packed fields.
2526 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002527
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002528 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002529 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002530
2531 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002532 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002533
Nicolas Geoffray39468442014-09-02 15:17:15 +01002534 // The environment associated with this instruction. Not null if the instruction
2535 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002536 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002537
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002538 // Set by the code generator.
2539 LocationSummary* locations_;
2540
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002541 // Set by the liveness analysis.
2542 LiveInterval* live_interval_;
2543
2544 // Set by the liveness analysis, this is the position in a linear
2545 // order of blocks where this instruction's live interval start.
2546 size_t lifetime_position_;
2547
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002548 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002549
Vladimir Markoa1de9182016-02-25 11:37:38 +00002550 // The reference handle part of the reference type info.
2551 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002552 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002553 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002554
David Brazdil1abb4192015-02-17 18:33:36 +00002555 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002556 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002557 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002558 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002559 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002560};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002561std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002562
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002563// Iterates over the instructions, while preserving the next instruction
2564// in case the current instruction gets removed from the list by the user
2565// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002566class HInstructionIterator : public ValueObject {
2567 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002568 explicit HInstructionIterator(const HInstructionList& instructions)
2569 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002570 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002571 }
2572
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002573 bool Done() const { return instruction_ == nullptr; }
2574 HInstruction* Current() const { return instruction_; }
2575 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002576 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002577 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002578 }
2579
2580 private:
2581 HInstruction* instruction_;
2582 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002583
2584 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002585};
2586
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002587// Iterates over the instructions without saving the next instruction,
2588// therefore handling changes in the graph potentially made by the user
2589// of this iterator.
2590class HInstructionIteratorHandleChanges : public ValueObject {
2591 public:
2592 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2593 : instruction_(instructions.first_instruction_) {
2594 }
2595
2596 bool Done() const { return instruction_ == nullptr; }
2597 HInstruction* Current() const { return instruction_; }
2598 void Advance() {
2599 instruction_ = instruction_->GetNext();
2600 }
2601
2602 private:
2603 HInstruction* instruction_;
2604
2605 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2606};
2607
2608
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002609class HBackwardInstructionIterator : public ValueObject {
2610 public:
2611 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2612 : instruction_(instructions.last_instruction_) {
2613 next_ = Done() ? nullptr : instruction_->GetPrevious();
2614 }
2615
2616 bool Done() const { return instruction_ == nullptr; }
2617 HInstruction* Current() const { return instruction_; }
2618 void Advance() {
2619 instruction_ = next_;
2620 next_ = Done() ? nullptr : instruction_->GetPrevious();
2621 }
2622
2623 private:
2624 HInstruction* instruction_;
2625 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002626
2627 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002628};
2629
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002630class HVariableInputSizeInstruction : public HInstruction {
2631 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002632 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002633 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002634 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2635 }
2636
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002637 void AddInput(HInstruction* input);
2638 void InsertInputAt(size_t index, HInstruction* input);
2639 void RemoveInputAt(size_t index);
2640
Igor Murashkind01745e2017-04-05 16:40:31 -07002641 // Removes all the inputs.
2642 // Also removes this instructions from each input's use list
2643 // (for non-environment uses only).
2644 void RemoveAllInputs();
2645
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002646 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302647 HVariableInputSizeInstruction(InstructionKind inst_kind,
2648 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002649 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002650 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002651 size_t number_of_inputs,
2652 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302653 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002654 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002655 HVariableInputSizeInstruction(InstructionKind inst_kind,
2656 DataType::Type type,
2657 SideEffects side_effects,
2658 uint32_t dex_pc,
2659 ArenaAllocator* allocator,
2660 size_t number_of_inputs,
2661 ArenaAllocKind kind)
2662 : HInstruction(inst_kind, type, side_effects, dex_pc),
2663 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002664
Artem Serovcced8ba2017-07-19 18:18:09 +01002665 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002666
Artem Serovcced8ba2017-07-19 18:18:09 +01002667 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002668};
2669
Vladimir Markof9f64412015-09-02 14:05:49 +01002670template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002671class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002672 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002673 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302674 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002675 HExpression<N>(InstructionKind kind,
2676 DataType::Type type,
2677 SideEffects side_effects,
2678 uint32_t dex_pc)
2679 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2680 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002681
Vladimir Marko372f10e2016-05-17 16:30:10 +01002682 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002683 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002684 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002685 }
2686
Artem Serovcced8ba2017-07-19 18:18:09 +01002687 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002688 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002689
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002690 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002691 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002692
2693 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002694};
2695
Vladimir Markobd785672018-05-03 17:09:09 +01002696// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002697template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002698class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002699 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002700 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002701
Vladimir Markobd785672018-05-03 17:09:09 +01002702 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002703
Vladimir Marko372f10e2016-05-17 16:30:10 +01002704 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002705 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002706 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002707 }
2708
Artem Serovcced8ba2017-07-19 18:18:09 +01002709 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002710 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002711
Vladimir Markof9f64412015-09-02 14:05:49 +01002712 private:
2713 friend class SsaBuilder;
2714};
2715
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002716// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2717// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002718class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002719 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002721 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302722 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002724 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002725
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002726 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002727
Artem Serovcced8ba2017-07-19 18:18:09 +01002728 protected:
2729 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002730};
2731
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002732// Represents dex's RETURN opcodes. A HReturn is a control flow
2733// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002734class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002735 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002736 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002737 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002738 SetRawInputAt(0, value);
2739 }
2740
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002741 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002742
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002743 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002744
Artem Serovcced8ba2017-07-19 18:18:09 +01002745 protected:
2746 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002747};
2748
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002749class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002750 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002751 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002752 uint32_t reg_number,
2753 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002755 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002756 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302757 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002758 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002759 SideEffects::None(),
2760 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002761 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002762 number_of_inputs,
2763 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002764 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002765 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002766 // Phis are constructed live and marked dead if conflicting or unused.
2767 // Individual steps of SsaBuilder should assume that if a phi has been
2768 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2769 SetPackedFlag<kFlagIsLive>(true);
2770 SetPackedFlag<kFlagCanBeNull>(true);
2771 }
2772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002773 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002774
David Brazdildee58d62016-04-07 09:54:26 +00002775 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 static DataType::Type ToPhiType(DataType::Type type) {
2777 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002778 }
2779
2780 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002782 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002783 // Make sure that only valid type changes occur. The following are allowed:
2784 // (1) int -> float/ref (primitive type propagation),
2785 // (2) long -> double (primitive type propagation).
2786 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002787 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2788 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2789 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002790 SetPackedField<TypeField>(new_type);
2791 }
2792
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002793 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002794 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2795
2796 uint32_t GetRegNumber() const { return reg_number_; }
2797
2798 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2799 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2800 bool IsDead() const { return !IsLive(); }
2801 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2802
Vladimir Markoe9004912016-06-16 16:50:52 +01002803 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002804 return other != nullptr
2805 && other->IsPhi()
2806 && other->AsPhi()->GetBlock() == GetBlock()
2807 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2808 }
2809
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002810 bool HasEquivalentPhi() const {
2811 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2812 return true;
2813 }
2814 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2815 return true;
2816 }
2817 return false;
2818 }
2819
David Brazdildee58d62016-04-07 09:54:26 +00002820 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2821 // An equivalent phi is a phi having the same dex register and type.
2822 // It assumes that phis with the same dex register are adjacent.
2823 HPhi* GetNextEquivalentPhiWithSameType() {
2824 HInstruction* next = GetNext();
2825 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2826 if (next->GetType() == GetType()) {
2827 return next->AsPhi();
2828 }
2829 next = next->GetNext();
2830 }
2831 return nullptr;
2832 }
2833
2834 DECLARE_INSTRUCTION(Phi);
2835
Artem Serovcced8ba2017-07-19 18:18:09 +01002836 protected:
2837 DEFAULT_COPY_CONSTRUCTOR(Phi);
2838
David Brazdildee58d62016-04-07 09:54:26 +00002839 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002840 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002841 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2842 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2843 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002844
David Brazdildee58d62016-04-07 09:54:26 +00002845 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002846};
2847
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002848// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002849// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002850// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002851class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002852 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302853 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002854 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302855 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002856
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002857 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002858
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002859 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002860
Artem Serovcced8ba2017-07-19 18:18:09 +01002861 protected:
2862 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002863};
2864
2865// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002866class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002867 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302868 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002869 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302870 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002871
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002872 bool IsClonable() const override { return true; }
2873 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002874
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002875 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002876 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002877 }
2878
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002879 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002880
Artem Serovcced8ba2017-07-19 18:18:09 +01002881 protected:
2882 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002883};
2884
Roland Levillain9867bc72015-08-05 10:21:34 +01002885class HConstant : public HExpression<0> {
2886 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302887 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2888 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2889 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002890
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002891 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002892
Roland Levillain1a653882016-03-18 18:05:57 +00002893 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002894 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002895 // Is this constant 0 in the arithmetic sense?
2896 virtual bool IsArithmeticZero() const { return false; }
2897 // Is this constant a 0-bit pattern?
2898 virtual bool IsZeroBitPattern() const { return false; }
2899 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002900 virtual bool IsOne() const { return false; }
2901
David Brazdil77a48ae2015-09-15 12:34:04 +00002902 virtual uint64_t GetValueAsUint64() const = 0;
2903
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002904 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002905
Artem Serovcced8ba2017-07-19 18:18:09 +01002906 protected:
2907 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002908};
2909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002910class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002911 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002912 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 return true;
2914 }
2915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002916 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002917
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002918 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002919
Roland Levillain1a653882016-03-18 18:05:57 +00002920 // The null constant representation is a 0-bit pattern.
2921 virtual bool IsZeroBitPattern() const { return true; }
2922
Roland Levillain9867bc72015-08-05 10:21:34 +01002923 DECLARE_INSTRUCTION(NullConstant);
2924
Artem Serovcced8ba2017-07-19 18:18:09 +01002925 protected:
2926 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2927
Roland Levillain9867bc72015-08-05 10:21:34 +01002928 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302930 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2931 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002932
2933 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002934};
2935
2936// Constants of the type int. Those can be from Dex instructions, or
2937// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002938class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002939 public:
2940 int32_t GetValue() const { return value_; }
2941
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002942 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002943 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2944 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002945
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002946 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002947 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002948 return other->AsIntConstant()->value_ == value_;
2949 }
2950
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002951 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002953 bool IsMinusOne() const override { return GetValue() == -1; }
2954 bool IsArithmeticZero() const override { return GetValue() == 0; }
2955 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2956 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002957
Roland Levillain1a653882016-03-18 18:05:57 +00002958 // Integer constants are used to encode Boolean values as well,
2959 // where 1 means true and 0 means false.
2960 bool IsTrue() const { return GetValue() == 1; }
2961 bool IsFalse() const { return GetValue() == 0; }
2962
Roland Levillain9867bc72015-08-05 10:21:34 +01002963 DECLARE_INSTRUCTION(IntConstant);
2964
Artem Serovcced8ba2017-07-19 18:18:09 +01002965 protected:
2966 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2967
Roland Levillain9867bc72015-08-05 10:21:34 +01002968 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002969 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302970 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2971 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002972 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302973 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2974 value_(value ? 1 : 0) {
2975 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002976
2977 const int32_t value_;
2978
2979 friend class HGraph;
2980 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2981 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002982};
2983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002984class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002985 public:
2986 int64_t GetValue() const { return value_; }
2987
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002988 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002990 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002991 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002992 return other->AsLongConstant()->value_ == value_;
2993 }
2994
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002995 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002996
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002997 bool IsMinusOne() const override { return GetValue() == -1; }
2998 bool IsArithmeticZero() const override { return GetValue() == 0; }
2999 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3000 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003001
3002 DECLARE_INSTRUCTION(LongConstant);
3003
Artem Serovcced8ba2017-07-19 18:18:09 +01003004 protected:
3005 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3006
Roland Levillain9867bc72015-08-05 10:21:34 +01003007 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003008 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303009 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3010 value_(value) {
3011 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003012
3013 const int64_t value_;
3014
3015 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003016};
Dave Allison20dfc792014-06-16 20:44:29 -07003017
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003018class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019 public:
3020 float GetValue() const { return value_; }
3021
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003022 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3024 }
3025
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003026 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 DCHECK(other->IsFloatConstant()) << other->DebugName();
3028 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3029 }
3030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003031 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003033 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003034 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3035 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003036 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003037 return std::fpclassify(value_) == FP_ZERO;
3038 }
3039 bool IsArithmeticPositiveZero() const {
3040 return IsArithmeticZero() && !std::signbit(value_);
3041 }
3042 bool IsArithmeticNegativeZero() const {
3043 return IsArithmeticZero() && std::signbit(value_);
3044 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003045 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003046 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003047 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003048 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003049 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3050 }
3051 bool IsNaN() const {
3052 return std::isnan(value_);
3053 }
3054
3055 DECLARE_INSTRUCTION(FloatConstant);
3056
Artem Serovcced8ba2017-07-19 18:18:09 +01003057 protected:
3058 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3059
Roland Levillain31dd3d62016-02-16 12:21:02 +00003060 private:
3061 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303062 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3063 value_(value) {
3064 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303066 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3067 value_(bit_cast<float, int32_t>(value)) {
3068 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069
3070 const float value_;
3071
3072 // Only the SsaBuilder and HGraph can create floating-point constants.
3073 friend class SsaBuilder;
3074 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003075};
3076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003077class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078 public:
3079 double GetValue() const { return value_; }
3080
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003081 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003083 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003084 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3085 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3086 }
3087
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003088 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003089
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003090 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003091 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3092 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003093 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003094 return std::fpclassify(value_) == FP_ZERO;
3095 }
3096 bool IsArithmeticPositiveZero() const {
3097 return IsArithmeticZero() && !std::signbit(value_);
3098 }
3099 bool IsArithmeticNegativeZero() const {
3100 return IsArithmeticZero() && std::signbit(value_);
3101 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003102 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003103 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003105 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003106 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3107 }
3108 bool IsNaN() const {
3109 return std::isnan(value_);
3110 }
3111
3112 DECLARE_INSTRUCTION(DoubleConstant);
3113
Artem Serovcced8ba2017-07-19 18:18:09 +01003114 protected:
3115 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3116
Roland Levillain31dd3d62016-02-16 12:21:02 +00003117 private:
3118 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303119 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3120 value_(value) {
3121 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003122 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303123 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3124 value_(bit_cast<double, int64_t>(value)) {
3125 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003126
3127 const double value_;
3128
3129 // Only the SsaBuilder and HGraph can create floating-point constants.
3130 friend class SsaBuilder;
3131 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003132};
3133
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003134// Conditional branch. A block ending with an HIf instruction must have
3135// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003136class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003137 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003138 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003139 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003140 SetRawInputAt(0, input);
3141 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003143 bool IsClonable() const override { return true; }
3144 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003145
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003146 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003147 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003148 }
3149
3150 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003151 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003152 }
3153
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003154 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003155
Artem Serovcced8ba2017-07-19 18:18:09 +01003156 protected:
3157 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003158};
3159
David Brazdilfc6a86a2015-06-26 10:33:45 +00003160
3161// Abstract instruction which marks the beginning and/or end of a try block and
3162// links it to the respective exception handlers. Behaves the same as a Goto in
3163// non-exceptional control flow.
3164// Normal-flow successor is stored at index zero, exception handlers under
3165// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003166class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003167 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003168 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003169 kEntry,
3170 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003171 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003172 };
3173
Artem Serovd1aa7d02018-06-22 11:35:46 +01003174 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3175 // across the catch block entering edges as GC might happen during throwing an exception.
3176 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3177 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003178 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003179 : HExpression(kTryBoundary,
3180 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3181 : SideEffects::None(),
3182 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003183 SetPackedField<BoundaryKindField>(kind);
3184 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003185
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003186 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003187
3188 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003189 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003190
David Brazdild26a4112015-11-10 11:07:31 +00003191 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3192 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3193 }
3194
David Brazdilfc6a86a2015-06-26 10:33:45 +00003195 // Returns whether `handler` is among its exception handlers (non-zero index
3196 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003197 bool HasExceptionHandler(const HBasicBlock& handler) const {
3198 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003199 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003200 }
3201
3202 // If not present already, adds `handler` to its block's list of exception
3203 // handlers.
3204 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003205 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003206 GetBlock()->AddSuccessor(handler);
3207 }
3208 }
3209
Vladimir Markoa1de9182016-02-25 11:37:38 +00003210 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3211 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003212
David Brazdilffee3d32015-07-06 11:48:53 +01003213 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3214
David Brazdilfc6a86a2015-06-26 10:33:45 +00003215 DECLARE_INSTRUCTION(TryBoundary);
3216
Artem Serovcced8ba2017-07-19 18:18:09 +01003217 protected:
3218 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3219
David Brazdilfc6a86a2015-06-26 10:33:45 +00003220 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003221 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3222 static constexpr size_t kFieldBoundaryKindSize =
3223 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3224 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3225 kFieldBoundaryKind + kFieldBoundaryKindSize;
3226 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3227 "Too many packed fields.");
3228 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003229};
3230
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003231// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003232class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003233 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003234 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3235 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003236 HDeoptimize(ArenaAllocator* allocator,
3237 HInstruction* cond,
3238 DeoptimizationKind kind,
3239 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003240 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303241 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003242 SideEffects::All(),
3243 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003244 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003245 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003246 kArenaAllocMisc) {
3247 SetPackedFlag<kFieldCanBeMoved>(false);
3248 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003249 SetRawInputAt(0, cond);
3250 }
3251
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003252 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003253
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003254 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3255 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3256 // instead of `guard`.
3257 // We set CanTriggerGC to prevent any intermediate address to be live
3258 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003259 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003260 HInstruction* cond,
3261 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003262 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003263 uint32_t dex_pc)
3264 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303265 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003266 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003267 SideEffects::CanTriggerGC(),
3268 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003269 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003270 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003271 kArenaAllocMisc) {
3272 SetPackedFlag<kFieldCanBeMoved>(true);
3273 SetPackedField<DeoptimizeKindField>(kind);
3274 SetRawInputAt(0, cond);
3275 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003276 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003278 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003280 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003281 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3282 }
3283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003284 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003285
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003286 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003287
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003288 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003289
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003290 bool GuardsAnInput() const {
3291 return InputCount() == 2;
3292 }
3293
3294 HInstruction* GuardedInput() const {
3295 DCHECK(GuardsAnInput());
3296 return InputAt(1);
3297 }
3298
3299 void RemoveGuard() {
3300 RemoveInputAt(1);
3301 }
3302
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003303 DECLARE_INSTRUCTION(Deoptimize);
3304
Artem Serovcced8ba2017-07-19 18:18:09 +01003305 protected:
3306 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3307
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003308 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003309 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3310 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3311 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003312 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003313 static constexpr size_t kNumberOfDeoptimizePackedBits =
3314 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3315 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3316 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003317 using DeoptimizeKindField =
3318 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003319};
3320
Mingyao Yang063fc772016-08-02 11:02:54 -07003321// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3322// The compiled code checks this flag value in a guard before devirtualized call and
3323// if it's true, starts to do deoptimization.
3324// It has a 4-byte slot on stack.
3325// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003326class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003327 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003328 // CHA guards are only optimized in a separate pass and it has no side effects
3329 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003330 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303331 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003332 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303333 SideEffects::None(),
3334 dex_pc,
3335 allocator,
3336 0,
3337 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003338 }
3339
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003340 // We do all CHA guard elimination/motion in a single pass, after which there is no
3341 // further guard elimination/motion since a guard might have been used for justification
3342 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3343 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003344 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003345
3346 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3347
Artem Serovcced8ba2017-07-19 18:18:09 +01003348 protected:
3349 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003350};
3351
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003352// Represents the ArtMethod that was passed as a first argument to
3353// the method. It is used by instructions that depend on it, like
3354// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003355class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003356 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003357 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303358 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3359 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003360
3361 DECLARE_INSTRUCTION(CurrentMethod);
3362
Artem Serovcced8ba2017-07-19 18:18:09 +01003363 protected:
3364 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003365};
3366
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003367// Fetches an ArtMethod from the virtual table or the interface method table
3368// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003369class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003370 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003371 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003372 kVTable,
3373 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003374 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003375 };
3376 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003377 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003378 TableKind kind,
3379 size_t index,
3380 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303381 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003382 index_(index) {
3383 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003384 SetRawInputAt(0, cls);
3385 }
3386
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003387 bool IsClonable() const override { return true; }
3388 bool CanBeMoved() const override { return true; }
3389 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003390 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003391 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003392 }
3393
Vladimir Markoa1de9182016-02-25 11:37:38 +00003394 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003395 size_t GetIndex() const { return index_; }
3396
3397 DECLARE_INSTRUCTION(ClassTableGet);
3398
Artem Serovcced8ba2017-07-19 18:18:09 +01003399 protected:
3400 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3401
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003402 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003403 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003404 static constexpr size_t kFieldTableKindSize =
3405 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3406 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3407 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3408 "Too many packed fields.");
3409 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3410
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003411 // The index of the ArtMethod in the table.
3412 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003413};
3414
Mark Mendellfe57faa2015-09-18 09:26:15 -04003415// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3416// have one successor for each entry in the switch table, and the final successor
3417// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003418class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003419 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003420 HPackedSwitch(int32_t start_value,
3421 uint32_t num_entries,
3422 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003423 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003424 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003425 start_value_(start_value),
3426 num_entries_(num_entries) {
3427 SetRawInputAt(0, input);
3428 }
3429
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003430 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003431
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003432 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003433
3434 int32_t GetStartValue() const { return start_value_; }
3435
Vladimir Marko211c2112015-09-24 16:52:33 +01003436 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003437
3438 HBasicBlock* GetDefaultBlock() const {
3439 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003440 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003441 }
3442 DECLARE_INSTRUCTION(PackedSwitch);
3443
Artem Serovcced8ba2017-07-19 18:18:09 +01003444 protected:
3445 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3446
Mark Mendellfe57faa2015-09-18 09:26:15 -04003447 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003448 const int32_t start_value_;
3449 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003450};
3451
Roland Levillain88cb1752014-10-20 16:36:47 +01003452class HUnaryOperation : public HExpression<1> {
3453 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303454 HUnaryOperation(InstructionKind kind,
3455 DataType::Type result_type,
3456 HInstruction* input,
3457 uint32_t dex_pc = kNoDexPc)
3458 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003459 SetRawInputAt(0, input);
3460 }
3461
Artem Serovcced8ba2017-07-19 18:18:09 +01003462 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003463 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003464
Roland Levillain88cb1752014-10-20 16:36:47 +01003465 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003466 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003467
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003468 bool CanBeMoved() const override { return true; }
3469 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003470 return true;
3471 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003472
Roland Levillain31dd3d62016-02-16 12:21:02 +00003473 // Try to statically evaluate `this` and return a HConstant
3474 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003475 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003476 HConstant* TryStaticEvaluation() const;
3477
3478 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003479 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3480 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003481 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3482 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003483
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003484 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003485
Artem Serovcced8ba2017-07-19 18:18:09 +01003486 protected:
3487 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003488};
3489
Dave Allison20dfc792014-06-16 20:44:29 -07003490class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003491 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303492 HBinaryOperation(InstructionKind kind,
3493 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003494 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003495 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003496 SideEffects side_effects = SideEffects::None(),
3497 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303498 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003499 SetRawInputAt(0, left);
3500 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003501 }
3502
Artem Serovcced8ba2017-07-19 18:18:09 +01003503 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003504 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003505
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003506 HInstruction* GetLeft() const { return InputAt(0); }
3507 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003508 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003509
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003510 virtual bool IsCommutative() const { return false; }
3511
3512 // Put constant on the right.
3513 // Returns whether order is changed.
3514 bool OrderInputsWithConstantOnTheRight() {
3515 HInstruction* left = InputAt(0);
3516 HInstruction* right = InputAt(1);
3517 if (left->IsConstant() && !right->IsConstant()) {
3518 ReplaceInput(right, 0);
3519 ReplaceInput(left, 1);
3520 return true;
3521 }
3522 return false;
3523 }
3524
3525 // Order inputs by instruction id, but favor constant on the right side.
3526 // This helps GVN for commutative ops.
3527 void OrderInputs() {
3528 DCHECK(IsCommutative());
3529 HInstruction* left = InputAt(0);
3530 HInstruction* right = InputAt(1);
3531 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3532 return;
3533 }
3534 if (OrderInputsWithConstantOnTheRight()) {
3535 return;
3536 }
3537 // Order according to instruction id.
3538 if (left->GetId() > right->GetId()) {
3539 ReplaceInput(right, 0);
3540 ReplaceInput(left, 1);
3541 }
3542 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003543
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003544 bool CanBeMoved() const override { return true; }
3545 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003546 return true;
3547 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003548
Roland Levillain31dd3d62016-02-16 12:21:02 +00003549 // Try to statically evaluate `this` and return a HConstant
3550 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003551 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003552 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003553
3554 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003555 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3556 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003557 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3558 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003559 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003560 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3561 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003562 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3563 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003564 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3565 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003566 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003567 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3568 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003569
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003570 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003571 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003572 HConstant* GetConstantRight() const;
3573
3574 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003575 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003576 HInstruction* GetLeastConstantLeft() const;
3577
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003578 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003579
Artem Serovcced8ba2017-07-19 18:18:09 +01003580 protected:
3581 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003582};
3583
Mark Mendellc4701932015-04-10 13:18:51 -04003584// The comparison bias applies for floating point operations and indicates how NaN
3585// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003586enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003587 kNoBias, // bias is not applicable (i.e. for long operation)
3588 kGtBias, // return 1 for NaN comparisons
3589 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003590 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003591};
3592
Roland Levillain31dd3d62016-02-16 12:21:02 +00003593std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3594
Dave Allison20dfc792014-06-16 20:44:29 -07003595class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003596 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303597 HCondition(InstructionKind kind,
3598 HInstruction* first,
3599 HInstruction* second,
3600 uint32_t dex_pc = kNoDexPc)
3601 : HBinaryOperation(kind,
3602 DataType::Type::kBool,
3603 first,
3604 second,
3605 SideEffects::None(),
3606 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003607 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3608 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003609
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003610 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003611 // `instruction`, and disregard moves in between.
3612 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003613
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003614 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003615
3616 virtual IfCondition GetCondition() const = 0;
3617
Mark Mendellc4701932015-04-10 13:18:51 -04003618 virtual IfCondition GetOppositeCondition() const = 0;
3619
Vladimir Markoa1de9182016-02-25 11:37:38 +00003620 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003621 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3622
Vladimir Markoa1de9182016-02-25 11:37:38 +00003623 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3624 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003625
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003626 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003627 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003628 }
3629
Roland Levillain4fa13f62015-07-06 18:11:54 +01003630 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003632 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003633 if (if_cond == kCondNE) {
3634 return true;
3635 } else if (if_cond == kCondEQ) {
3636 return false;
3637 }
3638 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003639 }
3640
3641 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003642 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003643 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003644 if (if_cond == kCondEQ) {
3645 return true;
3646 } else if (if_cond == kCondNE) {
3647 return false;
3648 }
3649 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003650 }
3651
Roland Levillain31dd3d62016-02-16 12:21:02 +00003652 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003653 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003654 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003655 static constexpr size_t kFieldComparisonBiasSize =
3656 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3657 static constexpr size_t kNumberOfConditionPackedBits =
3658 kFieldComparisonBias + kFieldComparisonBiasSize;
3659 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3660 using ComparisonBiasField =
3661 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3662
Roland Levillain31dd3d62016-02-16 12:21:02 +00003663 template <typename T>
3664 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3665
3666 template <typename T>
3667 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003668 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003669 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3670 // Handle the bias.
3671 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3672 }
3673
3674 // Return an integer constant containing the result of a condition evaluated at compile time.
3675 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3676 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3677 }
3678
Artem Serovcced8ba2017-07-19 18:18:09 +01003679 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003680};
3681
3682// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003683class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003684 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003685 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303686 : HCondition(kEqual, first, second, dex_pc) {
3687 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003688
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003689 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003690
Vladimir Marko9e23df52015-11-10 17:14:35 +00003691 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003692 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003693 return MakeConstantCondition(true, GetDexPc());
3694 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003695 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003696 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3697 }
3698 // In the following Evaluate methods, a HCompare instruction has
3699 // been merged into this HEqual instruction; evaluate it as
3700 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003701 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003702 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3703 GetDexPc());
3704 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003705 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003706 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3707 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003708 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003709 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003710 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003711
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003712 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003713
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003714 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003715 return kCondEQ;
3716 }
3717
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003718 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003719 return kCondNE;
3720 }
3721
Artem Serovcced8ba2017-07-19 18:18:09 +01003722 protected:
3723 DEFAULT_COPY_CONSTRUCTOR(Equal);
3724
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003725 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003726 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003727};
3728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003729class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003730 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303731 HNotEqual(HInstruction* first, HInstruction* second,
3732 uint32_t dex_pc = kNoDexPc)
3733 : HCondition(kNotEqual, first, second, dex_pc) {
3734 }
Dave Allison20dfc792014-06-16 20:44:29 -07003735
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003736 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003737
Vladimir Marko9e23df52015-11-10 17:14:35 +00003738 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003739 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 return MakeConstantCondition(false, GetDexPc());
3741 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003742 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3744 }
3745 // In the following Evaluate methods, a HCompare instruction has
3746 // been merged into this HNotEqual instruction; evaluate it as
3747 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003748 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003749 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3750 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003751 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003752 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3753 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003754 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003755 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003756 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003757
Dave Allison20dfc792014-06-16 20:44:29 -07003758 DECLARE_INSTRUCTION(NotEqual);
3759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003760 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003761 return kCondNE;
3762 }
3763
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003764 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003765 return kCondEQ;
3766 }
3767
Artem Serovcced8ba2017-07-19 18:18:09 +01003768 protected:
3769 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3770
Dave Allison20dfc792014-06-16 20:44:29 -07003771 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003772 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003773};
3774
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003775class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003776 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303777 HLessThan(HInstruction* first, HInstruction* second,
3778 uint32_t dex_pc = kNoDexPc)
3779 : HCondition(kLessThan, first, second, dex_pc) {
3780 }
Dave Allison20dfc792014-06-16 20:44:29 -07003781
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003782 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003783 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003784 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003785 // In the following Evaluate methods, a HCompare instruction has
3786 // been merged into this HLessThan instruction; evaluate it as
3787 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003788 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003789 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3790 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003792 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3793 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003794 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003795 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003796 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003797
Dave Allison20dfc792014-06-16 20:44:29 -07003798 DECLARE_INSTRUCTION(LessThan);
3799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003800 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003801 return kCondLT;
3802 }
3803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003804 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003805 return kCondGE;
3806 }
3807
Artem Serovcced8ba2017-07-19 18:18:09 +01003808 protected:
3809 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3810
Dave Allison20dfc792014-06-16 20:44:29 -07003811 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003812 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003813};
3814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003815class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003816 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303817 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3818 uint32_t dex_pc = kNoDexPc)
3819 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3820 }
Dave Allison20dfc792014-06-16 20:44:29 -07003821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003822 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003823 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003824 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003825 // In the following Evaluate methods, a HCompare instruction has
3826 // been merged into this HLessThanOrEqual instruction; evaluate it as
3827 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003828 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003829 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3830 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003831 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003832 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3833 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003834 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003835 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003836 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003837
Dave Allison20dfc792014-06-16 20:44:29 -07003838 DECLARE_INSTRUCTION(LessThanOrEqual);
3839
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003840 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003841 return kCondLE;
3842 }
3843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003844 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003845 return kCondGT;
3846 }
3847
Artem Serovcced8ba2017-07-19 18:18:09 +01003848 protected:
3849 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3850
Dave Allison20dfc792014-06-16 20:44:29 -07003851 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003852 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003853};
3854
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003855class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003856 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003857 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303858 : HCondition(kGreaterThan, first, second, dex_pc) {
3859 }
Dave Allison20dfc792014-06-16 20:44:29 -07003860
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003861 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003862 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003863 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003864 // In the following Evaluate methods, a HCompare instruction has
3865 // been merged into this HGreaterThan instruction; evaluate it as
3866 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003867 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003868 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3869 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003870 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003871 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3872 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003873 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003874 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003875 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003876
Dave Allison20dfc792014-06-16 20:44:29 -07003877 DECLARE_INSTRUCTION(GreaterThan);
3878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003879 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003880 return kCondGT;
3881 }
3882
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003883 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003884 return kCondLE;
3885 }
3886
Artem Serovcced8ba2017-07-19 18:18:09 +01003887 protected:
3888 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3889
Dave Allison20dfc792014-06-16 20:44:29 -07003890 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003891 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003892};
3893
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003894class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003895 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003896 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303897 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3898 }
Dave Allison20dfc792014-06-16 20:44:29 -07003899
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003900 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003901 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003902 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003903 // In the following Evaluate methods, a HCompare instruction has
3904 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3905 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003906 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003907 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3908 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003909 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003910 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3911 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003912 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003913 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003914 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003915
Dave Allison20dfc792014-06-16 20:44:29 -07003916 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3917
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003918 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003919 return kCondGE;
3920 }
3921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003922 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003923 return kCondLT;
3924 }
3925
Artem Serovcced8ba2017-07-19 18:18:09 +01003926 protected:
3927 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3928
Dave Allison20dfc792014-06-16 20:44:29 -07003929 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003930 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003931};
3932
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003933class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003934 public:
3935 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303936 : HCondition(kBelow, first, second, dex_pc) {
3937 }
Aart Bike9f37602015-10-09 11:15:55 -07003938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003939 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003940 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003941 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003943 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3944 }
3945 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003946 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003947 LOG(FATAL) << DebugName() << " is not defined for float values";
3948 UNREACHABLE();
3949 }
3950 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003951 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003952 LOG(FATAL) << DebugName() << " is not defined for double values";
3953 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003954 }
3955
3956 DECLARE_INSTRUCTION(Below);
3957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003958 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003959 return kCondB;
3960 }
3961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003962 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003963 return kCondAE;
3964 }
3965
Artem Serovcced8ba2017-07-19 18:18:09 +01003966 protected:
3967 DEFAULT_COPY_CONSTRUCTOR(Below);
3968
Aart Bike9f37602015-10-09 11:15:55 -07003969 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003970 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003971 return MakeUnsigned(x) < MakeUnsigned(y);
3972 }
Aart Bike9f37602015-10-09 11:15:55 -07003973};
3974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003975class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003976 public:
3977 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303978 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3979 }
Aart Bike9f37602015-10-09 11:15:55 -07003980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003981 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003982 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003983 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003984 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003985 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3986 }
3987 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003988 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003989 LOG(FATAL) << DebugName() << " is not defined for float values";
3990 UNREACHABLE();
3991 }
3992 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003993 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003994 LOG(FATAL) << DebugName() << " is not defined for double values";
3995 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003996 }
3997
3998 DECLARE_INSTRUCTION(BelowOrEqual);
3999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004000 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004001 return kCondBE;
4002 }
4003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004004 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004005 return kCondA;
4006 }
4007
Artem Serovcced8ba2017-07-19 18:18:09 +01004008 protected:
4009 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4010
Aart Bike9f37602015-10-09 11:15:55 -07004011 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004012 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004013 return MakeUnsigned(x) <= MakeUnsigned(y);
4014 }
Aart Bike9f37602015-10-09 11:15:55 -07004015};
4016
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004017class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004018 public:
4019 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304020 : HCondition(kAbove, first, second, dex_pc) {
4021 }
Aart Bike9f37602015-10-09 11:15:55 -07004022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004023 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004024 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004025 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004026 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004027 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4028 }
4029 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004030 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004031 LOG(FATAL) << DebugName() << " is not defined for float values";
4032 UNREACHABLE();
4033 }
4034 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004035 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004036 LOG(FATAL) << DebugName() << " is not defined for double values";
4037 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004038 }
4039
4040 DECLARE_INSTRUCTION(Above);
4041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004042 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004043 return kCondA;
4044 }
4045
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004046 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004047 return kCondBE;
4048 }
4049
Artem Serovcced8ba2017-07-19 18:18:09 +01004050 protected:
4051 DEFAULT_COPY_CONSTRUCTOR(Above);
4052
Aart Bike9f37602015-10-09 11:15:55 -07004053 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004054 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004055 return MakeUnsigned(x) > MakeUnsigned(y);
4056 }
Aart Bike9f37602015-10-09 11:15:55 -07004057};
4058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004059class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004060 public:
4061 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304062 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4063 }
Aart Bike9f37602015-10-09 11:15:55 -07004064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004065 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004066 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004067 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004068 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004069 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4070 }
4071 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004072 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004073 LOG(FATAL) << DebugName() << " is not defined for float values";
4074 UNREACHABLE();
4075 }
4076 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004077 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004078 LOG(FATAL) << DebugName() << " is not defined for double values";
4079 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004080 }
4081
4082 DECLARE_INSTRUCTION(AboveOrEqual);
4083
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004084 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004085 return kCondAE;
4086 }
4087
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004088 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004089 return kCondB;
4090 }
4091
Artem Serovcced8ba2017-07-19 18:18:09 +01004092 protected:
4093 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4094
Aart Bike9f37602015-10-09 11:15:55 -07004095 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004096 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004097 return MakeUnsigned(x) >= MakeUnsigned(y);
4098 }
Aart Bike9f37602015-10-09 11:15:55 -07004099};
Dave Allison20dfc792014-06-16 20:44:29 -07004100
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004101// Instruction to check how two inputs compare to each other.
4102// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004103class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004104 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004105 // Note that `comparison_type` is the type of comparison performed
4106 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004107 // HCompare instruction (which is always DataType::Type::kInt).
4108 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004109 HInstruction* first,
4110 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004111 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004112 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304113 : HBinaryOperation(kCompare,
4114 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 first,
4116 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004117 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004118 dex_pc) {
4119 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004120 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4121 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004122 }
4123
Roland Levillain9867bc72015-08-05 10:21:34 +01004124 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004125 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4126
4127 template <typename T>
4128 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004129 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004130 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4131 // Handle the bias.
4132 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4133 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004135 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004136 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4137 // reach this code path when processing a freshly built HIR
4138 // graph. However HCompare integer instructions can be synthesized
4139 // by the instruction simplifier to implement IntegerCompare and
4140 // IntegerSignum intrinsics, so we have to handle this case.
4141 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004142 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004143 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004144 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4145 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004146 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004147 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4148 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004149 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004150 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004151 }
4152
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004153 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004154 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004155 }
4156
Vladimir Markoa1de9182016-02-25 11:37:38 +00004157 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004158
Roland Levillain31dd3d62016-02-16 12:21:02 +00004159 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004160 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004161 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004162 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004163 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004164 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004166 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004167 // Comparisons do not require a runtime call in any back end.
4168 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004169 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004170
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004171 DECLARE_INSTRUCTION(Compare);
4172
Roland Levillain31dd3d62016-02-16 12:21:02 +00004173 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004174 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004175 static constexpr size_t kFieldComparisonBiasSize =
4176 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4177 static constexpr size_t kNumberOfComparePackedBits =
4178 kFieldComparisonBias + kFieldComparisonBiasSize;
4179 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4180 using ComparisonBiasField =
4181 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4182
Roland Levillain31dd3d62016-02-16 12:21:02 +00004183 // Return an integer constant containing the result of a comparison evaluated at compile time.
4184 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4185 DCHECK(value == -1 || value == 0 || value == 1) << value;
4186 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4187 }
4188
Artem Serovcced8ba2017-07-19 18:18:09 +01004189 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004190};
4191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004192class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004193 public:
4194 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004195 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004196 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004197 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004198 bool finalizable,
4199 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304200 : HExpression(kNewInstance,
4201 DataType::Type::kReference,
4202 SideEffects::CanTriggerGC(),
4203 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004204 type_index_(type_index),
4205 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004206 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004207 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004208 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004209 }
4210
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004211 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004212
Andreas Gampea5b09a62016-11-17 15:21:22 -08004213 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004214 const DexFile& GetDexFile() const { return dex_file_; }
4215
4216 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004217 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004218
Mingyao Yang062157f2016-03-02 10:15:36 -08004219 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004220 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004221
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004222 bool NeedsChecks() const {
4223 return entrypoint_ == kQuickAllocObjectWithChecks;
4224 }
David Brazdil6de19382016-01-08 17:37:10 +00004225
Vladimir Markoa1de9182016-02-25 11:37:38 +00004226 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004227
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004228 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004229
4230 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4231
4232 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4233 entrypoint_ = entrypoint;
4234 }
4235
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004236 HLoadClass* GetLoadClass() const {
4237 HInstruction* input = InputAt(0);
4238 if (input->IsClinitCheck()) {
4239 input = input->InputAt(0);
4240 }
4241 DCHECK(input->IsLoadClass());
4242 return input->AsLoadClass();
4243 }
4244
David Brazdil6de19382016-01-08 17:37:10 +00004245 bool IsStringAlloc() const;
4246
4247 DECLARE_INSTRUCTION(NewInstance);
4248
Artem Serovcced8ba2017-07-19 18:18:09 +01004249 protected:
4250 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4251
David Brazdil6de19382016-01-08 17:37:10 +00004252 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004253 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004254 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4255 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4256 "Too many packed fields.");
4257
Andreas Gampea5b09a62016-11-17 15:21:22 -08004258 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004259 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004260 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004261};
4262
Agi Csaki05f20562015-08-19 14:58:14 -07004263enum IntrinsicNeedsEnvironmentOrCache {
4264 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4265 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004266};
4267
Aart Bik5d75afe2015-12-14 11:57:01 -08004268enum IntrinsicSideEffects {
4269 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4270 kReadSideEffects, // Intrinsic may read heap memory.
4271 kWriteSideEffects, // Intrinsic may write heap memory.
4272 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4273};
4274
4275enum IntrinsicExceptions {
4276 kNoThrow, // Intrinsic does not throw any exceptions.
4277 kCanThrow // Intrinsic may throw exceptions.
4278};
4279
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004280class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004281 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004282 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004283
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004284 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004285 SetRawInputAt(index, argument);
4286 }
4287
Roland Levillain3e3d7332015-04-28 11:00:54 +01004288 // Return the number of arguments. This number can be lower than
4289 // the number of inputs returned by InputCount(), as some invoke
4290 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4291 // inputs at the end of their list of inputs.
4292 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4293
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004294 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4295
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004296 InvokeType GetInvokeType() const {
4297 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004298 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004299
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004300 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004301 return intrinsic_;
4302 }
4303
Aart Bik5d75afe2015-12-14 11:57:01 -08004304 void SetIntrinsic(Intrinsics intrinsic,
4305 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4306 IntrinsicSideEffects side_effects,
4307 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004308
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004309 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004310 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004311 }
4312
Aart Bikff7d89c2016-11-07 08:49:28 -08004313 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4314
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004315 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004316
Aart Bika8b8e9b2018-01-09 11:01:02 -08004317 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4318
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004319 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004321 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004323 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004324 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4325 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004326
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004327 uint32_t* GetIntrinsicOptimizations() {
4328 return &intrinsic_optimizations_;
4329 }
4330
4331 const uint32_t* GetIntrinsicOptimizations() const {
4332 return &intrinsic_optimizations_;
4333 }
4334
4335 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4336
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004337 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004338 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004339
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004340 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004341
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004342 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004343 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4344 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004345 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004346 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004347 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4348 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004349 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004350 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004351
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304352 HInvoke(InstructionKind kind,
4353 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004354 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004355 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004356 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004357 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004358 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004359 ArtMethod* resolved_method,
4360 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004361 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304362 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004363 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004364 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4365 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004366 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004367 number_of_arguments + number_of_other_inputs,
4368 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004369 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004370 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004371 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004372 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004373 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004374 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004375 // Check mutator lock, constructors lack annotalysis support.
4376 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4377 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004378 }
4379
Artem Serovcced8ba2017-07-19 18:18:09 +01004380 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4381
Roland Levillain3e3d7332015-04-28 11:00:54 +01004382 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004383 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004384 const uint32_t dex_method_index_;
4385 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004386
4387 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4388 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004389};
4390
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004391class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004392 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004393 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004394 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004395 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004396 uint32_t dex_pc,
4397 uint32_t dex_method_index,
4398 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304399 : HInvoke(kInvokeUnresolved,
4400 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004401 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004402 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004403 return_type,
4404 dex_pc,
4405 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004406 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004407 invoke_type) {
4408 }
4409
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004410 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004411
Calin Juravle175dc732015-08-25 15:42:32 +01004412 DECLARE_INSTRUCTION(InvokeUnresolved);
4413
Artem Serovcced8ba2017-07-19 18:18:09 +01004414 protected:
4415 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004416};
4417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004418class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004419 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004420 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004421 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004422 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004423 uint32_t dex_pc,
4424 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304425 : HInvoke(kInvokePolymorphic,
4426 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004427 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004428 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004429 return_type,
4430 dex_pc,
4431 dex_method_index,
4432 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304433 kVirtual) {
4434 }
Orion Hodsonac141392017-01-13 11:53:47 +00004435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004436 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004437
Orion Hodsonac141392017-01-13 11:53:47 +00004438 DECLARE_INSTRUCTION(InvokePolymorphic);
4439
Artem Serovcced8ba2017-07-19 18:18:09 +01004440 protected:
4441 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004442};
4443
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004444class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004445 public:
4446 HInvokeCustom(ArenaAllocator* allocator,
4447 uint32_t number_of_arguments,
4448 uint32_t call_site_index,
4449 DataType::Type return_type,
4450 uint32_t dex_pc)
4451 : HInvoke(kInvokeCustom,
4452 allocator,
4453 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004454 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004455 return_type,
4456 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004457 /* dex_method_index= */ dex::kDexNoIndex,
4458 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004459 kStatic),
4460 call_site_index_(call_site_index) {
4461 }
4462
4463 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004465 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004466
4467 DECLARE_INSTRUCTION(InvokeCustom);
4468
4469 protected:
4470 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4471
4472 private:
4473 uint32_t call_site_index_;
4474};
4475
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004476class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004477 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004478 // Requirements of this method call regarding the class
4479 // initialization (clinit) check of its declaring class.
4480 enum class ClinitCheckRequirement {
4481 kNone, // Class already initialized.
4482 kExplicit, // Static call having explicit clinit check as last input.
4483 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004484 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004485 };
4486
Vladimir Marko58155012015-08-19 12:49:41 +00004487 // Determines how to load the target ArtMethod*.
4488 enum class MethodLoadKind {
4489 // Use a String init ArtMethod* loaded from Thread entrypoints.
4490 kStringInit,
4491
4492 // Use the method's own ArtMethod* loaded by the register allocator.
4493 kRecursive,
4494
Vladimir Marko65979462017-05-19 17:25:12 +01004495 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4496 // Used for boot image methods referenced by boot image code.
4497 kBootImageLinkTimePcRelative,
4498
Vladimir Markob066d432018-01-03 13:14:37 +00004499 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4500 // Used for app->boot calls with relocatable image.
4501 kBootImageRelRo,
4502
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004503 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004504 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004505 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004506
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004507 // Use ArtMethod* at a known address, embed the direct address in the code.
4508 // Used for for JIT-compiled calls.
4509 kJitDirectAddress,
4510
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004511 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4512 // used when other kinds are unimplemented on a particular architecture.
4513 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004514 };
4515
4516 // Determines the location of the code pointer.
4517 enum class CodePtrLocation {
4518 // Recursive call, use local PC-relative call instruction.
4519 kCallSelf,
4520
Vladimir Marko58155012015-08-19 12:49:41 +00004521 // Use code pointer from the ArtMethod*.
4522 // Used when we don't know the target code. This is also the last-resort-kind used when
4523 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4524 kCallArtMethod,
4525 };
4526
4527 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004528 MethodLoadKind method_load_kind;
4529 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004530 // The method load data holds
4531 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4532 // Note that there are multiple string init methods, each having its own offset.
4533 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004534 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004535 };
4536
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004537 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004538 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004539 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004540 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004541 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004542 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004543 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004544 InvokeType invoke_type,
4545 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004546 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304547 : HInvoke(kInvokeStaticOrDirect,
4548 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004549 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004550 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004551 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004552 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004553 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004554 return_type,
4555 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004556 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004557 resolved_method,
4558 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004559 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004560 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004561 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4562 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004564 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004565
Vladimir Markodc151b22015-10-15 18:02:30 +01004566 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004567 bool had_current_method_input = HasCurrentMethodInput();
4568 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4569
4570 // Using the current method is the default and once we find a better
4571 // method load kind, we should not go back to using the current method.
4572 DCHECK(had_current_method_input || !needs_current_method_input);
4573
4574 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004575 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4576 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004577 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004578 dispatch_info_ = dispatch_info;
4579 }
4580
Aart Bik6daebeb2017-04-03 14:35:41 -07004581 DispatchInfo GetDispatchInfo() const {
4582 return dispatch_info_;
4583 }
4584
Vladimir Markoc53c0792015-11-19 15:48:33 +00004585 void AddSpecialInput(HInstruction* input) {
4586 // We allow only one special input.
4587 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4588 DCHECK(InputCount() == GetSpecialInputIndex() ||
4589 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4590 InsertInputAt(GetSpecialInputIndex(), input);
4591 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004592
Vladimir Marko372f10e2016-05-17 16:30:10 +01004593 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004594 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004595 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4596 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4597 DCHECK(!input_records.empty());
4598 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4599 HInstruction* last_input = input_records.back().GetInstruction();
4600 // Note: `last_input` may be null during arguments setup.
4601 if (last_input != nullptr) {
4602 // `last_input` is the last input of a static invoke marked as having
4603 // an explicit clinit check. It must either be:
4604 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4605 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4606 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4607 }
4608 }
4609 return input_records;
4610 }
4611
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004612 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004613 // We access the method via the dex cache so we can't do an implicit null check.
4614 // TODO: for intrinsics we can generate implicit null checks.
4615 return false;
4616 }
4617
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004618 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004619 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004620 }
4621
Vladimir Markoc53c0792015-11-19 15:48:33 +00004622 // Get the index of the special input, if any.
4623 //
David Brazdil6de19382016-01-08 17:37:10 +00004624 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4625 // method pointer; otherwise there may be one platform-specific special input,
4626 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004627 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004628 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004629
Vladimir Marko58155012015-08-19 12:49:41 +00004630 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4631 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4632 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004633 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004634 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004635 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004636 bool HasPcRelativeMethodLoadKind() const {
4637 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004638 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004639 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004640 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004641 bool HasCurrentMethodInput() const {
4642 // This function can be called only after the invoke has been fully initialized by the builder.
4643 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004644 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004645 return true;
4646 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004647 DCHECK(InputCount() == GetSpecialInputIndex() ||
4648 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004649 return false;
4650 }
4651 }
Vladimir Marko58155012015-08-19 12:49:41 +00004652
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004653 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004654 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004655 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004656 }
4657
4658 uint64_t GetMethodAddress() const {
4659 DCHECK(HasMethodAddress());
4660 return dispatch_info_.method_load_data;
4661 }
4662
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004663 const DexFile& GetDexFileForPcRelativeDexCache() const;
4664
Vladimir Markoa1de9182016-02-25 11:37:38 +00004665 ClinitCheckRequirement GetClinitCheckRequirement() const {
4666 return GetPackedField<ClinitCheckRequirementField>();
4667 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004668
Roland Levillain4c0eb422015-04-24 16:43:49 +01004669 // Is this instruction a call to a static method?
4670 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004671 return GetInvokeType() == kStatic;
4672 }
4673
4674 MethodReference GetTargetMethod() const {
4675 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004676 }
4677
Vladimir Markofbb184a2015-11-13 14:47:00 +00004678 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4679 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4680 // instruction; only relevant for static calls with explicit clinit check.
4681 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004682 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004683 size_t last_input_index = inputs_.size() - 1u;
4684 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004685 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004686 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004687 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004688 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004689 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004690 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004691 }
4692
4693 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004694 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004695 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004696 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004697 }
4698
4699 // Is this a call to a static method whose declaring class has an
4700 // implicit intialization check requirement?
4701 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004702 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004703 }
4704
Vladimir Markob554b5a2015-11-06 12:57:55 +00004705 // Does this method load kind need the current method as an input?
4706 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004707 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004708 }
4709
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004710 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004711
Artem Serovcced8ba2017-07-19 18:18:09 +01004712 protected:
4713 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4714
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004715 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004716 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004717 static constexpr size_t kFieldClinitCheckRequirementSize =
4718 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4719 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4720 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4721 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4722 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004723 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4724 kFieldClinitCheckRequirement,
4725 kFieldClinitCheckRequirementSize>;
4726
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004727 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004728 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004729 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004730};
Vladimir Markof64242a2015-12-01 14:58:23 +00004731std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004732std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004733
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004734class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004735 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004736 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004737 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004738 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004739 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004740 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004741 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004742 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304743 : HInvoke(kInvokeVirtual,
4744 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004745 number_of_arguments,
4746 0u,
4747 return_type,
4748 dex_pc,
4749 dex_method_index,
4750 resolved_method,
4751 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304752 vtable_index_(vtable_index) {
4753 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004754
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004755 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004757 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004758 switch (GetIntrinsic()) {
4759 case Intrinsics::kThreadCurrentThread:
4760 case Intrinsics::kStringBufferAppend:
4761 case Intrinsics::kStringBufferToString:
4762 case Intrinsics::kStringBuilderAppend:
4763 case Intrinsics::kStringBuilderToString:
4764 return false;
4765 default:
4766 return HInvoke::CanBeNull();
4767 }
4768 }
4769
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004770 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004771 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004772 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004773 }
4774
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004775 uint32_t GetVTableIndex() const { return vtable_index_; }
4776
4777 DECLARE_INSTRUCTION(InvokeVirtual);
4778
Artem Serovcced8ba2017-07-19 18:18:09 +01004779 protected:
4780 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4781
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004782 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004783 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004784 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004785};
4786
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004787class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004788 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004789 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004790 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004791 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004792 uint32_t dex_pc,
4793 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004794 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004795 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304796 : HInvoke(kInvokeInterface,
4797 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004798 number_of_arguments,
4799 0u,
4800 return_type,
4801 dex_pc,
4802 dex_method_index,
4803 resolved_method,
4804 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304805 imt_index_(imt_index) {
4806 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004807
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004808 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004809
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004810 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004811 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004812 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004813 }
4814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004815 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004816 // The assembly stub currently needs it.
4817 return true;
4818 }
4819
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004820 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004821
4822 DECLARE_INSTRUCTION(InvokeInterface);
4823
Artem Serovcced8ba2017-07-19 18:18:09 +01004824 protected:
4825 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4826
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004827 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004828 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004829 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004830};
4831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004832class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004833 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004834 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304835 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004836 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004837 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004838
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004839 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004840
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004841 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004842 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004843 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004844 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004845 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004846 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004847 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004848 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4849 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004850 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004851 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4852 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004853
Roland Levillain88cb1752014-10-20 16:36:47 +01004854 DECLARE_INSTRUCTION(Neg);
4855
Artem Serovcced8ba2017-07-19 18:18:09 +01004856 protected:
4857 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004858};
4859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004860class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004861 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004862 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304863 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004864 SetRawInputAt(0, cls);
4865 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004866 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004867 }
4868
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004869 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004870
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004871 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004872 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004873
Mingyao Yang0c365e62015-03-31 15:09:29 -07004874 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004875 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004876
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004877 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004878
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004879 HLoadClass* GetLoadClass() const {
4880 DCHECK(InputAt(0)->IsLoadClass());
4881 return InputAt(0)->AsLoadClass();
4882 }
4883
4884 HInstruction* GetLength() const {
4885 return InputAt(1);
4886 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004887
Vladimir Markob5461632018-10-15 14:24:21 +01004888 size_t GetComponentSizeShift() {
4889 return GetPackedField<ComponentSizeShiftField>();
4890 }
4891
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004892 DECLARE_INSTRUCTION(NewArray);
4893
Artem Serovcced8ba2017-07-19 18:18:09 +01004894 protected:
4895 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004896
4897 private:
4898 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4899 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4900 static constexpr size_t kNumberOfNewArrayPackedBits =
4901 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4902 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4903 using ComponentSizeShiftField =
4904 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004905};
4906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004907class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004908 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004909 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004910 HInstruction* left,
4911 HInstruction* right,
4912 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304913 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4914 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004916 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004917
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004918 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004919
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004920 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004921 return GetBlock()->GetGraph()->GetIntConstant(
4922 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004923 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004924 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004925 return GetBlock()->GetGraph()->GetLongConstant(
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(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004929 return GetBlock()->GetGraph()->GetFloatConstant(
4930 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4931 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004932 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004933 return GetBlock()->GetGraph()->GetDoubleConstant(
4934 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4935 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004936
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004937 DECLARE_INSTRUCTION(Add);
4938
Artem Serovcced8ba2017-07-19 18:18:09 +01004939 protected:
4940 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004941};
4942
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004943class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004944 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004945 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004946 HInstruction* left,
4947 HInstruction* right,
4948 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304949 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4950 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004951
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004952 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004954 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004955 return GetBlock()->GetGraph()->GetIntConstant(
4956 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004957 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004958 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004959 return GetBlock()->GetGraph()->GetLongConstant(
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(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004963 return GetBlock()->GetGraph()->GetFloatConstant(
4964 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4965 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004966 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004967 return GetBlock()->GetGraph()->GetDoubleConstant(
4968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4969 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004970
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004971 DECLARE_INSTRUCTION(Sub);
4972
Artem Serovcced8ba2017-07-19 18:18:09 +01004973 protected:
4974 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004975};
4976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004977class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004978 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004979 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 HInstruction* left,
4981 HInstruction* right,
4982 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304983 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4984 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004985
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004986 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004987
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004988 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004990 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004991 return GetBlock()->GetGraph()->GetIntConstant(
4992 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004993 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004994 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004995 return GetBlock()->GetGraph()->GetLongConstant(
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(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004999 return GetBlock()->GetGraph()->GetFloatConstant(
5000 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5001 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005002 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005003 return GetBlock()->GetGraph()->GetDoubleConstant(
5004 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5005 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005006
5007 DECLARE_INSTRUCTION(Mul);
5008
Artem Serovcced8ba2017-07-19 18:18:09 +01005009 protected:
5010 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005011};
5012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005013class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005014 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005015 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005016 HInstruction* left,
5017 HInstruction* right,
5018 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305019 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5020 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005021
Roland Levillain9867bc72015-08-05 10:21:34 +01005022 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005023 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005024 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005025 // Our graph structure ensures we never have 0 for `y` during
5026 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005027 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005028 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005029 return (y == -1) ? -x : x / y;
5030 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005031
Roland Levillain31dd3d62016-02-16 12:21:02 +00005032 template <typename T>
5033 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005034 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005035 return x / y;
5036 }
5037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005038 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005039 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005040 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005041 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005042 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005043 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005044 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5045 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005046 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005047 return GetBlock()->GetGraph()->GetFloatConstant(
5048 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5049 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005050 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005051 return GetBlock()->GetGraph()->GetDoubleConstant(
5052 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005053 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005054
5055 DECLARE_INSTRUCTION(Div);
5056
Artem Serovcced8ba2017-07-19 18:18:09 +01005057 protected:
5058 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005059};
5060
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005061class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005062 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005063 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005064 HInstruction* left,
5065 HInstruction* right,
5066 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305067 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5068 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005069
Roland Levillain9867bc72015-08-05 10:21:34 +01005070 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005071 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005072 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005073 // Our graph structure ensures we never have 0 for `y` during
5074 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005075 DCHECK_NE(y, 0);
5076 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5077 return (y == -1) ? 0 : x % y;
5078 }
5079
Roland Levillain31dd3d62016-02-16 12:21:02 +00005080 template <typename T>
5081 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005082 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005083 return std::fmod(x, y);
5084 }
5085
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005086 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005087 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005088 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005089 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005090 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005091 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005092 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005093 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005094 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005095 return GetBlock()->GetGraph()->GetFloatConstant(
5096 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5097 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005098 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005099 return GetBlock()->GetGraph()->GetDoubleConstant(
5100 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5101 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005102
5103 DECLARE_INSTRUCTION(Rem);
5104
Artem Serovcced8ba2017-07-19 18:18:09 +01005105 protected:
5106 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005107};
5108
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005109class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005110 public:
5111 HMin(DataType::Type result_type,
5112 HInstruction* left,
5113 HInstruction* right,
5114 uint32_t dex_pc)
5115 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005117 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005118
5119 // Evaluation for integral values.
5120 template <typename T> static T ComputeIntegral(T x, T y) {
5121 return (x <= y) ? x : y;
5122 }
5123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005124 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005125 return GetBlock()->GetGraph()->GetIntConstant(
5126 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5127 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005128 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005129 return GetBlock()->GetGraph()->GetLongConstant(
5130 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5131 }
5132 // TODO: Evaluation for floating-point values.
5133 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005134 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005135 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005136 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005137
5138 DECLARE_INSTRUCTION(Min);
5139
5140 protected:
5141 DEFAULT_COPY_CONSTRUCTOR(Min);
5142};
5143
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005144class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005145 public:
5146 HMax(DataType::Type result_type,
5147 HInstruction* left,
5148 HInstruction* right,
5149 uint32_t dex_pc)
5150 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005152 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005153
5154 // Evaluation for integral values.
5155 template <typename T> static T ComputeIntegral(T x, T y) {
5156 return (x >= y) ? x : y;
5157 }
5158
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005159 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005160 return GetBlock()->GetGraph()->GetIntConstant(
5161 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5162 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005163 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005164 return GetBlock()->GetGraph()->GetLongConstant(
5165 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5166 }
5167 // TODO: Evaluation for floating-point values.
5168 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005169 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005170 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005171 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005172
5173 DECLARE_INSTRUCTION(Max);
5174
5175 protected:
5176 DEFAULT_COPY_CONSTRUCTOR(Max);
5177};
5178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005179class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005180 public:
5181 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5182 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5183
5184 // Evaluation for integral values.
5185 template <typename T> static T ComputeIntegral(T x) {
5186 return x < 0 ? -x : x;
5187 }
5188
5189 // Evaluation for floating-point values.
5190 // Note, as a "quality of implementation", rather than pure "spec compliance",
5191 // we require that Math.abs() clears the sign bit (but changes nothing else)
5192 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5193 // http://b/30758343
5194 template <typename T, typename S> static T ComputeFP(T x) {
5195 S bits = bit_cast<S, T>(x);
5196 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5197 }
5198
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005199 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005200 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5201 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005202 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005203 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5204 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005205 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005206 return GetBlock()->GetGraph()->GetFloatConstant(
5207 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5208 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005210 return GetBlock()->GetGraph()->GetDoubleConstant(
5211 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5212 }
5213
5214 DECLARE_INSTRUCTION(Abs);
5215
5216 protected:
5217 DEFAULT_COPY_CONSTRUCTOR(Abs);
5218};
5219
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005220class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005221 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005222 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005223 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5224 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005225 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005226 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005227 SetRawInputAt(0, value);
5228 }
5229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005230 bool IsClonable() const override { return true; }
5231 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005232
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005233 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005234 return true;
5235 }
5236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005237 bool NeedsEnvironment() const override { return true; }
5238 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005239
Calin Juravled0d48522014-11-04 16:40:20 +00005240 DECLARE_INSTRUCTION(DivZeroCheck);
5241
Artem Serovcced8ba2017-07-19 18:18:09 +01005242 protected:
5243 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005244};
5245
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005246class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005247 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005248 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005249 HInstruction* value,
5250 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005251 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305252 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005253 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5254 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005255 }
5256
Roland Levillain5b5b9312016-03-22 14:57:31 +00005257 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005258 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005259 return value << (distance & max_shift_distance);
5260 }
5261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005262 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005263 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005264 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005265 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005266 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005267 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005268 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005269 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005270 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005271 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005272 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5273 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005274 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005275 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005276 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005277 LOG(FATAL) << DebugName() << " is not defined for float values";
5278 UNREACHABLE();
5279 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005280 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005281 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005282 LOG(FATAL) << DebugName() << " is not defined for double values";
5283 UNREACHABLE();
5284 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005285
5286 DECLARE_INSTRUCTION(Shl);
5287
Artem Serovcced8ba2017-07-19 18:18:09 +01005288 protected:
5289 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005290};
5291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005292class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005293 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005294 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005295 HInstruction* value,
5296 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005297 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305298 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005299 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5300 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005301 }
5302
Roland Levillain5b5b9312016-03-22 14:57:31 +00005303 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005304 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005305 return value >> (distance & max_shift_distance);
5306 }
5307
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005308 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005309 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005310 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005311 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005312 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005313 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005314 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005315 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005316 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005317 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005318 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5319 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005320 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005321 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005322 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005323 LOG(FATAL) << DebugName() << " is not defined for float values";
5324 UNREACHABLE();
5325 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005326 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005327 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005328 LOG(FATAL) << DebugName() << " is not defined for double values";
5329 UNREACHABLE();
5330 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005331
5332 DECLARE_INSTRUCTION(Shr);
5333
Artem Serovcced8ba2017-07-19 18:18:09 +01005334 protected:
5335 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005336};
5337
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005338class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005339 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005340 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005341 HInstruction* value,
5342 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005343 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305344 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005345 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5346 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005347 }
5348
Roland Levillain5b5b9312016-03-22 14:57:31 +00005349 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005350 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005351 typedef typename std::make_unsigned<T>::type V;
5352 V ux = static_cast<V>(value);
5353 return static_cast<T>(ux >> (distance & max_shift_distance));
5354 }
5355
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005356 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005357 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005358 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005359 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005360 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005361 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005362 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005363 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005364 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005365 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005366 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5367 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005368 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005369 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005370 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005371 LOG(FATAL) << DebugName() << " is not defined for float values";
5372 UNREACHABLE();
5373 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005374 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005375 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005376 LOG(FATAL) << DebugName() << " is not defined for double values";
5377 UNREACHABLE();
5378 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005379
5380 DECLARE_INSTRUCTION(UShr);
5381
Artem Serovcced8ba2017-07-19 18:18:09 +01005382 protected:
5383 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005384};
5385
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005386class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005387 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005388 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005389 HInstruction* left,
5390 HInstruction* right,
5391 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305392 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5393 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005395 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005396
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005397 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005398
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005399 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005400 return GetBlock()->GetGraph()->GetIntConstant(
5401 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005402 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005403 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005404 return GetBlock()->GetGraph()->GetLongConstant(
5405 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005406 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005407 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005408 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005409 LOG(FATAL) << DebugName() << " is not defined for float values";
5410 UNREACHABLE();
5411 }
5412 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005413 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005414 LOG(FATAL) << DebugName() << " is not defined for double values";
5415 UNREACHABLE();
5416 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005417
5418 DECLARE_INSTRUCTION(And);
5419
Artem Serovcced8ba2017-07-19 18:18:09 +01005420 protected:
5421 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005422};
5423
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005424class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005425 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005426 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005427 HInstruction* left,
5428 HInstruction* right,
5429 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305430 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5431 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005433 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005434
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005435 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005436
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005437 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005438 return GetBlock()->GetGraph()->GetIntConstant(
5439 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005440 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005441 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005442 return GetBlock()->GetGraph()->GetLongConstant(
5443 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005444 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005445 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005446 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005447 LOG(FATAL) << DebugName() << " is not defined for float values";
5448 UNREACHABLE();
5449 }
5450 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005451 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005452 LOG(FATAL) << DebugName() << " is not defined for double values";
5453 UNREACHABLE();
5454 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005455
5456 DECLARE_INSTRUCTION(Or);
5457
Artem Serovcced8ba2017-07-19 18:18:09 +01005458 protected:
5459 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005460};
5461
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005463 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005464 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005465 HInstruction* left,
5466 HInstruction* right,
5467 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305468 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5469 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005470
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005471 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005472
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005473 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005475 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005476 return GetBlock()->GetGraph()->GetIntConstant(
5477 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005478 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005480 return GetBlock()->GetGraph()->GetLongConstant(
5481 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005482 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005483 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005484 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005485 LOG(FATAL) << DebugName() << " is not defined for float values";
5486 UNREACHABLE();
5487 }
5488 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005489 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005490 LOG(FATAL) << DebugName() << " is not defined for double values";
5491 UNREACHABLE();
5492 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005493
5494 DECLARE_INSTRUCTION(Xor);
5495
Artem Serovcced8ba2017-07-19 18:18:09 +01005496 protected:
5497 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005498};
5499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005500class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005501 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305503 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005504 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5505 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005506 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005507
Roland Levillain5b5b9312016-03-22 14:57:31 +00005508 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005509 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005510 typedef typename std::make_unsigned<T>::type V;
5511 V ux = static_cast<V>(value);
5512 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005513 return static_cast<T>(ux);
5514 } else {
5515 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005516 return static_cast<T>(ux >> (distance & max_shift_value)) |
5517 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005518 }
5519 }
5520
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005521 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005522 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005523 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005524 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005525 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005526 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005527 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005528 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005529 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005530 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005531 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5532 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005533 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005534 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005535 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005536 LOG(FATAL) << DebugName() << " is not defined for float values";
5537 UNREACHABLE();
5538 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005539 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005540 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005541 LOG(FATAL) << DebugName() << " is not defined for double values";
5542 UNREACHABLE();
5543 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005544
5545 DECLARE_INSTRUCTION(Ror);
5546
Artem Serovcced8ba2017-07-19 18:18:09 +01005547 protected:
5548 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005549};
5550
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005551// The value of a parameter in this method. Its location depends on
5552// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005553class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005554 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005555 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005556 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005557 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005558 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005559 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305560 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005561 dex_file_(dex_file),
5562 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005563 index_(index) {
5564 SetPackedFlag<kFlagIsThis>(is_this);
5565 SetPackedFlag<kFlagCanBeNull>(!is_this);
5566 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005567
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005568 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005569 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005570 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005571 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005572
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005573 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005574 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005575
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005576 DECLARE_INSTRUCTION(ParameterValue);
5577
Artem Serovcced8ba2017-07-19 18:18:09 +01005578 protected:
5579 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5580
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005581 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005582 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005583 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005584 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5585 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5586 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5587 "Too many packed fields.");
5588
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005589 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005590 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005591 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005592 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005593 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005594};
5595
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005596class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005597 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005598 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305599 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5600 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005602 bool CanBeMoved() const override { return true; }
5603 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005604 return true;
5605 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005606
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005607 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005608
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005609 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005610 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005611 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005612 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005613 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005614 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005615 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005616 LOG(FATAL) << DebugName() << " is not defined for float values";
5617 UNREACHABLE();
5618 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005619 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005620 LOG(FATAL) << DebugName() << " is not defined for double values";
5621 UNREACHABLE();
5622 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005623
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005624 DECLARE_INSTRUCTION(Not);
5625
Artem Serovcced8ba2017-07-19 18:18:09 +01005626 protected:
5627 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005628};
5629
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005630class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005632 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305633 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5634 }
David Brazdil66d126e2015-04-03 16:02:44 +01005635
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005636 bool CanBeMoved() const override { return true; }
5637 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005638 return true;
5639 }
5640
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005641 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005642 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005643 return !x;
5644 }
5645
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005646 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005647 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005648 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005649 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005650 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005651 UNREACHABLE();
5652 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005653 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005654 LOG(FATAL) << DebugName() << " is not defined for float values";
5655 UNREACHABLE();
5656 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005657 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005658 LOG(FATAL) << DebugName() << " is not defined for double values";
5659 UNREACHABLE();
5660 }
David Brazdil66d126e2015-04-03 16:02:44 +01005661
5662 DECLARE_INSTRUCTION(BooleanNot);
5663
Artem Serovcced8ba2017-07-19 18:18:09 +01005664 protected:
5665 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005666};
5667
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005668class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005669 public:
5670 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005671 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305672 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005673 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005674 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005675 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005676 }
5677
5678 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005679 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5680 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005681
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005682 bool IsClonable() const override { return true; }
5683 bool CanBeMoved() const override { return true; }
5684 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005685 return true;
5686 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005687 // Return whether the conversion is implicit. This includes conversion to the same type.
5688 bool IsImplicitConversion() const {
5689 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5690 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005691
Mark Mendelle82549b2015-05-06 10:55:34 -04005692 // Try to statically evaluate the conversion and return a HConstant
5693 // containing the result. If the input cannot be converted, return nullptr.
5694 HConstant* TryStaticEvaluation() const;
5695
Roland Levillaindff1f282014-11-05 14:15:05 +00005696 DECLARE_INSTRUCTION(TypeConversion);
5697
Artem Serovcced8ba2017-07-19 18:18:09 +01005698 protected:
5699 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005700};
5701
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005702static constexpr uint32_t kNoRegNumber = -1;
5703
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005704class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005705 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005706 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005707 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5708 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005709 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005710 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005711 SetRawInputAt(0, value);
5712 }
5713
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005714 bool IsClonable() const override { return true; }
5715 bool CanBeMoved() const override { return true; }
5716 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005717 return true;
5718 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005719
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005720 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005721
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005722 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005724 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005725
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005726 DECLARE_INSTRUCTION(NullCheck);
5727
Artem Serovcced8ba2017-07-19 18:18:09 +01005728 protected:
5729 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005730};
5731
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005732// Embeds an ArtField and all the information required by the compiler. We cache
5733// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005734class FieldInfo : public ValueObject {
5735 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005736 FieldInfo(ArtField* field,
5737 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005738 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005739 bool is_volatile,
5740 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005741 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005742 const DexFile& dex_file)
5743 : field_(field),
5744 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005745 field_type_(field_type),
5746 is_volatile_(is_volatile),
5747 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005748 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005749 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005750
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005751 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005752 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005753 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005754 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005755 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005756 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005757 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005758
5759 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005760 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005761 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005762 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005763 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005764 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005765 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005766 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005767};
5768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005769class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005770 public:
5771 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005772 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005774 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005775 bool is_volatile,
5776 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005777 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005778 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005779 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305780 : HExpression(kInstanceFieldGet,
5781 field_type,
5782 SideEffects::FieldReadOfType(field_type, is_volatile),
5783 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005784 field_info_(field,
5785 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005786 field_type,
5787 is_volatile,
5788 field_idx,
5789 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005790 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005791 SetRawInputAt(0, value);
5792 }
5793
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005794 bool IsClonable() const override { return true; }
5795 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005796
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005797 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005798 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005799 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005800 }
5801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005802 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005803 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005804 }
5805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005806 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005807 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5808 }
5809
Calin Juravle52c48962014-12-16 17:02:57 +00005810 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005811 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005812 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005813 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005814
Vladimir Marko61b92282017-10-11 13:23:17 +01005815 void SetType(DataType::Type new_type) {
5816 DCHECK(DataType::IsIntegralType(GetType()));
5817 DCHECK(DataType::IsIntegralType(new_type));
5818 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5819 SetPackedField<TypeField>(new_type);
5820 }
5821
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005822 DECLARE_INSTRUCTION(InstanceFieldGet);
5823
Artem Serovcced8ba2017-07-19 18:18:09 +01005824 protected:
5825 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5826
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005827 private:
5828 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005829};
5830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005831class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005832 public:
5833 HInstanceFieldSet(HInstruction* object,
5834 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005835 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005836 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005837 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005838 bool is_volatile,
5839 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005840 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005841 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005842 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005843 : HExpression(kInstanceFieldSet,
5844 SideEffects::FieldWriteOfType(field_type, is_volatile),
5845 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005846 field_info_(field,
5847 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005848 field_type,
5849 is_volatile,
5850 field_idx,
5851 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005852 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005853 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005854 SetRawInputAt(0, object);
5855 SetRawInputAt(1, value);
5856 }
5857
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005858 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005860 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005861 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005862 }
5863
Calin Juravle52c48962014-12-16 17:02:57 +00005864 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005865 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005866 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005867 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005868 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005869 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5870 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005871
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005872 DECLARE_INSTRUCTION(InstanceFieldSet);
5873
Artem Serovcced8ba2017-07-19 18:18:09 +01005874 protected:
5875 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5876
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005877 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005878 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5879 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5880 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5881 "Too many packed fields.");
5882
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005883 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005884};
5885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005886class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005887 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005888 HArrayGet(HInstruction* array,
5889 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005890 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005891 uint32_t dex_pc)
5892 : HArrayGet(array,
5893 index,
5894 type,
5895 SideEffects::ArrayReadOfType(type),
5896 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005897 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305898 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005899
5900 HArrayGet(HInstruction* array,
5901 HInstruction* index,
5902 DataType::Type type,
5903 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005904 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005905 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305906 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005907 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005908 SetRawInputAt(0, array);
5909 SetRawInputAt(1, index);
5910 }
5911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005912 bool IsClonable() const override { return true; }
5913 bool CanBeMoved() const override { return true; }
5914 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005915 return true;
5916 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005917 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005918 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005919 // Currently, unless the array is the result of NewArray, the array access is always
5920 // preceded by some form of null NullCheck necessary for the bounds check, usually
5921 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5922 // dynamic BCE. There are cases when these could be removed to produce better code.
5923 // If we ever add optimizations to do so we should allow an implicit check here
5924 // (as long as the address falls in the first page).
5925 //
5926 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5927 // a = cond ? new int[1] : null;
5928 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005929 return false;
5930 }
5931
David Brazdil4833f5a2015-12-16 10:37:39 +00005932 bool IsEquivalentOf(HArrayGet* other) const {
5933 bool result = (GetDexPc() == other->GetDexPc());
5934 if (kIsDebugBuild && result) {
5935 DCHECK_EQ(GetBlock(), other->GetBlock());
5936 DCHECK_EQ(GetArray(), other->GetArray());
5937 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005938 if (DataType::IsIntOrLongType(GetType())) {
5939 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005940 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005941 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5942 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005943 }
5944 }
5945 return result;
5946 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005947
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005948 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5949
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005950 HInstruction* GetArray() const { return InputAt(0); }
5951 HInstruction* GetIndex() const { return InputAt(1); }
5952
Vladimir Marko61b92282017-10-11 13:23:17 +01005953 void SetType(DataType::Type new_type) {
5954 DCHECK(DataType::IsIntegralType(GetType()));
5955 DCHECK(DataType::IsIntegralType(new_type));
5956 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5957 SetPackedField<TypeField>(new_type);
5958 }
5959
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005960 DECLARE_INSTRUCTION(ArrayGet);
5961
Artem Serovcced8ba2017-07-19 18:18:09 +01005962 protected:
5963 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5964
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005965 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005966 // We treat a String as an array, creating the HArrayGet from String.charAt()
5967 // intrinsic in the instruction simplifier. We can always determine whether
5968 // a particular HArrayGet is actually a String.charAt() by looking at the type
5969 // of the input but that requires holding the mutator lock, so we prefer to use
5970 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005971 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005972 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5973 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5974 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005975};
5976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005977class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005978 public:
5979 HArraySet(HInstruction* array,
5980 HInstruction* index,
5981 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005982 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005983 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005984 : HArraySet(array,
5985 index,
5986 value,
5987 expected_component_type,
5988 // Make a best guess for side effects now, may be refined during SSA building.
5989 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305990 dex_pc) {
5991 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005992
5993 HArraySet(HInstruction* array,
5994 HInstruction* index,
5995 HInstruction* value,
5996 DataType::Type expected_component_type,
5997 SideEffects side_effects,
5998 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005999 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006000 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006001 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006002 SetPackedFlag<kFlagValueCanBeNull>(true);
6003 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006004 SetRawInputAt(0, array);
6005 SetRawInputAt(1, index);
6006 SetRawInputAt(2, value);
6007 }
6008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006009 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006010
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006011 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006012 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006013 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006014 }
6015
Mingyao Yang81014cb2015-06-02 03:16:27 -07006016 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006017 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006019 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006020 // TODO: Same as for ArrayGet.
6021 return false;
6022 }
6023
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006024 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006026 }
6027
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006028 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006029 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006030 }
6031
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006032 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006033 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006034 }
6035
Vladimir Markoa1de9182016-02-25 11:37:38 +00006036 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6037 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6038 bool StaticTypeOfArrayIsObjectArray() const {
6039 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6040 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006041
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006042 HInstruction* GetArray() const { return InputAt(0); }
6043 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006044 HInstruction* GetValue() const { return InputAt(2); }
6045
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006046 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006047 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6048 }
6049
6050 static DataType::Type GetComponentType(DataType::Type value_type,
6051 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006052 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006053 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006054 // be correct, we also check what is the value type. If it is a floating
6055 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006056 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006057 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006058 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006059 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006060
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006061 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006062 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006063 }
6064
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006065 static SideEffects ComputeSideEffects(DataType::Type type) {
6066 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006067 }
6068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006069 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6070 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6071 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006072 }
6073
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006074 DECLARE_INSTRUCTION(ArraySet);
6075
Artem Serovcced8ba2017-07-19 18:18:09 +01006076 protected:
6077 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6078
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006079 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006080 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6081 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006082 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006083 static constexpr size_t kFlagNeedsTypeCheck =
6084 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6085 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006086 // Cached information for the reference_type_info_ so that codegen
6087 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006088 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6089 static constexpr size_t kNumberOfArraySetPackedBits =
6090 kFlagStaticTypeOfArrayIsObjectArray + 1;
6091 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6092 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006093 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006094};
6095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006096class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006097 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006098 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306099 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006100 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006101 // Note that arrays do not change length, so the instruction does not
6102 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006103 SetRawInputAt(0, array);
6104 }
6105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006106 bool IsClonable() const override { return true; }
6107 bool CanBeMoved() const override { return true; }
6108 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006109 return true;
6110 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006111 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006112 return obj == InputAt(0);
6113 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006114
Vladimir Markodce016e2016-04-28 13:10:02 +01006115 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6116
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006117 DECLARE_INSTRUCTION(ArrayLength);
6118
Artem Serovcced8ba2017-07-19 18:18:09 +01006119 protected:
6120 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6121
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006122 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006123 // We treat a String as an array, creating the HArrayLength from String.length()
6124 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6125 // determine whether a particular HArrayLength is actually a String.length() by
6126 // looking at the type of the input but that requires holding the mutator lock, so
6127 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006128 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006129 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6130 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6131 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006132};
6133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006134class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006135 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006136 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006137 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6138 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006139 HBoundsCheck(HInstruction* index,
6140 HInstruction* length,
6141 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006142 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006143 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006144 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006145 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006146 SetRawInputAt(0, index);
6147 SetRawInputAt(1, length);
6148 }
6149
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006150 bool IsClonable() const override { return true; }
6151 bool CanBeMoved() const override { return true; }
6152 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006153 return true;
6154 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006155
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006156 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006157
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006158 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006159
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006160 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006161
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006162 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006163
6164 DECLARE_INSTRUCTION(BoundsCheck);
6165
Artem Serovcced8ba2017-07-19 18:18:09 +01006166 protected:
6167 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6168
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006169 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006170 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006171 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6172 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6173 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006174};
6175
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006176class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006177 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006178 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006179 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306180 slow_path_(nullptr) {
6181 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006182
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006183 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006185 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006186 return true;
6187 }
6188
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006189 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6190 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006191
6192 DECLARE_INSTRUCTION(SuspendCheck);
6193
Artem Serovcced8ba2017-07-19 18:18:09 +01006194 protected:
6195 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6196
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006197 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006198 // Only used for code generation, in order to share the same slow path between back edges
6199 // of a same loop.
6200 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006201};
6202
David Srbecky0cf44932015-12-09 14:09:59 +00006203// Pseudo-instruction which provides the native debugger with mapping information.
6204// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006205class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006206 public:
6207 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006208 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306209 }
David Srbecky0cf44932015-12-09 14:09:59 +00006210
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006211 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006212 return true;
6213 }
6214
6215 DECLARE_INSTRUCTION(NativeDebugInfo);
6216
Artem Serovcced8ba2017-07-19 18:18:09 +01006217 protected:
6218 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006219};
6220
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006221/**
6222 * Instruction to load a Class object.
6223 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006224class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006225 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006226 // Determines how to load the Class.
6227 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006228 // We cannot load this class. See HSharpening::SharpenLoadClass.
6229 kInvalid = -1,
6230
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006231 // Use the Class* from the method's own ArtMethod*.
6232 kReferrersClass,
6233
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006234 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006235 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006236 kBootImageLinkTimePcRelative,
6237
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006238 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006239 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006240 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006241
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006242 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006243 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006244 kBssEntry,
6245
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006246 // Use a known boot image Class* address, embedded in the code by the codegen.
6247 // Used for boot image classes referenced by apps in JIT-compiled code.
6248 kJitBootImageAddress,
6249
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006250 // Load from the root table associated with the JIT compiled method.
6251 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006252
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006253 // Load using a simple runtime call. This is the fall-back load kind when
6254 // the codegen is unable to use another appropriate kind.
6255 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006256
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006257 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006258 };
6259
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006260 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006261 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006262 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006263 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006264 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006265 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006266 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006267 : HInstruction(kLoadClass,
6268 DataType::Type::kReference,
6269 SideEffectsForArchRuntimeCalls(),
6270 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006271 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006272 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006273 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006274 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006275 // Referrers class should not need access check. We never inline unverified
6276 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006277 DCHECK(!is_referrers_class || !needs_access_check);
6278
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006279 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006280 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006281 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006282 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006283 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006284 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006285 }
6286
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006287 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006288
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006289 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006290
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006291 LoadKind GetLoadKind() const {
6292 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006293 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006294
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006295 bool HasPcRelativeLoadKind() const {
6296 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6297 GetLoadKind() == LoadKind::kBootImageRelRo ||
6298 GetLoadKind() == LoadKind::kBssEntry;
6299 }
6300
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006301 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006302
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006303 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006304
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006305 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006307 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006309 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006310 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006311 }
6312
Calin Juravle0ba218d2015-05-19 18:46:01 +01006313 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006314 // The entrypoint the code generator is going to call does not do
6315 // clinit of the class.
6316 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006317 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006318 }
6319
6320 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006321 return NeedsAccessCheck() ||
6322 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006323 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006324 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006325 }
6326
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006327 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006328 return NeedsAccessCheck() ||
6329 MustGenerateClinitCheck() ||
6330 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006331 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006332 GetLoadKind() == LoadKind::kBssEntry) &&
6333 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006334 }
6335
Calin Juravleacf735c2015-02-12 15:25:22 +00006336 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006337 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6338 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006339 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006340 } else {
6341 return ReferenceTypeInfo::CreateInvalid();
6342 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006343 }
6344
Vladimir Marko175e7862018-03-27 09:03:13 +00006345 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6346 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6347 DCHECK(klass_ != nullptr);
6348 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006349 }
6350
Andreas Gampea5b09a62016-11-17 15:21:22 -08006351 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006352 const DexFile& GetDexFile() const { return dex_file_; }
6353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006354 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006355 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006356 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006357
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006358 static SideEffects SideEffectsForArchRuntimeCalls() {
6359 return SideEffects::CanTriggerGC();
6360 }
6361
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006362 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006363 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006364 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006365 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006366
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006367 bool MustResolveTypeOnSlowPath() const {
6368 // Check that this instruction has a slow path.
6369 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6370 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6371 return GetLoadKind() == LoadKind::kBssEntry;
6372 }
6373
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006374 void MarkInBootImage() {
6375 SetPackedFlag<kFlagIsInBootImage>(true);
6376 }
6377
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006378 void AddSpecialInput(HInstruction* special_input);
6379
6380 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006381 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006382 return ArrayRef<HUserRecord<HInstruction*>>(
6383 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6384 }
6385
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006386 Handle<mirror::Class> GetClass() const {
6387 return klass_;
6388 }
6389
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006390 DECLARE_INSTRUCTION(LoadClass);
6391
Artem Serovcced8ba2017-07-19 18:18:09 +01006392 protected:
6393 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6394
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006395 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006396 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006397 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006398 // Whether this instruction must generate the initialization check.
6399 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006400 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006401 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6402 static constexpr size_t kFieldLoadKindSize =
6403 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006404 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6405 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006406 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006407 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6408
6409 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006410 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006411 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006412 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006413 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006414 }
6415
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006416 void SetLoadKindInternal(LoadKind load_kind);
6417
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006418 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006419 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006420 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006421 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006422
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006423 // A type index and dex file where the class can be accessed. The dex file can be:
6424 // - The compiling method's dex file if the class is defined there too.
6425 // - The compiling method's dex file if the class is referenced there.
6426 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006427 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006428 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006429 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006430
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006431 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006432};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006433std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6434
6435// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006436inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6437 // The load kind should be determined before inserting the instruction to the graph.
6438 DCHECK(GetBlock() == nullptr);
6439 DCHECK(GetEnvironment() == nullptr);
6440 SetPackedField<LoadKindField>(load_kind);
6441 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6442 special_input_ = HUserRecord<HInstruction*>(nullptr);
6443 }
6444 if (!NeedsEnvironment()) {
6445 SetSideEffects(SideEffects::None());
6446 }
6447}
6448
6449// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006450inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006451 // The special input is used for PC-relative loads on some architectures,
6452 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006453 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006454 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006455 GetLoadKind() == LoadKind::kBssEntry ||
6456 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006457 DCHECK(special_input_.GetInstruction() == nullptr);
6458 special_input_ = HUserRecord<HInstruction*>(special_input);
6459 special_input->AddUseAt(this, 0);
6460}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006461
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006462class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006463 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006464 // Determines how to load the String.
6465 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006466 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006467 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006468 kBootImageLinkTimePcRelative,
6469
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006470 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006471 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006472 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006473
Vladimir Markoaad75c62016-10-03 08:46:48 +00006474 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006475 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006476 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006477
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006478 // Use a known boot image String* address, embedded in the code by the codegen.
6479 // Used for boot image strings referenced by apps in JIT-compiled code.
6480 kJitBootImageAddress,
6481
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006482 // Load from the root table associated with the JIT compiled method.
6483 kJitTableAddress,
6484
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006485 // Load using a simple runtime call. This is the fall-back load kind when
6486 // the codegen is unable to use another appropriate kind.
6487 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006488
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006489 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006490 };
6491
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006492 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006493 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006494 const DexFile& dex_file,
6495 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006496 : HInstruction(kLoadString,
6497 DataType::Type::kReference,
6498 SideEffectsForArchRuntimeCalls(),
6499 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006500 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006501 string_index_(string_index),
6502 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006503 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006504 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006506 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006507
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006508 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006509
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006510 LoadKind GetLoadKind() const {
6511 return GetPackedField<LoadKindField>();
6512 }
6513
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006514 bool HasPcRelativeLoadKind() const {
6515 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6516 GetLoadKind() == LoadKind::kBootImageRelRo ||
6517 GetLoadKind() == LoadKind::kBssEntry;
6518 }
6519
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006520 const DexFile& GetDexFile() const {
6521 return dex_file_;
6522 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006523
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006524 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006525 return string_index_;
6526 }
6527
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006528 Handle<mirror::String> GetString() const {
6529 return string_;
6530 }
6531
6532 void SetString(Handle<mirror::String> str) {
6533 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006534 }
6535
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006536 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006537
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006538 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006539
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006540 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006541
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006542 // Will call the runtime if we need to load the string through
6543 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006544 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006545 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006546 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006547 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006548 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006549 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006550 return false;
6551 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006552 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006553 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006554
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006555 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006556 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006557 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006558
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006559 bool CanBeNull() const override { return false; }
6560 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006561
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006562 static SideEffects SideEffectsForArchRuntimeCalls() {
6563 return SideEffects::CanTriggerGC();
6564 }
6565
Vladimir Marko372f10e2016-05-17 16:30:10 +01006566 void AddSpecialInput(HInstruction* special_input);
6567
6568 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006569 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006570 return ArrayRef<HUserRecord<HInstruction*>>(
6571 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006572 }
6573
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006574 DECLARE_INSTRUCTION(LoadString);
6575
Artem Serovcced8ba2017-07-19 18:18:09 +01006576 protected:
6577 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6578
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006579 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006580 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006581 static constexpr size_t kFieldLoadKindSize =
6582 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6583 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006584 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006585 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006586
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006587 void SetLoadKindInternal(LoadKind load_kind);
6588
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006589 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006590 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006591 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006592 HUserRecord<HInstruction*> special_input_;
6593
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006594 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006595 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006596
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006597 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006598};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006599std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6600
6601// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006602inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6603 // The load kind should be determined before inserting the instruction to the graph.
6604 DCHECK(GetBlock() == nullptr);
6605 DCHECK(GetEnvironment() == nullptr);
6606 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6607 SetPackedField<LoadKindField>(load_kind);
6608 if (load_kind != LoadKind::kRuntimeCall) {
6609 special_input_ = HUserRecord<HInstruction*>(nullptr);
6610 }
6611 if (!NeedsEnvironment()) {
6612 SetSideEffects(SideEffects::None());
6613 }
6614}
6615
6616// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006617inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006618 // The special input is used for PC-relative loads on some architectures,
6619 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006620 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006621 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006622 GetLoadKind() == LoadKind::kBssEntry ||
6623 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006624 // HLoadString::GetInputRecords() returns an empty array at this point,
6625 // so use the GetInputRecords() from the base class to set the input record.
6626 DCHECK(special_input_.GetInstruction() == nullptr);
6627 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006628 special_input->AddUseAt(this, 0);
6629}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006630
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006631class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006632 public:
6633 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006634 uint16_t method_handle_idx,
6635 const DexFile& dex_file,
6636 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006637 : HInstruction(kLoadMethodHandle,
6638 DataType::Type::kReference,
6639 SideEffectsForArchRuntimeCalls(),
6640 dex_pc),
6641 special_input_(HUserRecord<HInstruction*>(current_method)),
6642 method_handle_idx_(method_handle_idx),
6643 dex_file_(dex_file) {
6644 }
6645
6646 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006647 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006648 return ArrayRef<HUserRecord<HInstruction*>>(
6649 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6650 }
6651
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006652 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006653
6654 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6655
6656 const DexFile& GetDexFile() const { return dex_file_; }
6657
6658 static SideEffects SideEffectsForArchRuntimeCalls() {
6659 return SideEffects::CanTriggerGC();
6660 }
6661
6662 DECLARE_INSTRUCTION(LoadMethodHandle);
6663
6664 protected:
6665 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6666
6667 private:
6668 // The special input is the HCurrentMethod for kRuntimeCall.
6669 HUserRecord<HInstruction*> special_input_;
6670
6671 const uint16_t method_handle_idx_;
6672 const DexFile& dex_file_;
6673};
6674
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006675class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006676 public:
6677 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006678 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006679 const DexFile& dex_file,
6680 uint32_t dex_pc)
6681 : HInstruction(kLoadMethodType,
6682 DataType::Type::kReference,
6683 SideEffectsForArchRuntimeCalls(),
6684 dex_pc),
6685 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006686 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006687 dex_file_(dex_file) {
6688 }
6689
6690 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006691 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006692 return ArrayRef<HUserRecord<HInstruction*>>(
6693 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6694 }
6695
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006696 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006697
Orion Hodson06d10a72018-05-14 08:53:38 +01006698 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006699
6700 const DexFile& GetDexFile() const { return dex_file_; }
6701
6702 static SideEffects SideEffectsForArchRuntimeCalls() {
6703 return SideEffects::CanTriggerGC();
6704 }
6705
6706 DECLARE_INSTRUCTION(LoadMethodType);
6707
6708 protected:
6709 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6710
6711 private:
6712 // The special input is the HCurrentMethod for kRuntimeCall.
6713 HUserRecord<HInstruction*> special_input_;
6714
Orion Hodson06d10a72018-05-14 08:53:38 +01006715 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006716 const DexFile& dex_file_;
6717};
6718
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006719/**
6720 * Performs an initialization check on its Class object input.
6721 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006722class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006723 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006724 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006725 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306726 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006728 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006729 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006730 SetRawInputAt(0, constant);
6731 }
Artem Serova6e26142018-06-19 14:55:17 +01006732 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006733 bool CanBeMoved() const override { return true; }
6734 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006735 return true;
6736 }
6737
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006738 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006739 // May call runtime to initialize the class.
6740 return true;
6741 }
6742
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006743 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006744
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006745 HLoadClass* GetLoadClass() const {
6746 DCHECK(InputAt(0)->IsLoadClass());
6747 return InputAt(0)->AsLoadClass();
6748 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006749
6750 DECLARE_INSTRUCTION(ClinitCheck);
6751
Artem Serovcced8ba2017-07-19 18:18:09 +01006752
6753 protected:
6754 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006755};
6756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006757class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006758 public:
6759 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006760 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006761 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006762 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006763 bool is_volatile,
6764 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006765 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006766 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006767 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306768 : HExpression(kStaticFieldGet,
6769 field_type,
6770 SideEffects::FieldReadOfType(field_type, is_volatile),
6771 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006772 field_info_(field,
6773 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006774 field_type,
6775 is_volatile,
6776 field_idx,
6777 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006778 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006779 SetRawInputAt(0, cls);
6780 }
6781
Calin Juravle52c48962014-12-16 17:02:57 +00006782
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006783 bool IsClonable() const override { return true; }
6784 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006786 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006787 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006788 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006789 }
6790
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006791 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006792 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6793 }
6794
Calin Juravle52c48962014-12-16 17:02:57 +00006795 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006796 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006797 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006798 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006799
Vladimir Marko61b92282017-10-11 13:23:17 +01006800 void SetType(DataType::Type new_type) {
6801 DCHECK(DataType::IsIntegralType(GetType()));
6802 DCHECK(DataType::IsIntegralType(new_type));
6803 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6804 SetPackedField<TypeField>(new_type);
6805 }
6806
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006807 DECLARE_INSTRUCTION(StaticFieldGet);
6808
Artem Serovcced8ba2017-07-19 18:18:09 +01006809 protected:
6810 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6811
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006812 private:
6813 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006814};
6815
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006816class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006817 public:
6818 HStaticFieldSet(HInstruction* cls,
6819 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006820 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006821 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006822 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006823 bool is_volatile,
6824 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006825 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006826 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006827 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006828 : HExpression(kStaticFieldSet,
6829 SideEffects::FieldWriteOfType(field_type, is_volatile),
6830 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006831 field_info_(field,
6832 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006833 field_type,
6834 is_volatile,
6835 field_idx,
6836 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006837 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006838 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006839 SetRawInputAt(0, cls);
6840 SetRawInputAt(1, value);
6841 }
6842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006843 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006844 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006845 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006846 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006847 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006848
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006849 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006850 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6851 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006852
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006853 DECLARE_INSTRUCTION(StaticFieldSet);
6854
Artem Serovcced8ba2017-07-19 18:18:09 +01006855 protected:
6856 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6857
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006858 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006859 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6860 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6861 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6862 "Too many packed fields.");
6863
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006864 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006865};
6866
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006867class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006868 public:
6869 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006870 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006871 uint32_t field_index,
6872 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306873 : HExpression(kUnresolvedInstanceFieldGet,
6874 field_type,
6875 SideEffects::AllExceptGCDependency(),
6876 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006877 field_index_(field_index) {
6878 SetRawInputAt(0, obj);
6879 }
6880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006881 bool IsClonable() const override { return true; }
6882 bool NeedsEnvironment() const override { return true; }
6883 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006885 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006886 uint32_t GetFieldIndex() const { return field_index_; }
6887
6888 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6889
Artem Serovcced8ba2017-07-19 18:18:09 +01006890 protected:
6891 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6892
Calin Juravlee460d1d2015-09-29 04:52:17 +01006893 private:
6894 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006895};
6896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006897class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006898 public:
6899 HUnresolvedInstanceFieldSet(HInstruction* obj,
6900 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006901 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006902 uint32_t field_index,
6903 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006904 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006905 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006906 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006907 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006908 SetRawInputAt(0, obj);
6909 SetRawInputAt(1, value);
6910 }
6911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006912 bool IsClonable() const override { return true; }
6913 bool NeedsEnvironment() const override { return true; }
6914 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006915
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006916 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006917 uint32_t GetFieldIndex() const { return field_index_; }
6918
6919 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6920
Artem Serovcced8ba2017-07-19 18:18:09 +01006921 protected:
6922 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6923
Calin Juravlee460d1d2015-09-29 04:52:17 +01006924 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006925 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6926 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006927 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006928 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6929 kFieldFieldType + kFieldFieldTypeSize;
6930 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6931 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006932 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006933
Calin Juravlee460d1d2015-09-29 04:52:17 +01006934 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006935};
6936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006937class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006938 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006939 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006940 uint32_t field_index,
6941 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306942 : HExpression(kUnresolvedStaticFieldGet,
6943 field_type,
6944 SideEffects::AllExceptGCDependency(),
6945 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006946 field_index_(field_index) {
6947 }
6948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006949 bool IsClonable() const override { return true; }
6950 bool NeedsEnvironment() const override { return true; }
6951 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006953 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006954 uint32_t GetFieldIndex() const { return field_index_; }
6955
6956 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6957
Artem Serovcced8ba2017-07-19 18:18:09 +01006958 protected:
6959 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6960
Calin Juravlee460d1d2015-09-29 04:52:17 +01006961 private:
6962 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006963};
6964
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006965class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006966 public:
6967 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006968 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006969 uint32_t field_index,
6970 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006971 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006972 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006973 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006974 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006975 SetRawInputAt(0, value);
6976 }
6977
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006978 bool IsClonable() const override { return true; }
6979 bool NeedsEnvironment() const override { return true; }
6980 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006981
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006982 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006983 uint32_t GetFieldIndex() const { return field_index_; }
6984
6985 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6986
Artem Serovcced8ba2017-07-19 18:18:09 +01006987 protected:
6988 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6989
Calin Juravlee460d1d2015-09-29 04:52:17 +01006990 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006991 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6992 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006993 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006994 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6995 kFieldFieldType + kFieldFieldTypeSize;
6996 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6997 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006998 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006999
Calin Juravlee460d1d2015-09-29 04:52:17 +01007000 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007001};
7002
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007003// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007004class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007005 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007006 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307007 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7008 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007010 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007011
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007012 DECLARE_INSTRUCTION(LoadException);
7013
Artem Serovcced8ba2017-07-19 18:18:09 +01007014 protected:
7015 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007016};
7017
David Brazdilcb1c0552015-08-04 16:22:25 +01007018// Implicit part of move-exception which clears thread-local exception storage.
7019// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007020class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007021 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007022 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007023 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307024 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007025
7026 DECLARE_INSTRUCTION(ClearException);
7027
Artem Serovcced8ba2017-07-19 18:18:09 +01007028 protected:
7029 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007030};
7031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007032class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007033 public:
7034 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007035 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007036 SetRawInputAt(0, exception);
7037 }
7038
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007039 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007040
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007041 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007042
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007043 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007044
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007045 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007046
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007047 DECLARE_INSTRUCTION(Throw);
7048
Artem Serovcced8ba2017-07-19 18:18:09 +01007049 protected:
7050 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007051};
7052
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007053/**
7054 * Implementation strategies for the code generator of a HInstanceOf
7055 * or `HCheckCast`.
7056 */
7057enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007058 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007059 kExactCheck, // Can do a single class compare.
7060 kClassHierarchyCheck, // Can just walk the super class chain.
7061 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7062 kInterfaceCheck, // No optimization yet when checking against an interface.
7063 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007064 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007065 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007066 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007067};
7068
Roland Levillain86503782016-02-11 19:07:30 +00007069std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7070
Vladimir Marko175e7862018-03-27 09:03:13 +00007071// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7072// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7073class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007074 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007075 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007076 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007077 HInstruction* object,
7078 HInstruction* target_class_or_null,
7079 TypeCheckKind check_kind,
7080 Handle<mirror::Class> klass,
7081 uint32_t dex_pc,
7082 ArenaAllocator* allocator,
7083 HIntConstant* bitstring_path_to_root,
7084 HIntConstant* bitstring_mask,
7085 SideEffects side_effects)
7086 : HVariableInputSizeInstruction(
7087 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007088 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007089 side_effects,
7090 dex_pc,
7091 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007092 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007093 kArenaAllocTypeCheckInputs),
7094 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007095 SetPackedField<TypeCheckKindField>(check_kind);
7096 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007097 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007098 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007099 SetRawInputAt(1, target_class_or_null);
7100 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7101 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7102 if (check_kind == TypeCheckKind::kBitstringCheck) {
7103 DCHECK(target_class_or_null->IsNullConstant());
7104 SetRawInputAt(2, bitstring_path_to_root);
7105 SetRawInputAt(3, bitstring_mask);
7106 } else {
7107 DCHECK(target_class_or_null->IsLoadClass());
7108 }
Vladimir Marko87584542017-12-12 17:47:52 +00007109 }
7110
7111 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007112 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007113 HInstruction* load_class = InputAt(1);
7114 DCHECK(load_class->IsLoadClass());
7115 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007116 }
7117
Vladimir Marko175e7862018-03-27 09:03:13 +00007118 uint32_t GetBitstringPathToRoot() const {
7119 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7120 HInstruction* path_to_root = InputAt(2);
7121 DCHECK(path_to_root->IsIntConstant());
7122 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7123 }
7124
7125 uint32_t GetBitstringMask() const {
7126 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7127 HInstruction* mask = InputAt(3);
7128 DCHECK(mask->IsIntConstant());
7129 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7130 }
7131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007132 bool IsClonable() const override { return true; }
7133 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007135 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007136 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7137 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007138 }
7139
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007140 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7141 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7142 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7143 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7144
Vladimir Marko175e7862018-03-27 09:03:13 +00007145 ReferenceTypeInfo GetTargetClassRTI() {
7146 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7147 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007148 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007149 } else {
7150 return ReferenceTypeInfo::CreateInvalid();
7151 }
7152 }
7153
7154 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7155 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7156 DCHECK(klass_ != nullptr);
7157 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7158 }
7159
7160 Handle<mirror::Class> GetClass() const {
7161 return klass_;
7162 }
7163
7164 protected:
7165 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7166
7167 private:
7168 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7169 static constexpr size_t kFieldTypeCheckKindSize =
7170 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7171 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7172 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7173 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7174 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7175 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7176
7177 Handle<mirror::Class> klass_;
7178};
7179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007180class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007181 public:
7182 HInstanceOf(HInstruction* object,
7183 HInstruction* target_class_or_null,
7184 TypeCheckKind check_kind,
7185 Handle<mirror::Class> klass,
7186 uint32_t dex_pc,
7187 ArenaAllocator* allocator,
7188 HIntConstant* bitstring_path_to_root,
7189 HIntConstant* bitstring_mask)
7190 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007191 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007192 object,
7193 target_class_or_null,
7194 check_kind,
7195 klass,
7196 dex_pc,
7197 allocator,
7198 bitstring_path_to_root,
7199 bitstring_mask,
7200 SideEffectsForArchRuntimeCalls(check_kind)) {}
7201
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007202 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007203
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007204 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007205 return CanCallRuntime(GetTypeCheckKind());
7206 }
7207
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007208 static bool CanCallRuntime(TypeCheckKind check_kind) {
7209 // Mips currently does runtime calls for any other checks.
7210 return check_kind != TypeCheckKind::kExactCheck;
7211 }
7212
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007213 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007214 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007215 }
7216
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007217 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007218
Artem Serovcced8ba2017-07-19 18:18:09 +01007219 protected:
7220 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007221};
7222
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007223class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007224 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007225 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307226 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007227 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7228 SetPackedFlag<kFlagUpperCanBeNull>(true);
7229 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007230 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007231 SetRawInputAt(0, input);
7232 }
7233
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007234 bool InstructionDataEquals(const HInstruction* other) const override;
7235 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007236
David Brazdilf5552582015-12-27 13:36:12 +00007237 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007238 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007239 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007240 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007241
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007242 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007243 DCHECK(GetUpperCanBeNull() || !can_be_null);
7244 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007245 }
7246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007247 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007248
Calin Juravleb1498f62015-02-16 13:13:29 +00007249 DECLARE_INSTRUCTION(BoundType);
7250
Artem Serovcced8ba2017-07-19 18:18:09 +01007251 protected:
7252 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7253
Calin Juravleb1498f62015-02-16 13:13:29 +00007254 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007255 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7256 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007257 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007258 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7259 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7260 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7261
Calin Juravleb1498f62015-02-16 13:13:29 +00007262 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007263 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7264 // It is used to bound the type in cases like:
7265 // if (x instanceof ClassX) {
7266 // // uper_bound_ will be ClassX
7267 // }
David Brazdilf5552582015-12-27 13:36:12 +00007268 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007269};
7270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007271class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007272 public:
7273 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007274 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007275 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007276 Handle<mirror::Class> klass,
7277 uint32_t dex_pc,
7278 ArenaAllocator* allocator,
7279 HIntConstant* bitstring_path_to_root,
7280 HIntConstant* bitstring_mask)
7281 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007282 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007283 object,
7284 target_class_or_null,
7285 check_kind,
7286 klass,
7287 dex_pc,
7288 allocator,
7289 bitstring_path_to_root,
7290 bitstring_mask,
7291 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007292
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007293 bool IsClonable() const override { return true; }
7294 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007295 // Instruction may throw a CheckCastError.
7296 return true;
7297 }
7298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007299 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007300
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007301 DECLARE_INSTRUCTION(CheckCast);
7302
Artem Serovcced8ba2017-07-19 18:18:09 +01007303 protected:
7304 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007305};
7306
Andreas Gampe26de38b2016-07-27 17:53:11 -07007307/**
7308 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7309 * @details We define the combined barrier types that are actually required
7310 * by the Java Memory Model, rather than using exactly the terminology from
7311 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7312 * primitives. Note that the JSR-133 cookbook generally does not deal with
7313 * store atomicity issues, and the recipes there are not always entirely sufficient.
7314 * The current recipe is as follows:
7315 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7316 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7317 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7318 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7319 * class has final fields.
7320 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7321 * store-to-memory instructions. Only generated together with non-temporal stores.
7322 */
7323enum MemBarrierKind {
7324 kAnyStore,
7325 kLoadAny,
7326 kStoreStore,
7327 kAnyAny,
7328 kNTStoreStore,
7329 kLastBarrierKind = kNTStoreStore
7330};
7331std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7332
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007333class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007334 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007335 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007336 : HExpression(kMemoryBarrier,
7337 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7338 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007339 SetPackedField<BarrierKindField>(barrier_kind);
7340 }
Calin Juravle27df7582015-04-17 19:12:31 +01007341
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007342 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007343
Vladimir Markoa1de9182016-02-25 11:37:38 +00007344 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007345
7346 DECLARE_INSTRUCTION(MemoryBarrier);
7347
Artem Serovcced8ba2017-07-19 18:18:09 +01007348 protected:
7349 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7350
Calin Juravle27df7582015-04-17 19:12:31 +01007351 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007352 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7353 static constexpr size_t kFieldBarrierKindSize =
7354 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7355 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7356 kFieldBarrierKind + kFieldBarrierKindSize;
7357 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7358 "Too many packed fields.");
7359 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007360};
7361
Igor Murashkind01745e2017-04-05 16:40:31 -07007362// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7363// the specified object(s), with respect to any subsequent store that might "publish"
7364// (i.e. make visible) the specified object to another thread.
7365//
7366// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7367// for all final fields (that were set) at the end of the invoked constructor.
7368//
7369// The constructor fence models the freeze actions for the final fields of an object
7370// being constructed (semantically at the end of the constructor). Constructor fences
7371// have a per-object affinity; two separate objects being constructed get two separate
7372// constructor fences.
7373//
7374// (Note: that if calling a super-constructor or forwarding to another constructor,
7375// the freezes would happen at the end of *that* constructor being invoked).
7376//
7377// The memory model guarantees that when the object being constructed is "published" after
7378// constructor completion (i.e. escapes the current thread via a store), then any final field
7379// writes must be observable on other threads (once they observe that publication).
7380//
7381// Further, anything written before the freeze, and read by dereferencing through the final field,
7382// must also be visible (so final object field could itself have an object with non-final fields;
7383// yet the freeze must also extend to them).
7384//
7385// Constructor example:
7386//
7387// class HasFinal {
7388// final int field; Optimizing IR for <init>()V:
7389// HasFinal() {
7390// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7391// // freeze(this.field); HConstructorFence(this)
7392// } HReturn
7393// }
7394//
7395// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7396// already-initialized classes; in that case the allocation must act as a "default-initializer"
7397// of the object which effectively writes the class pointer "final field".
7398//
7399// For example, we can model default-initialiation as roughly the equivalent of the following:
7400//
7401// class Object {
7402// private final Class header;
7403// }
7404//
7405// Java code: Optimizing IR:
7406//
7407// T new_instance<T>() {
7408// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7409// obj.header = T.class; // header write is done by above call.
7410// // freeze(obj.header) HConstructorFence(obj)
7411// return (T)obj;
7412// }
7413//
7414// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007415// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007416// * QuasiAtomic::ThreadFenceForConstructor
7417//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007418class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007419 // A fence has variable inputs because the inputs can be removed
7420 // after prepare_for_register_allocation phase.
7421 // (TODO: In the future a fence could freeze multiple objects
7422 // after merging two fences together.)
7423 public:
7424 // `fence_object` is the reference that needs to be protected for correct publication.
7425 //
7426 // It makes sense in the following situations:
7427 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7428 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7429 //
7430 // After construction the `fence_object` becomes the 0th input.
7431 // This is not an input in a real sense, but just a convenient place to stash the information
7432 // about the associated object.
7433 HConstructorFence(HInstruction* fence_object,
7434 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007435 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007436 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7437 // constraints described in the class header. We claim that these SideEffects constraints
7438 // enforce a superset of the real constraints.
7439 //
7440 // The ordering described above is conservatively modeled with SideEffects as follows:
7441 //
7442 // * To prevent reordering of the publication stores:
7443 // ----> "Reads of objects" is the initial SideEffect.
7444 // * For every primitive final field store in the constructor:
7445 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7446 // * If there are any stores to reference final fields in the constructor:
7447 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7448 // that are reachable from `fence_object` also need to be prevented for reordering
7449 // (and we do not want to do alias analysis to figure out what those stores are).
7450 //
7451 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7452 // even more conservative approach than the one described above, and prevents all of the
7453 // above reordering without analyzing any of the instructions in the constructor.
7454 //
7455 // If in a later phase we discover that there are no writes to reference final fields,
7456 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307457 : HVariableInputSizeInstruction(kConstructorFence,
7458 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007459 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007460 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007461 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007462 kArenaAllocConstructorFenceInputs) {
7463 DCHECK(fence_object != nullptr);
7464 SetRawInputAt(0, fence_object);
7465 }
7466
7467 // The object associated with this constructor fence.
7468 //
7469 // (Note: This will be null after the prepare_for_register_allocation phase,
7470 // as all constructor fence inputs are removed there).
7471 HInstruction* GetFenceObject() const {
7472 return InputAt(0);
7473 }
7474
7475 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7476 // - Delete `fence_use` from `this`'s use list.
7477 // - Delete `this` from `fence_use`'s inputs list.
7478 // - If the `fence_use` is dead, remove it from the graph.
7479 //
7480 // A fence is considered dead once it no longer has any uses
7481 // and all of the inputs are dead.
7482 //
7483 // This must *not* be called during/after prepare_for_register_allocation,
7484 // because that removes all the inputs to the fences but the fence is actually
7485 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007486 //
7487 // Returns how many HConstructorFence instructions were removed from graph.
7488 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007489
Igor Murashkindd018df2017-08-09 10:38:31 -07007490 // Combine all inputs of `this` and `other` instruction and remove
7491 // `other` from the graph.
7492 //
7493 // Inputs are unique after the merge.
7494 //
7495 // Requirement: `this` must not be the same as `other.
7496 void Merge(HConstructorFence* other);
7497
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007498 // Check if this constructor fence is protecting
7499 // an HNewInstance or HNewArray that is also the immediate
7500 // predecessor of `this`.
7501 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007502 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7503 // to be one of the inputs of `this`.
7504 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007505 // Returns the associated HNewArray or HNewInstance,
7506 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007507 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007508
Igor Murashkind01745e2017-04-05 16:40:31 -07007509 DECLARE_INSTRUCTION(ConstructorFence);
7510
Artem Serovcced8ba2017-07-19 18:18:09 +01007511 protected:
7512 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007513};
7514
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007515class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007516 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007517 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007518 kEnter,
7519 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007520 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007521 };
7522
7523 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007524 : HExpression(kMonitorOperation,
7525 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7526 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007527 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007528 SetRawInputAt(0, object);
7529 }
7530
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007531 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007532 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007533
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007534 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007535 // Verifier guarantees that monitor-exit cannot throw.
7536 // This is important because it allows the HGraphBuilder to remove
7537 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7538 return IsEnter();
7539 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007540
Vladimir Markoa1de9182016-02-25 11:37:38 +00007541 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7542 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007543
7544 DECLARE_INSTRUCTION(MonitorOperation);
7545
Artem Serovcced8ba2017-07-19 18:18:09 +01007546 protected:
7547 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7548
Calin Juravle52c48962014-12-16 17:02:57 +00007549 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007550 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7551 static constexpr size_t kFieldOperationKindSize =
7552 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7553 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7554 kFieldOperationKind + kFieldOperationKindSize;
7555 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7556 "Too many packed fields.");
7557 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007558};
7559
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007560class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007561 public:
7562 HSelect(HInstruction* condition,
7563 HInstruction* true_value,
7564 HInstruction* false_value,
7565 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307566 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007567 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7568
7569 // First input must be `true_value` or `false_value` to allow codegens to
7570 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7571 // that architectures which implement HSelect as a conditional move also
7572 // will not need to invert the condition.
7573 SetRawInputAt(0, false_value);
7574 SetRawInputAt(1, true_value);
7575 SetRawInputAt(2, condition);
7576 }
7577
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007578 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007579 HInstruction* GetFalseValue() const { return InputAt(0); }
7580 HInstruction* GetTrueValue() const { return InputAt(1); }
7581 HInstruction* GetCondition() const { return InputAt(2); }
7582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007583 bool CanBeMoved() const override { return true; }
7584 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007585 return true;
7586 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007587
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007588 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007589 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7590 }
7591
7592 DECLARE_INSTRUCTION(Select);
7593
Artem Serovcced8ba2017-07-19 18:18:09 +01007594 protected:
7595 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007596};
7597
Vladimir Markof9f64412015-09-02 14:05:49 +01007598class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007599 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007600 MoveOperands(Location source,
7601 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007602 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007603 HInstruction* instruction)
7604 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007605
7606 Location GetSource() const { return source_; }
7607 Location GetDestination() const { return destination_; }
7608
7609 void SetSource(Location value) { source_ = value; }
7610 void SetDestination(Location value) { destination_ = value; }
7611
7612 // The parallel move resolver marks moves as "in-progress" by clearing the
7613 // destination (but not the source).
7614 Location MarkPending() {
7615 DCHECK(!IsPending());
7616 Location dest = destination_;
7617 destination_ = Location::NoLocation();
7618 return dest;
7619 }
7620
7621 void ClearPending(Location dest) {
7622 DCHECK(IsPending());
7623 destination_ = dest;
7624 }
7625
7626 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007627 DCHECK(source_.IsValid() || destination_.IsInvalid());
7628 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007629 }
7630
7631 // True if this blocks a move from the given location.
7632 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007633 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007634 }
7635
7636 // A move is redundant if it's been eliminated, if its source and
7637 // destination are the same, or if its destination is unneeded.
7638 bool IsRedundant() const {
7639 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7640 }
7641
7642 // We clear both operands to indicate move that's been eliminated.
7643 void Eliminate() {
7644 source_ = destination_ = Location::NoLocation();
7645 }
7646
7647 bool IsEliminated() const {
7648 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7649 return source_.IsInvalid();
7650 }
7651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007652 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007653
Nicolas Geoffray90218252015-04-15 11:56:51 +01007654 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007655 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007656 }
7657
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007658 HInstruction* GetInstruction() const { return instruction_; }
7659
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007660 private:
7661 Location source_;
7662 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007663 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007664 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007665 // The instruction this move is assocatied with. Null when this move is
7666 // for moving an input in the expected locations of user (including a phi user).
7667 // This is only used in debug mode, to ensure we do not connect interval siblings
7668 // in the same parallel move.
7669 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007670};
7671
Roland Levillainc9285912015-12-18 10:38:42 +00007672std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7673
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007674static constexpr size_t kDefaultNumberOfMoves = 4;
7675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007676class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007677 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007678 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007679 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007680 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007681 moves_.reserve(kDefaultNumberOfMoves);
7682 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007683
Nicolas Geoffray90218252015-04-15 11:56:51 +01007684 void AddMove(Location source,
7685 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007686 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007687 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007688 DCHECK(source.IsValid());
7689 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007690 if (kIsDebugBuild) {
7691 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007692 for (const MoveOperands& move : moves_) {
7693 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007694 // Special case the situation where the move is for the spill slot
7695 // of the instruction.
7696 if ((GetPrevious() == instruction)
7697 || ((GetPrevious() == nullptr)
7698 && instruction->IsPhi()
7699 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007700 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007701 << "Doing parallel moves for the same instruction.";
7702 } else {
7703 DCHECK(false) << "Doing parallel moves for the same instruction.";
7704 }
7705 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007706 }
7707 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007708 for (const MoveOperands& move : moves_) {
7709 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007710 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007711 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007712 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007713 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007714 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007715 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007716 }
7717
Vladimir Marko225b6462015-09-28 12:17:40 +01007718 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007719 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007720 }
7721
Vladimir Marko225b6462015-09-28 12:17:40 +01007722 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007723
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007724 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007725
Artem Serovcced8ba2017-07-19 18:18:09 +01007726 protected:
7727 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7728
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007729 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007730 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007731};
7732
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007733// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7734// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7735// never used across anything that can trigger GC.
7736// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7737// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007738class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007739 public:
7740 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307741 : HExpression(kIntermediateAddress,
7742 DataType::Type::kInt32,
7743 SideEffects::DependsOnGC(),
7744 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007745 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7746 DataType::Size(DataType::Type::kReference))
7747 << "kPrimInt and kPrimNot have different sizes.";
7748 SetRawInputAt(0, base_address);
7749 SetRawInputAt(1, offset);
7750 }
7751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007752 bool IsClonable() const override { return true; }
7753 bool CanBeMoved() const override { return true; }
7754 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007755 return true;
7756 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007757 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007758
7759 HInstruction* GetBaseAddress() const { return InputAt(0); }
7760 HInstruction* GetOffset() const { return InputAt(1); }
7761
7762 DECLARE_INSTRUCTION(IntermediateAddress);
7763
Artem Serovcced8ba2017-07-19 18:18:09 +01007764 protected:
7765 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007766};
7767
7768
Mark Mendell0616ae02015-04-17 12:49:27 -04007769} // namespace art
7770
Aart Bikf8f5a162017-02-06 15:35:29 -08007771#include "nodes_vector.h"
7772
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007773#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7774#include "nodes_shared.h"
7775#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007776#ifdef ART_ENABLE_CODEGEN_mips
7777#include "nodes_mips.h"
7778#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307779#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007780#include "nodes_x86.h"
7781#endif
7782
7783namespace art {
7784
Igor Murashkin6ef45672017-08-08 13:59:55 -07007785class OptimizingCompilerStats;
7786
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007787class HGraphVisitor : public ValueObject {
7788 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007789 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7790 : stats_(stats),
7791 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007792 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007793
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007794 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007795 virtual void VisitBasicBlock(HBasicBlock* block);
7796
Roland Levillain633021e2014-10-01 14:12:25 +01007797 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007798 void VisitInsertionOrder();
7799
Roland Levillain633021e2014-10-01 14:12:25 +01007800 // Visit the graph following dominator tree reverse post-order.
7801 void VisitReversePostOrder();
7802
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007803 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007804
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007805 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007806#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007807 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7808
7809 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7810
7811#undef DECLARE_VISIT_INSTRUCTION
7812
Igor Murashkin6ef45672017-08-08 13:59:55 -07007813 protected:
7814 OptimizingCompilerStats* stats_;
7815
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007816 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007817 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007818
7819 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7820};
7821
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007822class HGraphDelegateVisitor : public HGraphVisitor {
7823 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007824 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7825 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007826 virtual ~HGraphDelegateVisitor() {}
7827
7828 // Visit functions that delegate to to super class.
7829#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007830 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007831
7832 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7833
7834#undef DECLARE_VISIT_INSTRUCTION
7835
7836 private:
7837 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7838};
7839
Artem Serovcced8ba2017-07-19 18:18:09 +01007840// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7841// and remove the old instruction.
7842HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7843
7844// Create a clone for each clonable instructions/phis and replace the original with the clone.
7845//
7846// Used for testing individual instruction cloner.
7847class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7848 public:
7849 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007850 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007851
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007852 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007853 if (instruction->IsClonable()) {
7854 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007855 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007856 }
7857 }
7858
Artem Serov7f4aff62017-06-21 17:02:18 +01007859 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007860
7861 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007862 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007863
7864 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7865};
7866
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007867// Iterator over the blocks that art part of the loop. Includes blocks part
7868// of an inner loop. The order in which the blocks are iterated is on their
7869// block id.
7870class HBlocksInLoopIterator : public ValueObject {
7871 public:
7872 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7873 : blocks_in_loop_(info.GetBlocks()),
7874 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7875 index_(0) {
7876 if (!blocks_in_loop_.IsBitSet(index_)) {
7877 Advance();
7878 }
7879 }
7880
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007881 bool Done() const { return index_ == blocks_.size(); }
7882 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007883 void Advance() {
7884 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007885 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007886 if (blocks_in_loop_.IsBitSet(index_)) {
7887 break;
7888 }
7889 }
7890 }
7891
7892 private:
7893 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007894 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007895 size_t index_;
7896
7897 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7898};
7899
Mingyao Yang3584bce2015-05-19 16:01:59 -07007900// Iterator over the blocks that art part of the loop. Includes blocks part
7901// of an inner loop. The order in which the blocks are iterated is reverse
7902// post order.
7903class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7904 public:
7905 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7906 : blocks_in_loop_(info.GetBlocks()),
7907 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7908 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007909 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007910 Advance();
7911 }
7912 }
7913
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007914 bool Done() const { return index_ == blocks_.size(); }
7915 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007916 void Advance() {
7917 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007918 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7919 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007920 break;
7921 }
7922 }
7923 }
7924
7925 private:
7926 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007927 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007928 size_t index_;
7929
7930 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7931};
7932
Aart Bikf3e61ee2017-04-12 17:09:20 -07007933// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007934inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007935 if (constant->IsIntConstant()) {
7936 return constant->AsIntConstant()->GetValue();
7937 } else if (constant->IsLongConstant()) {
7938 return constant->AsLongConstant()->GetValue();
7939 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007940 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007941 return 0;
7942 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007943}
7944
Aart Bikf3e61ee2017-04-12 17:09:20 -07007945// Returns true iff instruction is an integral constant (and sets value on success).
7946inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7947 if (instruction->IsIntConstant()) {
7948 *value = instruction->AsIntConstant()->GetValue();
7949 return true;
7950 } else if (instruction->IsLongConstant()) {
7951 *value = instruction->AsLongConstant()->GetValue();
7952 return true;
7953 } else if (instruction->IsNullConstant()) {
7954 *value = 0;
7955 return true;
7956 }
7957 return false;
7958}
7959
Aart Bik0148de42017-09-05 09:25:01 -07007960// Returns true iff instruction is the given integral constant.
7961inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7962 int64_t val = 0;
7963 return IsInt64AndGet(instruction, &val) && val == value;
7964}
7965
7966// Returns true iff instruction is a zero bit pattern.
7967inline bool IsZeroBitPattern(HInstruction* instruction) {
7968 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7969}
7970
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007971// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007972#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007973 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7974 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7975#undef INSTRUCTION_TYPE_CHECK
7976
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007977// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007978#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7979 std::is_base_of<BaseType, H##type>::value,
7980#define INSTRUCTION_TYPE_CHECK(type, super) \
7981 inline bool HInstruction::Is##type() const { \
7982 DCHECK_LT(GetKind(), kLastInstructionKind); \
7983 using BaseType = H##type; \
7984 static constexpr bool results[] = { \
7985 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7986 }; \
7987 return results[static_cast<size_t>(GetKind())]; \
7988 }
7989
7990 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7991#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007992#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007993
7994#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007995 inline const H##type* HInstruction::As##type() const { \
7996 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7997 } \
7998 inline H##type* HInstruction::As##type() { \
7999 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8000 }
8001
Vladimir Markoa90dd512018-05-04 15:04:45 +01008002 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8003#undef INSTRUCTION_TYPE_CAST
8004
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008005
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008006// Create space in `blocks` for adding `number_of_new_blocks` entries
8007// starting at location `at`. Blocks after `at` are moved accordingly.
8008inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8009 size_t number_of_new_blocks,
8010 size_t after) {
8011 DCHECK_LT(after, blocks->size());
8012 size_t old_size = blocks->size();
8013 size_t new_size = old_size + number_of_new_blocks;
8014 blocks->resize(new_size);
8015 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8016}
8017
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008018/*
8019 * Hunt "under the hood" of array lengths (leading to array references),
8020 * null checks (also leading to array references), and new arrays
8021 * (leading to the actual length). This makes it more likely related
8022 * instructions become actually comparable.
8023 */
8024inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8025 while (instruction->IsArrayLength() ||
8026 instruction->IsNullCheck() ||
8027 instruction->IsNewArray()) {
8028 instruction = instruction->IsNewArray()
8029 ? instruction->AsNewArray()->GetLength()
8030 : instruction->InputAt(0);
8031 }
8032 return instruction;
8033}
8034
Artem Serov21c7e6f2017-07-27 16:04:42 +01008035void RemoveEnvironmentUses(HInstruction* instruction);
8036bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8037void ResetEnvironmentInputRecords(HInstruction* instruction);
8038
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008039} // namespace art
8040
8041#endif // ART_COMPILER_OPTIMIZING_NODES_H_