blob: 8ac33a430947794e193bbf004e5f4bb64bf6dcf7 [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 Marko3fae1292019-06-07 11:26:25 +010028#include "base/intrusive_forward_list.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010029#include "base/iteration_range.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080030#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000032#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010033#include "base/transform_array_ref.h"
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +010034#include "art_method.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010035#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080037#include "dex/dex_file.h"
38#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080039#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070040#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000041#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000042#include "handle.h"
43#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010044#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010045#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000046#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010047#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048#include "offsets.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,
Hans Boehm206348c2018-12-05 11:11:33 -0800320 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100321 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000322 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100323 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100324 : allocator_(allocator),
325 arena_stack_(arena_stack),
326 blocks_(allocator->Adapter(kArenaAllocBlockList)),
327 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
328 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700329 entry_block_(nullptr),
330 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100331 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100332 number_of_vregs_(0),
333 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000334 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400335 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000336 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700337 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800338 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800340 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000341 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000342 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100343 dex_file_(dex_file),
344 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100345 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100346 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800347 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000349 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100350 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
351 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
352 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
353 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000354 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100355 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000356 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700357 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100358 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100359 blocks_.reserve(kDefaultNumberOfBlocks);
360 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000361
David Brazdilbadd8262016-02-02 16:28:56 +0000362 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700363 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000364
Vladimir Markoca6fff82017-10-03 14:49:14 +0100365 ArenaAllocator* GetAllocator() const { return allocator_; }
366 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100367 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
368
David Brazdil69ba7b72015-06-23 18:27:30 +0100369 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000370 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100371
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000372 HBasicBlock* GetEntryBlock() const { return entry_block_; }
373 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100374 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000375
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000376 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
377 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000378
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000379 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100380
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100381 void ComputeDominanceInformation();
382 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000383 void ClearLoopInformation();
384 void FindBackEdges(ArenaBitVector* visited);
385 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100386 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100387 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000388
David Brazdil4833f5a2015-12-16 10:37:39 +0000389 // Analyze all natural loops in this graph. Returns a code specifying that it
390 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000391 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000392 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100393
David Brazdilffee3d32015-07-06 11:48:53 +0100394 // Iterate over blocks to compute try block membership. Needs reverse post
395 // order and loop information.
396 void ComputeTryBlockInformation();
397
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000398 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000399 // Returns the instruction to replace the invoke expression or null if the
400 // invoke is for a void method. Note that the caller is responsible for replacing
401 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000402 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000403
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000404 // Update the loop and try membership of `block`, which was spawned from `reference`.
405 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
406 // should be the new back edge.
407 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
408 HBasicBlock* reference,
409 bool replace_if_back_edge);
410
Mingyao Yang3584bce2015-05-19 16:01:59 -0700411 // Need to add a couple of blocks to test if the loop body is entered and
412 // put deoptimization instructions, etc.
413 void TransformLoopHeaderForBCE(HBasicBlock* header);
414
Aart Bikf8f5a162017-02-06 15:35:29 -0800415 // Adds a new loop directly after the loop with the given header and exit.
416 // Returns the new preheader.
417 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
418 HBasicBlock* body,
419 HBasicBlock* exit);
420
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000421 // Removes `block` from the graph. Assumes `block` has been disconnected from
422 // other blocks and has no instructions or phis.
423 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000424
David Brazdilfc6a86a2015-06-26 10:33:45 +0000425 // Splits the edge between `block` and `successor` while preserving the
426 // indices in the predecessor/successor lists. If there are multiple edges
427 // between the blocks, the lowest indices are used.
428 // Returns the new block which is empty and has the same dex pc as `successor`.
429 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
430
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100431 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100432 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000433
434 // Transform a loop into a format with a single preheader.
435 //
436 // Each phi in the header should be split: original one in the header should only hold
437 // inputs reachable from the back edges and a single input from the preheader. The newly created
438 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
439 //
440 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
441 // that no longer have this property.
442 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
443
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100444 void SimplifyLoop(HBasicBlock* header);
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100447 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000448 return current_instruction_id_++;
449 }
450
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000451 int32_t GetCurrentInstructionId() const {
452 return current_instruction_id_;
453 }
454
455 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100456 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000457 current_instruction_id_ = id;
458 }
459
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100460 uint16_t GetMaximumNumberOfOutVRegs() const {
461 return maximum_number_of_out_vregs_;
462 }
463
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000464 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
465 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100466 }
467
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100468 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
469 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
470 }
471
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000472 void UpdateTemporariesVRegSlots(size_t slots) {
473 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 }
475
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000476 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100477 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000478 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100479 }
480
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100481 void SetNumberOfVRegs(uint16_t number_of_vregs) {
482 number_of_vregs_ = number_of_vregs;
483 }
484
485 uint16_t GetNumberOfVRegs() const {
486 return number_of_vregs_;
487 }
488
489 void SetNumberOfInVRegs(uint16_t value) {
490 number_of_in_vregs_ = value;
491 }
492
David Brazdildee58d62016-04-07 09:54:26 +0000493 uint16_t GetNumberOfInVRegs() const {
494 return number_of_in_vregs_;
495 }
496
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100497 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100498 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100499 return number_of_vregs_ - number_of_in_vregs_;
500 }
501
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100502 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100503 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100504 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100505
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100506 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
507 DCHECK(GetReversePostOrder()[0] == entry_block_);
508 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
509 }
510
511 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
512 return ReverseRange(GetReversePostOrder());
513 }
514
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100515 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100516 return linear_order_;
517 }
518
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100519 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
520 return ReverseRange(GetLinearOrder());
521 }
522
Mark Mendell1152c922015-04-24 17:06:35 -0400523 bool HasBoundsChecks() const {
524 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800525 }
526
Mark Mendell1152c922015-04-24 17:06:35 -0400527 void SetHasBoundsChecks(bool value) {
528 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800529 }
530
Hans Boehm206348c2018-12-05 11:11:33 -0800531 // Is the code known to be robust against eliminating dead references
532 // and the effects of early finalization?
533 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
534
535 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
536
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000537 bool IsDebuggable() const { return debuggable_; }
538
David Brazdil8d5b8b22015-03-24 10:51:52 +0000539 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 // already, it is created and inserted into the graph. This method is only for
541 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100542 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000543
544 // TODO: This is problematic for the consistency of reference type propagation
545 // because it can be created anytime after the pass and thus it will be left
546 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600547 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000548
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600549 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
550 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000551 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600552 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
553 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000554 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600555 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
556 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000557 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600558 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
559 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000560 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000561
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100562 HCurrentMethod* GetCurrentMethod();
563
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100564 const DexFile& GetDexFile() const {
565 return dex_file_;
566 }
567
568 uint32_t GetMethodIdx() const {
569 return method_idx_;
570 }
571
Igor Murashkind01745e2017-04-05 16:40:31 -0700572 // Get the method name (without the signature), e.g. "<init>"
573 const char* GetMethodName() const;
574
575 // Get the pretty method name (class + name + optionally signature).
576 std::string PrettyMethod(bool with_signature = true) const;
577
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100578 InvokeType GetInvokeType() const {
579 return invoke_type_;
580 }
581
Mark Mendellc4701932015-04-10 13:18:51 -0400582 InstructionSet GetInstructionSet() const {
583 return instruction_set_;
584 }
585
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000586 bool IsCompilingOsr() const { return osr_; }
587
Mingyao Yang063fc772016-08-02 11:02:54 -0700588 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
589 return cha_single_implementation_list_;
590 }
591
592 void AddCHASingleImplementationDependency(ArtMethod* method) {
593 cha_single_implementation_list_.insert(method);
594 }
595
596 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800597 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700598 }
599
David Brazdil77a48ae2015-09-15 12:34:04 +0000600 bool HasTryCatch() const { return has_try_catch_; }
601 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100602
Aart Bikb13c65b2017-03-21 20:14:07 -0700603 bool HasSIMD() const { return has_simd_; }
604 void SetHasSIMD(bool value) { has_simd_ = value; }
605
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800606 bool HasLoops() const { return has_loops_; }
607 void SetHasLoops(bool value) { has_loops_ = value; }
608
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000609 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
610 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
611
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100612 ArtMethod* GetArtMethod() const { return art_method_; }
613 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
614
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100615 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500616 // The instruction has been inserted into the graph, either as a constant, or
617 // before cursor.
618 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
619
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000620 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
621
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800622 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
623 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
624 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
625
David Brazdil2d7352b2015-04-20 14:52:42 +0100626 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000627 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100628 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000630 template <class InstructionType, typename ValueType>
631 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600632 ArenaSafeMap<ValueType, InstructionType*>* cache,
633 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000634 // Try to find an existing constant of the given value.
635 InstructionType* constant = nullptr;
636 auto cached_constant = cache->find(value);
637 if (cached_constant != cache->end()) {
638 constant = cached_constant->second;
639 }
640
641 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100642 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000643 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100644 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000645 cache->Overwrite(value, constant);
646 InsertConstant(constant);
647 }
648 return constant;
649 }
650
David Brazdil8d5b8b22015-03-24 10:51:52 +0000651 void InsertConstant(HConstant* instruction);
652
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000653 // Cache a float constant into the graph. This method should only be
654 // called by the SsaBuilder when creating "equivalent" instructions.
655 void CacheFloatConstant(HFloatConstant* constant);
656
657 // See CacheFloatConstant comment.
658 void CacheDoubleConstant(HDoubleConstant* constant);
659
Vladimir Markoca6fff82017-10-03 14:49:14 +0100660 ArenaAllocator* const allocator_;
661 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000662
663 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100664 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100666 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100667 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668
Aart Bik281c6812016-08-26 11:31:48 -0700669 // List of blocks to perform a linear order tree traversal. Unlike the reverse
670 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100671 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100672
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000673 HBasicBlock* entry_block_;
674 HBasicBlock* exit_block_;
675
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100676 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100677 uint16_t maximum_number_of_out_vregs_;
678
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100679 // The number of virtual registers in this method. Contains the parameters.
680 uint16_t number_of_vregs_;
681
682 // The number of virtual registers used by parameters of this method.
683 uint16_t number_of_in_vregs_;
684
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000685 // Number of vreg size slots that the temporaries use (used in baseline compiler).
686 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100687
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800688 // Flag whether there are bounds checks in the graph. We can skip
689 // BCE if it's false. It's only best effort to keep it up to date in
690 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400691 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800692
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800693 // Flag whether there are try/catch blocks in the graph. We will skip
694 // try/catch-related passes if it's false. It's only best effort to keep
695 // it up to date in the presence of code elimination so there might be
696 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000697 bool has_try_catch_;
698
Aart Bikb13c65b2017-03-21 20:14:07 -0700699 // Flag whether SIMD instructions appear in the graph. If true, the
700 // code generators may have to be more careful spilling the wider
701 // contents of SIMD registers.
702 bool has_simd_;
703
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800704 // Flag whether there are any loops in the graph. We can skip loop
705 // optimization if it's false. It's only best effort to keep it up
706 // to date in the presence of code elimination so there might be false
707 // positives.
708 bool has_loops_;
709
710 // Flag whether there are any irreducible loops in the graph. It's only
711 // best effort to keep it up to date in the presence of code elimination
712 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000713 bool has_irreducible_loops_;
714
Hans Boehm206348c2018-12-05 11:11:33 -0800715 // Is the code known to be robust against eliminating dead references
716 // and the effects of early finalization? If false, dead reference variables
717 // are kept if they might be visible to the garbage collector.
718 // Currently this means that the class was declared to be dead-reference-safe,
719 // the method accesses no reachability-sensitive fields or data, and the same
720 // is true for any methods that were inlined into the current one.
721 bool dead_reference_safe_;
722
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000723 // Indicates whether the graph should be compiled in a way that
724 // ensures full debuggability. If false, we can apply more
725 // aggressive optimizations that may limit the level of debugging.
726 const bool debuggable_;
727
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000728 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000729 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000730
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100731 // The dex file from which the method is from.
732 const DexFile& dex_file_;
733
734 // The method index in the dex file.
735 const uint32_t method_idx_;
736
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100737 // If inlined, this encodes how the callee is being invoked.
738 const InvokeType invoke_type_;
739
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100740 // Whether the graph has been transformed to SSA form. Only used
741 // in debug mode to ensure we are not using properties only valid
742 // for non-SSA form (like the number of temporaries).
743 bool in_ssa_form_;
744
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800745 // Number of CHA guards in the graph. Used to short-circuit the
746 // CHA guard optimization pass when there is no CHA guard left.
747 uint32_t number_of_cha_guards_;
748
Mathieu Chartiere401d142015-04-22 13:56:20 -0700749 const InstructionSet instruction_set_;
750
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000751 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000752 HNullConstant* cached_null_constant_;
753 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000754 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000755 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000756 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000757
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100758 HCurrentMethod* cached_current_method_;
759
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100760 // The ArtMethod this graph is for. Note that for AOT, it may be null,
761 // for example for methods whose declaring class could not be resolved
762 // (such as when the superclass could not be found).
763 ArtMethod* art_method_;
764
David Brazdil4833f5a2015-12-16 10:37:39 +0000765 // Keep the RTI of inexact Object to avoid having to pass stack handle
766 // collection pointer to passes which may create NullConstant.
767 ReferenceTypeInfo inexact_object_rti_;
768
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000769 // Whether we are compiling this graph for on stack replacement: this will
770 // make all loops seen as irreducible and emit special stack maps to mark
771 // compiled code entries which the interpreter can directly jump to.
772 const bool osr_;
773
Mingyao Yang063fc772016-08-02 11:02:54 -0700774 // List of methods that are assumed to have single implementation.
775 ArenaSet<ArtMethod*> cha_single_implementation_list_;
776
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000777 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100778 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000779 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000780 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000781 DISALLOW_COPY_AND_ASSIGN(HGraph);
782};
783
Vladimir Markof9f64412015-09-02 14:05:49 +0100784class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000785 public:
786 HLoopInformation(HBasicBlock* header, HGraph* graph)
787 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100788 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000789 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100790 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100791 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100792 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100793 blocks_(graph->GetAllocator(),
794 graph->GetBlocks().size(),
795 true,
796 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100797 back_edges_.reserve(kDefaultNumberOfBackEdges);
798 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100799
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000800 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100801 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000802
803 void Dump(std::ostream& os);
804
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100805 HBasicBlock* GetHeader() const {
806 return header_;
807 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000808
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000809 void SetHeader(HBasicBlock* block) {
810 header_ = block;
811 }
812
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100813 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
814 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
815 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
816
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000817 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100818 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000819 }
820
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100821 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100822 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100823 }
824
David Brazdil46e2a392015-03-16 17:31:52 +0000825 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100826 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100827 }
828
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000829 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100830 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000831 }
832
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100835 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100836 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837 }
838
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100839 // Returns the lifetime position of the back edge that has the
840 // greatest lifetime position.
841 size_t GetLifetimeEnd() const;
842
843 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100844 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100845 }
846
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000847 // Finds blocks that are part of this loop.
848 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100849
Artem Serov7f4aff62017-06-21 17:02:18 +0100850 // Updates blocks population of the loop and all of its outer' ones recursively after the
851 // population of the inner loop is updated.
852 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
853
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100854 // Returns whether this loop information contains `block`.
855 // Note that this loop information *must* be populated before entering this function.
856 bool Contains(const HBasicBlock& block) const;
857
858 // Returns whether this loop information is an inner loop of `other`.
859 // Note that `other` *must* be populated before entering this function.
860 bool IsIn(const HLoopInformation& other) const;
861
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800862 // Returns true if instruction is not defined within this loop.
863 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700864
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100865 const ArenaBitVector& GetBlocks() const { return blocks_; }
866
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000867 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000868 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000869
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000870 void ClearAllBlocks() {
871 blocks_.ClearAllBits();
872 }
873
David Brazdil3f4a5222016-05-06 12:46:21 +0100874 bool HasBackEdgeNotDominatedByHeader() const;
875
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100876 bool IsPopulated() const {
877 return blocks_.GetHighestBitSet() != -1;
878 }
879
Anton Shaminf89381f2016-05-16 16:44:13 +0600880 bool DominatesAllBackEdges(HBasicBlock* block);
881
David Sehrc757dec2016-11-04 15:48:34 -0700882 bool HasExitEdge() const;
883
Artem Serov7f4aff62017-06-21 17:02:18 +0100884 // Resets back edge and blocks-in-loop data.
885 void ResetBasicBlockData() {
886 back_edges_.clear();
887 ClearAllBlocks();
888 }
889
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100891 // Internal recursive implementation of `Populate`.
892 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100893 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100894
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000895 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100896 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000897 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100898 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100899 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000901
902 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
903};
904
David Brazdilec16f792015-08-19 15:04:01 +0100905// Stores try/catch information for basic blocks.
906// Note that HGraph is constructed so that catch blocks cannot simultaneously
907// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100908class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100909 public:
910 // Try block information constructor.
911 explicit TryCatchInformation(const HTryBoundary& try_entry)
912 : try_entry_(&try_entry),
913 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000914 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100915 DCHECK(try_entry_ != nullptr);
916 }
917
918 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800919 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100920 : try_entry_(nullptr),
921 catch_dex_file_(&dex_file),
922 catch_type_index_(catch_type_index) {}
923
924 bool IsTryBlock() const { return try_entry_ != nullptr; }
925
926 const HTryBoundary& GetTryEntry() const {
927 DCHECK(IsTryBlock());
928 return *try_entry_;
929 }
930
931 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
932
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000933 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100934 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000935 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100936 }
937
Andreas Gampea5b09a62016-11-17 15:21:22 -0800938 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100939 DCHECK(IsCatchBlock());
940 return catch_type_index_;
941 }
942
943 const DexFile& GetCatchDexFile() const {
944 DCHECK(IsCatchBlock());
945 return *catch_dex_file_;
946 }
947
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000948 void SetInvalidTypeIndex() {
949 catch_type_index_ = dex::TypeIndex::Invalid();
950 }
951
David Brazdilec16f792015-08-19 15:04:01 +0100952 private:
953 // One of possibly several TryBoundary instructions entering the block's try.
954 // Only set for try blocks.
955 const HTryBoundary* try_entry_;
956
957 // Exception type information. Only set for catch blocks.
958 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000959 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100960};
961
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100962static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100963static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100964
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965// A block in a method. Contains the list of instructions represented
966// as a double linked list. Each block knows its predecessors and
967// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100968
Vladimir Markof9f64412015-09-02 14:05:49 +0100969class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700971 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100973 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
974 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000975 loop_information_(nullptr),
976 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100977 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100978 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100979 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100980 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000981 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000982 try_catch_information_(nullptr) {
983 predecessors_.reserve(kDefaultNumberOfPredecessors);
984 successors_.reserve(kDefaultNumberOfSuccessors);
985 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
986 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987
Vladimir Marko60584552015-09-03 13:35:12 +0000988 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100989 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000990 }
991
Vladimir Marko60584552015-09-03 13:35:12 +0000992 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100993 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994 }
995
David Brazdild26a4112015-11-10 11:07:31 +0000996 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
997 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
998
Vladimir Marko60584552015-09-03 13:35:12 +0000999 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1000 return ContainsElement(successors_, block, start_from);
1001 }
1002
1003 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001004 return dominated_blocks_;
1005 }
1006
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001007 bool IsEntryBlock() const {
1008 return graph_->GetEntryBlock() == this;
1009 }
1010
1011 bool IsExitBlock() const {
1012 return graph_->GetExitBlock() == this;
1013 }
1014
David Brazdil46e2a392015-03-16 17:31:52 +00001015 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001016 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001017 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001018 bool IsSingleTryBoundary() const;
1019
1020 // Returns true if this block emits nothing but a jump.
1021 bool IsSingleJump() const {
1022 HLoopInformation* loop_info = GetLoopInformation();
1023 return (IsSingleGoto() || IsSingleTryBoundary())
1024 // Back edges generate a suspend check.
1025 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1026 }
David Brazdil46e2a392015-03-16 17:31:52 +00001027
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001028 void AddBackEdge(HBasicBlock* back_edge) {
1029 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001030 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001031 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001032 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001033 loop_information_->AddBackEdge(back_edge);
1034 }
1035
Artem Serov7f4aff62017-06-21 17:02:18 +01001036 // Registers a back edge; if the block was not a loop header before the call associates a newly
1037 // created loop info with it.
1038 //
1039 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1040 // info for all blocks during back edges recalculation.
1041 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1042 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1043 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1044 }
1045 loop_information_->AddBackEdge(back_edge);
1046 }
1047
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001048 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001049 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001050
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001051 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001052 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001053 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001055 HBasicBlock* GetDominator() const { return dominator_; }
1056 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001057 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1058
1059 void RemoveDominatedBlock(HBasicBlock* block) {
1060 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001061 }
Vladimir Marko60584552015-09-03 13:35:12 +00001062
1063 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1064 ReplaceElement(dominated_blocks_, existing, new_block);
1065 }
1066
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001067 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001068
1069 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001070 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001071 }
1072
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001073 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1074 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001075 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001076 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001077 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1078 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001080 HInstruction* GetFirstInstructionDisregardMoves() const;
1081
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001082 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001083 successors_.push_back(block);
1084 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001085 }
1086
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001087 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1088 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001089 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001090 new_block->predecessors_.push_back(this);
1091 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001092 }
1093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1095 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001097 new_block->successors_.push_back(this);
1098 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001099 }
1100
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001101 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001102 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001103 // `predecessor` and `successor`.
1104 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1105 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001106 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001107 successor->predecessors_[predecessor_index] = this;
1108 predecessor->successors_[successor_index] = this;
1109 successors_.push_back(successor);
1110 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001111 }
1112
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001113 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001114 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001115 }
1116
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001117 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001118 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001119 }
1120
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001121 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001122 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001123 }
1124
1125 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001126 predecessors_.push_back(block);
1127 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001128 }
1129
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001130 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001131 DCHECK_EQ(predecessors_.size(), 2u);
1132 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001133 }
1134
David Brazdil769c9e52015-04-27 13:54:09 +01001135 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001136 DCHECK_EQ(successors_.size(), 2u);
1137 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001138 }
1139
David Brazdilfc6a86a2015-06-26 10:33:45 +00001140 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001141 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001142 }
1143
David Brazdilfc6a86a2015-06-26 10:33:45 +00001144 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001145 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001146 }
1147
David Brazdilfc6a86a2015-06-26 10:33:45 +00001148 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001149 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001150 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001151 }
1152
1153 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001154 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001155 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001156 }
1157
1158 // Returns whether the first occurrence of `predecessor` in the list of
1159 // predecessors is at index `idx`.
1160 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001161 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001162 return GetPredecessorIndexOf(predecessor) == idx;
1163 }
1164
David Brazdild7558da2015-09-22 13:04:14 +01001165 // Create a new block between this block and its predecessors. The new block
1166 // is added to the graph, all predecessor edges are relinked to it and an edge
1167 // is created to `this`. Returns the new empty block. Reverse post order or
1168 // loop and try/catch information are not updated.
1169 HBasicBlock* CreateImmediateDominator();
1170
David Brazdilfc6a86a2015-06-26 10:33:45 +00001171 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001172 // created, latter block. Note that this method will add the block to the
1173 // graph, create a Goto at the end of the former block and will create an edge
1174 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001175 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001176 HBasicBlock* SplitBefore(HInstruction* cursor);
1177
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001178 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001179 // created block. Note that this method just updates raw block information,
1180 // like predecessors, successors, dominators, and instruction list. It does not
1181 // update the graph, reverse post order, loop information, nor make sure the
1182 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001183 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1184
1185 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1186 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001187
1188 // Merge `other` at the end of `this`. Successors and dominated blocks of
1189 // `other` are changed to be successors and dominated blocks of `this`. Note
1190 // that this method does not update the graph, reverse post order, loop
1191 // information, nor make sure the blocks are consistent (for example ending
1192 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001193 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001194
1195 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1196 // of `this` are moved to `other`.
1197 // Note that this method does not update the graph, reverse post order, loop
1198 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001199 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001200 void ReplaceWith(HBasicBlock* other);
1201
Aart Bik6b69e0a2017-01-11 10:20:43 -08001202 // Merges the instructions of `other` at the end of `this`.
1203 void MergeInstructionsWith(HBasicBlock* other);
1204
David Brazdil2d7352b2015-04-20 14:52:42 +01001205 // Merge `other` at the end of `this`. This method updates loops, reverse post
1206 // order, links to predecessors, successors, dominators and deletes the block
1207 // from the graph. The two blocks must be successive, i.e. `this` the only
1208 // predecessor of `other` and vice versa.
1209 void MergeWith(HBasicBlock* other);
1210
1211 // Disconnects `this` from all its predecessors, successors and dominator,
1212 // removes it from all loops it is included in and eventually from the graph.
1213 // The block must not dominate any other block. Predecessors and successors
1214 // are safely updated.
1215 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001216
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001217 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001218 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001219 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001220 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001221 // Replace phi `initial` with `replacement` within this block.
1222 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001223 // Replace instruction `initial` with `replacement` within this block.
1224 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1225 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001227 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001228 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1229 // instruction list. With 'ensure_safety' set to true, it verifies that the
1230 // instruction is not in use and removes it from the use lists of its inputs.
1231 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1232 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001233 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001234
1235 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001236 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001237 }
1238
Roland Levillain6b879dd2014-09-22 17:13:44 +01001239 bool IsLoopPreHeaderFirstPredecessor() const {
1240 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001241 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001242 }
1243
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001244 bool IsFirstPredecessorBackEdge() const {
1245 DCHECK(IsLoopHeader());
1246 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1247 }
1248
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001249 HLoopInformation* GetLoopInformation() const {
1250 return loop_information_;
1251 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001252
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001253 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001254 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001255 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001256 void SetInLoop(HLoopInformation* info) {
1257 if (IsLoopHeader()) {
1258 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001259 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001260 loop_information_ = info;
1261 } else if (loop_information_->Contains(*info->GetHeader())) {
1262 // Block is currently part of an outer loop. Make it part of this inner loop.
1263 // Note that a non loop header having a loop information means this loop information
1264 // has already been populated
1265 loop_information_ = info;
1266 } else {
1267 // Block is part of an inner loop. Do not update the loop information.
1268 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1269 // at this point, because this method is being called while populating `info`.
1270 }
1271 }
1272
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001273 // Raw update of the loop information.
1274 void SetLoopInformation(HLoopInformation* info) {
1275 loop_information_ = info;
1276 }
1277
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001278 bool IsInLoop() const { return loop_information_ != nullptr; }
1279
David Brazdilec16f792015-08-19 15:04:01 +01001280 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1281
1282 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1283 try_catch_information_ = try_catch_information;
1284 }
1285
1286 bool IsTryBlock() const {
1287 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1288 }
1289
1290 bool IsCatchBlock() const {
1291 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1292 }
David Brazdilffee3d32015-07-06 11:48:53 +01001293
1294 // Returns the try entry that this block's successors should have. They will
1295 // be in the same try, unless the block ends in a try boundary. In that case,
1296 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001297 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001298
Aart Bik75ff2c92018-04-21 01:28:11 +00001299 bool HasThrowingInstructions() const;
1300
David Brazdila4b8c212015-05-07 09:59:30 +01001301 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001302 bool Dominates(HBasicBlock* block) const;
1303
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001304 size_t GetLifetimeStart() const { return lifetime_start_; }
1305 size_t GetLifetimeEnd() const { return lifetime_end_; }
1306
1307 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1308 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1309
David Brazdil8d5b8b22015-03-24 10:51:52 +00001310 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001311 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001312 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001313 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001314 bool HasSinglePhi() const;
1315
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001316 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001317 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001318 ArenaVector<HBasicBlock*> predecessors_;
1319 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001320 HInstructionList instructions_;
1321 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001322 HLoopInformation* loop_information_;
1323 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001324 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001325 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001326 // The dex program counter of the first instruction of this block.
1327 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001328 size_t lifetime_start_;
1329 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001330 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001331
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001332 friend class HGraph;
1333 friend class HInstruction;
1334
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001335 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1336};
1337
David Brazdilb2bd1c52015-03-25 11:17:37 +00001338// Iterates over the LoopInformation of all loops which contain 'block'
1339// from the innermost to the outermost.
1340class HLoopInformationOutwardIterator : public ValueObject {
1341 public:
1342 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1343 : current_(block.GetLoopInformation()) {}
1344
1345 bool Done() const { return current_ == nullptr; }
1346
1347 void Advance() {
1348 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001349 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001350 }
1351
1352 HLoopInformation* Current() const {
1353 DCHECK(!Done());
1354 return current_;
1355 }
1356
1357 private:
1358 HLoopInformation* current_;
1359
1360 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1361};
1362
Alexandre Ramesef20f712015-06-09 10:29:30 +01001363#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001364 M(Above, Condition) \
1365 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001366 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001368 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001369 M(ArrayGet, Instruction) \
1370 M(ArrayLength, Instruction) \
1371 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001372 M(Below, Condition) \
1373 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001374 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001375 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001376 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001377 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001378 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001379 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001380 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001381 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001382 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001383 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001384 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001385 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001386 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001387 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001388 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001389 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001390 M(Exit, Instruction) \
1391 M(FloatConstant, Constant) \
1392 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001393 M(GreaterThan, Condition) \
1394 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001395 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(InstanceFieldGet, Instruction) \
1397 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001398 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001400 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001401 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001402 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001403 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001405 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001406 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001407 M(LessThan, Condition) \
1408 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001409 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001410 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001411 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001412 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001413 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001415 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001416 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001417 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001418 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001419 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001420 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001421 M(Neg, UnaryOperation) \
1422 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001424 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001425 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001426 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001427 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001428 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001429 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001430 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001431 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001432 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001433 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001434 M(Return, Instruction) \
1435 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001436 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001437 M(Shl, BinaryOperation) \
1438 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001439 M(StaticFieldGet, Instruction) \
1440 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001441 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001442 M(UnresolvedInstanceFieldGet, Instruction) \
1443 M(UnresolvedInstanceFieldSet, Instruction) \
1444 M(UnresolvedStaticFieldGet, Instruction) \
1445 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001446 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001447 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001448 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001449 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001450 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001451 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001452 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001453 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001454 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001455 M(VecExtractScalar, VecUnaryOperation) \
1456 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001457 M(VecCnv, VecUnaryOperation) \
1458 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001459 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001460 M(VecNot, VecUnaryOperation) \
1461 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001462 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001463 M(VecSub, VecBinaryOperation) \
1464 M(VecMul, VecBinaryOperation) \
1465 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001466 M(VecMin, VecBinaryOperation) \
1467 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001468 M(VecAnd, VecBinaryOperation) \
1469 M(VecAndNot, VecBinaryOperation) \
1470 M(VecOr, VecBinaryOperation) \
1471 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001472 M(VecSaturationAdd, VecBinaryOperation) \
1473 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001474 M(VecShl, VecBinaryOperation) \
1475 M(VecShr, VecBinaryOperation) \
1476 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001477 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001478 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001479 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001480 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001481 M(VecLoad, VecMemoryOperation) \
1482 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001483
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001484/*
1485 * Instructions, shared across several (not all) architectures.
1486 */
1487#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1488#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1489#else
1490#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001491 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001492 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001493 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001494 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001495#endif
1496
Alexandre Ramesef20f712015-06-09 10:29:30 +01001497#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1498
1499#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1500
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001501#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001502#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001503#else
1504#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1505 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001506 M(MipsPackedSwitch, Instruction) \
1507 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001508#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001509
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001510#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1511
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001512#ifndef ART_ENABLE_CODEGEN_x86
1513#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1514#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001515#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1516 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001517 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001518 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001519 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001520#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001521
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301522#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1523#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301524 M(X86AndNot, Instruction) \
1525 M(X86MaskOrResetLeastSetBit, Instruction) \
1526 M(VecAvxSub, VecOperation) \
1527 M(VecAvxAdd, VecOperation)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301528#else
1529#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1530#endif
1531
Alexandre Ramesef20f712015-06-09 10:29:30 +01001532#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1533
1534#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1535 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001536 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001537 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1538 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001539 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001540 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001541 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301542 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1543 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001544
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001545#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1546 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001547 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001548 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001549 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001550 M(Invoke, Instruction) \
1551 M(VecOperation, Instruction) \
1552 M(VecUnaryOperation, VecOperation) \
1553 M(VecBinaryOperation, VecOperation) \
1554 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001555
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001556#define FOR_EACH_INSTRUCTION(M) \
1557 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1558 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1559
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001560#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001561FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1562#undef FORWARD_DECLARATION
1563
Vladimir Marko372f10e2016-05-17 16:30:10 +01001564#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001565 private: \
1566 H##type& operator=(const H##type&) = delete; \
1567 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001568 const char* DebugName() const override { return #type; } \
1569 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001570 DCHECK(IsClonable()); \
1571 return new (arena) H##type(*this->As##type()); \
1572 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001573 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001574
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001575#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001576 private: \
1577 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001578 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001579
Artem Serovcced8ba2017-07-19 18:18:09 +01001580#define DEFAULT_COPY_CONSTRUCTOR(type) \
1581 explicit H##type(const H##type& other) = default;
1582
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001583template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001584class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1585 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001586 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001587 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001588 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001589 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001590 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001591 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001592 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001593
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001594 private:
David Brazdiled596192015-01-23 10:39:45 +00001595 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001596 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001597
1598 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001599 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001600
Vladimir Marko46817b82016-03-29 12:21:58 +01001601 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001602
1603 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1604};
1605
David Brazdiled596192015-01-23 10:39:45 +00001606template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001607using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001608
David Brazdil1abb4192015-02-17 18:33:36 +00001609// This class is used by HEnvironment and HInstruction classes to record the
1610// instructions they use and pointers to the corresponding HUseListNodes kept
1611// by the used instructions.
1612template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001613class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001614 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001615 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1616 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001617
Vladimir Marko46817b82016-03-29 12:21:58 +01001618 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1619 : HUserRecord(old_record.instruction_, before_use_node) {}
1620 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1621 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001622 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001623 }
1624
1625 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001626 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1627 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001628
1629 private:
1630 // Instruction used by the user.
1631 HInstruction* instruction_;
1632
Vladimir Marko46817b82016-03-29 12:21:58 +01001633 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1634 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001635};
1636
Vladimir Markoe9004912016-06-16 16:50:52 +01001637// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1638// This is used for HInstruction::GetInputs() to return a container wrapper providing
1639// HInstruction* values even though the underlying container has HUserRecord<>s.
1640struct HInputExtractor {
1641 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1642 return record.GetInstruction();
1643 }
1644 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1645 return record.GetInstruction();
1646 }
1647};
1648
1649using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1650using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1651
Aart Bik854a02b2015-07-14 16:07:00 -07001652/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001654 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001655 * For write/read dependences on fields/arrays, the dependence analysis uses
1656 * type disambiguation (e.g. a float field write cannot modify the value of an
1657 * integer field read) and the access type (e.g. a reference array write cannot
1658 * modify the value of a reference field read [although it may modify the
1659 * reference fetch prior to reading the field, which is represented by its own
1660 * write/read dependence]). The analysis makes conservative points-to
1661 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1662 * the same, and any reference read depends on any reference read without
1663 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001664 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001665 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1666 * alive across the point where garbage collection might happen.
1667 *
1668 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1669 *
1670 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1671 * those instructions from the compiler perspective (between this instruction and the next one
1672 * in the IR).
1673 *
1674 * Note: Instructions which can cause GC only on a fatal slow path do not need
1675 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1676 * one. However the execution may return to compiled code if there is a catch block in the
1677 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1678 * set.
1679 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001680 * The internal representation uses 38-bit and is described in the table below.
1681 * The first line indicates the side effect, and for field/array accesses the
1682 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001684 * The two numbered lines below indicate the bit position in the bitfield (read
1685 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001686 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001687 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1688 * +-------------+---------+---------+--------------+---------+---------+
1689 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1690 * | 3 |333333322|222222221| 1 |111111110|000000000|
1691 * | 7 |654321098|765432109| 8 |765432109|876543210|
1692 *
1693 * Note that, to ease the implementation, 'changes' bits are least significant
1694 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001695 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001696class SideEffects : public ValueObject {
1697 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001698 SideEffects() : flags_(0) {}
1699
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001700 static SideEffects None() {
1701 return SideEffects(0);
1702 }
1703
1704 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001705 return SideEffects(kAllChangeBits | kAllDependOnBits);
1706 }
1707
1708 static SideEffects AllChanges() {
1709 return SideEffects(kAllChangeBits);
1710 }
1711
1712 static SideEffects AllDependencies() {
1713 return SideEffects(kAllDependOnBits);
1714 }
1715
1716 static SideEffects AllExceptGCDependency() {
1717 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1718 }
1719
1720 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001721 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001722 }
1723
Aart Bik34c3ba92015-07-20 14:08:59 -07001724 static SideEffects AllWrites() {
1725 return SideEffects(kAllWrites);
1726 }
1727
1728 static SideEffects AllReads() {
1729 return SideEffects(kAllReads);
1730 }
1731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001732 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001733 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001734 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001735 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001736 }
1737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001738 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001739 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001740 }
1741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001743 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001744 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001745 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001746 }
1747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001748 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001749 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001750 }
1751
Artem Serovd1aa7d02018-06-22 11:35:46 +01001752 // Returns whether GC might happen across this instruction from the compiler perspective so
1753 // the next instruction in the IR would see that.
1754 //
1755 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 static SideEffects CanTriggerGC() {
1757 return SideEffects(1ULL << kCanTriggerGCBit);
1758 }
1759
Artem Serovd1aa7d02018-06-22 11:35:46 +01001760 // Returns whether the instruction must not be alive across a GC point.
1761 //
1762 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001763 static SideEffects DependsOnGC() {
1764 return SideEffects(1ULL << kDependsOnGCBit);
1765 }
1766
Aart Bik854a02b2015-07-14 16:07:00 -07001767 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001768 SideEffects Union(SideEffects other) const {
1769 return SideEffects(flags_ | other.flags_);
1770 }
1771
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001772 SideEffects Exclusion(SideEffects other) const {
1773 return SideEffects(flags_ & ~other.flags_);
1774 }
1775
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001776 void Add(SideEffects other) {
1777 flags_ |= other.flags_;
1778 }
1779
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001780 bool Includes(SideEffects other) const {
1781 return (other.flags_ & flags_) == other.flags_;
1782 }
1783
1784 bool HasSideEffects() const {
1785 return (flags_ & kAllChangeBits);
1786 }
1787
1788 bool HasDependencies() const {
1789 return (flags_ & kAllDependOnBits);
1790 }
1791
1792 // Returns true if there are no side effects or dependencies.
1793 bool DoesNothing() const {
1794 return flags_ == 0;
1795 }
1796
Aart Bik854a02b2015-07-14 16:07:00 -07001797 // Returns true if something is written.
1798 bool DoesAnyWrite() const {
1799 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001800 }
1801
Aart Bik854a02b2015-07-14 16:07:00 -07001802 // Returns true if something is read.
1803 bool DoesAnyRead() const {
1804 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001805 }
1806
Aart Bik854a02b2015-07-14 16:07:00 -07001807 // Returns true if potentially everything is written and read
1808 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001809 bool DoesAllReadWrite() const {
1810 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1811 }
1812
Aart Bik854a02b2015-07-14 16:07:00 -07001813 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001814 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001815 }
1816
Roland Levillain0d5a2812015-11-13 10:07:31 +00001817 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001818 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001819 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1820 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001821 }
1822
1823 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001824 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001825 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001826 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001827 for (int s = kLastBit; s >= 0; s--) {
1828 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1829 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1830 // This is a bit for the GC side effect.
1831 if (current_bit_is_set) {
1832 flags += "GC";
1833 }
Aart Bik854a02b2015-07-14 16:07:00 -07001834 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001835 } else {
1836 // This is a bit for the array/field analysis.
1837 // The underscore character stands for the 'can trigger GC' bit.
1838 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1839 if (current_bit_is_set) {
1840 flags += kDebug[s];
1841 }
1842 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1843 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1844 flags += "|";
1845 }
1846 }
Aart Bik854a02b2015-07-14 16:07:00 -07001847 }
1848 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001849 }
1850
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001851 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001852
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001853 private:
1854 static constexpr int kFieldArrayAnalysisBits = 9;
1855
1856 static constexpr int kFieldWriteOffset = 0;
1857 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1858 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1859 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1860
1861 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1862
1863 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1864 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1865 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1866 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1867
1868 static constexpr int kLastBit = kDependsOnGCBit;
1869 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1870
1871 // Aliases.
1872
1873 static_assert(kChangeBits == kDependOnBits,
1874 "the 'change' bits should match the 'depend on' bits.");
1875
1876 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1877 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1878 static constexpr uint64_t kAllWrites =
1879 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1880 static constexpr uint64_t kAllReads =
1881 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001882
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001883 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001884 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001885 int shift;
1886 switch (type) {
1887 case DataType::Type::kReference: shift = 0; break;
1888 case DataType::Type::kBool: shift = 1; break;
1889 case DataType::Type::kInt8: shift = 2; break;
1890 case DataType::Type::kUint16: shift = 3; break;
1891 case DataType::Type::kInt16: shift = 4; break;
1892 case DataType::Type::kInt32: shift = 5; break;
1893 case DataType::Type::kInt64: shift = 6; break;
1894 case DataType::Type::kFloat32: shift = 7; break;
1895 case DataType::Type::kFloat64: shift = 8; break;
1896 default:
1897 LOG(FATAL) << "Unexpected data type " << type;
1898 UNREACHABLE();
1899 }
Aart Bik854a02b2015-07-14 16:07:00 -07001900 DCHECK_LE(kFieldWriteOffset, shift);
1901 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001902 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001903 }
1904
1905 // Private constructor on direct flags value.
1906 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1907
1908 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001909};
1910
David Brazdiled596192015-01-23 10:39:45 +00001911// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001912class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001913 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001914 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001915 size_t number_of_vregs,
1916 ArtMethod* method,
1917 uint32_t dex_pc,
1918 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001919 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1920 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001921 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001922 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001923 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001924 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001925 }
1926
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001927 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1928 const HEnvironment& to_copy,
1929 HInstruction* holder)
1930 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001931 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001932 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001933 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001934 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001935
Vladimir Markoec32f642017-06-02 10:51:55 +01001936 void AllocateLocations() {
1937 DCHECK(locations_.empty());
1938 locations_.resize(vregs_.size());
1939 }
1940
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001941 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001942 if (parent_ != nullptr) {
1943 parent_->SetAndCopyParentChain(allocator, parent);
1944 } else {
1945 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1946 parent_->CopyFrom(parent);
1947 if (parent->GetParent() != nullptr) {
1948 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1949 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001950 }
David Brazdiled596192015-01-23 10:39:45 +00001951 }
1952
Vladimir Marko69d310e2017-10-09 14:12:23 +01001953 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001954 void CopyFrom(HEnvironment* environment);
1955
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001956 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1957 // input to the loop phi instead. This is for inserting instructions that
1958 // require an environment (like HDeoptimization) in the loop pre-header.
1959 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001960
1961 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001962 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001963 }
1964
1965 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001966 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001967 }
1968
David Brazdil1abb4192015-02-17 18:33:36 +00001969 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001970
Artem Serovca210e32017-12-15 13:43:20 +00001971 // Replaces the input at the position 'index' with the replacement; the replacement and old
1972 // input instructions' env_uses_ lists are adjusted. The function works similar to
1973 // HInstruction::ReplaceInput.
1974 void ReplaceInput(HInstruction* replacement, size_t index);
1975
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001976 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001977
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001978 HEnvironment* GetParent() const { return parent_; }
1979
1980 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001981 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001982 }
1983
1984 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001985 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001986 }
1987
1988 uint32_t GetDexPc() const {
1989 return dex_pc_;
1990 }
1991
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001992 ArtMethod* GetMethod() const {
1993 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001994 }
1995
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001996 HInstruction* GetHolder() const {
1997 return holder_;
1998 }
1999
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002000
2001 bool IsFromInlinedInvoke() const {
2002 return GetParent() != nullptr;
2003 }
2004
David Brazdiled596192015-01-23 10:39:45 +00002005 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002006 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2007 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002008 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002009 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002010 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002011
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002012 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002013 HInstruction* const holder_;
2014
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002015 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002016
2017 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2018};
2019
Vladimir Markof9f64412015-09-02 14:05:49 +01002020class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302022#define DECLARE_KIND(type, super) k##type,
2023 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002024 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302025 kLastInstructionKind
2026 };
2027#undef DECLARE_KIND
2028
2029 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002030 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2031
2032 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002033 : previous_(nullptr),
2034 next_(nullptr),
2035 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002036 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002037 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002038 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002039 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002040 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002041 locations_(nullptr),
2042 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002043 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002044 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002045 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302046 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002047 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002048 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2049 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002050
Dave Allison20dfc792014-06-16 20:44:29 -07002051 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002052
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002053
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002054 HInstruction* GetNext() const { return next_; }
2055 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002056
Calin Juravle77520bc2015-01-12 18:45:46 +00002057 HInstruction* GetNextDisregardingMoves() const;
2058 HInstruction* GetPreviousDisregardingMoves() const;
2059
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002060 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002061 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002062 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002063 bool IsInBlock() const { return block_ != nullptr; }
2064 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002065 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2066 bool IsIrreducibleLoopHeaderPhi() const {
2067 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2068 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002069
Vladimir Marko372f10e2016-05-17 16:30:10 +01002070 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2071
2072 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2073 // One virtual method is enough, just const_cast<> and then re-add the const.
2074 return ArrayRef<const HUserRecord<HInstruction*>>(
2075 const_cast<HInstruction*>(this)->GetInputRecords());
2076 }
2077
Vladimir Markoe9004912016-06-16 16:50:52 +01002078 HInputsRef GetInputs() {
2079 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002080 }
2081
Vladimir Markoe9004912016-06-16 16:50:52 +01002082 HConstInputsRef GetInputs() const {
2083 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002084 }
2085
2086 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002087 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002088
Aart Bik2767f4b2016-10-28 15:03:53 -07002089 bool HasInput(HInstruction* input) const {
2090 for (const HInstruction* i : GetInputs()) {
2091 if (i == input) {
2092 return true;
2093 }
2094 }
2095 return false;
2096 }
2097
Vladimir Marko372f10e2016-05-17 16:30:10 +01002098 void SetRawInputAt(size_t index, HInstruction* input) {
2099 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2100 }
2101
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002102 virtual void Accept(HGraphVisitor* visitor) = 0;
2103 virtual const char* DebugName() const = 0;
2104
Vladimir Markobd785672018-05-03 17:09:09 +01002105 DataType::Type GetType() const {
2106 return TypeField::Decode(GetPackedFields());
2107 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002108
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002109 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002110
2111 uint32_t GetDexPc() const { return dex_pc_; }
2112
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002113 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002114
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002115 // Can the instruction throw?
2116 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2117 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002118 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002119
2120 // Does the instruction always throw an exception unconditionally?
2121 virtual bool AlwaysThrows() const { return false; }
2122
David Brazdilec16f792015-08-19 15:04:01 +01002123 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002124
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002125 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002126 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002127
Calin Juravle10e244f2015-01-26 18:54:32 +00002128 // Does not apply for all instructions, but having this at top level greatly
2129 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002130 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002131 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002132 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002133 return true;
2134 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002135
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002136 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002137 return false;
2138 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002139
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002140 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2141 // with it. Otherwise return null.
2142 HNullCheck* GetImplicitNullCheck() const {
2143 // Find the first previous instruction which is not a move.
2144 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2145 if (first_prev_not_move != nullptr &&
2146 first_prev_not_move->IsNullCheck() &&
2147 first_prev_not_move->IsEmittedAtUseSite()) {
2148 return first_prev_not_move->AsNullCheck();
2149 }
2150 return nullptr;
2151 }
2152
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002153 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002155 }
2156
Calin Juravle2e768302015-07-28 14:41:11 +00002157 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002158
Calin Juravle61d544b2015-02-23 16:46:57 +00002159 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002160 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002161 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002162 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002163 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002164
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002165 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002166 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002167 // Note: fixup_end remains valid across push_front().
2168 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2169 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002170 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002171 uses_.push_front(*new_node);
2172 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002173 }
2174
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002175 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002176 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002177 // Note: env_fixup_end remains valid across push_front().
2178 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2179 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002180 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002181 env_uses_.push_front(*new_node);
2182 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002183 }
2184
David Brazdil1abb4192015-02-17 18:33:36 +00002185 void RemoveAsUserOfInput(size_t input) {
2186 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002187 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2188 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2189 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002190 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002191
Vladimir Marko372f10e2016-05-17 16:30:10 +01002192 void RemoveAsUserOfAllInputs() {
2193 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2194 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2195 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2196 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2197 }
2198 }
2199
David Brazdil1abb4192015-02-17 18:33:36 +00002200 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2201 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002202
Vladimir Marko46817b82016-03-29 12:21:58 +01002203 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2204 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2205 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002206 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002207 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002208 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002209
Aart Bikcc42be02016-10-20 16:14:16 -07002210 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002211 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002212 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002213 !CanThrow() &&
2214 !IsSuspendCheck() &&
2215 !IsControlFlow() &&
2216 !IsNativeDebugInfo() &&
2217 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002218 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002219 !IsMemoryBarrier() &&
2220 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002221 }
2222
Aart Bikcc42be02016-10-20 16:14:16 -07002223 bool IsDeadAndRemovable() const {
2224 return IsRemovable() && !HasUses();
2225 }
2226
Roland Levillain6c82d402014-10-13 16:10:27 +01002227 // Does this instruction strictly dominate `other_instruction`?
2228 // Returns false if this instruction and `other_instruction` are the same.
2229 // Aborts if this instruction and `other_instruction` are both phis.
2230 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002231
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002232 int GetId() const { return id_; }
2233 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002234
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002235 int GetSsaIndex() const { return ssa_index_; }
2236 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2237 bool HasSsaIndex() const { return ssa_index_ != -1; }
2238
2239 bool HasEnvironment() const { return environment_ != nullptr; }
2240 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002241 // Set the `environment_` field. Raw because this method does not
2242 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002243 void SetRawEnvironment(HEnvironment* environment) {
2244 DCHECK(environment_ == nullptr);
2245 DCHECK_EQ(environment->GetHolder(), this);
2246 environment_ = environment;
2247 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002248
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002249 void InsertRawEnvironment(HEnvironment* environment) {
2250 DCHECK(environment_ != nullptr);
2251 DCHECK_EQ(environment->GetHolder(), this);
2252 DCHECK(environment->GetParent() == nullptr);
2253 environment->parent_ = environment_;
2254 environment_ = environment;
2255 }
2256
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002257 void RemoveEnvironment();
2258
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002259 // Set the environment of this instruction, copying it from `environment`. While
2260 // copying, the uses lists are being updated.
2261 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002262 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002263 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002264 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002265 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002266 if (environment->GetParent() != nullptr) {
2267 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2268 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002269 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002270
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002271 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2272 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002273 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002274 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002275 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002276 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002277 if (environment->GetParent() != nullptr) {
2278 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2279 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002280 }
2281
Nicolas Geoffray39468442014-09-02 15:17:15 +01002282 // Returns the number of entries in the environment. Typically, that is the
2283 // number of dex registers in a method. It could be more in case of inlining.
2284 size_t EnvironmentSize() const;
2285
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002286 LocationSummary* GetLocations() const { return locations_; }
2287 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002288
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002289 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002290 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002291 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002292 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002293
Alexandre Rames188d4312015-04-09 18:30:21 +01002294 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2295 // uses of this instruction by `other` are *not* updated.
2296 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2297 ReplaceWith(other);
2298 other->ReplaceInput(this, use_index);
2299 }
2300
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002301 // Move `this` instruction before `cursor`
2302 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002303
Vladimir Markofb337ea2015-11-25 15:25:10 +00002304 // Move `this` before its first user and out of any loops. If there is no
2305 // out-of-loop user that dominates all other users, move the instruction
2306 // to the end of the out-of-loop common dominator of the user's blocks.
2307 //
2308 // This can be used only on non-throwing instructions with no side effects that
2309 // have at least one use but no environment uses.
2310 void MoveBeforeFirstUserAndOutOfLoops();
2311
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002312#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002313 bool Is##type() const;
2314
2315 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2316#undef INSTRUCTION_TYPE_CHECK
2317
2318#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002319 const H##type* As##type() const; \
2320 H##type* As##type();
2321
Vladimir Markoa90dd512018-05-04 15:04:45 +01002322 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2323#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002324
Artem Serovcced8ba2017-07-19 18:18:09 +01002325 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2326 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2327 // the instruction then the behaviour of this function is undefined.
2328 //
2329 // Note: It is semantically valid to create a clone of the instruction only until
2330 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2331 // copied.
2332 //
2333 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2334 // 'explicit HInstruction(const HInstruction& other)' for details.
2335 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2336 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2337 DebugName() << " " << GetId();
2338 UNREACHABLE();
2339 }
2340
2341 // Return whether instruction can be cloned (copied).
2342 virtual bool IsClonable() const { return false; }
2343
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002344 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002345 // TODO: this method is used by LICM and GVN with possibly different
2346 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002347 virtual bool CanBeMoved() const { return false; }
2348
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002349 // Returns whether any data encoded in the two instructions is equal.
2350 // This method does not look at the inputs. Both instructions must be
2351 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002352 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002353 return false;
2354 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002355
2356 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002357 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002358 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002359 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002360
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302361 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002362
2363 virtual size_t ComputeHashCode() const {
2364 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002365 for (const HInstruction* input : GetInputs()) {
2366 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002367 }
2368 return result;
2369 }
2370
2371 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002372 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002373 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002374
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002375 size_t GetLifetimePosition() const { return lifetime_position_; }
2376 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2377 LiveInterval* GetLiveInterval() const { return live_interval_; }
2378 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2379 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2380
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002381 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2382
2383 // Returns whether the code generation of the instruction will require to have access
2384 // to the current method. Such instructions are:
2385 // (1): Instructions that require an environment, as calling the runtime requires
2386 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002387 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2388 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002389 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002390 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002391 }
2392
Vladimir Markodc151b22015-10-15 18:02:30 +01002393 // Returns whether the code generation of the instruction will require to have access
2394 // to the dex cache of the current method's declaring class via the current method.
2395 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002396
Mark Mendellc4701932015-04-10 13:18:51 -04002397 // Does this instruction have any use in an environment before
2398 // control flow hits 'other'?
2399 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2400
2401 // Remove all references to environment uses of this instruction.
2402 // The caller must ensure that this is safe to do.
2403 void RemoveEnvironmentUsers();
2404
Vladimir Markoa1de9182016-02-25 11:37:38 +00002405 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2406 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002407
David Brazdil1abb4192015-02-17 18:33:36 +00002408 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002409 // If set, the machine code for this instruction is assumed to be generated by
2410 // its users. Used by liveness analysis to compute use positions accordingly.
2411 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2412 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302413 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2414 static constexpr size_t kFieldInstructionKindSize =
2415 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002416 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302417 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002418 static constexpr size_t kFieldTypeSize =
2419 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2420 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002421 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2422
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302423 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2424 "Too many generic packed fields");
2425
Vladimir Markobd785672018-05-03 17:09:09 +01002426 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2427
Vladimir Marko372f10e2016-05-17 16:30:10 +01002428 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2429 return GetInputRecords()[i];
2430 }
2431
2432 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2433 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2434 input_records[index] = input;
2435 }
David Brazdil1abb4192015-02-17 18:33:36 +00002436
Vladimir Markoa1de9182016-02-25 11:37:38 +00002437 uint32_t GetPackedFields() const {
2438 return packed_fields_;
2439 }
2440
2441 template <size_t flag>
2442 bool GetPackedFlag() const {
2443 return (packed_fields_ & (1u << flag)) != 0u;
2444 }
2445
2446 template <size_t flag>
2447 void SetPackedFlag(bool value = true) {
2448 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2449 }
2450
2451 template <typename BitFieldType>
2452 typename BitFieldType::value_type GetPackedField() const {
2453 return BitFieldType::Decode(packed_fields_);
2454 }
2455
2456 template <typename BitFieldType>
2457 void SetPackedField(typename BitFieldType::value_type value) {
2458 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2459 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2460 }
2461
Artem Serovcced8ba2017-07-19 18:18:09 +01002462 // Copy construction for the instruction (used for Clone function).
2463 //
2464 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2465 // prepare_for_register_allocator are not copied (set to default values).
2466 //
2467 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2468 // fine for most of them. However for some of the instructions a custom copy constructor must be
2469 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2470 // for copying them).
2471 explicit HInstruction(const HInstruction& other)
2472 : previous_(nullptr),
2473 next_(nullptr),
2474 block_(nullptr),
2475 dex_pc_(other.dex_pc_),
2476 id_(-1),
2477 ssa_index_(-1),
2478 packed_fields_(other.packed_fields_),
2479 environment_(nullptr),
2480 locations_(nullptr),
2481 live_interval_(nullptr),
2482 lifetime_position_(kNoLifetime),
2483 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302484 reference_type_handle_(other.reference_type_handle_) {
2485 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002486
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002487 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302488 using InstructionKindField =
2489 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2490
Vladimir Marko46817b82016-03-29 12:21:58 +01002491 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2492 auto before_use_node = uses_.before_begin();
2493 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2494 HInstruction* user = use_node->GetUser();
2495 size_t input_index = use_node->GetIndex();
2496 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2497 before_use_node = use_node;
2498 }
2499 }
2500
2501 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2502 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2503 if (next != uses_.end()) {
2504 HInstruction* next_user = next->GetUser();
2505 size_t next_index = next->GetIndex();
2506 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2507 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2508 }
2509 }
2510
2511 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2512 auto before_env_use_node = env_uses_.before_begin();
2513 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2514 HEnvironment* user = env_use_node->GetUser();
2515 size_t input_index = env_use_node->GetIndex();
2516 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2517 before_env_use_node = env_use_node;
2518 }
2519 }
2520
2521 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2522 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2523 if (next != env_uses_.end()) {
2524 HEnvironment* next_user = next->GetUser();
2525 size_t next_index = next->GetIndex();
2526 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2527 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2528 }
2529 }
David Brazdil1abb4192015-02-17 18:33:36 +00002530
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002531 HInstruction* previous_;
2532 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002533 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002534 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002535
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002536 // An instruction gets an id when it is added to the graph.
2537 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002538 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002539 int id_;
2540
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002541 // When doing liveness analysis, instructions that have uses get an SSA index.
2542 int ssa_index_;
2543
Vladimir Markoa1de9182016-02-25 11:37:38 +00002544 // Packed fields.
2545 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002546
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002547 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002548 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002549
2550 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002551 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002552
Nicolas Geoffray39468442014-09-02 15:17:15 +01002553 // The environment associated with this instruction. Not null if the instruction
2554 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002555 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002556
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002557 // Set by the code generator.
2558 LocationSummary* locations_;
2559
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002560 // Set by the liveness analysis.
2561 LiveInterval* live_interval_;
2562
2563 // Set by the liveness analysis, this is the position in a linear
2564 // order of blocks where this instruction's live interval start.
2565 size_t lifetime_position_;
2566
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002567 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002568
Vladimir Markoa1de9182016-02-25 11:37:38 +00002569 // The reference handle part of the reference type info.
2570 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002571 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002572 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002573
David Brazdil1abb4192015-02-17 18:33:36 +00002574 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002575 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002576 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002577 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002578 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002579};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002580std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002581
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002582// Iterates over the instructions, while preserving the next instruction
2583// in case the current instruction gets removed from the list by the user
2584// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002585class HInstructionIterator : public ValueObject {
2586 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002587 explicit HInstructionIterator(const HInstructionList& instructions)
2588 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002589 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002590 }
2591
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002592 bool Done() const { return instruction_ == nullptr; }
2593 HInstruction* Current() const { return instruction_; }
2594 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002595 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002596 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002597 }
2598
2599 private:
2600 HInstruction* instruction_;
2601 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002602
2603 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002604};
2605
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002606// Iterates over the instructions without saving the next instruction,
2607// therefore handling changes in the graph potentially made by the user
2608// of this iterator.
2609class HInstructionIteratorHandleChanges : public ValueObject {
2610 public:
2611 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2612 : instruction_(instructions.first_instruction_) {
2613 }
2614
2615 bool Done() const { return instruction_ == nullptr; }
2616 HInstruction* Current() const { return instruction_; }
2617 void Advance() {
2618 instruction_ = instruction_->GetNext();
2619 }
2620
2621 private:
2622 HInstruction* instruction_;
2623
2624 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2625};
2626
2627
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002628class HBackwardInstructionIterator : public ValueObject {
2629 public:
2630 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2631 : instruction_(instructions.last_instruction_) {
2632 next_ = Done() ? nullptr : instruction_->GetPrevious();
2633 }
2634
2635 bool Done() const { return instruction_ == nullptr; }
2636 HInstruction* Current() const { return instruction_; }
2637 void Advance() {
2638 instruction_ = next_;
2639 next_ = Done() ? nullptr : instruction_->GetPrevious();
2640 }
2641
2642 private:
2643 HInstruction* instruction_;
2644 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002645
2646 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002647};
2648
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002649class HVariableInputSizeInstruction : public HInstruction {
2650 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002651 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002652 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002653 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2654 }
2655
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002656 void AddInput(HInstruction* input);
2657 void InsertInputAt(size_t index, HInstruction* input);
2658 void RemoveInputAt(size_t index);
2659
Igor Murashkind01745e2017-04-05 16:40:31 -07002660 // Removes all the inputs.
2661 // Also removes this instructions from each input's use list
2662 // (for non-environment uses only).
2663 void RemoveAllInputs();
2664
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002665 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302666 HVariableInputSizeInstruction(InstructionKind inst_kind,
2667 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002668 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002669 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002670 size_t number_of_inputs,
2671 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302672 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002673 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002674 HVariableInputSizeInstruction(InstructionKind inst_kind,
2675 DataType::Type type,
2676 SideEffects side_effects,
2677 uint32_t dex_pc,
2678 ArenaAllocator* allocator,
2679 size_t number_of_inputs,
2680 ArenaAllocKind kind)
2681 : HInstruction(inst_kind, type, side_effects, dex_pc),
2682 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002683
Artem Serovcced8ba2017-07-19 18:18:09 +01002684 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002685
Artem Serovcced8ba2017-07-19 18:18:09 +01002686 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002687};
2688
Vladimir Markof9f64412015-09-02 14:05:49 +01002689template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002690class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002691 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002692 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302693 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002694 HExpression<N>(InstructionKind kind,
2695 DataType::Type type,
2696 SideEffects side_effects,
2697 uint32_t dex_pc)
2698 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2699 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002700
Vladimir Marko372f10e2016-05-17 16:30:10 +01002701 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002702 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002703 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002704 }
2705
Artem Serovcced8ba2017-07-19 18:18:09 +01002706 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002707 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002708
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002709 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002710 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002711
2712 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002713};
2714
Vladimir Markobd785672018-05-03 17:09:09 +01002715// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002716template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002717class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002718 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002719 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720
Vladimir Markobd785672018-05-03 17:09:09 +01002721 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002722
Vladimir Marko372f10e2016-05-17 16:30:10 +01002723 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002724 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002725 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002726 }
2727
Artem Serovcced8ba2017-07-19 18:18:09 +01002728 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002729 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002730
Vladimir Markof9f64412015-09-02 14:05:49 +01002731 private:
2732 friend class SsaBuilder;
2733};
2734
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002735// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2736// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002737class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002738 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002739 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002740 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302741 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002742
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002743 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002744
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002745 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002746
Artem Serovcced8ba2017-07-19 18:18:09 +01002747 protected:
2748 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002749};
2750
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002751// Represents dex's RETURN opcodes. A HReturn is a control flow
2752// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002753class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002754 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002755 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002756 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002757 SetRawInputAt(0, value);
2758 }
2759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002760 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002761
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002762 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002763
Artem Serovcced8ba2017-07-19 18:18:09 +01002764 protected:
2765 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002766};
2767
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002768class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002769 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002770 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002771 uint32_t reg_number,
2772 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002773 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002774 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002775 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302776 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002777 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002778 SideEffects::None(),
2779 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002780 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002781 number_of_inputs,
2782 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002783 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002784 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002785 // Phis are constructed live and marked dead if conflicting or unused.
2786 // Individual steps of SsaBuilder should assume that if a phi has been
2787 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2788 SetPackedFlag<kFlagIsLive>(true);
2789 SetPackedFlag<kFlagCanBeNull>(true);
2790 }
2791
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002792 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002793
David Brazdildee58d62016-04-07 09:54:26 +00002794 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002795 static DataType::Type ToPhiType(DataType::Type type) {
2796 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002797 }
2798
2799 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2800
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002802 // Make sure that only valid type changes occur. The following are allowed:
2803 // (1) int -> float/ref (primitive type propagation),
2804 // (2) long -> double (primitive type propagation).
2805 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002806 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2807 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2808 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002809 SetPackedField<TypeField>(new_type);
2810 }
2811
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002812 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002813 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2814
2815 uint32_t GetRegNumber() const { return reg_number_; }
2816
2817 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2818 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2819 bool IsDead() const { return !IsLive(); }
2820 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2821
Vladimir Markoe9004912016-06-16 16:50:52 +01002822 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002823 return other != nullptr
2824 && other->IsPhi()
2825 && other->AsPhi()->GetBlock() == GetBlock()
2826 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2827 }
2828
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002829 bool HasEquivalentPhi() const {
2830 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2831 return true;
2832 }
2833 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2834 return true;
2835 }
2836 return false;
2837 }
2838
David Brazdildee58d62016-04-07 09:54:26 +00002839 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2840 // An equivalent phi is a phi having the same dex register and type.
2841 // It assumes that phis with the same dex register are adjacent.
2842 HPhi* GetNextEquivalentPhiWithSameType() {
2843 HInstruction* next = GetNext();
2844 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2845 if (next->GetType() == GetType()) {
2846 return next->AsPhi();
2847 }
2848 next = next->GetNext();
2849 }
2850 return nullptr;
2851 }
2852
2853 DECLARE_INSTRUCTION(Phi);
2854
Artem Serovcced8ba2017-07-19 18:18:09 +01002855 protected:
2856 DEFAULT_COPY_CONSTRUCTOR(Phi);
2857
David Brazdildee58d62016-04-07 09:54:26 +00002858 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002859 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002860 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2861 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2862 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002863
David Brazdildee58d62016-04-07 09:54:26 +00002864 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002865};
2866
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002867// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002868// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002869// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002870class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002871 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302872 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002873 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302874 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002876 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002877
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002878 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002879
Artem Serovcced8ba2017-07-19 18:18:09 +01002880 protected:
2881 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002882};
2883
2884// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002885class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002886 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302887 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002888 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302889 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002890
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002891 bool IsClonable() const override { return true; }
2892 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002893
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002894 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002895 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002896 }
2897
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002898 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002899
Artem Serovcced8ba2017-07-19 18:18:09 +01002900 protected:
2901 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002902};
2903
Roland Levillain9867bc72015-08-05 10:21:34 +01002904class HConstant : public HExpression<0> {
2905 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302906 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2907 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2908 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002910 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002911
Roland Levillain1a653882016-03-18 18:05:57 +00002912 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002914 // Is this constant 0 in the arithmetic sense?
2915 virtual bool IsArithmeticZero() const { return false; }
2916 // Is this constant a 0-bit pattern?
2917 virtual bool IsZeroBitPattern() const { return false; }
2918 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002919 virtual bool IsOne() const { return false; }
2920
David Brazdil77a48ae2015-09-15 12:34:04 +00002921 virtual uint64_t GetValueAsUint64() const = 0;
2922
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002923 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002924
Artem Serovcced8ba2017-07-19 18:18:09 +01002925 protected:
2926 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002927};
2928
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002929class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002930 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002931 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002932 return true;
2933 }
2934
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002935 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002937 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002938
Roland Levillain1a653882016-03-18 18:05:57 +00002939 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002940 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002941
Roland Levillain9867bc72015-08-05 10:21:34 +01002942 DECLARE_INSTRUCTION(NullConstant);
2943
Artem Serovcced8ba2017-07-19 18:18:09 +01002944 protected:
2945 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2946
Roland Levillain9867bc72015-08-05 10:21:34 +01002947 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002948 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302949 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2950 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002951
2952 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002953};
2954
2955// Constants of the type int. Those can be from Dex instructions, or
2956// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002957class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002958 public:
2959 int32_t GetValue() const { return value_; }
2960
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002961 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002962 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2963 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002964
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002965 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002966 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002967 return other->AsIntConstant()->value_ == value_;
2968 }
2969
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002970 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002971
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002972 bool IsMinusOne() const override { return GetValue() == -1; }
2973 bool IsArithmeticZero() const override { return GetValue() == 0; }
2974 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2975 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002976
Roland Levillain1a653882016-03-18 18:05:57 +00002977 // Integer constants are used to encode Boolean values as well,
2978 // where 1 means true and 0 means false.
2979 bool IsTrue() const { return GetValue() == 1; }
2980 bool IsFalse() const { return GetValue() == 0; }
2981
Roland Levillain9867bc72015-08-05 10:21:34 +01002982 DECLARE_INSTRUCTION(IntConstant);
2983
Artem Serovcced8ba2017-07-19 18:18:09 +01002984 protected:
2985 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2986
Roland Levillain9867bc72015-08-05 10:21:34 +01002987 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002988 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302989 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2990 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002991 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302992 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2993 value_(value ? 1 : 0) {
2994 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002995
2996 const int32_t value_;
2997
2998 friend class HGraph;
2999 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3000 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003001};
3002
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003003class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003004 public:
3005 int64_t GetValue() const { return value_; }
3006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003007 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003009 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003010 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003011 return other->AsLongConstant()->value_ == value_;
3012 }
3013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003014 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003015
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003016 bool IsMinusOne() const override { return GetValue() == -1; }
3017 bool IsArithmeticZero() const override { return GetValue() == 0; }
3018 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3019 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003020
3021 DECLARE_INSTRUCTION(LongConstant);
3022
Artem Serovcced8ba2017-07-19 18:18:09 +01003023 protected:
3024 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3025
Roland Levillain9867bc72015-08-05 10:21:34 +01003026 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003027 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303028 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3029 value_(value) {
3030 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003031
3032 const int64_t value_;
3033
3034 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003035};
Dave Allison20dfc792014-06-16 20:44:29 -07003036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003037class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003038 public:
3039 float GetValue() const { return value_; }
3040
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003041 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003042 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3043 }
3044
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003045 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003046 DCHECK(other->IsFloatConstant()) << other->DebugName();
3047 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3048 }
3049
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003050 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003051
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003052 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003053 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3054 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003055 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003056 return std::fpclassify(value_) == FP_ZERO;
3057 }
3058 bool IsArithmeticPositiveZero() const {
3059 return IsArithmeticZero() && !std::signbit(value_);
3060 }
3061 bool IsArithmeticNegativeZero() const {
3062 return IsArithmeticZero() && std::signbit(value_);
3063 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003064 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003065 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003067 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3069 }
3070 bool IsNaN() const {
3071 return std::isnan(value_);
3072 }
3073
3074 DECLARE_INSTRUCTION(FloatConstant);
3075
Artem Serovcced8ba2017-07-19 18:18:09 +01003076 protected:
3077 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3078
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 private:
3080 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303081 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3082 value_(value) {
3083 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003084 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303085 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3086 value_(bit_cast<float, int32_t>(value)) {
3087 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003088
3089 const float value_;
3090
3091 // Only the SsaBuilder and HGraph can create floating-point constants.
3092 friend class SsaBuilder;
3093 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094};
3095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003096class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003097 public:
3098 double GetValue() const { return value_; }
3099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003100 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003101
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003102 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003103 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3104 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3105 }
3106
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003107 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003108
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003109 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3111 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003112 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003113 return std::fpclassify(value_) == FP_ZERO;
3114 }
3115 bool IsArithmeticPositiveZero() const {
3116 return IsArithmeticZero() && !std::signbit(value_);
3117 }
3118 bool IsArithmeticNegativeZero() const {
3119 return IsArithmeticZero() && std::signbit(value_);
3120 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003121 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003122 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003123 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003124 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003125 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3126 }
3127 bool IsNaN() const {
3128 return std::isnan(value_);
3129 }
3130
3131 DECLARE_INSTRUCTION(DoubleConstant);
3132
Artem Serovcced8ba2017-07-19 18:18:09 +01003133 protected:
3134 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3135
Roland Levillain31dd3d62016-02-16 12:21:02 +00003136 private:
3137 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303138 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3139 value_(value) {
3140 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003141 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303142 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3143 value_(bit_cast<double, int64_t>(value)) {
3144 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003145
3146 const double value_;
3147
3148 // Only the SsaBuilder and HGraph can create floating-point constants.
3149 friend class SsaBuilder;
3150 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003151};
3152
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003153// Conditional branch. A block ending with an HIf instruction must have
3154// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003155class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003157 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003158 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003159 SetRawInputAt(0, input);
3160 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003162 bool IsClonable() const override { return true; }
3163 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003164
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003165 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003166 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003167 }
3168
3169 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003170 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003171 }
3172
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003173 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003174
Artem Serovcced8ba2017-07-19 18:18:09 +01003175 protected:
3176 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003177};
3178
David Brazdilfc6a86a2015-06-26 10:33:45 +00003179
3180// Abstract instruction which marks the beginning and/or end of a try block and
3181// links it to the respective exception handlers. Behaves the same as a Goto in
3182// non-exceptional control flow.
3183// Normal-flow successor is stored at index zero, exception handlers under
3184// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003185class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003186 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003187 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003188 kEntry,
3189 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003190 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003191 };
3192
Artem Serovd1aa7d02018-06-22 11:35:46 +01003193 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3194 // across the catch block entering edges as GC might happen during throwing an exception.
3195 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3196 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003197 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003198 : HExpression(kTryBoundary,
3199 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3200 : SideEffects::None(),
3201 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003202 SetPackedField<BoundaryKindField>(kind);
3203 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003204
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003205 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003206
3207 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003208 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003209
David Brazdild26a4112015-11-10 11:07:31 +00003210 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3211 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3212 }
3213
David Brazdilfc6a86a2015-06-26 10:33:45 +00003214 // Returns whether `handler` is among its exception handlers (non-zero index
3215 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003216 bool HasExceptionHandler(const HBasicBlock& handler) const {
3217 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003218 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003219 }
3220
3221 // If not present already, adds `handler` to its block's list of exception
3222 // handlers.
3223 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003224 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003225 GetBlock()->AddSuccessor(handler);
3226 }
3227 }
3228
Vladimir Markoa1de9182016-02-25 11:37:38 +00003229 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3230 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003231
David Brazdilffee3d32015-07-06 11:48:53 +01003232 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3233
David Brazdilfc6a86a2015-06-26 10:33:45 +00003234 DECLARE_INSTRUCTION(TryBoundary);
3235
Artem Serovcced8ba2017-07-19 18:18:09 +01003236 protected:
3237 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3238
David Brazdilfc6a86a2015-06-26 10:33:45 +00003239 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003240 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3241 static constexpr size_t kFieldBoundaryKindSize =
3242 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3243 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3244 kFieldBoundaryKind + kFieldBoundaryKindSize;
3245 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3246 "Too many packed fields.");
3247 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003248};
3249
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003250// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003251class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003252 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003253 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3254 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003255 HDeoptimize(ArenaAllocator* allocator,
3256 HInstruction* cond,
3257 DeoptimizationKind kind,
3258 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003259 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303260 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003261 SideEffects::All(),
3262 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003263 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003264 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003265 kArenaAllocMisc) {
3266 SetPackedFlag<kFieldCanBeMoved>(false);
3267 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003268 SetRawInputAt(0, cond);
3269 }
3270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003271 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003272
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003273 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3274 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3275 // instead of `guard`.
3276 // We set CanTriggerGC to prevent any intermediate address to be live
3277 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003278 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003279 HInstruction* cond,
3280 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003281 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003282 uint32_t dex_pc)
3283 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303284 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003285 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003286 SideEffects::CanTriggerGC(),
3287 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003288 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003289 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003290 kArenaAllocMisc) {
3291 SetPackedFlag<kFieldCanBeMoved>(true);
3292 SetPackedField<DeoptimizeKindField>(kind);
3293 SetRawInputAt(0, cond);
3294 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003295 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003297 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003299 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003300 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3301 }
3302
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003303 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003304
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003305 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003306
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003307 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003308
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003309 bool GuardsAnInput() const {
3310 return InputCount() == 2;
3311 }
3312
3313 HInstruction* GuardedInput() const {
3314 DCHECK(GuardsAnInput());
3315 return InputAt(1);
3316 }
3317
3318 void RemoveGuard() {
3319 RemoveInputAt(1);
3320 }
3321
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003322 DECLARE_INSTRUCTION(Deoptimize);
3323
Artem Serovcced8ba2017-07-19 18:18:09 +01003324 protected:
3325 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3326
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003327 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003328 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3329 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3330 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003331 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003332 static constexpr size_t kNumberOfDeoptimizePackedBits =
3333 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3334 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3335 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003336 using DeoptimizeKindField =
3337 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003338};
3339
Mingyao Yang063fc772016-08-02 11:02:54 -07003340// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3341// The compiled code checks this flag value in a guard before devirtualized call and
3342// if it's true, starts to do deoptimization.
3343// It has a 4-byte slot on stack.
3344// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003345class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003346 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003347 // CHA guards are only optimized in a separate pass and it has no side effects
3348 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003349 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303350 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003351 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303352 SideEffects::None(),
3353 dex_pc,
3354 allocator,
3355 0,
3356 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003357 }
3358
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003359 // We do all CHA guard elimination/motion in a single pass, after which there is no
3360 // further guard elimination/motion since a guard might have been used for justification
3361 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3362 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003363 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003364
3365 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3366
Artem Serovcced8ba2017-07-19 18:18:09 +01003367 protected:
3368 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003369};
3370
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003371// Represents the ArtMethod that was passed as a first argument to
3372// the method. It is used by instructions that depend on it, like
3373// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003374class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003375 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003376 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303377 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3378 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003379
3380 DECLARE_INSTRUCTION(CurrentMethod);
3381
Artem Serovcced8ba2017-07-19 18:18:09 +01003382 protected:
3383 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003384};
3385
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003386// Fetches an ArtMethod from the virtual table or the interface method table
3387// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003388class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003389 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003390 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003391 kVTable,
3392 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003393 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003394 };
3395 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003396 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003397 TableKind kind,
3398 size_t index,
3399 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303400 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003401 index_(index) {
3402 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003403 SetRawInputAt(0, cls);
3404 }
3405
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003406 bool IsClonable() const override { return true; }
3407 bool CanBeMoved() const override { return true; }
3408 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003409 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003410 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003411 }
3412
Vladimir Markoa1de9182016-02-25 11:37:38 +00003413 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003414 size_t GetIndex() const { return index_; }
3415
3416 DECLARE_INSTRUCTION(ClassTableGet);
3417
Artem Serovcced8ba2017-07-19 18:18:09 +01003418 protected:
3419 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3420
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003421 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003422 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003423 static constexpr size_t kFieldTableKindSize =
3424 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3425 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3426 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3427 "Too many packed fields.");
3428 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3429
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003430 // The index of the ArtMethod in the table.
3431 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003432};
3433
Mark Mendellfe57faa2015-09-18 09:26:15 -04003434// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3435// have one successor for each entry in the switch table, and the final successor
3436// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003437class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003438 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003439 HPackedSwitch(int32_t start_value,
3440 uint32_t num_entries,
3441 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003442 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003443 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003444 start_value_(start_value),
3445 num_entries_(num_entries) {
3446 SetRawInputAt(0, input);
3447 }
3448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003449 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003450
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003451 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003452
3453 int32_t GetStartValue() const { return start_value_; }
3454
Vladimir Marko211c2112015-09-24 16:52:33 +01003455 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003456
3457 HBasicBlock* GetDefaultBlock() const {
3458 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003459 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003460 }
3461 DECLARE_INSTRUCTION(PackedSwitch);
3462
Artem Serovcced8ba2017-07-19 18:18:09 +01003463 protected:
3464 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3465
Mark Mendellfe57faa2015-09-18 09:26:15 -04003466 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003467 const int32_t start_value_;
3468 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003469};
3470
Roland Levillain88cb1752014-10-20 16:36:47 +01003471class HUnaryOperation : public HExpression<1> {
3472 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303473 HUnaryOperation(InstructionKind kind,
3474 DataType::Type result_type,
3475 HInstruction* input,
3476 uint32_t dex_pc = kNoDexPc)
3477 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003478 SetRawInputAt(0, input);
3479 }
3480
Artem Serovcced8ba2017-07-19 18:18:09 +01003481 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003482 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003483
Roland Levillain88cb1752014-10-20 16:36:47 +01003484 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003485 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003486
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003487 bool CanBeMoved() const override { return true; }
3488 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003489 return true;
3490 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003491
Roland Levillain31dd3d62016-02-16 12:21:02 +00003492 // Try to statically evaluate `this` and return a HConstant
3493 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003494 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003495 HConstant* TryStaticEvaluation() const;
3496
3497 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003498 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3499 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3501 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003502
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003503 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003504
Artem Serovcced8ba2017-07-19 18:18:09 +01003505 protected:
3506 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003507};
3508
Dave Allison20dfc792014-06-16 20:44:29 -07003509class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003510 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303511 HBinaryOperation(InstructionKind kind,
3512 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003513 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003514 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003515 SideEffects side_effects = SideEffects::None(),
3516 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303517 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003518 SetRawInputAt(0, left);
3519 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003520 }
3521
Artem Serovcced8ba2017-07-19 18:18:09 +01003522 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003523 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003524
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003525 HInstruction* GetLeft() const { return InputAt(0); }
3526 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003527 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003528
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003529 virtual bool IsCommutative() const { return false; }
3530
3531 // Put constant on the right.
3532 // Returns whether order is changed.
3533 bool OrderInputsWithConstantOnTheRight() {
3534 HInstruction* left = InputAt(0);
3535 HInstruction* right = InputAt(1);
3536 if (left->IsConstant() && !right->IsConstant()) {
3537 ReplaceInput(right, 0);
3538 ReplaceInput(left, 1);
3539 return true;
3540 }
3541 return false;
3542 }
3543
3544 // Order inputs by instruction id, but favor constant on the right side.
3545 // This helps GVN for commutative ops.
3546 void OrderInputs() {
3547 DCHECK(IsCommutative());
3548 HInstruction* left = InputAt(0);
3549 HInstruction* right = InputAt(1);
3550 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3551 return;
3552 }
3553 if (OrderInputsWithConstantOnTheRight()) {
3554 return;
3555 }
3556 // Order according to instruction id.
3557 if (left->GetId() > right->GetId()) {
3558 ReplaceInput(right, 0);
3559 ReplaceInput(left, 1);
3560 }
3561 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003563 bool CanBeMoved() const override { return true; }
3564 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003565 return true;
3566 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003567
Roland Levillain31dd3d62016-02-16 12:21:02 +00003568 // Try to statically evaluate `this` and return a HConstant
3569 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003570 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003571 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003572
3573 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003574 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3575 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003576 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3577 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003578 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003579 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3580 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003581 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3582 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003583 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3584 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003585 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3587 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003588
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003589 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003590 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003591 HConstant* GetConstantRight() const;
3592
3593 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003594 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003595 HInstruction* GetLeastConstantLeft() const;
3596
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003597 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003598
Artem Serovcced8ba2017-07-19 18:18:09 +01003599 protected:
3600 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003601};
3602
Mark Mendellc4701932015-04-10 13:18:51 -04003603// The comparison bias applies for floating point operations and indicates how NaN
3604// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003605enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003606 kNoBias, // bias is not applicable (i.e. for long operation)
3607 kGtBias, // return 1 for NaN comparisons
3608 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003609 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003610};
3611
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3613
Dave Allison20dfc792014-06-16 20:44:29 -07003614class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003615 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303616 HCondition(InstructionKind kind,
3617 HInstruction* first,
3618 HInstruction* second,
3619 uint32_t dex_pc = kNoDexPc)
3620 : HBinaryOperation(kind,
3621 DataType::Type::kBool,
3622 first,
3623 second,
3624 SideEffects::None(),
3625 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003626 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3627 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003628
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003629 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003630 // `instruction`, and disregard moves in between.
3631 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003632
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003633 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003634
3635 virtual IfCondition GetCondition() const = 0;
3636
Mark Mendellc4701932015-04-10 13:18:51 -04003637 virtual IfCondition GetOppositeCondition() const = 0;
3638
Vladimir Markoa1de9182016-02-25 11:37:38 +00003639 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003640 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3641
Vladimir Markoa1de9182016-02-25 11:37:38 +00003642 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3643 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003644
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003645 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003646 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003647 }
3648
Roland Levillain4fa13f62015-07-06 18:11:54 +01003649 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003650 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003651 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003652 if (if_cond == kCondNE) {
3653 return true;
3654 } else if (if_cond == kCondEQ) {
3655 return false;
3656 }
3657 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003658 }
3659
3660 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003661 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003662 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003663 if (if_cond == kCondEQ) {
3664 return true;
3665 } else if (if_cond == kCondNE) {
3666 return false;
3667 }
3668 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003669 }
3670
Roland Levillain31dd3d62016-02-16 12:21:02 +00003671 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003672 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003673 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003674 static constexpr size_t kFieldComparisonBiasSize =
3675 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3676 static constexpr size_t kNumberOfConditionPackedBits =
3677 kFieldComparisonBias + kFieldComparisonBiasSize;
3678 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3679 using ComparisonBiasField =
3680 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3681
Roland Levillain31dd3d62016-02-16 12:21:02 +00003682 template <typename T>
3683 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3684
3685 template <typename T>
3686 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003688 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3689 // Handle the bias.
3690 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3691 }
3692
3693 // Return an integer constant containing the result of a condition evaluated at compile time.
3694 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3695 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3696 }
3697
Artem Serovcced8ba2017-07-19 18:18:09 +01003698 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003699};
3700
3701// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003702class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003703 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003704 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303705 : HCondition(kEqual, first, second, dex_pc) {
3706 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003707
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003708 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003709
Vladimir Marko9e23df52015-11-10 17:14:35 +00003710 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003711 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003712 return MakeConstantCondition(true, GetDexPc());
3713 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003714 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003715 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3716 }
3717 // In the following Evaluate methods, a HCompare instruction has
3718 // been merged into this HEqual instruction; evaluate it as
3719 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003720 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003721 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3722 GetDexPc());
3723 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003724 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003725 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3726 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003727 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003729 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003730
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003731 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003732
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003733 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003734 return kCondEQ;
3735 }
3736
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003737 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003738 return kCondNE;
3739 }
3740
Artem Serovcced8ba2017-07-19 18:18:09 +01003741 protected:
3742 DEFAULT_COPY_CONSTRUCTOR(Equal);
3743
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003744 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003745 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003746};
3747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003748class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003749 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303750 HNotEqual(HInstruction* first, HInstruction* second,
3751 uint32_t dex_pc = kNoDexPc)
3752 : HCondition(kNotEqual, first, second, dex_pc) {
3753 }
Dave Allison20dfc792014-06-16 20:44:29 -07003754
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003755 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003756
Vladimir Marko9e23df52015-11-10 17:14:35 +00003757 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003758 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003759 return MakeConstantCondition(false, GetDexPc());
3760 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003761 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003762 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3763 }
3764 // In the following Evaluate methods, a HCompare instruction has
3765 // been merged into this HNotEqual instruction; evaluate it as
3766 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003767 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003768 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3769 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003770 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003771 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3772 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003773 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003774 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003775 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003776
Dave Allison20dfc792014-06-16 20:44:29 -07003777 DECLARE_INSTRUCTION(NotEqual);
3778
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003779 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003780 return kCondNE;
3781 }
3782
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003783 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003784 return kCondEQ;
3785 }
3786
Artem Serovcced8ba2017-07-19 18:18:09 +01003787 protected:
3788 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3789
Dave Allison20dfc792014-06-16 20:44:29 -07003790 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003791 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003792};
3793
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003794class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003795 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303796 HLessThan(HInstruction* first, HInstruction* second,
3797 uint32_t dex_pc = kNoDexPc)
3798 : HCondition(kLessThan, first, second, dex_pc) {
3799 }
Dave Allison20dfc792014-06-16 20:44:29 -07003800
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003801 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003802 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003803 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003804 // In the following Evaluate methods, a HCompare instruction has
3805 // been merged into this HLessThan instruction; evaluate it as
3806 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003807 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003808 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3809 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003810 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003811 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3812 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003813 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003814 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003815 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003816
Dave Allison20dfc792014-06-16 20:44:29 -07003817 DECLARE_INSTRUCTION(LessThan);
3818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003819 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003820 return kCondLT;
3821 }
3822
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003823 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003824 return kCondGE;
3825 }
3826
Artem Serovcced8ba2017-07-19 18:18:09 +01003827 protected:
3828 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3829
Dave Allison20dfc792014-06-16 20:44:29 -07003830 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003831 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003832};
3833
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003834class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003835 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303836 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3837 uint32_t dex_pc = kNoDexPc)
3838 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3839 }
Dave Allison20dfc792014-06-16 20:44:29 -07003840
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003841 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003842 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003843 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003844 // In the following Evaluate methods, a HCompare instruction has
3845 // been merged into this HLessThanOrEqual instruction; evaluate it as
3846 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003847 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003848 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3849 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003850 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003851 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3852 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003853 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003854 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003855 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003856
Dave Allison20dfc792014-06-16 20:44:29 -07003857 DECLARE_INSTRUCTION(LessThanOrEqual);
3858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003859 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003860 return kCondLE;
3861 }
3862
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003863 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003864 return kCondGT;
3865 }
3866
Artem Serovcced8ba2017-07-19 18:18:09 +01003867 protected:
3868 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3869
Dave Allison20dfc792014-06-16 20:44:29 -07003870 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003871 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003872};
3873
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003874class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003875 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003876 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303877 : HCondition(kGreaterThan, first, second, dex_pc) {
3878 }
Dave Allison20dfc792014-06-16 20:44:29 -07003879
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003881 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003882 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003883 // In the following Evaluate methods, a HCompare instruction has
3884 // been merged into this HGreaterThan instruction; evaluate it as
3885 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003886 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003887 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3888 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003889 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003890 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3891 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003892 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003893 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003894 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003895
Dave Allison20dfc792014-06-16 20:44:29 -07003896 DECLARE_INSTRUCTION(GreaterThan);
3897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003898 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003899 return kCondGT;
3900 }
3901
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003902 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003903 return kCondLE;
3904 }
3905
Artem Serovcced8ba2017-07-19 18:18:09 +01003906 protected:
3907 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3908
Dave Allison20dfc792014-06-16 20:44:29 -07003909 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003910 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003911};
3912
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003913class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003914 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003915 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303916 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3917 }
Dave Allison20dfc792014-06-16 20:44:29 -07003918
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003919 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003920 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003921 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003922 // In the following Evaluate methods, a HCompare instruction has
3923 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3924 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003925 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003926 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3927 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003928 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003929 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3930 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003931 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003932 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003933 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003934
Dave Allison20dfc792014-06-16 20:44:29 -07003935 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003937 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003938 return kCondGE;
3939 }
3940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003941 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003942 return kCondLT;
3943 }
3944
Artem Serovcced8ba2017-07-19 18:18:09 +01003945 protected:
3946 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3947
Dave Allison20dfc792014-06-16 20:44:29 -07003948 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003949 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003950};
3951
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003952class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003953 public:
3954 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303955 : HCondition(kBelow, first, second, dex_pc) {
3956 }
Aart Bike9f37602015-10-09 11:15:55 -07003957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003958 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003959 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003960 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003961 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003962 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3963 }
3964 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003965 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003966 LOG(FATAL) << DebugName() << " is not defined for float values";
3967 UNREACHABLE();
3968 }
3969 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003970 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003971 LOG(FATAL) << DebugName() << " is not defined for double values";
3972 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003973 }
3974
3975 DECLARE_INSTRUCTION(Below);
3976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003977 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003978 return kCondB;
3979 }
3980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003981 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003982 return kCondAE;
3983 }
3984
Artem Serovcced8ba2017-07-19 18:18:09 +01003985 protected:
3986 DEFAULT_COPY_CONSTRUCTOR(Below);
3987
Aart Bike9f37602015-10-09 11:15:55 -07003988 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003989 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003990 return MakeUnsigned(x) < MakeUnsigned(y);
3991 }
Aart Bike9f37602015-10-09 11:15:55 -07003992};
3993
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003994class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003995 public:
3996 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303997 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3998 }
Aart Bike9f37602015-10-09 11:15:55 -07003999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004000 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004001 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004002 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004003 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004004 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4005 }
4006 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004007 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004008 LOG(FATAL) << DebugName() << " is not defined for float values";
4009 UNREACHABLE();
4010 }
4011 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004012 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004013 LOG(FATAL) << DebugName() << " is not defined for double values";
4014 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004015 }
4016
4017 DECLARE_INSTRUCTION(BelowOrEqual);
4018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004019 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004020 return kCondBE;
4021 }
4022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004023 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004024 return kCondA;
4025 }
4026
Artem Serovcced8ba2017-07-19 18:18:09 +01004027 protected:
4028 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4029
Aart Bike9f37602015-10-09 11:15:55 -07004030 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004031 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004032 return MakeUnsigned(x) <= MakeUnsigned(y);
4033 }
Aart Bike9f37602015-10-09 11:15:55 -07004034};
4035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004036class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004037 public:
4038 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304039 : HCondition(kAbove, first, second, dex_pc) {
4040 }
Aart Bike9f37602015-10-09 11:15:55 -07004041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004042 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004043 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004044 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004045 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004046 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4047 }
4048 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004049 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004050 LOG(FATAL) << DebugName() << " is not defined for float values";
4051 UNREACHABLE();
4052 }
4053 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004054 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004055 LOG(FATAL) << DebugName() << " is not defined for double values";
4056 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004057 }
4058
4059 DECLARE_INSTRUCTION(Above);
4060
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004061 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004062 return kCondA;
4063 }
4064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004065 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004066 return kCondBE;
4067 }
4068
Artem Serovcced8ba2017-07-19 18:18:09 +01004069 protected:
4070 DEFAULT_COPY_CONSTRUCTOR(Above);
4071
Aart Bike9f37602015-10-09 11:15:55 -07004072 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004073 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004074 return MakeUnsigned(x) > MakeUnsigned(y);
4075 }
Aart Bike9f37602015-10-09 11:15:55 -07004076};
4077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004078class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004079 public:
4080 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304081 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4082 }
Aart Bike9f37602015-10-09 11:15:55 -07004083
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004084 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004085 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004086 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004087 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004088 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4089 }
4090 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004091 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004092 LOG(FATAL) << DebugName() << " is not defined for float values";
4093 UNREACHABLE();
4094 }
4095 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004096 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004097 LOG(FATAL) << DebugName() << " is not defined for double values";
4098 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004099 }
4100
4101 DECLARE_INSTRUCTION(AboveOrEqual);
4102
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004103 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004104 return kCondAE;
4105 }
4106
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004107 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004108 return kCondB;
4109 }
4110
Artem Serovcced8ba2017-07-19 18:18:09 +01004111 protected:
4112 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4113
Aart Bike9f37602015-10-09 11:15:55 -07004114 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004115 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004116 return MakeUnsigned(x) >= MakeUnsigned(y);
4117 }
Aart Bike9f37602015-10-09 11:15:55 -07004118};
Dave Allison20dfc792014-06-16 20:44:29 -07004119
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004120// Instruction to check how two inputs compare to each other.
4121// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004122class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004123 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004124 // Note that `comparison_type` is the type of comparison performed
4125 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004126 // HCompare instruction (which is always DataType::Type::kInt).
4127 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004128 HInstruction* first,
4129 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004130 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004131 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304132 : HBinaryOperation(kCompare,
4133 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004134 first,
4135 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004136 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004137 dex_pc) {
4138 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004139 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4140 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004141 }
4142
Roland Levillain9867bc72015-08-05 10:21:34 +01004143 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004144 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4145
4146 template <typename T>
4147 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004148 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004149 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4150 // Handle the bias.
4151 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4152 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004153
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004154 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004155 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4156 // reach this code path when processing a freshly built HIR
4157 // graph. However HCompare integer instructions can be synthesized
4158 // by the instruction simplifier to implement IntegerCompare and
4159 // IntegerSignum intrinsics, so we have to handle this case.
4160 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004161 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004162 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004163 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4164 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004165 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004166 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4167 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004168 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004169 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004170 }
4171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004172 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004173 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004174 }
4175
Vladimir Markoa1de9182016-02-25 11:37:38 +00004176 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004177
Roland Levillain31dd3d62016-02-16 12:21:02 +00004178 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004179 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004180 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004181 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004182 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004183 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004184
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004185 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004186 // Comparisons do not require a runtime call in any back end.
4187 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004188 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004189
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004190 DECLARE_INSTRUCTION(Compare);
4191
Roland Levillain31dd3d62016-02-16 12:21:02 +00004192 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004193 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004194 static constexpr size_t kFieldComparisonBiasSize =
4195 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4196 static constexpr size_t kNumberOfComparePackedBits =
4197 kFieldComparisonBias + kFieldComparisonBiasSize;
4198 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4199 using ComparisonBiasField =
4200 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4201
Roland Levillain31dd3d62016-02-16 12:21:02 +00004202 // Return an integer constant containing the result of a comparison evaluated at compile time.
4203 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4204 DCHECK(value == -1 || value == 0 || value == 1) << value;
4205 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4206 }
4207
Artem Serovcced8ba2017-07-19 18:18:09 +01004208 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004209};
4210
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004211class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004212 public:
4213 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004214 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004215 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004216 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004217 bool finalizable,
4218 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304219 : HExpression(kNewInstance,
4220 DataType::Type::kReference,
4221 SideEffects::CanTriggerGC(),
4222 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004223 type_index_(type_index),
4224 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004225 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004226 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004227 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004228 }
4229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004230 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004231
Andreas Gampea5b09a62016-11-17 15:21:22 -08004232 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004233 const DexFile& GetDexFile() const { return dex_file_; }
4234
4235 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004236 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004237
Mingyao Yang062157f2016-03-02 10:15:36 -08004238 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004239 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004240
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004241 bool NeedsChecks() const {
4242 return entrypoint_ == kQuickAllocObjectWithChecks;
4243 }
David Brazdil6de19382016-01-08 17:37:10 +00004244
Vladimir Markoa1de9182016-02-25 11:37:38 +00004245 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004247 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004248
4249 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4250
4251 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4252 entrypoint_ = entrypoint;
4253 }
4254
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004255 HLoadClass* GetLoadClass() const {
4256 HInstruction* input = InputAt(0);
4257 if (input->IsClinitCheck()) {
4258 input = input->InputAt(0);
4259 }
4260 DCHECK(input->IsLoadClass());
4261 return input->AsLoadClass();
4262 }
4263
David Brazdil6de19382016-01-08 17:37:10 +00004264 bool IsStringAlloc() const;
4265
4266 DECLARE_INSTRUCTION(NewInstance);
4267
Artem Serovcced8ba2017-07-19 18:18:09 +01004268 protected:
4269 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4270
David Brazdil6de19382016-01-08 17:37:10 +00004271 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004272 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004273 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4274 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4275 "Too many packed fields.");
4276
Andreas Gampea5b09a62016-11-17 15:21:22 -08004277 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004278 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004279 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004280};
4281
Agi Csaki05f20562015-08-19 14:58:14 -07004282enum IntrinsicNeedsEnvironmentOrCache {
4283 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4284 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004285};
4286
Aart Bik5d75afe2015-12-14 11:57:01 -08004287enum IntrinsicSideEffects {
4288 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4289 kReadSideEffects, // Intrinsic may read heap memory.
4290 kWriteSideEffects, // Intrinsic may write heap memory.
4291 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4292};
4293
4294enum IntrinsicExceptions {
4295 kNoThrow, // Intrinsic does not throw any exceptions.
4296 kCanThrow // Intrinsic may throw exceptions.
4297};
4298
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004299class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004300 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004301 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004302
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004303 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004304 SetRawInputAt(index, argument);
4305 }
4306
Roland Levillain3e3d7332015-04-28 11:00:54 +01004307 // Return the number of arguments. This number can be lower than
4308 // the number of inputs returned by InputCount(), as some invoke
4309 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4310 // inputs at the end of their list of inputs.
4311 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4312
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004313 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4314
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004315 InvokeType GetInvokeType() const {
4316 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004317 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004318
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004319 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004320 return intrinsic_;
4321 }
4322
Aart Bik5d75afe2015-12-14 11:57:01 -08004323 void SetIntrinsic(Intrinsics intrinsic,
4324 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4325 IntrinsicSideEffects side_effects,
4326 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004327
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004328 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004329 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004330 }
4331
Aart Bikff7d89c2016-11-07 08:49:28 -08004332 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004334 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004335
Aart Bika8b8e9b2018-01-09 11:01:02 -08004336 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4337
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004338 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004339
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004340 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004341
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004342 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004343 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4344 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004345
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004346 uint32_t* GetIntrinsicOptimizations() {
4347 return &intrinsic_optimizations_;
4348 }
4349
4350 const uint32_t* GetIntrinsicOptimizations() const {
4351 return &intrinsic_optimizations_;
4352 }
4353
4354 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4355
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004356 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004357 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004358
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004359 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004360
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004361 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004362 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4363 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004364 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004365 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004366 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4367 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004368 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004369 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004370
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304371 HInvoke(InstructionKind kind,
4372 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004373 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004374 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004375 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004376 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004377 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004378 ArtMethod* resolved_method,
4379 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004380 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304381 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004382 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004383 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4384 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004385 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004386 number_of_arguments + number_of_other_inputs,
4387 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004388 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004389 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004390 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004391 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004392 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004393 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004394 // Check mutator lock, constructors lack annotalysis support.
4395 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4396 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004397 }
4398
Artem Serovcced8ba2017-07-19 18:18:09 +01004399 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4400
Roland Levillain3e3d7332015-04-28 11:00:54 +01004401 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004402 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004403 const uint32_t dex_method_index_;
4404 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004405
4406 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4407 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004408};
4409
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004410class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004411 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004412 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004413 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004414 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004415 uint32_t dex_pc,
4416 uint32_t dex_method_index,
4417 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304418 : HInvoke(kInvokeUnresolved,
4419 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004420 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004421 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004422 return_type,
4423 dex_pc,
4424 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004425 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004426 invoke_type) {
4427 }
4428
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004429 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004430
Calin Juravle175dc732015-08-25 15:42:32 +01004431 DECLARE_INSTRUCTION(InvokeUnresolved);
4432
Artem Serovcced8ba2017-07-19 18:18:09 +01004433 protected:
4434 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004435};
4436
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004437class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004438 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004439 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004440 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004441 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004442 uint32_t dex_pc,
4443 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304444 : HInvoke(kInvokePolymorphic,
4445 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004446 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004447 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004448 return_type,
4449 dex_pc,
4450 dex_method_index,
4451 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304452 kVirtual) {
4453 }
Orion Hodsonac141392017-01-13 11:53:47 +00004454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004455 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004456
Orion Hodsonac141392017-01-13 11:53:47 +00004457 DECLARE_INSTRUCTION(InvokePolymorphic);
4458
Artem Serovcced8ba2017-07-19 18:18:09 +01004459 protected:
4460 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004461};
4462
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004463class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004464 public:
4465 HInvokeCustom(ArenaAllocator* allocator,
4466 uint32_t number_of_arguments,
4467 uint32_t call_site_index,
4468 DataType::Type return_type,
4469 uint32_t dex_pc)
4470 : HInvoke(kInvokeCustom,
4471 allocator,
4472 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004473 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004474 return_type,
4475 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004476 /* dex_method_index= */ dex::kDexNoIndex,
4477 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004478 kStatic),
4479 call_site_index_(call_site_index) {
4480 }
4481
4482 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4483
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004484 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004485
4486 DECLARE_INSTRUCTION(InvokeCustom);
4487
4488 protected:
4489 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4490
4491 private:
4492 uint32_t call_site_index_;
4493};
4494
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004495class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004496 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004497 // Requirements of this method call regarding the class
4498 // initialization (clinit) check of its declaring class.
4499 enum class ClinitCheckRequirement {
4500 kNone, // Class already initialized.
4501 kExplicit, // Static call having explicit clinit check as last input.
4502 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004503 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004504 };
4505
Vladimir Marko58155012015-08-19 12:49:41 +00004506 // Determines how to load the target ArtMethod*.
4507 enum class MethodLoadKind {
4508 // Use a String init ArtMethod* loaded from Thread entrypoints.
4509 kStringInit,
4510
4511 // Use the method's own ArtMethod* loaded by the register allocator.
4512 kRecursive,
4513
Vladimir Marko65979462017-05-19 17:25:12 +01004514 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4515 // Used for boot image methods referenced by boot image code.
4516 kBootImageLinkTimePcRelative,
4517
Vladimir Markob066d432018-01-03 13:14:37 +00004518 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4519 // Used for app->boot calls with relocatable image.
4520 kBootImageRelRo,
4521
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004522 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004523 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004524 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004525
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004526 // Use ArtMethod* at a known address, embed the direct address in the code.
4527 // Used for for JIT-compiled calls.
4528 kJitDirectAddress,
4529
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004530 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4531 // used when other kinds are unimplemented on a particular architecture.
4532 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004533 };
4534
4535 // Determines the location of the code pointer.
4536 enum class CodePtrLocation {
4537 // Recursive call, use local PC-relative call instruction.
4538 kCallSelf,
4539
Vladimir Marko58155012015-08-19 12:49:41 +00004540 // Use code pointer from the ArtMethod*.
4541 // Used when we don't know the target code. This is also the last-resort-kind used when
4542 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4543 kCallArtMethod,
4544 };
4545
4546 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004547 MethodLoadKind method_load_kind;
4548 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004549 // The method load data holds
4550 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4551 // Note that there are multiple string init methods, each having its own offset.
4552 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004553 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004554 };
4555
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004556 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004557 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004558 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004559 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004560 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004561 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004562 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004563 InvokeType invoke_type,
4564 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004565 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304566 : HInvoke(kInvokeStaticOrDirect,
4567 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004568 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004569 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004570 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004571 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004572 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004573 return_type,
4574 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004575 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004576 resolved_method,
4577 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004578 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004579 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004580 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4581 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004583 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004584
Vladimir Markodc151b22015-10-15 18:02:30 +01004585 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004586 bool had_current_method_input = HasCurrentMethodInput();
4587 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4588
4589 // Using the current method is the default and once we find a better
4590 // method load kind, we should not go back to using the current method.
4591 DCHECK(had_current_method_input || !needs_current_method_input);
4592
4593 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004594 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4595 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004596 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004597 dispatch_info_ = dispatch_info;
4598 }
4599
Aart Bik6daebeb2017-04-03 14:35:41 -07004600 DispatchInfo GetDispatchInfo() const {
4601 return dispatch_info_;
4602 }
4603
Vladimir Markoc53c0792015-11-19 15:48:33 +00004604 void AddSpecialInput(HInstruction* input) {
4605 // We allow only one special input.
4606 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4607 DCHECK(InputCount() == GetSpecialInputIndex() ||
4608 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4609 InsertInputAt(GetSpecialInputIndex(), input);
4610 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004611
Vladimir Marko372f10e2016-05-17 16:30:10 +01004612 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004613 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004614 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4615 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4616 DCHECK(!input_records.empty());
4617 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4618 HInstruction* last_input = input_records.back().GetInstruction();
4619 // Note: `last_input` may be null during arguments setup.
4620 if (last_input != nullptr) {
4621 // `last_input` is the last input of a static invoke marked as having
4622 // an explicit clinit check. It must either be:
4623 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4624 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4625 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4626 }
4627 }
4628 return input_records;
4629 }
4630
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004631 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004632 // We access the method via the dex cache so we can't do an implicit null check.
4633 // TODO: for intrinsics we can generate implicit null checks.
4634 return false;
4635 }
4636
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004637 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004638 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004639 }
4640
Vladimir Markoc53c0792015-11-19 15:48:33 +00004641 // Get the index of the special input, if any.
4642 //
David Brazdil6de19382016-01-08 17:37:10 +00004643 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4644 // method pointer; otherwise there may be one platform-specific special input,
4645 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004646 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004647 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004648
Vladimir Marko58155012015-08-19 12:49:41 +00004649 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4650 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4651 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004652 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004653 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004654 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004655 bool HasPcRelativeMethodLoadKind() const {
4656 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004657 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004658 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004659 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004660 bool HasCurrentMethodInput() const {
4661 // This function can be called only after the invoke has been fully initialized by the builder.
4662 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004663 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004664 return true;
4665 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004666 DCHECK(InputCount() == GetSpecialInputIndex() ||
4667 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004668 return false;
4669 }
4670 }
Vladimir Marko58155012015-08-19 12:49:41 +00004671
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004672 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004673 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004674 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004675 }
4676
4677 uint64_t GetMethodAddress() const {
4678 DCHECK(HasMethodAddress());
4679 return dispatch_info_.method_load_data;
4680 }
4681
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004682 const DexFile& GetDexFileForPcRelativeDexCache() const;
4683
Vladimir Markoa1de9182016-02-25 11:37:38 +00004684 ClinitCheckRequirement GetClinitCheckRequirement() const {
4685 return GetPackedField<ClinitCheckRequirementField>();
4686 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004687
Roland Levillain4c0eb422015-04-24 16:43:49 +01004688 // Is this instruction a call to a static method?
4689 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004690 return GetInvokeType() == kStatic;
4691 }
4692
4693 MethodReference GetTargetMethod() const {
4694 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004695 }
4696
Vladimir Markofbb184a2015-11-13 14:47:00 +00004697 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4698 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4699 // instruction; only relevant for static calls with explicit clinit check.
4700 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004701 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004702 size_t last_input_index = inputs_.size() - 1u;
4703 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004704 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004705 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004706 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004707 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004708 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004709 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004710 }
4711
4712 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004713 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004714 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004715 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004716 }
4717
4718 // Is this a call to a static method whose declaring class has an
4719 // implicit intialization check requirement?
4720 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004721 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004722 }
4723
Vladimir Markob554b5a2015-11-06 12:57:55 +00004724 // Does this method load kind need the current method as an input?
4725 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004726 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004727 }
4728
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004729 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004730
Artem Serovcced8ba2017-07-19 18:18:09 +01004731 protected:
4732 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4733
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004734 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004735 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004736 static constexpr size_t kFieldClinitCheckRequirementSize =
4737 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4738 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4739 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4740 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4741 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004742 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4743 kFieldClinitCheckRequirement,
4744 kFieldClinitCheckRequirementSize>;
4745
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004746 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004747 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004748 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004749};
Vladimir Markof64242a2015-12-01 14:58:23 +00004750std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004751std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004752
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004753class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004754 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004755 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004756 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004757 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004758 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004759 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004760 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004761 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304762 : HInvoke(kInvokeVirtual,
4763 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004764 number_of_arguments,
4765 0u,
4766 return_type,
4767 dex_pc,
4768 dex_method_index,
4769 resolved_method,
4770 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304771 vtable_index_(vtable_index) {
4772 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004774 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004775
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004776 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004777 switch (GetIntrinsic()) {
4778 case Intrinsics::kThreadCurrentThread:
4779 case Intrinsics::kStringBufferAppend:
4780 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004781 case Intrinsics::kStringBuilderAppendObject:
4782 case Intrinsics::kStringBuilderAppendString:
4783 case Intrinsics::kStringBuilderAppendCharSequence:
4784 case Intrinsics::kStringBuilderAppendCharArray:
4785 case Intrinsics::kStringBuilderAppendBoolean:
4786 case Intrinsics::kStringBuilderAppendChar:
4787 case Intrinsics::kStringBuilderAppendInt:
4788 case Intrinsics::kStringBuilderAppendLong:
4789 case Intrinsics::kStringBuilderAppendFloat:
4790 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004791 case Intrinsics::kStringBuilderToString:
4792 return false;
4793 default:
4794 return HInvoke::CanBeNull();
4795 }
4796 }
4797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004798 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004799 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004800 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004801 }
4802
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004803 uint32_t GetVTableIndex() const { return vtable_index_; }
4804
4805 DECLARE_INSTRUCTION(InvokeVirtual);
4806
Artem Serovcced8ba2017-07-19 18:18:09 +01004807 protected:
4808 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4809
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004810 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004811 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004812 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004813};
4814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004815class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004816 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004817 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004818 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004819 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004820 uint32_t dex_pc,
4821 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004822 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004823 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304824 : HInvoke(kInvokeInterface,
4825 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004826 number_of_arguments,
4827 0u,
4828 return_type,
4829 dex_pc,
4830 dex_method_index,
4831 resolved_method,
4832 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304833 imt_index_(imt_index) {
4834 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004835
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004836 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004837
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004838 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004839 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004840 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004841 }
4842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004843 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004844 // The assembly stub currently needs it.
4845 return true;
4846 }
4847
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004848 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004849
4850 DECLARE_INSTRUCTION(InvokeInterface);
4851
Artem Serovcced8ba2017-07-19 18:18:09 +01004852 protected:
4853 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4854
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004855 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004856 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004857 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004858};
4859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004860class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004861 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004862 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304863 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004864 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004865 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004866
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004867 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004868
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004869 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004870 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004871 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004872 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004873 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004874 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004875 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004876 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4877 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004878 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004879 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4880 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004881
Roland Levillain88cb1752014-10-20 16:36:47 +01004882 DECLARE_INSTRUCTION(Neg);
4883
Artem Serovcced8ba2017-07-19 18:18:09 +01004884 protected:
4885 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004886};
4887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004888class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004889 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004890 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304891 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004892 SetRawInputAt(0, cls);
4893 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004894 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004895 }
4896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004897 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004898
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004899 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004900 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004901
Mingyao Yang0c365e62015-03-31 15:09:29 -07004902 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004903 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004905 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004906
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004907 HLoadClass* GetLoadClass() const {
4908 DCHECK(InputAt(0)->IsLoadClass());
4909 return InputAt(0)->AsLoadClass();
4910 }
4911
4912 HInstruction* GetLength() const {
4913 return InputAt(1);
4914 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004915
Vladimir Markob5461632018-10-15 14:24:21 +01004916 size_t GetComponentSizeShift() {
4917 return GetPackedField<ComponentSizeShiftField>();
4918 }
4919
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004920 DECLARE_INSTRUCTION(NewArray);
4921
Artem Serovcced8ba2017-07-19 18:18:09 +01004922 protected:
4923 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004924
4925 private:
4926 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4927 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4928 static constexpr size_t kNumberOfNewArrayPackedBits =
4929 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4930 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4931 using ComponentSizeShiftField =
4932 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004933};
4934
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004935class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004936 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004937 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004938 HInstruction* left,
4939 HInstruction* right,
4940 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304941 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4942 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004943
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004944 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004945
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004946 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004947
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004948 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004949 return GetBlock()->GetGraph()->GetIntConstant(
4950 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004951 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004952 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004953 return GetBlock()->GetGraph()->GetLongConstant(
4954 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004955 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004956 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004957 return GetBlock()->GetGraph()->GetFloatConstant(
4958 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4959 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004960 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004961 return GetBlock()->GetGraph()->GetDoubleConstant(
4962 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4963 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004964
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004965 DECLARE_INSTRUCTION(Add);
4966
Artem Serovcced8ba2017-07-19 18:18:09 +01004967 protected:
4968 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004969};
4970
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004971class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004972 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004973 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004974 HInstruction* left,
4975 HInstruction* right,
4976 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304977 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4978 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004979
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004980 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004982 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004983 return GetBlock()->GetGraph()->GetIntConstant(
4984 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004985 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004986 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004987 return GetBlock()->GetGraph()->GetLongConstant(
4988 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004989 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004990 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004991 return GetBlock()->GetGraph()->GetFloatConstant(
4992 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4993 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004994 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004995 return GetBlock()->GetGraph()->GetDoubleConstant(
4996 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4997 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004998
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004999 DECLARE_INSTRUCTION(Sub);
5000
Artem Serovcced8ba2017-07-19 18:18:09 +01005001 protected:
5002 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005003};
5004
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005005class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005006 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005007 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005008 HInstruction* left,
5009 HInstruction* right,
5010 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305011 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5012 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005014 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005015
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005016 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005017
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005018 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005019 return GetBlock()->GetGraph()->GetIntConstant(
5020 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005021 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005022 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005023 return GetBlock()->GetGraph()->GetLongConstant(
5024 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005025 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005026 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005027 return GetBlock()->GetGraph()->GetFloatConstant(
5028 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5029 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005030 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005031 return GetBlock()->GetGraph()->GetDoubleConstant(
5032 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5033 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005034
5035 DECLARE_INSTRUCTION(Mul);
5036
Artem Serovcced8ba2017-07-19 18:18:09 +01005037 protected:
5038 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005039};
5040
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005041class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005042 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005043 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005044 HInstruction* left,
5045 HInstruction* right,
5046 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305047 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5048 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005049
Roland Levillain9867bc72015-08-05 10:21:34 +01005050 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005051 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005052 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005053 // Our graph structure ensures we never have 0 for `y` during
5054 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005055 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005056 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005057 return (y == -1) ? -x : x / y;
5058 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005059
Roland Levillain31dd3d62016-02-16 12:21:02 +00005060 template <typename T>
5061 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005062 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005063 return x / y;
5064 }
5065
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005066 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005067 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005068 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005069 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005070 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005071 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005072 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5073 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005074 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005075 return GetBlock()->GetGraph()->GetFloatConstant(
5076 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5077 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005078 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005079 return GetBlock()->GetGraph()->GetDoubleConstant(
5080 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005081 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005082
5083 DECLARE_INSTRUCTION(Div);
5084
Artem Serovcced8ba2017-07-19 18:18:09 +01005085 protected:
5086 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005087};
5088
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005089class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005090 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005091 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005092 HInstruction* left,
5093 HInstruction* right,
5094 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305095 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5096 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005097
Roland Levillain9867bc72015-08-05 10:21:34 +01005098 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005099 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005100 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005101 // Our graph structure ensures we never have 0 for `y` during
5102 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005103 DCHECK_NE(y, 0);
5104 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5105 return (y == -1) ? 0 : x % y;
5106 }
5107
Roland Levillain31dd3d62016-02-16 12:21:02 +00005108 template <typename T>
5109 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005110 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005111 return std::fmod(x, y);
5112 }
5113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005114 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005115 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005116 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005117 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005118 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005119 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005120 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005121 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005122 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005123 return GetBlock()->GetGraph()->GetFloatConstant(
5124 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5125 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005126 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005127 return GetBlock()->GetGraph()->GetDoubleConstant(
5128 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5129 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005130
5131 DECLARE_INSTRUCTION(Rem);
5132
Artem Serovcced8ba2017-07-19 18:18:09 +01005133 protected:
5134 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005135};
5136
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005137class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005138 public:
5139 HMin(DataType::Type result_type,
5140 HInstruction* left,
5141 HInstruction* right,
5142 uint32_t dex_pc)
5143 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5144
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005145 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005146
5147 // Evaluation for integral values.
5148 template <typename T> static T ComputeIntegral(T x, T y) {
5149 return (x <= y) ? x : y;
5150 }
5151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005152 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005153 return GetBlock()->GetGraph()->GetIntConstant(
5154 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5155 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005156 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005157 return GetBlock()->GetGraph()->GetLongConstant(
5158 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5159 }
5160 // TODO: Evaluation for floating-point values.
5161 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005162 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005163 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005164 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005165
5166 DECLARE_INSTRUCTION(Min);
5167
5168 protected:
5169 DEFAULT_COPY_CONSTRUCTOR(Min);
5170};
5171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005172class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005173 public:
5174 HMax(DataType::Type result_type,
5175 HInstruction* left,
5176 HInstruction* right,
5177 uint32_t dex_pc)
5178 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005180 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005181
5182 // Evaluation for integral values.
5183 template <typename T> static T ComputeIntegral(T x, T y) {
5184 return (x >= y) ? x : y;
5185 }
5186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005187 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005188 return GetBlock()->GetGraph()->GetIntConstant(
5189 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5190 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005191 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005192 return GetBlock()->GetGraph()->GetLongConstant(
5193 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5194 }
5195 // TODO: Evaluation for floating-point values.
5196 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005197 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005198 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005199 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005200
5201 DECLARE_INSTRUCTION(Max);
5202
5203 protected:
5204 DEFAULT_COPY_CONSTRUCTOR(Max);
5205};
5206
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005207class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005208 public:
5209 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5210 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5211
5212 // Evaluation for integral values.
5213 template <typename T> static T ComputeIntegral(T x) {
5214 return x < 0 ? -x : x;
5215 }
5216
5217 // Evaluation for floating-point values.
5218 // Note, as a "quality of implementation", rather than pure "spec compliance",
5219 // we require that Math.abs() clears the sign bit (but changes nothing else)
5220 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5221 // http://b/30758343
5222 template <typename T, typename S> static T ComputeFP(T x) {
5223 S bits = bit_cast<S, T>(x);
5224 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5225 }
5226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005227 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005228 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5229 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005230 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005231 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5232 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005233 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005234 return GetBlock()->GetGraph()->GetFloatConstant(
5235 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5236 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005237 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005238 return GetBlock()->GetGraph()->GetDoubleConstant(
5239 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5240 }
5241
5242 DECLARE_INSTRUCTION(Abs);
5243
5244 protected:
5245 DEFAULT_COPY_CONSTRUCTOR(Abs);
5246};
5247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005248class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005249 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005250 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005251 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5252 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005253 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005254 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005255 SetRawInputAt(0, value);
5256 }
5257
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005258 bool IsClonable() const override { return true; }
5259 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005260
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005261 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005262 return true;
5263 }
5264
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005265 bool NeedsEnvironment() const override { return true; }
5266 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005267
Calin Juravled0d48522014-11-04 16:40:20 +00005268 DECLARE_INSTRUCTION(DivZeroCheck);
5269
Artem Serovcced8ba2017-07-19 18:18:09 +01005270 protected:
5271 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005272};
5273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005274class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005275 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005276 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005277 HInstruction* value,
5278 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005279 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305280 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005281 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5282 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005283 }
5284
Roland Levillain5b5b9312016-03-22 14:57:31 +00005285 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005286 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005287 return value << (distance & max_shift_distance);
5288 }
5289
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005290 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005291 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005292 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005293 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005294 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005295 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005296 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005297 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005298 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005299 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005300 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5301 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005302 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005303 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005304 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005305 LOG(FATAL) << DebugName() << " is not defined for float values";
5306 UNREACHABLE();
5307 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005308 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005309 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005310 LOG(FATAL) << DebugName() << " is not defined for double values";
5311 UNREACHABLE();
5312 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005313
5314 DECLARE_INSTRUCTION(Shl);
5315
Artem Serovcced8ba2017-07-19 18:18:09 +01005316 protected:
5317 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005318};
5319
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005320class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005321 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005322 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005323 HInstruction* value,
5324 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005325 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305326 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005327 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5328 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005329 }
5330
Roland Levillain5b5b9312016-03-22 14:57:31 +00005331 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005332 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005333 return value >> (distance & max_shift_distance);
5334 }
5335
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005336 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005337 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005338 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005339 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005340 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005341 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005342 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005343 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005344 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005345 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005346 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5347 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005348 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005349 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005350 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005351 LOG(FATAL) << DebugName() << " is not defined for float values";
5352 UNREACHABLE();
5353 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005354 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005355 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005356 LOG(FATAL) << DebugName() << " is not defined for double values";
5357 UNREACHABLE();
5358 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005359
5360 DECLARE_INSTRUCTION(Shr);
5361
Artem Serovcced8ba2017-07-19 18:18:09 +01005362 protected:
5363 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005364};
5365
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005366class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005367 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005368 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005369 HInstruction* value,
5370 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005371 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305372 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005373 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5374 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005375 }
5376
Roland Levillain5b5b9312016-03-22 14:57:31 +00005377 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005378 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005379 typedef typename std::make_unsigned<T>::type V;
5380 V ux = static_cast<V>(value);
5381 return static_cast<T>(ux >> (distance & max_shift_distance));
5382 }
5383
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005384 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005385 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005386 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005387 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005388 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005389 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005390 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005391 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005392 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005393 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005394 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5395 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005396 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005397 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005398 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005399 LOG(FATAL) << DebugName() << " is not defined for float values";
5400 UNREACHABLE();
5401 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005402 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005403 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005404 LOG(FATAL) << DebugName() << " is not defined for double values";
5405 UNREACHABLE();
5406 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005407
5408 DECLARE_INSTRUCTION(UShr);
5409
Artem Serovcced8ba2017-07-19 18:18:09 +01005410 protected:
5411 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005412};
5413
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005414class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005415 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005416 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005417 HInstruction* left,
5418 HInstruction* right,
5419 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305420 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5421 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005423 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005424
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005425 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005426
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005427 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005428 return GetBlock()->GetGraph()->GetIntConstant(
5429 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005430 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005431 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005432 return GetBlock()->GetGraph()->GetLongConstant(
5433 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005434 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005435 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005436 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005437 LOG(FATAL) << DebugName() << " is not defined for float values";
5438 UNREACHABLE();
5439 }
5440 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005441 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005442 LOG(FATAL) << DebugName() << " is not defined for double values";
5443 UNREACHABLE();
5444 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005445
5446 DECLARE_INSTRUCTION(And);
5447
Artem Serovcced8ba2017-07-19 18:18:09 +01005448 protected:
5449 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005450};
5451
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005452class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005453 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005454 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005455 HInstruction* left,
5456 HInstruction* right,
5457 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305458 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5459 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005461 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005462
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005463 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005465 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005466 return GetBlock()->GetGraph()->GetIntConstant(
5467 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005468 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005469 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005470 return GetBlock()->GetGraph()->GetLongConstant(
5471 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005472 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005473 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005474 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005475 LOG(FATAL) << DebugName() << " is not defined for float values";
5476 UNREACHABLE();
5477 }
5478 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005480 LOG(FATAL) << DebugName() << " is not defined for double values";
5481 UNREACHABLE();
5482 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005483
5484 DECLARE_INSTRUCTION(Or);
5485
Artem Serovcced8ba2017-07-19 18:18:09 +01005486 protected:
5487 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005488};
5489
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005490class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005491 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005492 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005493 HInstruction* left,
5494 HInstruction* right,
5495 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305496 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5497 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005498
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005499 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005500
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005501 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005502
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005503 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005504 return GetBlock()->GetGraph()->GetIntConstant(
5505 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005506 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005507 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005508 return GetBlock()->GetGraph()->GetLongConstant(
5509 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005510 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005511 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005512 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005513 LOG(FATAL) << DebugName() << " is not defined for float values";
5514 UNREACHABLE();
5515 }
5516 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005517 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005518 LOG(FATAL) << DebugName() << " is not defined for double values";
5519 UNREACHABLE();
5520 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005521
5522 DECLARE_INSTRUCTION(Xor);
5523
Artem Serovcced8ba2017-07-19 18:18:09 +01005524 protected:
5525 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005526};
5527
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005528class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005529 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305531 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005532 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5533 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005534 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005535
Roland Levillain5b5b9312016-03-22 14:57:31 +00005536 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005537 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005538 typedef typename std::make_unsigned<T>::type V;
5539 V ux = static_cast<V>(value);
5540 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005541 return static_cast<T>(ux);
5542 } else {
5543 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005544 return static_cast<T>(ux >> (distance & max_shift_value)) |
5545 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005546 }
5547 }
5548
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005549 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005550 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005551 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005552 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005553 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005554 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005555 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005556 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005557 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005558 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005559 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5560 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005561 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005562 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005563 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005564 LOG(FATAL) << DebugName() << " is not defined for float values";
5565 UNREACHABLE();
5566 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005567 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005568 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005569 LOG(FATAL) << DebugName() << " is not defined for double values";
5570 UNREACHABLE();
5571 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005572
5573 DECLARE_INSTRUCTION(Ror);
5574
Artem Serovcced8ba2017-07-19 18:18:09 +01005575 protected:
5576 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005577};
5578
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005579// The value of a parameter in this method. Its location depends on
5580// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005581class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005582 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005583 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005584 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005585 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005586 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005587 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305588 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005589 dex_file_(dex_file),
5590 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005591 index_(index) {
5592 SetPackedFlag<kFlagIsThis>(is_this);
5593 SetPackedFlag<kFlagCanBeNull>(!is_this);
5594 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005595
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005596 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005597 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005598 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005599 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005600
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005601 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005602 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005603
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005604 DECLARE_INSTRUCTION(ParameterValue);
5605
Artem Serovcced8ba2017-07-19 18:18:09 +01005606 protected:
5607 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5608
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005609 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005610 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005611 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005612 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5613 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5614 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5615 "Too many packed fields.");
5616
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005617 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005618 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005619 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005620 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005621 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005622};
5623
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005624class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005625 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005626 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305627 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5628 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005629
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005630 bool CanBeMoved() const override { return true; }
5631 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005632 return true;
5633 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005634
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005635 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005636
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005637 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005638 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005639 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005640 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005641 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005642 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005643 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005644 LOG(FATAL) << DebugName() << " is not defined for float values";
5645 UNREACHABLE();
5646 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005647 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005648 LOG(FATAL) << DebugName() << " is not defined for double values";
5649 UNREACHABLE();
5650 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005651
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005652 DECLARE_INSTRUCTION(Not);
5653
Artem Serovcced8ba2017-07-19 18:18:09 +01005654 protected:
5655 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005656};
5657
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005658class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005659 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005660 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305661 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5662 }
David Brazdil66d126e2015-04-03 16:02:44 +01005663
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005664 bool CanBeMoved() const override { return true; }
5665 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005666 return true;
5667 }
5668
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005669 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005670 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005671 return !x;
5672 }
5673
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005674 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005675 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005676 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005677 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005678 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005679 UNREACHABLE();
5680 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005681 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005682 LOG(FATAL) << DebugName() << " is not defined for float values";
5683 UNREACHABLE();
5684 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005685 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005686 LOG(FATAL) << DebugName() << " is not defined for double values";
5687 UNREACHABLE();
5688 }
David Brazdil66d126e2015-04-03 16:02:44 +01005689
5690 DECLARE_INSTRUCTION(BooleanNot);
5691
Artem Serovcced8ba2017-07-19 18:18:09 +01005692 protected:
5693 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005694};
5695
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005696class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005697 public:
5698 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005699 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305700 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005701 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005702 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005703 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005704 }
5705
5706 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005707 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5708 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005709
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005710 bool IsClonable() const override { return true; }
5711 bool CanBeMoved() const override { return true; }
5712 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005713 return true;
5714 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005715 // Return whether the conversion is implicit. This includes conversion to the same type.
5716 bool IsImplicitConversion() const {
5717 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5718 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005719
Mark Mendelle82549b2015-05-06 10:55:34 -04005720 // Try to statically evaluate the conversion and return a HConstant
5721 // containing the result. If the input cannot be converted, return nullptr.
5722 HConstant* TryStaticEvaluation() const;
5723
Roland Levillaindff1f282014-11-05 14:15:05 +00005724 DECLARE_INSTRUCTION(TypeConversion);
5725
Artem Serovcced8ba2017-07-19 18:18:09 +01005726 protected:
5727 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005728};
5729
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005730static constexpr uint32_t kNoRegNumber = -1;
5731
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005732class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005733 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005734 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005735 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5736 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005737 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005738 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005739 SetRawInputAt(0, value);
5740 }
5741
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005742 bool IsClonable() const override { return true; }
5743 bool CanBeMoved() const override { return true; }
5744 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005745 return true;
5746 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005748 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005750 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005752 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005753
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005754 DECLARE_INSTRUCTION(NullCheck);
5755
Artem Serovcced8ba2017-07-19 18:18:09 +01005756 protected:
5757 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005758};
5759
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005760// Embeds an ArtField and all the information required by the compiler. We cache
5761// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005762class FieldInfo : public ValueObject {
5763 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005764 FieldInfo(ArtField* field,
5765 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005766 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005767 bool is_volatile,
5768 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005769 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005770 const DexFile& dex_file)
5771 : field_(field),
5772 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005773 field_type_(field_type),
5774 is_volatile_(is_volatile),
5775 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005776 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005777 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005778
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005779 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005780 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005782 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005783 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005784 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005785 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005786
5787 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005788 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005789 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005790 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005791 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005792 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005793 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005794 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005795};
5796
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005797class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005798 public:
5799 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005800 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005801 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005802 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005803 bool is_volatile,
5804 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005805 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005806 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005807 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305808 : HExpression(kInstanceFieldGet,
5809 field_type,
5810 SideEffects::FieldReadOfType(field_type, is_volatile),
5811 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005812 field_info_(field,
5813 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005814 field_type,
5815 is_volatile,
5816 field_idx,
5817 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005818 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005819 SetRawInputAt(0, value);
5820 }
5821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005822 bool IsClonable() const override { return true; }
5823 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005825 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005826 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005827 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005828 }
5829
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005830 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005831 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005832 }
5833
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005834 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005835 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5836 }
5837
Calin Juravle52c48962014-12-16 17:02:57 +00005838 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005839 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005841 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005842
Vladimir Marko61b92282017-10-11 13:23:17 +01005843 void SetType(DataType::Type new_type) {
5844 DCHECK(DataType::IsIntegralType(GetType()));
5845 DCHECK(DataType::IsIntegralType(new_type));
5846 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5847 SetPackedField<TypeField>(new_type);
5848 }
5849
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005850 DECLARE_INSTRUCTION(InstanceFieldGet);
5851
Artem Serovcced8ba2017-07-19 18:18:09 +01005852 protected:
5853 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5854
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005855 private:
5856 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005857};
5858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005859class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005860 public:
5861 HInstanceFieldSet(HInstruction* object,
5862 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005863 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005864 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005865 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005866 bool is_volatile,
5867 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005868 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005869 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005870 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005871 : HExpression(kInstanceFieldSet,
5872 SideEffects::FieldWriteOfType(field_type, is_volatile),
5873 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005874 field_info_(field,
5875 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005876 field_type,
5877 is_volatile,
5878 field_idx,
5879 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005880 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005881 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005882 SetRawInputAt(0, object);
5883 SetRawInputAt(1, value);
5884 }
5885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005886 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005888 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005889 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005890 }
5891
Calin Juravle52c48962014-12-16 17:02:57 +00005892 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005893 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005895 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005896 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005897 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5898 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005899
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005900 DECLARE_INSTRUCTION(InstanceFieldSet);
5901
Artem Serovcced8ba2017-07-19 18:18:09 +01005902 protected:
5903 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5904
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005905 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005906 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5907 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5908 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5909 "Too many packed fields.");
5910
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005911 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005912};
5913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005914class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005915 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005916 HArrayGet(HInstruction* array,
5917 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005918 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005919 uint32_t dex_pc)
5920 : HArrayGet(array,
5921 index,
5922 type,
5923 SideEffects::ArrayReadOfType(type),
5924 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005925 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305926 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005927
5928 HArrayGet(HInstruction* array,
5929 HInstruction* index,
5930 DataType::Type type,
5931 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005932 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005933 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305934 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005935 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005936 SetRawInputAt(0, array);
5937 SetRawInputAt(1, index);
5938 }
5939
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005940 bool IsClonable() const override { return true; }
5941 bool CanBeMoved() const override { return true; }
5942 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005943 return true;
5944 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005945 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005946 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005947 // Currently, unless the array is the result of NewArray, the array access is always
5948 // preceded by some form of null NullCheck necessary for the bounds check, usually
5949 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5950 // dynamic BCE. There are cases when these could be removed to produce better code.
5951 // If we ever add optimizations to do so we should allow an implicit check here
5952 // (as long as the address falls in the first page).
5953 //
5954 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5955 // a = cond ? new int[1] : null;
5956 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005957 return false;
5958 }
5959
David Brazdil4833f5a2015-12-16 10:37:39 +00005960 bool IsEquivalentOf(HArrayGet* other) const {
5961 bool result = (GetDexPc() == other->GetDexPc());
5962 if (kIsDebugBuild && result) {
5963 DCHECK_EQ(GetBlock(), other->GetBlock());
5964 DCHECK_EQ(GetArray(), other->GetArray());
5965 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005966 if (DataType::IsIntOrLongType(GetType())) {
5967 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005968 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005969 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5970 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005971 }
5972 }
5973 return result;
5974 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005975
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005976 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5977
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005978 HInstruction* GetArray() const { return InputAt(0); }
5979 HInstruction* GetIndex() const { return InputAt(1); }
5980
Vladimir Marko61b92282017-10-11 13:23:17 +01005981 void SetType(DataType::Type new_type) {
5982 DCHECK(DataType::IsIntegralType(GetType()));
5983 DCHECK(DataType::IsIntegralType(new_type));
5984 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5985 SetPackedField<TypeField>(new_type);
5986 }
5987
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005988 DECLARE_INSTRUCTION(ArrayGet);
5989
Artem Serovcced8ba2017-07-19 18:18:09 +01005990 protected:
5991 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5992
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005993 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005994 // We treat a String as an array, creating the HArrayGet from String.charAt()
5995 // intrinsic in the instruction simplifier. We can always determine whether
5996 // a particular HArrayGet is actually a String.charAt() by looking at the type
5997 // of the input but that requires holding the mutator lock, so we prefer to use
5998 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005999 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006000 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6001 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6002 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006003};
6004
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006005class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006006 public:
6007 HArraySet(HInstruction* array,
6008 HInstruction* index,
6009 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006010 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006011 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006012 : HArraySet(array,
6013 index,
6014 value,
6015 expected_component_type,
6016 // Make a best guess for side effects now, may be refined during SSA building.
6017 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306018 dex_pc) {
6019 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006020
6021 HArraySet(HInstruction* array,
6022 HInstruction* index,
6023 HInstruction* value,
6024 DataType::Type expected_component_type,
6025 SideEffects side_effects,
6026 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006027 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006028 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006029 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006030 SetPackedFlag<kFlagValueCanBeNull>(true);
6031 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006032 SetRawInputAt(0, array);
6033 SetRawInputAt(1, index);
6034 SetRawInputAt(2, value);
6035 }
6036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006037 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006038
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006039 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006040 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006041 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006042 }
6043
Mingyao Yang81014cb2015-06-02 03:16:27 -07006044 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006045 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006047 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006048 // TODO: Same as for ArrayGet.
6049 return false;
6050 }
6051
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006052 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006053 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006054 }
6055
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006056 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006057 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006058 }
6059
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006060 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006062 }
6063
Vladimir Markoa1de9182016-02-25 11:37:38 +00006064 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6065 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6066 bool StaticTypeOfArrayIsObjectArray() const {
6067 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6068 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006069
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006070 HInstruction* GetArray() const { return InputAt(0); }
6071 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006072 HInstruction* GetValue() const { return InputAt(2); }
6073
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006074 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006075 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6076 }
6077
6078 static DataType::Type GetComponentType(DataType::Type value_type,
6079 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006080 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006081 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006082 // be correct, we also check what is the value type. If it is a floating
6083 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006084 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006085 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006086 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006087 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006088
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006089 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006090 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006091 }
6092
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006093 static SideEffects ComputeSideEffects(DataType::Type type) {
6094 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006095 }
6096
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006097 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6098 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6099 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006100 }
6101
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006102 DECLARE_INSTRUCTION(ArraySet);
6103
Artem Serovcced8ba2017-07-19 18:18:09 +01006104 protected:
6105 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6106
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006107 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006108 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6109 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006110 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006111 static constexpr size_t kFlagNeedsTypeCheck =
6112 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6113 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006114 // Cached information for the reference_type_info_ so that codegen
6115 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6117 static constexpr size_t kNumberOfArraySetPackedBits =
6118 kFlagStaticTypeOfArrayIsObjectArray + 1;
6119 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6120 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006121 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006122};
6123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006124class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006125 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006126 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306127 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006128 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006129 // Note that arrays do not change length, so the instruction does not
6130 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006131 SetRawInputAt(0, array);
6132 }
6133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006134 bool IsClonable() const override { return true; }
6135 bool CanBeMoved() const override { return true; }
6136 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006137 return true;
6138 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006139 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006140 return obj == InputAt(0);
6141 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006142
Vladimir Markodce016e2016-04-28 13:10:02 +01006143 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6144
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006145 DECLARE_INSTRUCTION(ArrayLength);
6146
Artem Serovcced8ba2017-07-19 18:18:09 +01006147 protected:
6148 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6149
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006150 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006151 // We treat a String as an array, creating the HArrayLength from String.length()
6152 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6153 // determine whether a particular HArrayLength is actually a String.length() by
6154 // looking at the type of the input but that requires holding the mutator lock, so
6155 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006156 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006157 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6158 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6159 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006160};
6161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006162class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006163 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006164 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006165 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6166 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006167 HBoundsCheck(HInstruction* index,
6168 HInstruction* length,
6169 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006170 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006171 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006172 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006173 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006174 SetRawInputAt(0, index);
6175 SetRawInputAt(1, length);
6176 }
6177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006178 bool IsClonable() const override { return true; }
6179 bool CanBeMoved() const override { return true; }
6180 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006181 return true;
6182 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006183
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006184 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006185
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006186 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006187
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006188 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006189
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006190 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006191
6192 DECLARE_INSTRUCTION(BoundsCheck);
6193
Artem Serovcced8ba2017-07-19 18:18:09 +01006194 protected:
6195 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006197 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006198 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006199 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6200 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6201 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006202};
6203
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006204class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006205 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006206 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006207 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306208 slow_path_(nullptr) {
6209 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006210
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006211 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006213 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006214 return true;
6215 }
6216
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006217 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6218 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006219
6220 DECLARE_INSTRUCTION(SuspendCheck);
6221
Artem Serovcced8ba2017-07-19 18:18:09 +01006222 protected:
6223 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6224
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006225 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006226 // Only used for code generation, in order to share the same slow path between back edges
6227 // of a same loop.
6228 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006229};
6230
David Srbecky0cf44932015-12-09 14:09:59 +00006231// Pseudo-instruction which provides the native debugger with mapping information.
6232// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006233class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006234 public:
6235 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006236 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306237 }
David Srbecky0cf44932015-12-09 14:09:59 +00006238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006239 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006240 return true;
6241 }
6242
6243 DECLARE_INSTRUCTION(NativeDebugInfo);
6244
Artem Serovcced8ba2017-07-19 18:18:09 +01006245 protected:
6246 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006247};
6248
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006249/**
6250 * Instruction to load a Class object.
6251 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006252class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006253 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006254 // Determines how to load the Class.
6255 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006256 // We cannot load this class. See HSharpening::SharpenLoadClass.
6257 kInvalid = -1,
6258
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006259 // Use the Class* from the method's own ArtMethod*.
6260 kReferrersClass,
6261
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006262 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006263 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006264 kBootImageLinkTimePcRelative,
6265
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006266 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006267 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006268 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006269
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006270 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006271 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006272 kBssEntry,
6273
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006274 // Use a known boot image Class* address, embedded in the code by the codegen.
6275 // Used for boot image classes referenced by apps in JIT-compiled code.
6276 kJitBootImageAddress,
6277
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006278 // Load from the root table associated with the JIT compiled method.
6279 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006280
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006281 // Load using a simple runtime call. This is the fall-back load kind when
6282 // the codegen is unable to use another appropriate kind.
6283 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006284
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006285 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006286 };
6287
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006288 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006289 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006290 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006291 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006292 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006293 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006294 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006295 : HInstruction(kLoadClass,
6296 DataType::Type::kReference,
6297 SideEffectsForArchRuntimeCalls(),
6298 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006299 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006300 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006301 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006302 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006303 // Referrers class should not need access check. We never inline unverified
6304 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006305 DCHECK(!is_referrers_class || !needs_access_check);
6306
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006307 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006308 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006309 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006310 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006311 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006312 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006313 }
6314
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006315 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006316
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006317 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006318
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006319 LoadKind GetLoadKind() const {
6320 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006321 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006322
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006323 bool HasPcRelativeLoadKind() const {
6324 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6325 GetLoadKind() == LoadKind::kBootImageRelRo ||
6326 GetLoadKind() == LoadKind::kBssEntry;
6327 }
6328
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006329 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006330
Yi Kong39402542019-03-24 02:47:16 -07006331 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006332
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006333 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006334
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006335 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006336
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006337 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006338 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006339 }
6340
Calin Juravle0ba218d2015-05-19 18:46:01 +01006341 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006342 // The entrypoint the code generator is going to call does not do
6343 // clinit of the class.
6344 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006345 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006346 }
6347
6348 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006349 return NeedsAccessCheck() ||
6350 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006351 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006352 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006353 }
6354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006355 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006356 return NeedsAccessCheck() ||
6357 MustGenerateClinitCheck() ||
6358 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006359 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006360 GetLoadKind() == LoadKind::kBssEntry) &&
6361 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006362 }
6363
Calin Juravleacf735c2015-02-12 15:25:22 +00006364 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006365 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6366 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006367 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006368 } else {
6369 return ReferenceTypeInfo::CreateInvalid();
6370 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006371 }
6372
Vladimir Marko175e7862018-03-27 09:03:13 +00006373 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6374 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6375 DCHECK(klass_ != nullptr);
6376 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006377 }
6378
Andreas Gampea5b09a62016-11-17 15:21:22 -08006379 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006380 const DexFile& GetDexFile() const { return dex_file_; }
6381
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006382 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006383 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006384 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006385
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006386 static SideEffects SideEffectsForArchRuntimeCalls() {
6387 return SideEffects::CanTriggerGC();
6388 }
6389
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006390 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006391 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006392 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006393 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006394
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006395 bool MustResolveTypeOnSlowPath() const {
6396 // Check that this instruction has a slow path.
6397 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6398 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6399 return GetLoadKind() == LoadKind::kBssEntry;
6400 }
6401
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006402 void MarkInBootImage() {
6403 SetPackedFlag<kFlagIsInBootImage>(true);
6404 }
6405
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006406 void AddSpecialInput(HInstruction* special_input);
6407
6408 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006409 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006410 return ArrayRef<HUserRecord<HInstruction*>>(
6411 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6412 }
6413
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006414 Handle<mirror::Class> GetClass() const {
6415 return klass_;
6416 }
6417
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006418 DECLARE_INSTRUCTION(LoadClass);
6419
Artem Serovcced8ba2017-07-19 18:18:09 +01006420 protected:
6421 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6422
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006423 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006424 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006425 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006426 // Whether this instruction must generate the initialization check.
6427 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006428 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006429 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6430 static constexpr size_t kFieldLoadKindSize =
6431 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006432 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6433 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006434 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006435 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6436
6437 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006438 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006439 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006440 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006441 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006442 }
6443
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006444 void SetLoadKindInternal(LoadKind load_kind);
6445
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006446 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006447 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006448 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006449 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006450
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006451 // A type index and dex file where the class can be accessed. The dex file can be:
6452 // - The compiling method's dex file if the class is defined there too.
6453 // - The compiling method's dex file if the class is referenced there.
6454 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006455 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006456 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006457 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006458
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006459 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006460};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006461std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6462
6463// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006464inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6465 // The load kind should be determined before inserting the instruction to the graph.
6466 DCHECK(GetBlock() == nullptr);
6467 DCHECK(GetEnvironment() == nullptr);
6468 SetPackedField<LoadKindField>(load_kind);
6469 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6470 special_input_ = HUserRecord<HInstruction*>(nullptr);
6471 }
6472 if (!NeedsEnvironment()) {
6473 SetSideEffects(SideEffects::None());
6474 }
6475}
6476
6477// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006478inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006479 // The special input is used for PC-relative loads on some architectures,
6480 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006481 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006482 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006483 GetLoadKind() == LoadKind::kBssEntry ||
6484 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006485 DCHECK(special_input_.GetInstruction() == nullptr);
6486 special_input_ = HUserRecord<HInstruction*>(special_input);
6487 special_input->AddUseAt(this, 0);
6488}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006489
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006490class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006491 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006492 // Determines how to load the String.
6493 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006494 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006495 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006496 kBootImageLinkTimePcRelative,
6497
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006498 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006499 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006500 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006501
Vladimir Markoaad75c62016-10-03 08:46:48 +00006502 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006503 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006504 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006505
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006506 // Use a known boot image String* address, embedded in the code by the codegen.
6507 // Used for boot image strings referenced by apps in JIT-compiled code.
6508 kJitBootImageAddress,
6509
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006510 // Load from the root table associated with the JIT compiled method.
6511 kJitTableAddress,
6512
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006513 // Load using a simple runtime call. This is the fall-back load kind when
6514 // the codegen is unable to use another appropriate kind.
6515 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006516
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006517 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006518 };
6519
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006520 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006521 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006522 const DexFile& dex_file,
6523 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006524 : HInstruction(kLoadString,
6525 DataType::Type::kReference,
6526 SideEffectsForArchRuntimeCalls(),
6527 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006528 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006529 string_index_(string_index),
6530 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006531 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006532 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006533
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006534 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006535
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006536 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006537
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006538 LoadKind GetLoadKind() const {
6539 return GetPackedField<LoadKindField>();
6540 }
6541
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006542 bool HasPcRelativeLoadKind() const {
6543 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6544 GetLoadKind() == LoadKind::kBootImageRelRo ||
6545 GetLoadKind() == LoadKind::kBssEntry;
6546 }
6547
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006548 const DexFile& GetDexFile() const {
6549 return dex_file_;
6550 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006551
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006552 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006553 return string_index_;
6554 }
6555
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006556 Handle<mirror::String> GetString() const {
6557 return string_;
6558 }
6559
6560 void SetString(Handle<mirror::String> str) {
6561 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006562 }
6563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006564 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006565
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006566 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006567
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006568 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006569
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006570 // Will call the runtime if we need to load the string through
6571 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006572 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006573 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006574 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006575 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006576 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006577 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006578 return false;
6579 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006580 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006581 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006583 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006584 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006585 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006586
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006587 bool CanBeNull() const override { return false; }
6588 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006589
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006590 static SideEffects SideEffectsForArchRuntimeCalls() {
6591 return SideEffects::CanTriggerGC();
6592 }
6593
Vladimir Marko372f10e2016-05-17 16:30:10 +01006594 void AddSpecialInput(HInstruction* special_input);
6595
6596 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006597 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006598 return ArrayRef<HUserRecord<HInstruction*>>(
6599 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006600 }
6601
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006602 DECLARE_INSTRUCTION(LoadString);
6603
Artem Serovcced8ba2017-07-19 18:18:09 +01006604 protected:
6605 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6606
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006607 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006608 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006609 static constexpr size_t kFieldLoadKindSize =
6610 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6611 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006612 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006613 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006614
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006615 void SetLoadKindInternal(LoadKind load_kind);
6616
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006617 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006618 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006619 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006620 HUserRecord<HInstruction*> special_input_;
6621
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006622 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006623 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006624
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006625 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006626};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006627std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6628
6629// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006630inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6631 // The load kind should be determined before inserting the instruction to the graph.
6632 DCHECK(GetBlock() == nullptr);
6633 DCHECK(GetEnvironment() == nullptr);
6634 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6635 SetPackedField<LoadKindField>(load_kind);
6636 if (load_kind != LoadKind::kRuntimeCall) {
6637 special_input_ = HUserRecord<HInstruction*>(nullptr);
6638 }
6639 if (!NeedsEnvironment()) {
6640 SetSideEffects(SideEffects::None());
6641 }
6642}
6643
6644// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006645inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006646 // The special input is used for PC-relative loads on some architectures,
6647 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006648 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006649 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006650 GetLoadKind() == LoadKind::kBssEntry ||
6651 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006652 // HLoadString::GetInputRecords() returns an empty array at this point,
6653 // so use the GetInputRecords() from the base class to set the input record.
6654 DCHECK(special_input_.GetInstruction() == nullptr);
6655 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006656 special_input->AddUseAt(this, 0);
6657}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006658
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006659class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006660 public:
6661 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006662 uint16_t method_handle_idx,
6663 const DexFile& dex_file,
6664 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006665 : HInstruction(kLoadMethodHandle,
6666 DataType::Type::kReference,
6667 SideEffectsForArchRuntimeCalls(),
6668 dex_pc),
6669 special_input_(HUserRecord<HInstruction*>(current_method)),
6670 method_handle_idx_(method_handle_idx),
6671 dex_file_(dex_file) {
6672 }
6673
6674 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006675 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006676 return ArrayRef<HUserRecord<HInstruction*>>(
6677 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6678 }
6679
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006680 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006681
6682 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6683
6684 const DexFile& GetDexFile() const { return dex_file_; }
6685
6686 static SideEffects SideEffectsForArchRuntimeCalls() {
6687 return SideEffects::CanTriggerGC();
6688 }
6689
6690 DECLARE_INSTRUCTION(LoadMethodHandle);
6691
6692 protected:
6693 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6694
6695 private:
6696 // The special input is the HCurrentMethod for kRuntimeCall.
6697 HUserRecord<HInstruction*> special_input_;
6698
6699 const uint16_t method_handle_idx_;
6700 const DexFile& dex_file_;
6701};
6702
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006703class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006704 public:
6705 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006706 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006707 const DexFile& dex_file,
6708 uint32_t dex_pc)
6709 : HInstruction(kLoadMethodType,
6710 DataType::Type::kReference,
6711 SideEffectsForArchRuntimeCalls(),
6712 dex_pc),
6713 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006714 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006715 dex_file_(dex_file) {
6716 }
6717
6718 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006719 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006720 return ArrayRef<HUserRecord<HInstruction*>>(
6721 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6722 }
6723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006724 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006725
Orion Hodson06d10a72018-05-14 08:53:38 +01006726 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006727
6728 const DexFile& GetDexFile() const { return dex_file_; }
6729
6730 static SideEffects SideEffectsForArchRuntimeCalls() {
6731 return SideEffects::CanTriggerGC();
6732 }
6733
6734 DECLARE_INSTRUCTION(LoadMethodType);
6735
6736 protected:
6737 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6738
6739 private:
6740 // The special input is the HCurrentMethod for kRuntimeCall.
6741 HUserRecord<HInstruction*> special_input_;
6742
Orion Hodson06d10a72018-05-14 08:53:38 +01006743 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006744 const DexFile& dex_file_;
6745};
6746
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006747/**
6748 * Performs an initialization check on its Class object input.
6749 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006750class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006751 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006752 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006753 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306754 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006755 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006756 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006757 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006758 SetRawInputAt(0, constant);
6759 }
Artem Serova6e26142018-06-19 14:55:17 +01006760 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006761 bool CanBeMoved() const override { return true; }
6762 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006763 return true;
6764 }
6765
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006766 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006767 // May call runtime to initialize the class.
6768 return true;
6769 }
6770
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006771 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006772
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006773 HLoadClass* GetLoadClass() const {
6774 DCHECK(InputAt(0)->IsLoadClass());
6775 return InputAt(0)->AsLoadClass();
6776 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006777
6778 DECLARE_INSTRUCTION(ClinitCheck);
6779
Artem Serovcced8ba2017-07-19 18:18:09 +01006780
6781 protected:
6782 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006783};
6784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006785class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006786 public:
6787 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006788 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006789 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006790 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006791 bool is_volatile,
6792 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006793 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006794 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006795 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306796 : HExpression(kStaticFieldGet,
6797 field_type,
6798 SideEffects::FieldReadOfType(field_type, is_volatile),
6799 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006800 field_info_(field,
6801 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006802 field_type,
6803 is_volatile,
6804 field_idx,
6805 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006806 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006807 SetRawInputAt(0, cls);
6808 }
6809
Calin Juravle52c48962014-12-16 17:02:57 +00006810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006811 bool IsClonable() const override { return true; }
6812 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006814 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006815 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006816 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006817 }
6818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006819 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006820 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6821 }
6822
Calin Juravle52c48962014-12-16 17:02:57 +00006823 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006824 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006825 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006826 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006827
Vladimir Marko61b92282017-10-11 13:23:17 +01006828 void SetType(DataType::Type new_type) {
6829 DCHECK(DataType::IsIntegralType(GetType()));
6830 DCHECK(DataType::IsIntegralType(new_type));
6831 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6832 SetPackedField<TypeField>(new_type);
6833 }
6834
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006835 DECLARE_INSTRUCTION(StaticFieldGet);
6836
Artem Serovcced8ba2017-07-19 18:18:09 +01006837 protected:
6838 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6839
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006840 private:
6841 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006842};
6843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006844class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006845 public:
6846 HStaticFieldSet(HInstruction* cls,
6847 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006848 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006849 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006850 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006851 bool is_volatile,
6852 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006853 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006854 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006855 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006856 : HExpression(kStaticFieldSet,
6857 SideEffects::FieldWriteOfType(field_type, is_volatile),
6858 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006859 field_info_(field,
6860 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006861 field_type,
6862 is_volatile,
6863 field_idx,
6864 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006865 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006866 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006867 SetRawInputAt(0, cls);
6868 SetRawInputAt(1, value);
6869 }
6870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006871 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006872 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006873 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006874 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006875 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006876
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006877 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006878 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6879 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006880
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006881 DECLARE_INSTRUCTION(StaticFieldSet);
6882
Artem Serovcced8ba2017-07-19 18:18:09 +01006883 protected:
6884 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6885
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006886 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006887 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6888 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6889 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6890 "Too many packed fields.");
6891
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006892 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006893};
6894
Vladimir Marko552a1342017-10-31 10:56:47 +00006895class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6896 public:
6897 HStringBuilderAppend(HIntConstant* format,
6898 uint32_t number_of_arguments,
6899 ArenaAllocator* allocator,
6900 uint32_t dex_pc)
6901 : HVariableInputSizeInstruction(
6902 kStringBuilderAppend,
6903 DataType::Type::kReference,
6904 // The runtime call may read memory from inputs. It never writes outside
6905 // of the newly allocated result object (or newly allocated helper objects).
6906 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6907 dex_pc,
6908 allocator,
6909 number_of_arguments + /* format */ 1u,
6910 kArenaAllocInvokeInputs) {
6911 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6912 SetRawInputAt(FormatIndex(), format);
6913 }
6914
6915 void SetArgumentAt(size_t index, HInstruction* argument) {
6916 DCHECK_LE(index, GetNumberOfArguments());
6917 SetRawInputAt(index, argument);
6918 }
6919
6920 // Return the number of arguments, excluding the format.
6921 size_t GetNumberOfArguments() const {
6922 DCHECK_GE(InputCount(), 1u);
6923 return InputCount() - 1u;
6924 }
6925
6926 size_t FormatIndex() const {
6927 return GetNumberOfArguments();
6928 }
6929
6930 HIntConstant* GetFormat() {
6931 return InputAt(FormatIndex())->AsIntConstant();
6932 }
6933
6934 bool NeedsEnvironment() const override { return true; }
6935
6936 bool CanThrow() const override { return true; }
6937
6938 DECLARE_INSTRUCTION(StringBuilderAppend);
6939
6940 protected:
6941 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
6942};
6943
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006944class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006945 public:
6946 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006947 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006948 uint32_t field_index,
6949 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306950 : HExpression(kUnresolvedInstanceFieldGet,
6951 field_type,
6952 SideEffects::AllExceptGCDependency(),
6953 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006954 field_index_(field_index) {
6955 SetRawInputAt(0, obj);
6956 }
6957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006958 bool IsClonable() const override { return true; }
6959 bool NeedsEnvironment() const override { return true; }
6960 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006961
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006962 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006963 uint32_t GetFieldIndex() const { return field_index_; }
6964
6965 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6966
Artem Serovcced8ba2017-07-19 18:18:09 +01006967 protected:
6968 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6969
Calin Juravlee460d1d2015-09-29 04:52:17 +01006970 private:
6971 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006972};
6973
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006974class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006975 public:
6976 HUnresolvedInstanceFieldSet(HInstruction* obj,
6977 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006978 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006979 uint32_t field_index,
6980 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006981 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006982 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006983 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006984 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006985 SetRawInputAt(0, obj);
6986 SetRawInputAt(1, value);
6987 }
6988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006989 bool IsClonable() const override { return true; }
6990 bool NeedsEnvironment() const override { return true; }
6991 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006993 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006994 uint32_t GetFieldIndex() const { return field_index_; }
6995
6996 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6997
Artem Serovcced8ba2017-07-19 18:18:09 +01006998 protected:
6999 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7000
Calin Juravlee460d1d2015-09-29 04:52:17 +01007001 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007002 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7003 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007004 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007005 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7006 kFieldFieldType + kFieldFieldTypeSize;
7007 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7008 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007009 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007010
Calin Juravlee460d1d2015-09-29 04:52:17 +01007011 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007012};
7013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007014class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007015 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007016 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007017 uint32_t field_index,
7018 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307019 : HExpression(kUnresolvedStaticFieldGet,
7020 field_type,
7021 SideEffects::AllExceptGCDependency(),
7022 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007023 field_index_(field_index) {
7024 }
7025
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007026 bool IsClonable() const override { return true; }
7027 bool NeedsEnvironment() const override { return true; }
7028 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007030 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007031 uint32_t GetFieldIndex() const { return field_index_; }
7032
7033 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7034
Artem Serovcced8ba2017-07-19 18:18:09 +01007035 protected:
7036 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7037
Calin Juravlee460d1d2015-09-29 04:52:17 +01007038 private:
7039 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007040};
7041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007042class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007043 public:
7044 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007045 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007046 uint32_t field_index,
7047 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007048 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007049 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007050 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007051 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007052 SetRawInputAt(0, value);
7053 }
7054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007055 bool IsClonable() const override { return true; }
7056 bool NeedsEnvironment() const override { return true; }
7057 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007058
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007059 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007060 uint32_t GetFieldIndex() const { return field_index_; }
7061
7062 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7063
Artem Serovcced8ba2017-07-19 18:18:09 +01007064 protected:
7065 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7066
Calin Juravlee460d1d2015-09-29 04:52:17 +01007067 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007068 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7069 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007070 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007071 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7072 kFieldFieldType + kFieldFieldTypeSize;
7073 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7074 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007075 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007076
Calin Juravlee460d1d2015-09-29 04:52:17 +01007077 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007078};
7079
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007080// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007081class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007082 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007083 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307084 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7085 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007086
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007087 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007088
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007089 DECLARE_INSTRUCTION(LoadException);
7090
Artem Serovcced8ba2017-07-19 18:18:09 +01007091 protected:
7092 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007093};
7094
David Brazdilcb1c0552015-08-04 16:22:25 +01007095// Implicit part of move-exception which clears thread-local exception storage.
7096// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007097class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007098 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007099 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007100 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307101 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007102
7103 DECLARE_INSTRUCTION(ClearException);
7104
Artem Serovcced8ba2017-07-19 18:18:09 +01007105 protected:
7106 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007107};
7108
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007109class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007110 public:
7111 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007112 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007113 SetRawInputAt(0, exception);
7114 }
7115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007116 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007117
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007118 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007120 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007122 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007123
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007124 DECLARE_INSTRUCTION(Throw);
7125
Artem Serovcced8ba2017-07-19 18:18:09 +01007126 protected:
7127 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007128};
7129
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007130/**
7131 * Implementation strategies for the code generator of a HInstanceOf
7132 * or `HCheckCast`.
7133 */
7134enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007135 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007136 kExactCheck, // Can do a single class compare.
7137 kClassHierarchyCheck, // Can just walk the super class chain.
7138 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7139 kInterfaceCheck, // No optimization yet when checking against an interface.
7140 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007141 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007142 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007143 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007144};
7145
Roland Levillain86503782016-02-11 19:07:30 +00007146std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7147
Vladimir Marko175e7862018-03-27 09:03:13 +00007148// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7149// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7150class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007151 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007152 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007153 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007154 HInstruction* object,
7155 HInstruction* target_class_or_null,
7156 TypeCheckKind check_kind,
7157 Handle<mirror::Class> klass,
7158 uint32_t dex_pc,
7159 ArenaAllocator* allocator,
7160 HIntConstant* bitstring_path_to_root,
7161 HIntConstant* bitstring_mask,
7162 SideEffects side_effects)
7163 : HVariableInputSizeInstruction(
7164 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007165 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007166 side_effects,
7167 dex_pc,
7168 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007169 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007170 kArenaAllocTypeCheckInputs),
7171 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007172 SetPackedField<TypeCheckKindField>(check_kind);
7173 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007174 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007175 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007176 SetRawInputAt(1, target_class_or_null);
7177 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7178 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7179 if (check_kind == TypeCheckKind::kBitstringCheck) {
7180 DCHECK(target_class_or_null->IsNullConstant());
7181 SetRawInputAt(2, bitstring_path_to_root);
7182 SetRawInputAt(3, bitstring_mask);
7183 } else {
7184 DCHECK(target_class_or_null->IsLoadClass());
7185 }
Vladimir Marko87584542017-12-12 17:47:52 +00007186 }
7187
7188 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007189 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007190 HInstruction* load_class = InputAt(1);
7191 DCHECK(load_class->IsLoadClass());
7192 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007193 }
7194
Vladimir Marko175e7862018-03-27 09:03:13 +00007195 uint32_t GetBitstringPathToRoot() const {
7196 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7197 HInstruction* path_to_root = InputAt(2);
7198 DCHECK(path_to_root->IsIntConstant());
7199 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7200 }
7201
7202 uint32_t GetBitstringMask() const {
7203 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7204 HInstruction* mask = InputAt(3);
7205 DCHECK(mask->IsIntConstant());
7206 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7207 }
7208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007209 bool IsClonable() const override { return true; }
7210 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007211
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007212 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007213 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7214 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007215 }
7216
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007217 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7218 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7219 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7220 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7221
Vladimir Marko175e7862018-03-27 09:03:13 +00007222 ReferenceTypeInfo GetTargetClassRTI() {
7223 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7224 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007225 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007226 } else {
7227 return ReferenceTypeInfo::CreateInvalid();
7228 }
7229 }
7230
7231 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7232 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7233 DCHECK(klass_ != nullptr);
7234 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7235 }
7236
7237 Handle<mirror::Class> GetClass() const {
7238 return klass_;
7239 }
7240
7241 protected:
7242 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7243
7244 private:
7245 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7246 static constexpr size_t kFieldTypeCheckKindSize =
7247 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7248 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7249 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7250 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7251 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7252 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7253
7254 Handle<mirror::Class> klass_;
7255};
7256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007257class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007258 public:
7259 HInstanceOf(HInstruction* object,
7260 HInstruction* target_class_or_null,
7261 TypeCheckKind check_kind,
7262 Handle<mirror::Class> klass,
7263 uint32_t dex_pc,
7264 ArenaAllocator* allocator,
7265 HIntConstant* bitstring_path_to_root,
7266 HIntConstant* bitstring_mask)
7267 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007268 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007269 object,
7270 target_class_or_null,
7271 check_kind,
7272 klass,
7273 dex_pc,
7274 allocator,
7275 bitstring_path_to_root,
7276 bitstring_mask,
7277 SideEffectsForArchRuntimeCalls(check_kind)) {}
7278
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007279 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007280
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007281 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007282 return CanCallRuntime(GetTypeCheckKind());
7283 }
7284
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007285 static bool CanCallRuntime(TypeCheckKind check_kind) {
7286 // Mips currently does runtime calls for any other checks.
7287 return check_kind != TypeCheckKind::kExactCheck;
7288 }
7289
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007290 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007291 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007292 }
7293
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007294 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007295
Artem Serovcced8ba2017-07-19 18:18:09 +01007296 protected:
7297 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007298};
7299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007300class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007301 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007302 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307303 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007304 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7305 SetPackedFlag<kFlagUpperCanBeNull>(true);
7306 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007307 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007308 SetRawInputAt(0, input);
7309 }
7310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007311 bool InstructionDataEquals(const HInstruction* other) const override;
7312 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007313
David Brazdilf5552582015-12-27 13:36:12 +00007314 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007315 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007316 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007317 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007318
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007319 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007320 DCHECK(GetUpperCanBeNull() || !can_be_null);
7321 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007322 }
7323
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007324 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007325
Calin Juravleb1498f62015-02-16 13:13:29 +00007326 DECLARE_INSTRUCTION(BoundType);
7327
Artem Serovcced8ba2017-07-19 18:18:09 +01007328 protected:
7329 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7330
Calin Juravleb1498f62015-02-16 13:13:29 +00007331 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007332 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7333 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007334 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007335 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7336 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7337 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7338
Calin Juravleb1498f62015-02-16 13:13:29 +00007339 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007340 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7341 // It is used to bound the type in cases like:
7342 // if (x instanceof ClassX) {
7343 // // uper_bound_ will be ClassX
7344 // }
David Brazdilf5552582015-12-27 13:36:12 +00007345 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007346};
7347
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007348class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007349 public:
7350 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007351 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007352 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007353 Handle<mirror::Class> klass,
7354 uint32_t dex_pc,
7355 ArenaAllocator* allocator,
7356 HIntConstant* bitstring_path_to_root,
7357 HIntConstant* bitstring_mask)
7358 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007359 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007360 object,
7361 target_class_or_null,
7362 check_kind,
7363 klass,
7364 dex_pc,
7365 allocator,
7366 bitstring_path_to_root,
7367 bitstring_mask,
7368 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007369
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007370 bool IsClonable() const override { return true; }
7371 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007372 // Instruction may throw a CheckCastError.
7373 return true;
7374 }
7375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007376 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007377
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007378 DECLARE_INSTRUCTION(CheckCast);
7379
Artem Serovcced8ba2017-07-19 18:18:09 +01007380 protected:
7381 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007382};
7383
Andreas Gampe26de38b2016-07-27 17:53:11 -07007384/**
7385 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7386 * @details We define the combined barrier types that are actually required
7387 * by the Java Memory Model, rather than using exactly the terminology from
7388 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7389 * primitives. Note that the JSR-133 cookbook generally does not deal with
7390 * store atomicity issues, and the recipes there are not always entirely sufficient.
7391 * The current recipe is as follows:
7392 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7393 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7394 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7395 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7396 * class has final fields.
7397 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7398 * store-to-memory instructions. Only generated together with non-temporal stores.
7399 */
7400enum MemBarrierKind {
7401 kAnyStore,
7402 kLoadAny,
7403 kStoreStore,
7404 kAnyAny,
7405 kNTStoreStore,
7406 kLastBarrierKind = kNTStoreStore
7407};
7408std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7409
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007410class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007411 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007412 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007413 : HExpression(kMemoryBarrier,
7414 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7415 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007416 SetPackedField<BarrierKindField>(barrier_kind);
7417 }
Calin Juravle27df7582015-04-17 19:12:31 +01007418
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007419 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007420
Vladimir Markoa1de9182016-02-25 11:37:38 +00007421 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007422
7423 DECLARE_INSTRUCTION(MemoryBarrier);
7424
Artem Serovcced8ba2017-07-19 18:18:09 +01007425 protected:
7426 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7427
Calin Juravle27df7582015-04-17 19:12:31 +01007428 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007429 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7430 static constexpr size_t kFieldBarrierKindSize =
7431 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7432 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7433 kFieldBarrierKind + kFieldBarrierKindSize;
7434 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7435 "Too many packed fields.");
7436 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007437};
7438
Igor Murashkind01745e2017-04-05 16:40:31 -07007439// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7440// the specified object(s), with respect to any subsequent store that might "publish"
7441// (i.e. make visible) the specified object to another thread.
7442//
7443// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7444// for all final fields (that were set) at the end of the invoked constructor.
7445//
7446// The constructor fence models the freeze actions for the final fields of an object
7447// being constructed (semantically at the end of the constructor). Constructor fences
7448// have a per-object affinity; two separate objects being constructed get two separate
7449// constructor fences.
7450//
7451// (Note: that if calling a super-constructor or forwarding to another constructor,
7452// the freezes would happen at the end of *that* constructor being invoked).
7453//
7454// The memory model guarantees that when the object being constructed is "published" after
7455// constructor completion (i.e. escapes the current thread via a store), then any final field
7456// writes must be observable on other threads (once they observe that publication).
7457//
7458// Further, anything written before the freeze, and read by dereferencing through the final field,
7459// must also be visible (so final object field could itself have an object with non-final fields;
7460// yet the freeze must also extend to them).
7461//
7462// Constructor example:
7463//
7464// class HasFinal {
7465// final int field; Optimizing IR for <init>()V:
7466// HasFinal() {
7467// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7468// // freeze(this.field); HConstructorFence(this)
7469// } HReturn
7470// }
7471//
7472// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7473// already-initialized classes; in that case the allocation must act as a "default-initializer"
7474// of the object which effectively writes the class pointer "final field".
7475//
7476// For example, we can model default-initialiation as roughly the equivalent of the following:
7477//
7478// class Object {
7479// private final Class header;
7480// }
7481//
7482// Java code: Optimizing IR:
7483//
7484// T new_instance<T>() {
7485// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7486// obj.header = T.class; // header write is done by above call.
7487// // freeze(obj.header) HConstructorFence(obj)
7488// return (T)obj;
7489// }
7490//
7491// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007492// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007493// * QuasiAtomic::ThreadFenceForConstructor
7494//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007495class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007496 // A fence has variable inputs because the inputs can be removed
7497 // after prepare_for_register_allocation phase.
7498 // (TODO: In the future a fence could freeze multiple objects
7499 // after merging two fences together.)
7500 public:
7501 // `fence_object` is the reference that needs to be protected for correct publication.
7502 //
7503 // It makes sense in the following situations:
7504 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7505 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7506 //
7507 // After construction the `fence_object` becomes the 0th input.
7508 // This is not an input in a real sense, but just a convenient place to stash the information
7509 // about the associated object.
7510 HConstructorFence(HInstruction* fence_object,
7511 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007512 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007513 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7514 // constraints described in the class header. We claim that these SideEffects constraints
7515 // enforce a superset of the real constraints.
7516 //
7517 // The ordering described above is conservatively modeled with SideEffects as follows:
7518 //
7519 // * To prevent reordering of the publication stores:
7520 // ----> "Reads of objects" is the initial SideEffect.
7521 // * For every primitive final field store in the constructor:
7522 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7523 // * If there are any stores to reference final fields in the constructor:
7524 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7525 // that are reachable from `fence_object` also need to be prevented for reordering
7526 // (and we do not want to do alias analysis to figure out what those stores are).
7527 //
7528 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7529 // even more conservative approach than the one described above, and prevents all of the
7530 // above reordering without analyzing any of the instructions in the constructor.
7531 //
7532 // If in a later phase we discover that there are no writes to reference final fields,
7533 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307534 : HVariableInputSizeInstruction(kConstructorFence,
7535 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007536 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007537 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007538 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007539 kArenaAllocConstructorFenceInputs) {
7540 DCHECK(fence_object != nullptr);
7541 SetRawInputAt(0, fence_object);
7542 }
7543
7544 // The object associated with this constructor fence.
7545 //
7546 // (Note: This will be null after the prepare_for_register_allocation phase,
7547 // as all constructor fence inputs are removed there).
7548 HInstruction* GetFenceObject() const {
7549 return InputAt(0);
7550 }
7551
7552 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7553 // - Delete `fence_use` from `this`'s use list.
7554 // - Delete `this` from `fence_use`'s inputs list.
7555 // - If the `fence_use` is dead, remove it from the graph.
7556 //
7557 // A fence is considered dead once it no longer has any uses
7558 // and all of the inputs are dead.
7559 //
7560 // This must *not* be called during/after prepare_for_register_allocation,
7561 // because that removes all the inputs to the fences but the fence is actually
7562 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007563 //
7564 // Returns how many HConstructorFence instructions were removed from graph.
7565 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007566
Igor Murashkindd018df2017-08-09 10:38:31 -07007567 // Combine all inputs of `this` and `other` instruction and remove
7568 // `other` from the graph.
7569 //
7570 // Inputs are unique after the merge.
7571 //
7572 // Requirement: `this` must not be the same as `other.
7573 void Merge(HConstructorFence* other);
7574
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007575 // Check if this constructor fence is protecting
7576 // an HNewInstance or HNewArray that is also the immediate
7577 // predecessor of `this`.
7578 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007579 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7580 // to be one of the inputs of `this`.
7581 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007582 // Returns the associated HNewArray or HNewInstance,
7583 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007584 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007585
Igor Murashkind01745e2017-04-05 16:40:31 -07007586 DECLARE_INSTRUCTION(ConstructorFence);
7587
Artem Serovcced8ba2017-07-19 18:18:09 +01007588 protected:
7589 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007590};
7591
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007592class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007593 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007594 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007595 kEnter,
7596 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007597 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007598 };
7599
7600 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007601 : HExpression(kMonitorOperation,
7602 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7603 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007604 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007605 SetRawInputAt(0, object);
7606 }
7607
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007608 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007609 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007610
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007611 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007612 // Verifier guarantees that monitor-exit cannot throw.
7613 // This is important because it allows the HGraphBuilder to remove
7614 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7615 return IsEnter();
7616 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007617
Vladimir Markoa1de9182016-02-25 11:37:38 +00007618 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7619 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007620
7621 DECLARE_INSTRUCTION(MonitorOperation);
7622
Artem Serovcced8ba2017-07-19 18:18:09 +01007623 protected:
7624 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7625
Calin Juravle52c48962014-12-16 17:02:57 +00007626 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007627 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7628 static constexpr size_t kFieldOperationKindSize =
7629 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7630 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7631 kFieldOperationKind + kFieldOperationKindSize;
7632 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7633 "Too many packed fields.");
7634 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007635};
7636
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007637class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007638 public:
7639 HSelect(HInstruction* condition,
7640 HInstruction* true_value,
7641 HInstruction* false_value,
7642 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307643 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007644 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7645
7646 // First input must be `true_value` or `false_value` to allow codegens to
7647 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7648 // that architectures which implement HSelect as a conditional move also
7649 // will not need to invert the condition.
7650 SetRawInputAt(0, false_value);
7651 SetRawInputAt(1, true_value);
7652 SetRawInputAt(2, condition);
7653 }
7654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007655 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007656 HInstruction* GetFalseValue() const { return InputAt(0); }
7657 HInstruction* GetTrueValue() const { return InputAt(1); }
7658 HInstruction* GetCondition() const { return InputAt(2); }
7659
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007660 bool CanBeMoved() const override { return true; }
7661 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007662 return true;
7663 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007664
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007665 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007666 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7667 }
7668
7669 DECLARE_INSTRUCTION(Select);
7670
Artem Serovcced8ba2017-07-19 18:18:09 +01007671 protected:
7672 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007673};
7674
Vladimir Markof9f64412015-09-02 14:05:49 +01007675class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007676 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007677 MoveOperands(Location source,
7678 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007679 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007680 HInstruction* instruction)
7681 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007682
7683 Location GetSource() const { return source_; }
7684 Location GetDestination() const { return destination_; }
7685
7686 void SetSource(Location value) { source_ = value; }
7687 void SetDestination(Location value) { destination_ = value; }
7688
7689 // The parallel move resolver marks moves as "in-progress" by clearing the
7690 // destination (but not the source).
7691 Location MarkPending() {
7692 DCHECK(!IsPending());
7693 Location dest = destination_;
7694 destination_ = Location::NoLocation();
7695 return dest;
7696 }
7697
7698 void ClearPending(Location dest) {
7699 DCHECK(IsPending());
7700 destination_ = dest;
7701 }
7702
7703 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007704 DCHECK(source_.IsValid() || destination_.IsInvalid());
7705 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007706 }
7707
7708 // True if this blocks a move from the given location.
7709 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007710 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007711 }
7712
7713 // A move is redundant if it's been eliminated, if its source and
7714 // destination are the same, or if its destination is unneeded.
7715 bool IsRedundant() const {
7716 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7717 }
7718
7719 // We clear both operands to indicate move that's been eliminated.
7720 void Eliminate() {
7721 source_ = destination_ = Location::NoLocation();
7722 }
7723
7724 bool IsEliminated() const {
7725 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7726 return source_.IsInvalid();
7727 }
7728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007729 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007730
Nicolas Geoffray90218252015-04-15 11:56:51 +01007731 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007732 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007733 }
7734
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007735 HInstruction* GetInstruction() const { return instruction_; }
7736
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007737 private:
7738 Location source_;
7739 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007740 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007741 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007742 // The instruction this move is assocatied with. Null when this move is
7743 // for moving an input in the expected locations of user (including a phi user).
7744 // This is only used in debug mode, to ensure we do not connect interval siblings
7745 // in the same parallel move.
7746 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007747};
7748
Roland Levillainc9285912015-12-18 10:38:42 +00007749std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7750
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007751static constexpr size_t kDefaultNumberOfMoves = 4;
7752
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007753class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007754 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007755 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007756 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007757 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007758 moves_.reserve(kDefaultNumberOfMoves);
7759 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007760
Nicolas Geoffray90218252015-04-15 11:56:51 +01007761 void AddMove(Location source,
7762 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007763 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007764 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007765 DCHECK(source.IsValid());
7766 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007767 if (kIsDebugBuild) {
7768 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007769 for (const MoveOperands& move : moves_) {
7770 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007771 // Special case the situation where the move is for the spill slot
7772 // of the instruction.
7773 if ((GetPrevious() == instruction)
7774 || ((GetPrevious() == nullptr)
7775 && instruction->IsPhi()
7776 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007777 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007778 << "Doing parallel moves for the same instruction.";
7779 } else {
7780 DCHECK(false) << "Doing parallel moves for the same instruction.";
7781 }
7782 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007783 }
7784 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007785 for (const MoveOperands& move : moves_) {
7786 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007787 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007788 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007789 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007790 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007791 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007792 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007793 }
7794
Vladimir Marko225b6462015-09-28 12:17:40 +01007795 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007796 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007797 }
7798
Vladimir Marko225b6462015-09-28 12:17:40 +01007799 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007800
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007801 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007802
Artem Serovcced8ba2017-07-19 18:18:09 +01007803 protected:
7804 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7805
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007806 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007807 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007808};
7809
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007810// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7811// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7812// never used across anything that can trigger GC.
7813// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7814// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007815class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007816 public:
7817 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307818 : HExpression(kIntermediateAddress,
7819 DataType::Type::kInt32,
7820 SideEffects::DependsOnGC(),
7821 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007822 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7823 DataType::Size(DataType::Type::kReference))
7824 << "kPrimInt and kPrimNot have different sizes.";
7825 SetRawInputAt(0, base_address);
7826 SetRawInputAt(1, offset);
7827 }
7828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007829 bool IsClonable() const override { return true; }
7830 bool CanBeMoved() const override { return true; }
7831 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007832 return true;
7833 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007834 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007835
7836 HInstruction* GetBaseAddress() const { return InputAt(0); }
7837 HInstruction* GetOffset() const { return InputAt(1); }
7838
7839 DECLARE_INSTRUCTION(IntermediateAddress);
7840
Artem Serovcced8ba2017-07-19 18:18:09 +01007841 protected:
7842 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007843};
7844
7845
Mark Mendell0616ae02015-04-17 12:49:27 -04007846} // namespace art
7847
Aart Bikf8f5a162017-02-06 15:35:29 -08007848#include "nodes_vector.h"
7849
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007850#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7851#include "nodes_shared.h"
7852#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007853#ifdef ART_ENABLE_CODEGEN_mips
7854#include "nodes_mips.h"
7855#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307856#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007857#include "nodes_x86.h"
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05307858#include "nodes_vector_x86.h"
Mark Mendell0616ae02015-04-17 12:49:27 -04007859#endif
7860
7861namespace art {
7862
Igor Murashkin6ef45672017-08-08 13:59:55 -07007863class OptimizingCompilerStats;
7864
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007865class HGraphVisitor : public ValueObject {
7866 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007867 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7868 : stats_(stats),
7869 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007870 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007871
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007872 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007873 virtual void VisitBasicBlock(HBasicBlock* block);
7874
Roland Levillain633021e2014-10-01 14:12:25 +01007875 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007876 void VisitInsertionOrder();
7877
Roland Levillain633021e2014-10-01 14:12:25 +01007878 // Visit the graph following dominator tree reverse post-order.
7879 void VisitReversePostOrder();
7880
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007881 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007882
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007883 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007884#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007885 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7886
7887 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7888
7889#undef DECLARE_VISIT_INSTRUCTION
7890
Igor Murashkin6ef45672017-08-08 13:59:55 -07007891 protected:
7892 OptimizingCompilerStats* stats_;
7893
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007894 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007895 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007896
7897 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7898};
7899
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007900class HGraphDelegateVisitor : public HGraphVisitor {
7901 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007902 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7903 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007904 virtual ~HGraphDelegateVisitor() {}
7905
7906 // Visit functions that delegate to to super class.
7907#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007908 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007909
7910 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7911
7912#undef DECLARE_VISIT_INSTRUCTION
7913
7914 private:
7915 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7916};
7917
Artem Serovcced8ba2017-07-19 18:18:09 +01007918// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7919// and remove the old instruction.
7920HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7921
7922// Create a clone for each clonable instructions/phis and replace the original with the clone.
7923//
7924// Used for testing individual instruction cloner.
7925class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7926 public:
7927 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007928 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007930 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007931 if (instruction->IsClonable()) {
7932 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007933 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007934 }
7935 }
7936
Artem Serov7f4aff62017-06-21 17:02:18 +01007937 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007938
7939 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007940 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007941
7942 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7943};
7944
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007945// Iterator over the blocks that art part of the loop. Includes blocks part
7946// of an inner loop. The order in which the blocks are iterated is on their
7947// block id.
7948class HBlocksInLoopIterator : public ValueObject {
7949 public:
7950 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7951 : blocks_in_loop_(info.GetBlocks()),
7952 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7953 index_(0) {
7954 if (!blocks_in_loop_.IsBitSet(index_)) {
7955 Advance();
7956 }
7957 }
7958
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007959 bool Done() const { return index_ == blocks_.size(); }
7960 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007961 void Advance() {
7962 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007963 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007964 if (blocks_in_loop_.IsBitSet(index_)) {
7965 break;
7966 }
7967 }
7968 }
7969
7970 private:
7971 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007972 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007973 size_t index_;
7974
7975 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7976};
7977
Mingyao Yang3584bce2015-05-19 16:01:59 -07007978// Iterator over the blocks that art part of the loop. Includes blocks part
7979// of an inner loop. The order in which the blocks are iterated is reverse
7980// post order.
7981class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7982 public:
7983 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7984 : blocks_in_loop_(info.GetBlocks()),
7985 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7986 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007987 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007988 Advance();
7989 }
7990 }
7991
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007992 bool Done() const { return index_ == blocks_.size(); }
7993 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007994 void Advance() {
7995 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007996 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7997 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007998 break;
7999 }
8000 }
8001 }
8002
8003 private:
8004 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008005 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008006 size_t index_;
8007
8008 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8009};
8010
Aart Bikf3e61ee2017-04-12 17:09:20 -07008011// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008012inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008013 if (constant->IsIntConstant()) {
8014 return constant->AsIntConstant()->GetValue();
8015 } else if (constant->IsLongConstant()) {
8016 return constant->AsLongConstant()->GetValue();
8017 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008018 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008019 return 0;
8020 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008021}
8022
Aart Bikf3e61ee2017-04-12 17:09:20 -07008023// Returns true iff instruction is an integral constant (and sets value on success).
8024inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8025 if (instruction->IsIntConstant()) {
8026 *value = instruction->AsIntConstant()->GetValue();
8027 return true;
8028 } else if (instruction->IsLongConstant()) {
8029 *value = instruction->AsLongConstant()->GetValue();
8030 return true;
8031 } else if (instruction->IsNullConstant()) {
8032 *value = 0;
8033 return true;
8034 }
8035 return false;
8036}
8037
Aart Bik0148de42017-09-05 09:25:01 -07008038// Returns true iff instruction is the given integral constant.
8039inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8040 int64_t val = 0;
8041 return IsInt64AndGet(instruction, &val) && val == value;
8042}
8043
8044// Returns true iff instruction is a zero bit pattern.
8045inline bool IsZeroBitPattern(HInstruction* instruction) {
8046 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8047}
8048
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008049// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008050#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008051 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8052 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8053#undef INSTRUCTION_TYPE_CHECK
8054
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008055// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008056#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8057 std::is_base_of<BaseType, H##type>::value,
8058#define INSTRUCTION_TYPE_CHECK(type, super) \
8059 inline bool HInstruction::Is##type() const { \
8060 DCHECK_LT(GetKind(), kLastInstructionKind); \
8061 using BaseType = H##type; \
8062 static constexpr bool results[] = { \
8063 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8064 }; \
8065 return results[static_cast<size_t>(GetKind())]; \
8066 }
8067
8068 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8069#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008070#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008071
8072#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008073 inline const H##type* HInstruction::As##type() const { \
8074 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8075 } \
8076 inline H##type* HInstruction::As##type() { \
8077 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8078 }
8079
Vladimir Markoa90dd512018-05-04 15:04:45 +01008080 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8081#undef INSTRUCTION_TYPE_CAST
8082
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008083
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008084// Create space in `blocks` for adding `number_of_new_blocks` entries
8085// starting at location `at`. Blocks after `at` are moved accordingly.
8086inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8087 size_t number_of_new_blocks,
8088 size_t after) {
8089 DCHECK_LT(after, blocks->size());
8090 size_t old_size = blocks->size();
8091 size_t new_size = old_size + number_of_new_blocks;
8092 blocks->resize(new_size);
8093 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8094}
8095
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008096/*
8097 * Hunt "under the hood" of array lengths (leading to array references),
8098 * null checks (also leading to array references), and new arrays
8099 * (leading to the actual length). This makes it more likely related
8100 * instructions become actually comparable.
8101 */
8102inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8103 while (instruction->IsArrayLength() ||
8104 instruction->IsNullCheck() ||
8105 instruction->IsNewArray()) {
8106 instruction = instruction->IsNewArray()
8107 ? instruction->AsNewArray()->GetLength()
8108 : instruction->InputAt(0);
8109 }
8110 return instruction;
8111}
8112
Artem Serov21c7e6f2017-07-27 16:04:42 +01008113void RemoveEnvironmentUses(HInstruction* instruction);
8114bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8115void ResetEnvironmentInputRecords(HInstruction* instruction);
8116
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008117} // namespace art
8118
8119#endif // ART_COMPILER_OPTIMIZING_NODES_H_