blob: 373641392773cf06ffe6e7243b535b957744ca92 [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),
Nicolas Geoffray2fef66b2019-06-26 22:00:02 +0000358 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 {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002143 // Go over previous non-move instructions that are emitted at use site.
2144 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2145 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2146 if (prev_not_move->IsNullCheck()) {
2147 return prev_not_move->AsNullCheck();
2148 }
2149 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002150 }
2151 return nullptr;
2152 }
2153
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002154 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002155 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002156 }
2157
Calin Juravle2e768302015-07-28 14:41:11 +00002158 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002159
Calin Juravle61d544b2015-02-23 16:46:57 +00002160 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002162 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002163 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002164 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002166 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002167 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002168 // Note: fixup_end remains valid across push_front().
2169 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2170 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002171 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002172 uses_.push_front(*new_node);
2173 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002174 }
2175
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002176 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002177 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002178 // Note: env_fixup_end remains valid across push_front().
2179 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2180 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002181 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002182 env_uses_.push_front(*new_node);
2183 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002184 }
2185
David Brazdil1abb4192015-02-17 18:33:36 +00002186 void RemoveAsUserOfInput(size_t input) {
2187 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002188 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2189 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2190 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002191 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192
Vladimir Marko372f10e2016-05-17 16:30:10 +01002193 void RemoveAsUserOfAllInputs() {
2194 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2195 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2196 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2197 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2198 }
2199 }
2200
David Brazdil1abb4192015-02-17 18:33:36 +00002201 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2202 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002203
Vladimir Marko46817b82016-03-29 12:21:58 +01002204 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2205 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2206 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002207 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002208 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002209 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002210
Aart Bikcc42be02016-10-20 16:14:16 -07002211 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002212 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002213 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002214 !CanThrow() &&
2215 !IsSuspendCheck() &&
2216 !IsControlFlow() &&
2217 !IsNativeDebugInfo() &&
2218 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002219 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002220 !IsMemoryBarrier() &&
2221 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002222 }
2223
Aart Bikcc42be02016-10-20 16:14:16 -07002224 bool IsDeadAndRemovable() const {
2225 return IsRemovable() && !HasUses();
2226 }
2227
Roland Levillain6c82d402014-10-13 16:10:27 +01002228 // Does this instruction strictly dominate `other_instruction`?
2229 // Returns false if this instruction and `other_instruction` are the same.
2230 // Aborts if this instruction and `other_instruction` are both phis.
2231 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002232
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002233 int GetId() const { return id_; }
2234 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002235
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002236 int GetSsaIndex() const { return ssa_index_; }
2237 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2238 bool HasSsaIndex() const { return ssa_index_ != -1; }
2239
2240 bool HasEnvironment() const { return environment_ != nullptr; }
2241 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002242 // Set the `environment_` field. Raw because this method does not
2243 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002244 void SetRawEnvironment(HEnvironment* environment) {
2245 DCHECK(environment_ == nullptr);
2246 DCHECK_EQ(environment->GetHolder(), this);
2247 environment_ = environment;
2248 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002249
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002250 void InsertRawEnvironment(HEnvironment* environment) {
2251 DCHECK(environment_ != nullptr);
2252 DCHECK_EQ(environment->GetHolder(), this);
2253 DCHECK(environment->GetParent() == nullptr);
2254 environment->parent_ = environment_;
2255 environment_ = environment;
2256 }
2257
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002258 void RemoveEnvironment();
2259
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002260 // Set the environment of this instruction, copying it from `environment`. While
2261 // copying, the uses lists are being updated.
2262 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002263 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002264 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002265 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002266 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002267 if (environment->GetParent() != nullptr) {
2268 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2269 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002270 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002271
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002272 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2273 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002274 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002275 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002276 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002277 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002278 if (environment->GetParent() != nullptr) {
2279 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2280 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002281 }
2282
Nicolas Geoffray39468442014-09-02 15:17:15 +01002283 // Returns the number of entries in the environment. Typically, that is the
2284 // number of dex registers in a method. It could be more in case of inlining.
2285 size_t EnvironmentSize() const;
2286
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002287 LocationSummary* GetLocations() const { return locations_; }
2288 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002289
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002290 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002291 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002292 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002293 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002294
Alexandre Rames188d4312015-04-09 18:30:21 +01002295 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2296 // uses of this instruction by `other` are *not* updated.
2297 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2298 ReplaceWith(other);
2299 other->ReplaceInput(this, use_index);
2300 }
2301
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002302 // Move `this` instruction before `cursor`
2303 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002304
Vladimir Markofb337ea2015-11-25 15:25:10 +00002305 // Move `this` before its first user and out of any loops. If there is no
2306 // out-of-loop user that dominates all other users, move the instruction
2307 // to the end of the out-of-loop common dominator of the user's blocks.
2308 //
2309 // This can be used only on non-throwing instructions with no side effects that
2310 // have at least one use but no environment uses.
2311 void MoveBeforeFirstUserAndOutOfLoops();
2312
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002313#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002314 bool Is##type() const;
2315
2316 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2317#undef INSTRUCTION_TYPE_CHECK
2318
2319#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002320 const H##type* As##type() const; \
2321 H##type* As##type();
2322
Vladimir Markoa90dd512018-05-04 15:04:45 +01002323 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2324#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002325
Artem Serovcced8ba2017-07-19 18:18:09 +01002326 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2327 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2328 // the instruction then the behaviour of this function is undefined.
2329 //
2330 // Note: It is semantically valid to create a clone of the instruction only until
2331 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2332 // copied.
2333 //
2334 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2335 // 'explicit HInstruction(const HInstruction& other)' for details.
2336 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2337 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2338 DebugName() << " " << GetId();
2339 UNREACHABLE();
2340 }
2341
2342 // Return whether instruction can be cloned (copied).
2343 virtual bool IsClonable() const { return false; }
2344
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002345 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002346 // TODO: this method is used by LICM and GVN with possibly different
2347 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002348 virtual bool CanBeMoved() const { return false; }
2349
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002350 // Returns whether any data encoded in the two instructions is equal.
2351 // This method does not look at the inputs. Both instructions must be
2352 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002353 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002354 return false;
2355 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002356
2357 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002358 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002359 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002360 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002361
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302362 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002363
2364 virtual size_t ComputeHashCode() const {
2365 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002366 for (const HInstruction* input : GetInputs()) {
2367 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002368 }
2369 return result;
2370 }
2371
2372 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002373 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002374 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002375
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002376 size_t GetLifetimePosition() const { return lifetime_position_; }
2377 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2378 LiveInterval* GetLiveInterval() const { return live_interval_; }
2379 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2380 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2381
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002382 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2383
2384 // Returns whether the code generation of the instruction will require to have access
2385 // to the current method. Such instructions are:
2386 // (1): Instructions that require an environment, as calling the runtime requires
2387 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002388 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2389 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002390 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002391 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002392 }
2393
Vladimir Markodc151b22015-10-15 18:02:30 +01002394 // Returns whether the code generation of the instruction will require to have access
2395 // to the dex cache of the current method's declaring class via the current method.
2396 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002397
Mark Mendellc4701932015-04-10 13:18:51 -04002398 // Does this instruction have any use in an environment before
2399 // control flow hits 'other'?
2400 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2401
2402 // Remove all references to environment uses of this instruction.
2403 // The caller must ensure that this is safe to do.
2404 void RemoveEnvironmentUsers();
2405
Vladimir Markoa1de9182016-02-25 11:37:38 +00002406 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2407 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002408
David Brazdil1abb4192015-02-17 18:33:36 +00002409 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002410 // If set, the machine code for this instruction is assumed to be generated by
2411 // its users. Used by liveness analysis to compute use positions accordingly.
2412 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2413 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302414 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2415 static constexpr size_t kFieldInstructionKindSize =
2416 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002417 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302418 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002419 static constexpr size_t kFieldTypeSize =
2420 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2421 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002422 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2423
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302424 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2425 "Too many generic packed fields");
2426
Vladimir Markobd785672018-05-03 17:09:09 +01002427 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2428
Vladimir Marko372f10e2016-05-17 16:30:10 +01002429 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2430 return GetInputRecords()[i];
2431 }
2432
2433 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2434 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2435 input_records[index] = input;
2436 }
David Brazdil1abb4192015-02-17 18:33:36 +00002437
Vladimir Markoa1de9182016-02-25 11:37:38 +00002438 uint32_t GetPackedFields() const {
2439 return packed_fields_;
2440 }
2441
2442 template <size_t flag>
2443 bool GetPackedFlag() const {
2444 return (packed_fields_ & (1u << flag)) != 0u;
2445 }
2446
2447 template <size_t flag>
2448 void SetPackedFlag(bool value = true) {
2449 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2450 }
2451
2452 template <typename BitFieldType>
2453 typename BitFieldType::value_type GetPackedField() const {
2454 return BitFieldType::Decode(packed_fields_);
2455 }
2456
2457 template <typename BitFieldType>
2458 void SetPackedField(typename BitFieldType::value_type value) {
2459 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2460 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2461 }
2462
Artem Serovcced8ba2017-07-19 18:18:09 +01002463 // Copy construction for the instruction (used for Clone function).
2464 //
2465 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2466 // prepare_for_register_allocator are not copied (set to default values).
2467 //
2468 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2469 // fine for most of them. However for some of the instructions a custom copy constructor must be
2470 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2471 // for copying them).
2472 explicit HInstruction(const HInstruction& other)
2473 : previous_(nullptr),
2474 next_(nullptr),
2475 block_(nullptr),
2476 dex_pc_(other.dex_pc_),
2477 id_(-1),
2478 ssa_index_(-1),
2479 packed_fields_(other.packed_fields_),
2480 environment_(nullptr),
2481 locations_(nullptr),
2482 live_interval_(nullptr),
2483 lifetime_position_(kNoLifetime),
2484 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302485 reference_type_handle_(other.reference_type_handle_) {
2486 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002487
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002488 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302489 using InstructionKindField =
2490 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2491
Vladimir Marko46817b82016-03-29 12:21:58 +01002492 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2493 auto before_use_node = uses_.before_begin();
2494 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2495 HInstruction* user = use_node->GetUser();
2496 size_t input_index = use_node->GetIndex();
2497 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2498 before_use_node = use_node;
2499 }
2500 }
2501
2502 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2503 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2504 if (next != uses_.end()) {
2505 HInstruction* next_user = next->GetUser();
2506 size_t next_index = next->GetIndex();
2507 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2508 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2509 }
2510 }
2511
2512 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2513 auto before_env_use_node = env_uses_.before_begin();
2514 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2515 HEnvironment* user = env_use_node->GetUser();
2516 size_t input_index = env_use_node->GetIndex();
2517 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2518 before_env_use_node = env_use_node;
2519 }
2520 }
2521
2522 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2523 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2524 if (next != env_uses_.end()) {
2525 HEnvironment* next_user = next->GetUser();
2526 size_t next_index = next->GetIndex();
2527 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2528 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2529 }
2530 }
David Brazdil1abb4192015-02-17 18:33:36 +00002531
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002532 HInstruction* previous_;
2533 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002534 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002535 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002536
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002537 // An instruction gets an id when it is added to the graph.
2538 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002539 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002540 int id_;
2541
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002542 // When doing liveness analysis, instructions that have uses get an SSA index.
2543 int ssa_index_;
2544
Vladimir Markoa1de9182016-02-25 11:37:38 +00002545 // Packed fields.
2546 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002547
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002548 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002549 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002550
2551 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002552 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002553
Nicolas Geoffray39468442014-09-02 15:17:15 +01002554 // The environment associated with this instruction. Not null if the instruction
2555 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002556 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002557
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002558 // Set by the code generator.
2559 LocationSummary* locations_;
2560
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002561 // Set by the liveness analysis.
2562 LiveInterval* live_interval_;
2563
2564 // Set by the liveness analysis, this is the position in a linear
2565 // order of blocks where this instruction's live interval start.
2566 size_t lifetime_position_;
2567
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002568 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002569
Vladimir Markoa1de9182016-02-25 11:37:38 +00002570 // The reference handle part of the reference type info.
2571 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002572 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002573 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002574
David Brazdil1abb4192015-02-17 18:33:36 +00002575 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002576 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002577 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002578 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002579 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002580};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002581std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002582
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002583// Iterates over the instructions, while preserving the next instruction
2584// in case the current instruction gets removed from the list by the user
2585// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002586class HInstructionIterator : public ValueObject {
2587 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002588 explicit HInstructionIterator(const HInstructionList& instructions)
2589 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002590 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002591 }
2592
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002593 bool Done() const { return instruction_ == nullptr; }
2594 HInstruction* Current() const { return instruction_; }
2595 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002596 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002597 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002598 }
2599
2600 private:
2601 HInstruction* instruction_;
2602 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002603
2604 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002605};
2606
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002607// Iterates over the instructions without saving the next instruction,
2608// therefore handling changes in the graph potentially made by the user
2609// of this iterator.
2610class HInstructionIteratorHandleChanges : public ValueObject {
2611 public:
2612 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2613 : instruction_(instructions.first_instruction_) {
2614 }
2615
2616 bool Done() const { return instruction_ == nullptr; }
2617 HInstruction* Current() const { return instruction_; }
2618 void Advance() {
2619 instruction_ = instruction_->GetNext();
2620 }
2621
2622 private:
2623 HInstruction* instruction_;
2624
2625 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2626};
2627
2628
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002629class HBackwardInstructionIterator : public ValueObject {
2630 public:
2631 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2632 : instruction_(instructions.last_instruction_) {
2633 next_ = Done() ? nullptr : instruction_->GetPrevious();
2634 }
2635
2636 bool Done() const { return instruction_ == nullptr; }
2637 HInstruction* Current() const { return instruction_; }
2638 void Advance() {
2639 instruction_ = next_;
2640 next_ = Done() ? nullptr : instruction_->GetPrevious();
2641 }
2642
2643 private:
2644 HInstruction* instruction_;
2645 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002646
2647 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002648};
2649
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002650class HVariableInputSizeInstruction : public HInstruction {
2651 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002652 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002653 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002654 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2655 }
2656
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002657 void AddInput(HInstruction* input);
2658 void InsertInputAt(size_t index, HInstruction* input);
2659 void RemoveInputAt(size_t index);
2660
Igor Murashkind01745e2017-04-05 16:40:31 -07002661 // Removes all the inputs.
2662 // Also removes this instructions from each input's use list
2663 // (for non-environment uses only).
2664 void RemoveAllInputs();
2665
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002666 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302667 HVariableInputSizeInstruction(InstructionKind inst_kind,
2668 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002669 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002670 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002671 size_t number_of_inputs,
2672 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302673 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002674 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002675 HVariableInputSizeInstruction(InstructionKind inst_kind,
2676 DataType::Type type,
2677 SideEffects side_effects,
2678 uint32_t dex_pc,
2679 ArenaAllocator* allocator,
2680 size_t number_of_inputs,
2681 ArenaAllocKind kind)
2682 : HInstruction(inst_kind, type, side_effects, dex_pc),
2683 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002684
Artem Serovcced8ba2017-07-19 18:18:09 +01002685 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002686
Artem Serovcced8ba2017-07-19 18:18:09 +01002687 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002688};
2689
Vladimir Markof9f64412015-09-02 14:05:49 +01002690template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002691class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002692 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002693 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302694 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002695 HExpression<N>(InstructionKind kind,
2696 DataType::Type type,
2697 SideEffects side_effects,
2698 uint32_t dex_pc)
2699 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2700 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002701
Vladimir Marko372f10e2016-05-17 16:30:10 +01002702 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002703 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002704 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002705 }
2706
Artem Serovcced8ba2017-07-19 18:18:09 +01002707 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002708 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002709
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002710 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002711 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002712
2713 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002714};
2715
Vladimir Markobd785672018-05-03 17:09:09 +01002716// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002717template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002718class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002719 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002720 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721
Vladimir Markobd785672018-05-03 17:09:09 +01002722 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002723
Vladimir Marko372f10e2016-05-17 16:30:10 +01002724 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002725 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002726 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002727 }
2728
Artem Serovcced8ba2017-07-19 18:18:09 +01002729 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002730 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002731
Vladimir Markof9f64412015-09-02 14:05:49 +01002732 private:
2733 friend class SsaBuilder;
2734};
2735
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002736// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2737// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002738class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002739 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002740 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002741 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302742 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002743
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002744 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002745
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002746 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002747
Artem Serovcced8ba2017-07-19 18:18:09 +01002748 protected:
2749 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002750};
2751
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002752// Represents dex's RETURN opcodes. A HReturn is a control flow
2753// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002754class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002755 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002757 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002758 SetRawInputAt(0, value);
2759 }
2760
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002761 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002762
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002763 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002764
Artem Serovcced8ba2017-07-19 18:18:09 +01002765 protected:
2766 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002767};
2768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002769class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002770 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002771 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002772 uint32_t reg_number,
2773 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002774 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002775 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002776 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302777 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002778 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002779 SideEffects::None(),
2780 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002781 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002782 number_of_inputs,
2783 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002784 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002786 // Phis are constructed live and marked dead if conflicting or unused.
2787 // Individual steps of SsaBuilder should assume that if a phi has been
2788 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2789 SetPackedFlag<kFlagIsLive>(true);
2790 SetPackedFlag<kFlagCanBeNull>(true);
2791 }
2792
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002793 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002794
David Brazdildee58d62016-04-07 09:54:26 +00002795 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002796 static DataType::Type ToPhiType(DataType::Type type) {
2797 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002798 }
2799
2800 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002803 // Make sure that only valid type changes occur. The following are allowed:
2804 // (1) int -> float/ref (primitive type propagation),
2805 // (2) long -> double (primitive type propagation).
2806 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002807 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2808 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2809 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002810 SetPackedField<TypeField>(new_type);
2811 }
2812
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002813 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002814 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2815
2816 uint32_t GetRegNumber() const { return reg_number_; }
2817
2818 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2819 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2820 bool IsDead() const { return !IsLive(); }
2821 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2822
Vladimir Markoe9004912016-06-16 16:50:52 +01002823 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002824 return other != nullptr
2825 && other->IsPhi()
2826 && other->AsPhi()->GetBlock() == GetBlock()
2827 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2828 }
2829
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002830 bool HasEquivalentPhi() const {
2831 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2832 return true;
2833 }
2834 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2835 return true;
2836 }
2837 return false;
2838 }
2839
David Brazdildee58d62016-04-07 09:54:26 +00002840 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2841 // An equivalent phi is a phi having the same dex register and type.
2842 // It assumes that phis with the same dex register are adjacent.
2843 HPhi* GetNextEquivalentPhiWithSameType() {
2844 HInstruction* next = GetNext();
2845 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2846 if (next->GetType() == GetType()) {
2847 return next->AsPhi();
2848 }
2849 next = next->GetNext();
2850 }
2851 return nullptr;
2852 }
2853
2854 DECLARE_INSTRUCTION(Phi);
2855
Artem Serovcced8ba2017-07-19 18:18:09 +01002856 protected:
2857 DEFAULT_COPY_CONSTRUCTOR(Phi);
2858
David Brazdildee58d62016-04-07 09:54:26 +00002859 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002860 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002861 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2862 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2863 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002864
David Brazdildee58d62016-04-07 09:54:26 +00002865 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002866};
2867
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002868// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002869// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002870// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002871class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002872 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302873 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002874 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302875 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002876
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002877 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002878
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002879 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002880
Artem Serovcced8ba2017-07-19 18:18:09 +01002881 protected:
2882 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002883};
2884
2885// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002886class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002887 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302888 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002889 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302890 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002892 bool IsClonable() const override { return true; }
2893 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002894
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002895 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002896 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002897 }
2898
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002899 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002900
Artem Serovcced8ba2017-07-19 18:18:09 +01002901 protected:
2902 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002903};
2904
Roland Levillain9867bc72015-08-05 10:21:34 +01002905class HConstant : public HExpression<0> {
2906 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302907 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2908 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2909 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002911 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002912
Roland Levillain1a653882016-03-18 18:05:57 +00002913 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002915 // Is this constant 0 in the arithmetic sense?
2916 virtual bool IsArithmeticZero() const { return false; }
2917 // Is this constant a 0-bit pattern?
2918 virtual bool IsZeroBitPattern() const { return false; }
2919 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002920 virtual bool IsOne() const { return false; }
2921
David Brazdil77a48ae2015-09-15 12:34:04 +00002922 virtual uint64_t GetValueAsUint64() const = 0;
2923
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002924 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002925
Artem Serovcced8ba2017-07-19 18:18:09 +01002926 protected:
2927 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002928};
2929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002930class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002931 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002932 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002933 return true;
2934 }
2935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002936 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002937
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002938 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002939
Roland Levillain1a653882016-03-18 18:05:57 +00002940 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002941 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002942
Roland Levillain9867bc72015-08-05 10:21:34 +01002943 DECLARE_INSTRUCTION(NullConstant);
2944
Artem Serovcced8ba2017-07-19 18:18:09 +01002945 protected:
2946 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2947
Roland Levillain9867bc72015-08-05 10:21:34 +01002948 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002949 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302950 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2951 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002952
2953 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002954};
2955
2956// Constants of the type int. Those can be from Dex instructions, or
2957// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002958class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002959 public:
2960 int32_t GetValue() const { return value_; }
2961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002962 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002963 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2964 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002965
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002966 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002967 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002968 return other->AsIntConstant()->value_ == value_;
2969 }
2970
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002971 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002972
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002973 bool IsMinusOne() const override { return GetValue() == -1; }
2974 bool IsArithmeticZero() const override { return GetValue() == 0; }
2975 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2976 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002977
Roland Levillain1a653882016-03-18 18:05:57 +00002978 // Integer constants are used to encode Boolean values as well,
2979 // where 1 means true and 0 means false.
2980 bool IsTrue() const { return GetValue() == 1; }
2981 bool IsFalse() const { return GetValue() == 0; }
2982
Roland Levillain9867bc72015-08-05 10:21:34 +01002983 DECLARE_INSTRUCTION(IntConstant);
2984
Artem Serovcced8ba2017-07-19 18:18:09 +01002985 protected:
2986 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2987
Roland Levillain9867bc72015-08-05 10:21:34 +01002988 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002989 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302990 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2991 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002992 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302993 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2994 value_(value ? 1 : 0) {
2995 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002996
2997 const int32_t value_;
2998
2999 friend class HGraph;
3000 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3001 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003002};
3003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003004class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003005 public:
3006 int64_t GetValue() const { return value_; }
3007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003008 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003010 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003011 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003012 return other->AsLongConstant()->value_ == value_;
3013 }
3014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003015 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003016
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003017 bool IsMinusOne() const override { return GetValue() == -1; }
3018 bool IsArithmeticZero() const override { return GetValue() == 0; }
3019 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3020 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003021
3022 DECLARE_INSTRUCTION(LongConstant);
3023
Artem Serovcced8ba2017-07-19 18:18:09 +01003024 protected:
3025 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3026
Roland Levillain9867bc72015-08-05 10:21:34 +01003027 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003028 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303029 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3030 value_(value) {
3031 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003032
3033 const int64_t value_;
3034
3035 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003036};
Dave Allison20dfc792014-06-16 20:44:29 -07003037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003038class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003039 public:
3040 float GetValue() const { return value_; }
3041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003042 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003043 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3044 }
3045
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003046 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003047 DCHECK(other->IsFloatConstant()) << other->DebugName();
3048 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3049 }
3050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003051 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003052
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003053 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003054 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3055 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003056 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003057 return std::fpclassify(value_) == FP_ZERO;
3058 }
3059 bool IsArithmeticPositiveZero() const {
3060 return IsArithmeticZero() && !std::signbit(value_);
3061 }
3062 bool IsArithmeticNegativeZero() const {
3063 return IsArithmeticZero() && std::signbit(value_);
3064 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003065 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003066 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003067 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003068 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3070 }
3071 bool IsNaN() const {
3072 return std::isnan(value_);
3073 }
3074
3075 DECLARE_INSTRUCTION(FloatConstant);
3076
Artem Serovcced8ba2017-07-19 18:18:09 +01003077 protected:
3078 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3079
Roland Levillain31dd3d62016-02-16 12:21:02 +00003080 private:
3081 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303082 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3083 value_(value) {
3084 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003085 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303086 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3087 value_(bit_cast<float, int32_t>(value)) {
3088 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003089
3090 const float value_;
3091
3092 // Only the SsaBuilder and HGraph can create floating-point constants.
3093 friend class SsaBuilder;
3094 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003095};
3096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003097class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003098 public:
3099 double GetValue() const { return value_; }
3100
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003101 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003102
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003103 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3105 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3106 }
3107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003108 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003110 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003111 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3112 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003113 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003114 return std::fpclassify(value_) == FP_ZERO;
3115 }
3116 bool IsArithmeticPositiveZero() const {
3117 return IsArithmeticZero() && !std::signbit(value_);
3118 }
3119 bool IsArithmeticNegativeZero() const {
3120 return IsArithmeticZero() && std::signbit(value_);
3121 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003122 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003123 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003124 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003125 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003126 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3127 }
3128 bool IsNaN() const {
3129 return std::isnan(value_);
3130 }
3131
3132 DECLARE_INSTRUCTION(DoubleConstant);
3133
Artem Serovcced8ba2017-07-19 18:18:09 +01003134 protected:
3135 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3136
Roland Levillain31dd3d62016-02-16 12:21:02 +00003137 private:
3138 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303139 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3140 value_(value) {
3141 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003142 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303143 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3144 value_(bit_cast<double, int64_t>(value)) {
3145 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003146
3147 const double value_;
3148
3149 // Only the SsaBuilder and HGraph can create floating-point constants.
3150 friend class SsaBuilder;
3151 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003152};
3153
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003154// Conditional branch. A block ending with an HIf instruction must have
3155// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003156class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003157 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003158 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003159 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003160 SetRawInputAt(0, input);
3161 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003162
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003163 bool IsClonable() const override { return true; }
3164 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003165
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003166 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003167 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003168 }
3169
3170 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003171 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003172 }
3173
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003174 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003175
Artem Serovcced8ba2017-07-19 18:18:09 +01003176 protected:
3177 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003178};
3179
David Brazdilfc6a86a2015-06-26 10:33:45 +00003180
3181// Abstract instruction which marks the beginning and/or end of a try block and
3182// links it to the respective exception handlers. Behaves the same as a Goto in
3183// non-exceptional control flow.
3184// Normal-flow successor is stored at index zero, exception handlers under
3185// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003186class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003187 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003188 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003189 kEntry,
3190 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003191 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003192 };
3193
Artem Serovd1aa7d02018-06-22 11:35:46 +01003194 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3195 // across the catch block entering edges as GC might happen during throwing an exception.
3196 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3197 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003198 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003199 : HExpression(kTryBoundary,
3200 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3201 : SideEffects::None(),
3202 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003203 SetPackedField<BoundaryKindField>(kind);
3204 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003206 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003207
3208 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003209 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003210
David Brazdild26a4112015-11-10 11:07:31 +00003211 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3212 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3213 }
3214
David Brazdilfc6a86a2015-06-26 10:33:45 +00003215 // Returns whether `handler` is among its exception handlers (non-zero index
3216 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003217 bool HasExceptionHandler(const HBasicBlock& handler) const {
3218 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003219 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003220 }
3221
3222 // If not present already, adds `handler` to its block's list of exception
3223 // handlers.
3224 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003225 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003226 GetBlock()->AddSuccessor(handler);
3227 }
3228 }
3229
Vladimir Markoa1de9182016-02-25 11:37:38 +00003230 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3231 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003232
David Brazdilffee3d32015-07-06 11:48:53 +01003233 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3234
David Brazdilfc6a86a2015-06-26 10:33:45 +00003235 DECLARE_INSTRUCTION(TryBoundary);
3236
Artem Serovcced8ba2017-07-19 18:18:09 +01003237 protected:
3238 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3239
David Brazdilfc6a86a2015-06-26 10:33:45 +00003240 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003241 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3242 static constexpr size_t kFieldBoundaryKindSize =
3243 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3244 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3245 kFieldBoundaryKind + kFieldBoundaryKindSize;
3246 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3247 "Too many packed fields.");
3248 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003249};
3250
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003251// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003252class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003253 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003254 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3255 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003256 HDeoptimize(ArenaAllocator* allocator,
3257 HInstruction* cond,
3258 DeoptimizationKind kind,
3259 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003260 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303261 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003262 SideEffects::All(),
3263 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003264 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003265 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003266 kArenaAllocMisc) {
3267 SetPackedFlag<kFieldCanBeMoved>(false);
3268 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003269 SetRawInputAt(0, cond);
3270 }
3271
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003272 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003273
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003274 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3275 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3276 // instead of `guard`.
3277 // We set CanTriggerGC to prevent any intermediate address to be live
3278 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003279 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003280 HInstruction* cond,
3281 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003282 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003283 uint32_t dex_pc)
3284 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303285 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003286 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003287 SideEffects::CanTriggerGC(),
3288 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003289 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003290 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003291 kArenaAllocMisc) {
3292 SetPackedFlag<kFieldCanBeMoved>(true);
3293 SetPackedField<DeoptimizeKindField>(kind);
3294 SetRawInputAt(0, cond);
3295 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003296 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003297
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003298 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003300 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003301 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3302 }
3303
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003304 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003305
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003306 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003307
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003308 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003309
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003310 bool GuardsAnInput() const {
3311 return InputCount() == 2;
3312 }
3313
3314 HInstruction* GuardedInput() const {
3315 DCHECK(GuardsAnInput());
3316 return InputAt(1);
3317 }
3318
3319 void RemoveGuard() {
3320 RemoveInputAt(1);
3321 }
3322
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003323 DECLARE_INSTRUCTION(Deoptimize);
3324
Artem Serovcced8ba2017-07-19 18:18:09 +01003325 protected:
3326 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3327
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003328 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003329 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3330 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3331 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003332 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003333 static constexpr size_t kNumberOfDeoptimizePackedBits =
3334 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3335 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3336 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003337 using DeoptimizeKindField =
3338 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003339};
3340
Mingyao Yang063fc772016-08-02 11:02:54 -07003341// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3342// The compiled code checks this flag value in a guard before devirtualized call and
3343// if it's true, starts to do deoptimization.
3344// It has a 4-byte slot on stack.
3345// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003346class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003347 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003348 // CHA guards are only optimized in a separate pass and it has no side effects
3349 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003350 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303351 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003352 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303353 SideEffects::None(),
3354 dex_pc,
3355 allocator,
3356 0,
3357 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003358 }
3359
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003360 // We do all CHA guard elimination/motion in a single pass, after which there is no
3361 // further guard elimination/motion since a guard might have been used for justification
3362 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3363 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003364 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003365
3366 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3367
Artem Serovcced8ba2017-07-19 18:18:09 +01003368 protected:
3369 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003370};
3371
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003372// Represents the ArtMethod that was passed as a first argument to
3373// the method. It is used by instructions that depend on it, like
3374// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003375class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003376 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003377 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303378 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3379 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003380
3381 DECLARE_INSTRUCTION(CurrentMethod);
3382
Artem Serovcced8ba2017-07-19 18:18:09 +01003383 protected:
3384 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003385};
3386
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003387// Fetches an ArtMethod from the virtual table or the interface method table
3388// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003389class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003390 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003391 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003392 kVTable,
3393 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003394 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003395 };
3396 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003397 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003398 TableKind kind,
3399 size_t index,
3400 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303401 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003402 index_(index) {
3403 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003404 SetRawInputAt(0, cls);
3405 }
3406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003407 bool IsClonable() const override { return true; }
3408 bool CanBeMoved() const override { return true; }
3409 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003410 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003411 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003412 }
3413
Vladimir Markoa1de9182016-02-25 11:37:38 +00003414 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003415 size_t GetIndex() const { return index_; }
3416
3417 DECLARE_INSTRUCTION(ClassTableGet);
3418
Artem Serovcced8ba2017-07-19 18:18:09 +01003419 protected:
3420 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3421
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003422 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003423 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003424 static constexpr size_t kFieldTableKindSize =
3425 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3426 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3427 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3428 "Too many packed fields.");
3429 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3430
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003431 // The index of the ArtMethod in the table.
3432 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003433};
3434
Mark Mendellfe57faa2015-09-18 09:26:15 -04003435// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3436// have one successor for each entry in the switch table, and the final successor
3437// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003438class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003439 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003440 HPackedSwitch(int32_t start_value,
3441 uint32_t num_entries,
3442 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003443 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003444 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003445 start_value_(start_value),
3446 num_entries_(num_entries) {
3447 SetRawInputAt(0, input);
3448 }
3449
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003450 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003451
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003452 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003453
3454 int32_t GetStartValue() const { return start_value_; }
3455
Vladimir Marko211c2112015-09-24 16:52:33 +01003456 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003457
3458 HBasicBlock* GetDefaultBlock() const {
3459 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003460 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003461 }
3462 DECLARE_INSTRUCTION(PackedSwitch);
3463
Artem Serovcced8ba2017-07-19 18:18:09 +01003464 protected:
3465 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3466
Mark Mendellfe57faa2015-09-18 09:26:15 -04003467 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003468 const int32_t start_value_;
3469 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003470};
3471
Roland Levillain88cb1752014-10-20 16:36:47 +01003472class HUnaryOperation : public HExpression<1> {
3473 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303474 HUnaryOperation(InstructionKind kind,
3475 DataType::Type result_type,
3476 HInstruction* input,
3477 uint32_t dex_pc = kNoDexPc)
3478 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003479 SetRawInputAt(0, input);
3480 }
3481
Artem Serovcced8ba2017-07-19 18:18:09 +01003482 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003483 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003484
Roland Levillain88cb1752014-10-20 16:36:47 +01003485 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003486 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003487
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003488 bool CanBeMoved() const override { return true; }
3489 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003490 return true;
3491 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003492
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 // Try to statically evaluate `this` and return a HConstant
3494 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003495 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003496 HConstant* TryStaticEvaluation() const;
3497
3498 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003499 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3500 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003501 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3502 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003503
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003504 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003505
Artem Serovcced8ba2017-07-19 18:18:09 +01003506 protected:
3507 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003508};
3509
Dave Allison20dfc792014-06-16 20:44:29 -07003510class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003511 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303512 HBinaryOperation(InstructionKind kind,
3513 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003514 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003515 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003516 SideEffects side_effects = SideEffects::None(),
3517 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303518 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003519 SetRawInputAt(0, left);
3520 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003521 }
3522
Artem Serovcced8ba2017-07-19 18:18:09 +01003523 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003524 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003525
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003526 HInstruction* GetLeft() const { return InputAt(0); }
3527 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003528 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003529
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003530 virtual bool IsCommutative() const { return false; }
3531
3532 // Put constant on the right.
3533 // Returns whether order is changed.
3534 bool OrderInputsWithConstantOnTheRight() {
3535 HInstruction* left = InputAt(0);
3536 HInstruction* right = InputAt(1);
3537 if (left->IsConstant() && !right->IsConstant()) {
3538 ReplaceInput(right, 0);
3539 ReplaceInput(left, 1);
3540 return true;
3541 }
3542 return false;
3543 }
3544
3545 // Order inputs by instruction id, but favor constant on the right side.
3546 // This helps GVN for commutative ops.
3547 void OrderInputs() {
3548 DCHECK(IsCommutative());
3549 HInstruction* left = InputAt(0);
3550 HInstruction* right = InputAt(1);
3551 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3552 return;
3553 }
3554 if (OrderInputsWithConstantOnTheRight()) {
3555 return;
3556 }
3557 // Order according to instruction id.
3558 if (left->GetId() > right->GetId()) {
3559 ReplaceInput(right, 0);
3560 ReplaceInput(left, 1);
3561 }
3562 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003564 bool CanBeMoved() const override { return true; }
3565 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003566 return true;
3567 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003568
Roland Levillain31dd3d62016-02-16 12:21:02 +00003569 // Try to statically evaluate `this` and return a HConstant
3570 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003571 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003572 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003573
3574 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003575 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3576 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003577 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3578 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003579 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003580 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3581 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003582 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3583 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003584 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3585 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003586 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3588 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003589
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003590 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003591 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003592 HConstant* GetConstantRight() const;
3593
3594 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003595 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003596 HInstruction* GetLeastConstantLeft() const;
3597
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003598 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003599
Artem Serovcced8ba2017-07-19 18:18:09 +01003600 protected:
3601 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003602};
3603
Mark Mendellc4701932015-04-10 13:18:51 -04003604// The comparison bias applies for floating point operations and indicates how NaN
3605// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003606enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003607 kNoBias, // bias is not applicable (i.e. for long operation)
3608 kGtBias, // return 1 for NaN comparisons
3609 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003610 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003611};
3612
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3614
Dave Allison20dfc792014-06-16 20:44:29 -07003615class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003616 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303617 HCondition(InstructionKind kind,
3618 HInstruction* first,
3619 HInstruction* second,
3620 uint32_t dex_pc = kNoDexPc)
3621 : HBinaryOperation(kind,
3622 DataType::Type::kBool,
3623 first,
3624 second,
3625 SideEffects::None(),
3626 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003627 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3628 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003629
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003630 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003631 // `instruction`, and disregard moves in between.
3632 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003633
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003634 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003635
3636 virtual IfCondition GetCondition() const = 0;
3637
Mark Mendellc4701932015-04-10 13:18:51 -04003638 virtual IfCondition GetOppositeCondition() const = 0;
3639
Vladimir Markoa1de9182016-02-25 11:37:38 +00003640 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003641 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3642
Vladimir Markoa1de9182016-02-25 11:37:38 +00003643 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3644 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003645
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003646 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003647 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003648 }
3649
Roland Levillain4fa13f62015-07-06 18:11:54 +01003650 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003651 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003652 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003653 if (if_cond == kCondNE) {
3654 return true;
3655 } else if (if_cond == kCondEQ) {
3656 return false;
3657 }
3658 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003659 }
3660
3661 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003662 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003663 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003664 if (if_cond == kCondEQ) {
3665 return true;
3666 } else if (if_cond == kCondNE) {
3667 return false;
3668 }
3669 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003670 }
3671
Roland Levillain31dd3d62016-02-16 12:21:02 +00003672 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003673 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003674 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003675 static constexpr size_t kFieldComparisonBiasSize =
3676 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3677 static constexpr size_t kNumberOfConditionPackedBits =
3678 kFieldComparisonBias + kFieldComparisonBiasSize;
3679 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3680 using ComparisonBiasField =
3681 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3682
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 template <typename T>
3684 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3685
3686 template <typename T>
3687 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003688 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003689 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3690 // Handle the bias.
3691 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3692 }
3693
3694 // Return an integer constant containing the result of a condition evaluated at compile time.
3695 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3696 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3697 }
3698
Artem Serovcced8ba2017-07-19 18:18:09 +01003699 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003700};
3701
3702// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003703class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003704 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003705 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303706 : HCondition(kEqual, first, second, dex_pc) {
3707 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003708
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003709 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003710
Vladimir Marko9e23df52015-11-10 17:14:35 +00003711 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003712 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003713 return MakeConstantCondition(true, GetDexPc());
3714 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003716 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3717 }
3718 // In the following Evaluate methods, a HCompare instruction has
3719 // been merged into this HEqual instruction; evaluate it as
3720 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003721 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003722 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3723 GetDexPc());
3724 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003725 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003726 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3727 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003728 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003729 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003730 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003731
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003732 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003733
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003734 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003735 return kCondEQ;
3736 }
3737
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003738 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003739 return kCondNE;
3740 }
3741
Artem Serovcced8ba2017-07-19 18:18:09 +01003742 protected:
3743 DEFAULT_COPY_CONSTRUCTOR(Equal);
3744
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003745 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003746 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003747};
3748
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003749class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003750 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303751 HNotEqual(HInstruction* first, HInstruction* second,
3752 uint32_t dex_pc = kNoDexPc)
3753 : HCondition(kNotEqual, first, second, dex_pc) {
3754 }
Dave Allison20dfc792014-06-16 20:44:29 -07003755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003756 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003757
Vladimir Marko9e23df52015-11-10 17:14:35 +00003758 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003759 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003760 return MakeConstantCondition(false, GetDexPc());
3761 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003762 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003763 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3764 }
3765 // In the following Evaluate methods, a HCompare instruction has
3766 // been merged into this HNotEqual instruction; evaluate it as
3767 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003768 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003769 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3770 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003771 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003772 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3773 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003774 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003775 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003776 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003777
Dave Allison20dfc792014-06-16 20:44:29 -07003778 DECLARE_INSTRUCTION(NotEqual);
3779
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003780 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003781 return kCondNE;
3782 }
3783
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003784 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003785 return kCondEQ;
3786 }
3787
Artem Serovcced8ba2017-07-19 18:18:09 +01003788 protected:
3789 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3790
Dave Allison20dfc792014-06-16 20:44:29 -07003791 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003792 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003793};
3794
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003795class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003796 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303797 HLessThan(HInstruction* first, HInstruction* second,
3798 uint32_t dex_pc = kNoDexPc)
3799 : HCondition(kLessThan, first, second, dex_pc) {
3800 }
Dave Allison20dfc792014-06-16 20:44:29 -07003801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003802 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003803 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003804 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003805 // In the following Evaluate methods, a HCompare instruction has
3806 // been merged into this HLessThan instruction; evaluate it as
3807 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003808 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003809 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3810 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003811 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003812 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3813 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003814 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003815 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003816 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003817
Dave Allison20dfc792014-06-16 20:44:29 -07003818 DECLARE_INSTRUCTION(LessThan);
3819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003820 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003821 return kCondLT;
3822 }
3823
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003824 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003825 return kCondGE;
3826 }
3827
Artem Serovcced8ba2017-07-19 18:18:09 +01003828 protected:
3829 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3830
Dave Allison20dfc792014-06-16 20:44:29 -07003831 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003832 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003833};
3834
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003835class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003836 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303837 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3838 uint32_t dex_pc = kNoDexPc)
3839 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3840 }
Dave Allison20dfc792014-06-16 20:44:29 -07003841
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003842 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003843 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003844 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003845 // In the following Evaluate methods, a HCompare instruction has
3846 // been merged into this HLessThanOrEqual instruction; evaluate it as
3847 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003848 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003849 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3850 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003851 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003852 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3853 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003854 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003855 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003856 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003857
Dave Allison20dfc792014-06-16 20:44:29 -07003858 DECLARE_INSTRUCTION(LessThanOrEqual);
3859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003861 return kCondLE;
3862 }
3863
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003864 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003865 return kCondGT;
3866 }
3867
Artem Serovcced8ba2017-07-19 18:18:09 +01003868 protected:
3869 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3870
Dave Allison20dfc792014-06-16 20:44:29 -07003871 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003872 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003873};
3874
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003875class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003876 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003877 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303878 : HCondition(kGreaterThan, first, second, dex_pc) {
3879 }
Dave Allison20dfc792014-06-16 20:44:29 -07003880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003881 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003882 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003883 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003884 // In the following Evaluate methods, a HCompare instruction has
3885 // been merged into this HGreaterThan instruction; evaluate it as
3886 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003887 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003888 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3889 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003890 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003891 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3892 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003893 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003894 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003895 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003896
Dave Allison20dfc792014-06-16 20:44:29 -07003897 DECLARE_INSTRUCTION(GreaterThan);
3898
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003899 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003900 return kCondGT;
3901 }
3902
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003903 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003904 return kCondLE;
3905 }
3906
Artem Serovcced8ba2017-07-19 18:18:09 +01003907 protected:
3908 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3909
Dave Allison20dfc792014-06-16 20:44:29 -07003910 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003911 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003912};
3913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003914class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003915 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003916 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303917 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3918 }
Dave Allison20dfc792014-06-16 20:44:29 -07003919
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003920 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003921 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003922 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003923 // In the following Evaluate methods, a HCompare instruction has
3924 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3925 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003926 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003927 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3928 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003929 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003930 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3931 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003932 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003933 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003934 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003935
Dave Allison20dfc792014-06-16 20:44:29 -07003936 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3937
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003938 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003939 return kCondGE;
3940 }
3941
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003942 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003943 return kCondLT;
3944 }
3945
Artem Serovcced8ba2017-07-19 18:18:09 +01003946 protected:
3947 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3948
Dave Allison20dfc792014-06-16 20:44:29 -07003949 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003950 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003951};
3952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003953class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003954 public:
3955 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303956 : HCondition(kBelow, first, second, dex_pc) {
3957 }
Aart Bike9f37602015-10-09 11:15:55 -07003958
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003959 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003960 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003961 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003962 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003963 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3964 }
3965 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003966 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003967 LOG(FATAL) << DebugName() << " is not defined for float values";
3968 UNREACHABLE();
3969 }
3970 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003971 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003972 LOG(FATAL) << DebugName() << " is not defined for double values";
3973 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003974 }
3975
3976 DECLARE_INSTRUCTION(Below);
3977
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003978 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003979 return kCondB;
3980 }
3981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003982 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003983 return kCondAE;
3984 }
3985
Artem Serovcced8ba2017-07-19 18:18:09 +01003986 protected:
3987 DEFAULT_COPY_CONSTRUCTOR(Below);
3988
Aart Bike9f37602015-10-09 11:15:55 -07003989 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003990 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003991 return MakeUnsigned(x) < MakeUnsigned(y);
3992 }
Aart Bike9f37602015-10-09 11:15:55 -07003993};
3994
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003995class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003996 public:
3997 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303998 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3999 }
Aart Bike9f37602015-10-09 11:15:55 -07004000
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004002 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004003 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004004 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004005 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4006 }
4007 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004008 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004009 LOG(FATAL) << DebugName() << " is not defined for float values";
4010 UNREACHABLE();
4011 }
4012 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004013 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004014 LOG(FATAL) << DebugName() << " is not defined for double values";
4015 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004016 }
4017
4018 DECLARE_INSTRUCTION(BelowOrEqual);
4019
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004020 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004021 return kCondBE;
4022 }
4023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004024 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004025 return kCondA;
4026 }
4027
Artem Serovcced8ba2017-07-19 18:18:09 +01004028 protected:
4029 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4030
Aart Bike9f37602015-10-09 11:15:55 -07004031 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004032 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004033 return MakeUnsigned(x) <= MakeUnsigned(y);
4034 }
Aart Bike9f37602015-10-09 11:15:55 -07004035};
4036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004037class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004038 public:
4039 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304040 : HCondition(kAbove, first, second, dex_pc) {
4041 }
Aart Bike9f37602015-10-09 11:15:55 -07004042
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004043 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004044 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004045 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004046 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004047 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4048 }
4049 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004050 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004051 LOG(FATAL) << DebugName() << " is not defined for float values";
4052 UNREACHABLE();
4053 }
4054 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004055 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004056 LOG(FATAL) << DebugName() << " is not defined for double values";
4057 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004058 }
4059
4060 DECLARE_INSTRUCTION(Above);
4061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004062 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004063 return kCondA;
4064 }
4065
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004066 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004067 return kCondBE;
4068 }
4069
Artem Serovcced8ba2017-07-19 18:18:09 +01004070 protected:
4071 DEFAULT_COPY_CONSTRUCTOR(Above);
4072
Aart Bike9f37602015-10-09 11:15:55 -07004073 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004074 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004075 return MakeUnsigned(x) > MakeUnsigned(y);
4076 }
Aart Bike9f37602015-10-09 11:15:55 -07004077};
4078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004079class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004080 public:
4081 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304082 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4083 }
Aart Bike9f37602015-10-09 11:15:55 -07004084
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004085 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004086 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004087 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004088 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004089 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4090 }
4091 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004092 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004093 LOG(FATAL) << DebugName() << " is not defined for float values";
4094 UNREACHABLE();
4095 }
4096 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004097 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004098 LOG(FATAL) << DebugName() << " is not defined for double values";
4099 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004100 }
4101
4102 DECLARE_INSTRUCTION(AboveOrEqual);
4103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004104 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004105 return kCondAE;
4106 }
4107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004108 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004109 return kCondB;
4110 }
4111
Artem Serovcced8ba2017-07-19 18:18:09 +01004112 protected:
4113 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4114
Aart Bike9f37602015-10-09 11:15:55 -07004115 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004116 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004117 return MakeUnsigned(x) >= MakeUnsigned(y);
4118 }
Aart Bike9f37602015-10-09 11:15:55 -07004119};
Dave Allison20dfc792014-06-16 20:44:29 -07004120
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004121// Instruction to check how two inputs compare to each other.
4122// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004123class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004124 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004125 // Note that `comparison_type` is the type of comparison performed
4126 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004127 // HCompare instruction (which is always DataType::Type::kInt).
4128 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004129 HInstruction* first,
4130 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004131 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004132 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304133 : HBinaryOperation(kCompare,
4134 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004135 first,
4136 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004137 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004138 dex_pc) {
4139 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004140 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4141 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004142 }
4143
Roland Levillain9867bc72015-08-05 10:21:34 +01004144 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004145 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4146
4147 template <typename T>
4148 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004150 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4151 // Handle the bias.
4152 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4153 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004155 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004156 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4157 // reach this code path when processing a freshly built HIR
4158 // graph. However HCompare integer instructions can be synthesized
4159 // by the instruction simplifier to implement IntegerCompare and
4160 // IntegerSignum intrinsics, so we have to handle this case.
4161 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004162 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004163 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004164 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4165 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004166 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004167 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4168 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004169 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004170 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004171 }
4172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004173 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004174 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004175 }
4176
Vladimir Markoa1de9182016-02-25 11:37:38 +00004177 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004178
Roland Levillain31dd3d62016-02-16 12:21:02 +00004179 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004180 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004181 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004182 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004183 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004184 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004186 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004187 // Comparisons do not require a runtime call in any back end.
4188 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004189 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004190
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004191 DECLARE_INSTRUCTION(Compare);
4192
Roland Levillain31dd3d62016-02-16 12:21:02 +00004193 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004194 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004195 static constexpr size_t kFieldComparisonBiasSize =
4196 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4197 static constexpr size_t kNumberOfComparePackedBits =
4198 kFieldComparisonBias + kFieldComparisonBiasSize;
4199 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4200 using ComparisonBiasField =
4201 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4202
Roland Levillain31dd3d62016-02-16 12:21:02 +00004203 // Return an integer constant containing the result of a comparison evaluated at compile time.
4204 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4205 DCHECK(value == -1 || value == 0 || value == 1) << value;
4206 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4207 }
4208
Artem Serovcced8ba2017-07-19 18:18:09 +01004209 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004210};
4211
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004212class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004213 public:
4214 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004215 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004216 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004217 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004218 bool finalizable,
4219 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304220 : HExpression(kNewInstance,
4221 DataType::Type::kReference,
4222 SideEffects::CanTriggerGC(),
4223 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004224 type_index_(type_index),
4225 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004226 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004227 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004228 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004229 }
4230
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004231 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004232
Andreas Gampea5b09a62016-11-17 15:21:22 -08004233 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004234 const DexFile& GetDexFile() const { return dex_file_; }
4235
4236 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004237 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004238
Mingyao Yang062157f2016-03-02 10:15:36 -08004239 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004240 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004241
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004242 bool NeedsChecks() const {
4243 return entrypoint_ == kQuickAllocObjectWithChecks;
4244 }
David Brazdil6de19382016-01-08 17:37:10 +00004245
Vladimir Markoa1de9182016-02-25 11:37:38 +00004246 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004248 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004249
4250 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4251
4252 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4253 entrypoint_ = entrypoint;
4254 }
4255
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004256 HLoadClass* GetLoadClass() const {
4257 HInstruction* input = InputAt(0);
4258 if (input->IsClinitCheck()) {
4259 input = input->InputAt(0);
4260 }
4261 DCHECK(input->IsLoadClass());
4262 return input->AsLoadClass();
4263 }
4264
David Brazdil6de19382016-01-08 17:37:10 +00004265 bool IsStringAlloc() const;
4266
4267 DECLARE_INSTRUCTION(NewInstance);
4268
Artem Serovcced8ba2017-07-19 18:18:09 +01004269 protected:
4270 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4271
David Brazdil6de19382016-01-08 17:37:10 +00004272 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004273 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004274 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4275 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4276 "Too many packed fields.");
4277
Andreas Gampea5b09a62016-11-17 15:21:22 -08004278 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004279 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004280 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004281};
4282
Agi Csaki05f20562015-08-19 14:58:14 -07004283enum IntrinsicNeedsEnvironmentOrCache {
4284 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4285 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004286};
4287
Aart Bik5d75afe2015-12-14 11:57:01 -08004288enum IntrinsicSideEffects {
4289 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4290 kReadSideEffects, // Intrinsic may read heap memory.
4291 kWriteSideEffects, // Intrinsic may write heap memory.
4292 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4293};
4294
4295enum IntrinsicExceptions {
4296 kNoThrow, // Intrinsic does not throw any exceptions.
4297 kCanThrow // Intrinsic may throw exceptions.
4298};
4299
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004300class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004301 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004302 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004303
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004304 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004305 SetRawInputAt(index, argument);
4306 }
4307
Roland Levillain3e3d7332015-04-28 11:00:54 +01004308 // Return the number of arguments. This number can be lower than
4309 // the number of inputs returned by InputCount(), as some invoke
4310 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4311 // inputs at the end of their list of inputs.
4312 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4313
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004314 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4315
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004316 InvokeType GetInvokeType() const {
4317 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004318 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004319
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004320 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004321 return intrinsic_;
4322 }
4323
Aart Bik5d75afe2015-12-14 11:57:01 -08004324 void SetIntrinsic(Intrinsics intrinsic,
4325 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4326 IntrinsicSideEffects side_effects,
4327 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004328
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004329 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004330 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004331 }
4332
Aart Bikff7d89c2016-11-07 08:49:28 -08004333 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4334
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004335 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004336
Aart Bika8b8e9b2018-01-09 11:01:02 -08004337 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4338
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004339 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004340
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004341 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004343 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004344 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4345 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004346
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004347 uint32_t* GetIntrinsicOptimizations() {
4348 return &intrinsic_optimizations_;
4349 }
4350
4351 const uint32_t* GetIntrinsicOptimizations() const {
4352 return &intrinsic_optimizations_;
4353 }
4354
4355 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4356
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004357 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004358 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004359
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004360 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004361
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004362 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004363 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4364 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004365 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004366 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004367 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4368 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004369 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004370 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004371
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304372 HInvoke(InstructionKind kind,
4373 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004374 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004375 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004376 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004377 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004378 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004379 ArtMethod* resolved_method,
4380 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004381 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304382 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004383 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004384 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4385 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004386 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004387 number_of_arguments + number_of_other_inputs,
4388 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004389 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004390 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004391 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004392 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004393 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004394 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004395 // Check mutator lock, constructors lack annotalysis support.
4396 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4397 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004398 }
4399
Artem Serovcced8ba2017-07-19 18:18:09 +01004400 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4401
Roland Levillain3e3d7332015-04-28 11:00:54 +01004402 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004403 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004404 const uint32_t dex_method_index_;
4405 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004406
4407 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4408 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004409};
4410
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004411class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004412 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004413 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004414 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004415 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004416 uint32_t dex_pc,
4417 uint32_t dex_method_index,
4418 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304419 : HInvoke(kInvokeUnresolved,
4420 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004421 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004422 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004423 return_type,
4424 dex_pc,
4425 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004426 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004427 invoke_type) {
4428 }
4429
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004430 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004431
Calin Juravle175dc732015-08-25 15:42:32 +01004432 DECLARE_INSTRUCTION(InvokeUnresolved);
4433
Artem Serovcced8ba2017-07-19 18:18:09 +01004434 protected:
4435 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004436};
4437
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004438class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004439 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004440 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004441 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004442 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004443 uint32_t dex_pc,
4444 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304445 : HInvoke(kInvokePolymorphic,
4446 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004447 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004448 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004449 return_type,
4450 dex_pc,
4451 dex_method_index,
4452 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304453 kVirtual) {
4454 }
Orion Hodsonac141392017-01-13 11:53:47 +00004455
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004456 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004457
Orion Hodsonac141392017-01-13 11:53:47 +00004458 DECLARE_INSTRUCTION(InvokePolymorphic);
4459
Artem Serovcced8ba2017-07-19 18:18:09 +01004460 protected:
4461 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004462};
4463
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004464class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004465 public:
4466 HInvokeCustom(ArenaAllocator* allocator,
4467 uint32_t number_of_arguments,
4468 uint32_t call_site_index,
4469 DataType::Type return_type,
4470 uint32_t dex_pc)
4471 : HInvoke(kInvokeCustom,
4472 allocator,
4473 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004474 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004475 return_type,
4476 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004477 /* dex_method_index= */ dex::kDexNoIndex,
4478 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004479 kStatic),
4480 call_site_index_(call_site_index) {
4481 }
4482
4483 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4484
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004485 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004486
4487 DECLARE_INSTRUCTION(InvokeCustom);
4488
4489 protected:
4490 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4491
4492 private:
4493 uint32_t call_site_index_;
4494};
4495
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004496class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004497 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004498 // Requirements of this method call regarding the class
4499 // initialization (clinit) check of its declaring class.
4500 enum class ClinitCheckRequirement {
4501 kNone, // Class already initialized.
4502 kExplicit, // Static call having explicit clinit check as last input.
4503 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004504 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004505 };
4506
Vladimir Marko58155012015-08-19 12:49:41 +00004507 // Determines how to load the target ArtMethod*.
4508 enum class MethodLoadKind {
4509 // Use a String init ArtMethod* loaded from Thread entrypoints.
4510 kStringInit,
4511
4512 // Use the method's own ArtMethod* loaded by the register allocator.
4513 kRecursive,
4514
Vladimir Marko65979462017-05-19 17:25:12 +01004515 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4516 // Used for boot image methods referenced by boot image code.
4517 kBootImageLinkTimePcRelative,
4518
Vladimir Markob066d432018-01-03 13:14:37 +00004519 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4520 // Used for app->boot calls with relocatable image.
4521 kBootImageRelRo,
4522
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004523 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004524 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004525 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004526
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004527 // Use ArtMethod* at a known address, embed the direct address in the code.
4528 // Used for for JIT-compiled calls.
4529 kJitDirectAddress,
4530
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004531 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4532 // used when other kinds are unimplemented on a particular architecture.
4533 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004534 };
4535
4536 // Determines the location of the code pointer.
4537 enum class CodePtrLocation {
4538 // Recursive call, use local PC-relative call instruction.
4539 kCallSelf,
4540
Vladimir Marko58155012015-08-19 12:49:41 +00004541 // Use code pointer from the ArtMethod*.
4542 // Used when we don't know the target code. This is also the last-resort-kind used when
4543 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4544 kCallArtMethod,
4545 };
4546
4547 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004548 MethodLoadKind method_load_kind;
4549 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004550 // The method load data holds
4551 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4552 // Note that there are multiple string init methods, each having its own offset.
4553 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004554 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004555 };
4556
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004557 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004558 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004559 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004560 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004561 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004562 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004563 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004564 InvokeType invoke_type,
4565 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004566 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304567 : HInvoke(kInvokeStaticOrDirect,
4568 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004569 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004570 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004571 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004572 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004573 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004574 return_type,
4575 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004576 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004577 resolved_method,
4578 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004579 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004580 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004581 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4582 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004583
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004584 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004585
Vladimir Markodc151b22015-10-15 18:02:30 +01004586 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004587 bool had_current_method_input = HasCurrentMethodInput();
4588 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4589
4590 // Using the current method is the default and once we find a better
4591 // method load kind, we should not go back to using the current method.
4592 DCHECK(had_current_method_input || !needs_current_method_input);
4593
4594 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004595 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4596 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004597 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004598 dispatch_info_ = dispatch_info;
4599 }
4600
Aart Bik6daebeb2017-04-03 14:35:41 -07004601 DispatchInfo GetDispatchInfo() const {
4602 return dispatch_info_;
4603 }
4604
Vladimir Markoc53c0792015-11-19 15:48:33 +00004605 void AddSpecialInput(HInstruction* input) {
4606 // We allow only one special input.
4607 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4608 DCHECK(InputCount() == GetSpecialInputIndex() ||
4609 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4610 InsertInputAt(GetSpecialInputIndex(), input);
4611 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004612
Vladimir Marko372f10e2016-05-17 16:30:10 +01004613 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004614 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004615 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4616 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4617 DCHECK(!input_records.empty());
4618 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4619 HInstruction* last_input = input_records.back().GetInstruction();
4620 // Note: `last_input` may be null during arguments setup.
4621 if (last_input != nullptr) {
4622 // `last_input` is the last input of a static invoke marked as having
4623 // an explicit clinit check. It must either be:
4624 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4625 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4626 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4627 }
4628 }
4629 return input_records;
4630 }
4631
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004632 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004633 // We access the method via the dex cache so we can't do an implicit null check.
4634 // TODO: for intrinsics we can generate implicit null checks.
4635 return false;
4636 }
4637
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004638 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004639 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004640 }
4641
Vladimir Markoc53c0792015-11-19 15:48:33 +00004642 // Get the index of the special input, if any.
4643 //
David Brazdil6de19382016-01-08 17:37:10 +00004644 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4645 // method pointer; otherwise there may be one platform-specific special input,
4646 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004647 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004648 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004649
Vladimir Marko58155012015-08-19 12:49:41 +00004650 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4651 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4652 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004653 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004654 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004655 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004656 bool HasPcRelativeMethodLoadKind() const {
4657 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004658 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004659 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004660 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004661 bool HasCurrentMethodInput() const {
4662 // This function can be called only after the invoke has been fully initialized by the builder.
4663 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004664 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004665 return true;
4666 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004667 DCHECK(InputCount() == GetSpecialInputIndex() ||
4668 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004669 return false;
4670 }
4671 }
Vladimir Marko58155012015-08-19 12:49:41 +00004672
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004673 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004674 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004675 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004676 }
4677
4678 uint64_t GetMethodAddress() const {
4679 DCHECK(HasMethodAddress());
4680 return dispatch_info_.method_load_data;
4681 }
4682
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004683 const DexFile& GetDexFileForPcRelativeDexCache() const;
4684
Vladimir Markoa1de9182016-02-25 11:37:38 +00004685 ClinitCheckRequirement GetClinitCheckRequirement() const {
4686 return GetPackedField<ClinitCheckRequirementField>();
4687 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004688
Roland Levillain4c0eb422015-04-24 16:43:49 +01004689 // Is this instruction a call to a static method?
4690 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004691 return GetInvokeType() == kStatic;
4692 }
4693
4694 MethodReference GetTargetMethod() const {
4695 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004696 }
4697
Vladimir Markofbb184a2015-11-13 14:47:00 +00004698 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4699 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4700 // instruction; only relevant for static calls with explicit clinit check.
4701 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004702 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004703 size_t last_input_index = inputs_.size() - 1u;
4704 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004705 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004706 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004707 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004708 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004709 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004710 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004711 }
4712
4713 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004714 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004715 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004716 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004717 }
4718
4719 // Is this a call to a static method whose declaring class has an
4720 // implicit intialization check requirement?
4721 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004722 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004723 }
4724
Vladimir Markob554b5a2015-11-06 12:57:55 +00004725 // Does this method load kind need the current method as an input?
4726 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004727 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004728 }
4729
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004730 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004731
Artem Serovcced8ba2017-07-19 18:18:09 +01004732 protected:
4733 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4734
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004735 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004736 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004737 static constexpr size_t kFieldClinitCheckRequirementSize =
4738 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4739 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4740 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4741 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4742 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004743 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4744 kFieldClinitCheckRequirement,
4745 kFieldClinitCheckRequirementSize>;
4746
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004747 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004748 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004749 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004750};
Vladimir Markof64242a2015-12-01 14:58:23 +00004751std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004752std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004753
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004754class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004755 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004756 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004757 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004758 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004759 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004760 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004761 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004762 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304763 : HInvoke(kInvokeVirtual,
4764 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004765 number_of_arguments,
4766 0u,
4767 return_type,
4768 dex_pc,
4769 dex_method_index,
4770 resolved_method,
4771 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304772 vtable_index_(vtable_index) {
4773 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004774
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004775 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004777 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004778 switch (GetIntrinsic()) {
4779 case Intrinsics::kThreadCurrentThread:
4780 case Intrinsics::kStringBufferAppend:
4781 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004782 case Intrinsics::kStringBuilderAppendObject:
4783 case Intrinsics::kStringBuilderAppendString:
4784 case Intrinsics::kStringBuilderAppendCharSequence:
4785 case Intrinsics::kStringBuilderAppendCharArray:
4786 case Intrinsics::kStringBuilderAppendBoolean:
4787 case Intrinsics::kStringBuilderAppendChar:
4788 case Intrinsics::kStringBuilderAppendInt:
4789 case Intrinsics::kStringBuilderAppendLong:
4790 case Intrinsics::kStringBuilderAppendFloat:
4791 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004792 case Intrinsics::kStringBuilderToString:
4793 return false;
4794 default:
4795 return HInvoke::CanBeNull();
4796 }
4797 }
4798
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004799 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004800 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004801 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004802 }
4803
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004804 uint32_t GetVTableIndex() const { return vtable_index_; }
4805
4806 DECLARE_INSTRUCTION(InvokeVirtual);
4807
Artem Serovcced8ba2017-07-19 18:18:09 +01004808 protected:
4809 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4810
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004811 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004812 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004813 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004814};
4815
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004816class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004817 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004818 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004819 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004820 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004821 uint32_t dex_pc,
4822 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004823 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004824 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304825 : HInvoke(kInvokeInterface,
4826 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004827 number_of_arguments,
4828 0u,
4829 return_type,
4830 dex_pc,
4831 dex_method_index,
4832 resolved_method,
4833 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304834 imt_index_(imt_index) {
4835 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004837 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004839 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004840 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004841 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004842 }
4843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004844 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004845 // The assembly stub currently needs it.
4846 return true;
4847 }
4848
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004849 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004850
4851 DECLARE_INSTRUCTION(InvokeInterface);
4852
Artem Serovcced8ba2017-07-19 18:18:09 +01004853 protected:
4854 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4855
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004856 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004857 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004858 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004859};
4860
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004861class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004862 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004863 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304864 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004866 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004867
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004868 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004869
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004870 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004871 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004872 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004873 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004874 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004875 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004876 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004877 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4878 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004879 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004880 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4881 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004882
Roland Levillain88cb1752014-10-20 16:36:47 +01004883 DECLARE_INSTRUCTION(Neg);
4884
Artem Serovcced8ba2017-07-19 18:18:09 +01004885 protected:
4886 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004887};
4888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004889class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004890 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004891 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304892 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004893 SetRawInputAt(0, cls);
4894 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004895 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004896 }
4897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004898 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004899
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004900 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004901 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004902
Mingyao Yang0c365e62015-03-31 15:09:29 -07004903 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004904 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004906 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004907
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004908 HLoadClass* GetLoadClass() const {
4909 DCHECK(InputAt(0)->IsLoadClass());
4910 return InputAt(0)->AsLoadClass();
4911 }
4912
4913 HInstruction* GetLength() const {
4914 return InputAt(1);
4915 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004916
Vladimir Markob5461632018-10-15 14:24:21 +01004917 size_t GetComponentSizeShift() {
4918 return GetPackedField<ComponentSizeShiftField>();
4919 }
4920
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004921 DECLARE_INSTRUCTION(NewArray);
4922
Artem Serovcced8ba2017-07-19 18:18:09 +01004923 protected:
4924 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004925
4926 private:
4927 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4928 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4929 static constexpr size_t kNumberOfNewArrayPackedBits =
4930 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4931 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4932 using ComponentSizeShiftField =
4933 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004934};
4935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004936class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004937 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004938 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004939 HInstruction* left,
4940 HInstruction* right,
4941 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304942 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4943 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004945 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004946
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004947 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004949 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004950 return GetBlock()->GetGraph()->GetIntConstant(
4951 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004952 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004953 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004954 return GetBlock()->GetGraph()->GetLongConstant(
4955 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004956 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004957 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004958 return GetBlock()->GetGraph()->GetFloatConstant(
4959 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4960 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004961 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004962 return GetBlock()->GetGraph()->GetDoubleConstant(
4963 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4964 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004965
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004966 DECLARE_INSTRUCTION(Add);
4967
Artem Serovcced8ba2017-07-19 18:18:09 +01004968 protected:
4969 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004970};
4971
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004972class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004973 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004974 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004975 HInstruction* left,
4976 HInstruction* right,
4977 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304978 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4979 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004980
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004981 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004982
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004983 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004984 return GetBlock()->GetGraph()->GetIntConstant(
4985 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004986 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004987 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004988 return GetBlock()->GetGraph()->GetLongConstant(
4989 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004990 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004991 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004992 return GetBlock()->GetGraph()->GetFloatConstant(
4993 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4994 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004995 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004996 return GetBlock()->GetGraph()->GetDoubleConstant(
4997 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4998 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004999
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005000 DECLARE_INSTRUCTION(Sub);
5001
Artem Serovcced8ba2017-07-19 18:18:09 +01005002 protected:
5003 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005004};
5005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005006class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005007 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005008 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005009 HInstruction* left,
5010 HInstruction* right,
5011 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305012 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5013 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005015 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005016
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005017 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005019 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005020 return GetBlock()->GetGraph()->GetIntConstant(
5021 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005022 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005023 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005024 return GetBlock()->GetGraph()->GetLongConstant(
5025 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005026 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005027 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005028 return GetBlock()->GetGraph()->GetFloatConstant(
5029 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5030 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005031 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005032 return GetBlock()->GetGraph()->GetDoubleConstant(
5033 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5034 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005035
5036 DECLARE_INSTRUCTION(Mul);
5037
Artem Serovcced8ba2017-07-19 18:18:09 +01005038 protected:
5039 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005040};
5041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005042class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005043 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005044 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005045 HInstruction* left,
5046 HInstruction* right,
5047 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305048 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5049 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005050
Roland Levillain9867bc72015-08-05 10:21:34 +01005051 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005052 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005053 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005054 // Our graph structure ensures we never have 0 for `y` during
5055 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005056 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005057 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005058 return (y == -1) ? -x : x / y;
5059 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005060
Roland Levillain31dd3d62016-02-16 12:21:02 +00005061 template <typename T>
5062 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005063 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005064 return x / y;
5065 }
5066
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005067 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005068 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005069 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005070 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005071 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005072 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005073 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5074 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005075 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005076 return GetBlock()->GetGraph()->GetFloatConstant(
5077 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5078 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005079 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005080 return GetBlock()->GetGraph()->GetDoubleConstant(
5081 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005082 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005083
5084 DECLARE_INSTRUCTION(Div);
5085
Artem Serovcced8ba2017-07-19 18:18:09 +01005086 protected:
5087 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005088};
5089
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005090class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005091 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005092 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005093 HInstruction* left,
5094 HInstruction* right,
5095 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305096 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5097 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005098
Roland Levillain9867bc72015-08-05 10:21:34 +01005099 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005100 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005101 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005102 // Our graph structure ensures we never have 0 for `y` during
5103 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005104 DCHECK_NE(y, 0);
5105 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5106 return (y == -1) ? 0 : x % y;
5107 }
5108
Roland Levillain31dd3d62016-02-16 12:21:02 +00005109 template <typename T>
5110 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005111 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005112 return std::fmod(x, y);
5113 }
5114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005115 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005116 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005117 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005118 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005119 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005120 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005121 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005122 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005123 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005124 return GetBlock()->GetGraph()->GetFloatConstant(
5125 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5126 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005127 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005128 return GetBlock()->GetGraph()->GetDoubleConstant(
5129 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5130 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005131
5132 DECLARE_INSTRUCTION(Rem);
5133
Artem Serovcced8ba2017-07-19 18:18:09 +01005134 protected:
5135 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005136};
5137
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005138class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005139 public:
5140 HMin(DataType::Type result_type,
5141 HInstruction* left,
5142 HInstruction* right,
5143 uint32_t dex_pc)
5144 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5145
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005146 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005147
5148 // Evaluation for integral values.
5149 template <typename T> static T ComputeIntegral(T x, T y) {
5150 return (x <= y) ? x : y;
5151 }
5152
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005153 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005154 return GetBlock()->GetGraph()->GetIntConstant(
5155 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5156 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005157 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005158 return GetBlock()->GetGraph()->GetLongConstant(
5159 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5160 }
5161 // TODO: Evaluation for floating-point values.
5162 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005163 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005164 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005165 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005166
5167 DECLARE_INSTRUCTION(Min);
5168
5169 protected:
5170 DEFAULT_COPY_CONSTRUCTOR(Min);
5171};
5172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005173class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005174 public:
5175 HMax(DataType::Type result_type,
5176 HInstruction* left,
5177 HInstruction* right,
5178 uint32_t dex_pc)
5179 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5180
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005181 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005182
5183 // Evaluation for integral values.
5184 template <typename T> static T ComputeIntegral(T x, T y) {
5185 return (x >= y) ? x : y;
5186 }
5187
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005188 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005189 return GetBlock()->GetGraph()->GetIntConstant(
5190 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5191 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005192 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005193 return GetBlock()->GetGraph()->GetLongConstant(
5194 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5195 }
5196 // TODO: Evaluation for floating-point values.
5197 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005198 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005199 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005200 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005201
5202 DECLARE_INSTRUCTION(Max);
5203
5204 protected:
5205 DEFAULT_COPY_CONSTRUCTOR(Max);
5206};
5207
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005208class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005209 public:
5210 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5211 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5212
5213 // Evaluation for integral values.
5214 template <typename T> static T ComputeIntegral(T x) {
5215 return x < 0 ? -x : x;
5216 }
5217
5218 // Evaluation for floating-point values.
5219 // Note, as a "quality of implementation", rather than pure "spec compliance",
5220 // we require that Math.abs() clears the sign bit (but changes nothing else)
5221 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5222 // http://b/30758343
5223 template <typename T, typename S> static T ComputeFP(T x) {
5224 S bits = bit_cast<S, T>(x);
5225 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5226 }
5227
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005228 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005229 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5230 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005231 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005232 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5233 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005234 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005235 return GetBlock()->GetGraph()->GetFloatConstant(
5236 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5237 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005238 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005239 return GetBlock()->GetGraph()->GetDoubleConstant(
5240 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5241 }
5242
5243 DECLARE_INSTRUCTION(Abs);
5244
5245 protected:
5246 DEFAULT_COPY_CONSTRUCTOR(Abs);
5247};
5248
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005249class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005250 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005251 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005252 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5253 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005254 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005255 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005256 SetRawInputAt(0, value);
5257 }
5258
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005259 bool IsClonable() const override { return true; }
5260 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005262 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005263 return true;
5264 }
5265
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005266 bool NeedsEnvironment() const override { return true; }
5267 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005268
Calin Juravled0d48522014-11-04 16:40:20 +00005269 DECLARE_INSTRUCTION(DivZeroCheck);
5270
Artem Serovcced8ba2017-07-19 18:18:09 +01005271 protected:
5272 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005273};
5274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005275class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005276 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005277 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005278 HInstruction* value,
5279 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005280 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305281 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005282 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5283 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005284 }
5285
Roland Levillain5b5b9312016-03-22 14:57:31 +00005286 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005287 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005288 return value << (distance & max_shift_distance);
5289 }
5290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005291 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005292 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005293 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005294 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005295 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005296 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005297 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005298 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005299 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005300 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005301 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5302 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005303 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005304 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005305 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005306 LOG(FATAL) << DebugName() << " is not defined for float values";
5307 UNREACHABLE();
5308 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005309 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005310 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005311 LOG(FATAL) << DebugName() << " is not defined for double values";
5312 UNREACHABLE();
5313 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005314
5315 DECLARE_INSTRUCTION(Shl);
5316
Artem Serovcced8ba2017-07-19 18:18:09 +01005317 protected:
5318 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005319};
5320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005321class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005322 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005323 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005324 HInstruction* value,
5325 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005326 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305327 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005328 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5329 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005330 }
5331
Roland Levillain5b5b9312016-03-22 14:57:31 +00005332 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005333 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005334 return value >> (distance & max_shift_distance);
5335 }
5336
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005337 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005338 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005339 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005340 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005341 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005342 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005343 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005344 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005345 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005346 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005347 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5348 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005349 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005350 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005351 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005352 LOG(FATAL) << DebugName() << " is not defined for float values";
5353 UNREACHABLE();
5354 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005355 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005356 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005357 LOG(FATAL) << DebugName() << " is not defined for double values";
5358 UNREACHABLE();
5359 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005360
5361 DECLARE_INSTRUCTION(Shr);
5362
Artem Serovcced8ba2017-07-19 18:18:09 +01005363 protected:
5364 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005365};
5366
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005367class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005368 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005369 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005370 HInstruction* value,
5371 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005372 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305373 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005374 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5375 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005376 }
5377
Roland Levillain5b5b9312016-03-22 14:57:31 +00005378 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005379 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005380 typedef typename std::make_unsigned<T>::type V;
5381 V ux = static_cast<V>(value);
5382 return static_cast<T>(ux >> (distance & max_shift_distance));
5383 }
5384
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005385 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005386 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005387 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005388 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005389 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005390 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005391 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005392 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005393 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005394 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005395 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5396 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005397 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005398 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005399 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005400 LOG(FATAL) << DebugName() << " is not defined for float values";
5401 UNREACHABLE();
5402 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005403 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005404 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005405 LOG(FATAL) << DebugName() << " is not defined for double values";
5406 UNREACHABLE();
5407 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005408
5409 DECLARE_INSTRUCTION(UShr);
5410
Artem Serovcced8ba2017-07-19 18:18:09 +01005411 protected:
5412 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005413};
5414
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005415class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005416 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005417 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005418 HInstruction* left,
5419 HInstruction* right,
5420 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305421 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5422 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005423
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005424 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005425
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005426 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005427
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005428 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005429 return GetBlock()->GetGraph()->GetIntConstant(
5430 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005431 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005432 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005433 return GetBlock()->GetGraph()->GetLongConstant(
5434 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005435 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005436 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005437 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005438 LOG(FATAL) << DebugName() << " is not defined for float values";
5439 UNREACHABLE();
5440 }
5441 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005442 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005443 LOG(FATAL) << DebugName() << " is not defined for double values";
5444 UNREACHABLE();
5445 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005446
5447 DECLARE_INSTRUCTION(And);
5448
Artem Serovcced8ba2017-07-19 18:18:09 +01005449 protected:
5450 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005451};
5452
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005453class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005454 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005455 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005456 HInstruction* left,
5457 HInstruction* right,
5458 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305459 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5460 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005461
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005463
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005464 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005465
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005466 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005467 return GetBlock()->GetGraph()->GetIntConstant(
5468 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005469 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005470 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005471 return GetBlock()->GetGraph()->GetLongConstant(
5472 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005473 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005474 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005475 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005476 LOG(FATAL) << DebugName() << " is not defined for float values";
5477 UNREACHABLE();
5478 }
5479 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005480 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005481 LOG(FATAL) << DebugName() << " is not defined for double values";
5482 UNREACHABLE();
5483 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005484
5485 DECLARE_INSTRUCTION(Or);
5486
Artem Serovcced8ba2017-07-19 18:18:09 +01005487 protected:
5488 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005489};
5490
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005491class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005492 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005493 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005494 HInstruction* left,
5495 HInstruction* right,
5496 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305497 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5498 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005500 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005501
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005502 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005503
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005504 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005505 return GetBlock()->GetGraph()->GetIntConstant(
5506 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005507 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005508 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005509 return GetBlock()->GetGraph()->GetLongConstant(
5510 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005511 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005512 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005513 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005514 LOG(FATAL) << DebugName() << " is not defined for float values";
5515 UNREACHABLE();
5516 }
5517 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005518 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005519 LOG(FATAL) << DebugName() << " is not defined for double values";
5520 UNREACHABLE();
5521 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005522
5523 DECLARE_INSTRUCTION(Xor);
5524
Artem Serovcced8ba2017-07-19 18:18:09 +01005525 protected:
5526 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005527};
5528
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005529class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005530 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005531 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305532 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005533 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5534 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005535 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005536
Roland Levillain5b5b9312016-03-22 14:57:31 +00005537 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005538 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005539 typedef typename std::make_unsigned<T>::type V;
5540 V ux = static_cast<V>(value);
5541 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005542 return static_cast<T>(ux);
5543 } else {
5544 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005545 return static_cast<T>(ux >> (distance & max_shift_value)) |
5546 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005547 }
5548 }
5549
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005550 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005551 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005552 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005553 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005554 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005555 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005556 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005557 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005558 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005559 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005560 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5561 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005562 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005563 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005564 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005565 LOG(FATAL) << DebugName() << " is not defined for float values";
5566 UNREACHABLE();
5567 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005568 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005569 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005570 LOG(FATAL) << DebugName() << " is not defined for double values";
5571 UNREACHABLE();
5572 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005573
5574 DECLARE_INSTRUCTION(Ror);
5575
Artem Serovcced8ba2017-07-19 18:18:09 +01005576 protected:
5577 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005578};
5579
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005580// The value of a parameter in this method. Its location depends on
5581// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005582class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005583 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005584 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005585 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005586 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005587 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005588 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305589 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005590 dex_file_(dex_file),
5591 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005592 index_(index) {
5593 SetPackedFlag<kFlagIsThis>(is_this);
5594 SetPackedFlag<kFlagCanBeNull>(!is_this);
5595 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005596
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005597 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005598 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005599 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005600 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005602 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005603 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005604
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005605 DECLARE_INSTRUCTION(ParameterValue);
5606
Artem Serovcced8ba2017-07-19 18:18:09 +01005607 protected:
5608 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5609
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005610 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005611 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005612 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005613 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5614 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5615 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5616 "Too many packed fields.");
5617
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005618 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005619 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005620 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005621 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005622 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005623};
5624
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005625class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005626 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005627 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305628 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5629 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005630
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005631 bool CanBeMoved() const override { return true; }
5632 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005633 return true;
5634 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005635
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005636 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005637
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005638 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005639 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005640 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005641 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005642 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005643 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005644 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005645 LOG(FATAL) << DebugName() << " is not defined for float values";
5646 UNREACHABLE();
5647 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005648 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005649 LOG(FATAL) << DebugName() << " is not defined for double values";
5650 UNREACHABLE();
5651 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005652
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005653 DECLARE_INSTRUCTION(Not);
5654
Artem Serovcced8ba2017-07-19 18:18:09 +01005655 protected:
5656 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005657};
5658
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005659class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005660 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005661 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305662 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5663 }
David Brazdil66d126e2015-04-03 16:02:44 +01005664
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005665 bool CanBeMoved() const override { return true; }
5666 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005667 return true;
5668 }
5669
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005670 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005671 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005672 return !x;
5673 }
5674
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005675 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005676 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005677 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005678 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005679 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005680 UNREACHABLE();
5681 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005682 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005683 LOG(FATAL) << DebugName() << " is not defined for float values";
5684 UNREACHABLE();
5685 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005686 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005687 LOG(FATAL) << DebugName() << " is not defined for double values";
5688 UNREACHABLE();
5689 }
David Brazdil66d126e2015-04-03 16:02:44 +01005690
5691 DECLARE_INSTRUCTION(BooleanNot);
5692
Artem Serovcced8ba2017-07-19 18:18:09 +01005693 protected:
5694 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005695};
5696
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005697class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005698 public:
5699 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005700 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305701 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005702 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005703 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005704 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005705 }
5706
5707 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005708 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5709 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005710
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005711 bool IsClonable() const override { return true; }
5712 bool CanBeMoved() const override { return true; }
5713 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005714 return true;
5715 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005716 // Return whether the conversion is implicit. This includes conversion to the same type.
5717 bool IsImplicitConversion() const {
5718 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5719 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005720
Mark Mendelle82549b2015-05-06 10:55:34 -04005721 // Try to statically evaluate the conversion and return a HConstant
5722 // containing the result. If the input cannot be converted, return nullptr.
5723 HConstant* TryStaticEvaluation() const;
5724
Roland Levillaindff1f282014-11-05 14:15:05 +00005725 DECLARE_INSTRUCTION(TypeConversion);
5726
Artem Serovcced8ba2017-07-19 18:18:09 +01005727 protected:
5728 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005729};
5730
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005731static constexpr uint32_t kNoRegNumber = -1;
5732
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005733class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005734 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005735 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005736 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5737 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005738 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005739 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005740 SetRawInputAt(0, value);
5741 }
5742
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005743 bool IsClonable() const override { return true; }
5744 bool CanBeMoved() const override { return true; }
5745 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005746 return true;
5747 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005748
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005749 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005750
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005751 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005752
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005753 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005754
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005755 DECLARE_INSTRUCTION(NullCheck);
5756
Artem Serovcced8ba2017-07-19 18:18:09 +01005757 protected:
5758 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005759};
5760
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005761// Embeds an ArtField and all the information required by the compiler. We cache
5762// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005763class FieldInfo : public ValueObject {
5764 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005765 FieldInfo(ArtField* field,
5766 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005767 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005768 bool is_volatile,
5769 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005770 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005771 const DexFile& dex_file)
5772 : field_(field),
5773 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005774 field_type_(field_type),
5775 is_volatile_(is_volatile),
5776 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005777 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005778 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005779
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005780 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005781 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005782 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005783 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005784 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005785 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005786 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005787
5788 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005789 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005790 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005791 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005792 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005793 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005794 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005795 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005796};
5797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005798class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005799 public:
5800 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005801 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005802 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005803 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005804 bool is_volatile,
5805 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005806 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005807 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005808 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305809 : HExpression(kInstanceFieldGet,
5810 field_type,
5811 SideEffects::FieldReadOfType(field_type, is_volatile),
5812 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005813 field_info_(field,
5814 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005815 field_type,
5816 is_volatile,
5817 field_idx,
5818 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005819 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005820 SetRawInputAt(0, value);
5821 }
5822
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005823 bool IsClonable() const override { return true; }
5824 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005825
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005826 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005827 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005828 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005829 }
5830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005831 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005832 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005833 }
5834
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005835 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005836 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5837 }
5838
Calin Juravle52c48962014-12-16 17:02:57 +00005839 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005840 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005841 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005842 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005843
Vladimir Marko61b92282017-10-11 13:23:17 +01005844 void SetType(DataType::Type new_type) {
5845 DCHECK(DataType::IsIntegralType(GetType()));
5846 DCHECK(DataType::IsIntegralType(new_type));
5847 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5848 SetPackedField<TypeField>(new_type);
5849 }
5850
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005851 DECLARE_INSTRUCTION(InstanceFieldGet);
5852
Artem Serovcced8ba2017-07-19 18:18:09 +01005853 protected:
5854 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5855
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005856 private:
5857 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005858};
5859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005860class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005861 public:
5862 HInstanceFieldSet(HInstruction* object,
5863 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005864 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005865 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005866 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005867 bool is_volatile,
5868 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005869 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005870 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005871 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005872 : HExpression(kInstanceFieldSet,
5873 SideEffects::FieldWriteOfType(field_type, is_volatile),
5874 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005875 field_info_(field,
5876 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005877 field_type,
5878 is_volatile,
5879 field_idx,
5880 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005881 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005882 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005883 SetRawInputAt(0, object);
5884 SetRawInputAt(1, value);
5885 }
5886
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005887 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005889 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005890 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005891 }
5892
Calin Juravle52c48962014-12-16 17:02:57 +00005893 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005894 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005896 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005897 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005898 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5899 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005900
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005901 DECLARE_INSTRUCTION(InstanceFieldSet);
5902
Artem Serovcced8ba2017-07-19 18:18:09 +01005903 protected:
5904 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5905
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005906 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005907 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5908 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5909 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5910 "Too many packed fields.");
5911
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005912 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005913};
5914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005915class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005916 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005917 HArrayGet(HInstruction* array,
5918 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005919 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005920 uint32_t dex_pc)
5921 : HArrayGet(array,
5922 index,
5923 type,
5924 SideEffects::ArrayReadOfType(type),
5925 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005926 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305927 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005928
5929 HArrayGet(HInstruction* array,
5930 HInstruction* index,
5931 DataType::Type type,
5932 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005933 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005934 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305935 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005936 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005937 SetRawInputAt(0, array);
5938 SetRawInputAt(1, index);
5939 }
5940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005941 bool IsClonable() const override { return true; }
5942 bool CanBeMoved() const override { return true; }
5943 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005944 return true;
5945 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005946 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005947 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005948 // Currently, unless the array is the result of NewArray, the array access is always
5949 // preceded by some form of null NullCheck necessary for the bounds check, usually
5950 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5951 // dynamic BCE. There are cases when these could be removed to produce better code.
5952 // If we ever add optimizations to do so we should allow an implicit check here
5953 // (as long as the address falls in the first page).
5954 //
5955 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5956 // a = cond ? new int[1] : null;
5957 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005958 return false;
5959 }
5960
David Brazdil4833f5a2015-12-16 10:37:39 +00005961 bool IsEquivalentOf(HArrayGet* other) const {
5962 bool result = (GetDexPc() == other->GetDexPc());
5963 if (kIsDebugBuild && result) {
5964 DCHECK_EQ(GetBlock(), other->GetBlock());
5965 DCHECK_EQ(GetArray(), other->GetArray());
5966 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 if (DataType::IsIntOrLongType(GetType())) {
5968 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005969 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005970 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5971 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005972 }
5973 }
5974 return result;
5975 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005976
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005977 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5978
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005979 HInstruction* GetArray() const { return InputAt(0); }
5980 HInstruction* GetIndex() const { return InputAt(1); }
5981
Vladimir Marko61b92282017-10-11 13:23:17 +01005982 void SetType(DataType::Type new_type) {
5983 DCHECK(DataType::IsIntegralType(GetType()));
5984 DCHECK(DataType::IsIntegralType(new_type));
5985 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5986 SetPackedField<TypeField>(new_type);
5987 }
5988
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005989 DECLARE_INSTRUCTION(ArrayGet);
5990
Artem Serovcced8ba2017-07-19 18:18:09 +01005991 protected:
5992 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5993
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005994 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005995 // We treat a String as an array, creating the HArrayGet from String.charAt()
5996 // intrinsic in the instruction simplifier. We can always determine whether
5997 // a particular HArrayGet is actually a String.charAt() by looking at the type
5998 // of the input but that requires holding the mutator lock, so we prefer to use
5999 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006000 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006001 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6002 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6003 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006004};
6005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006006class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006007 public:
6008 HArraySet(HInstruction* array,
6009 HInstruction* index,
6010 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006011 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006012 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006013 : HArraySet(array,
6014 index,
6015 value,
6016 expected_component_type,
6017 // Make a best guess for side effects now, may be refined during SSA building.
6018 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306019 dex_pc) {
6020 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006021
6022 HArraySet(HInstruction* array,
6023 HInstruction* index,
6024 HInstruction* value,
6025 DataType::Type expected_component_type,
6026 SideEffects side_effects,
6027 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006028 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006029 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006030 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006031 SetPackedFlag<kFlagValueCanBeNull>(true);
6032 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006033 SetRawInputAt(0, array);
6034 SetRawInputAt(1, index);
6035 SetRawInputAt(2, value);
6036 }
6037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006038 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006039
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006040 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006041 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006042 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006043 }
6044
Mingyao Yang81014cb2015-06-02 03:16:27 -07006045 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006046 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006047
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006048 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006049 // TODO: Same as for ArrayGet.
6050 return false;
6051 }
6052
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006053 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006054 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006055 }
6056
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006057 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006058 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006059 }
6060
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006061 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006062 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006063 }
6064
Vladimir Markoa1de9182016-02-25 11:37:38 +00006065 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6066 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6067 bool StaticTypeOfArrayIsObjectArray() const {
6068 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6069 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006070
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006071 HInstruction* GetArray() const { return InputAt(0); }
6072 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006073 HInstruction* GetValue() const { return InputAt(2); }
6074
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006075 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006076 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6077 }
6078
6079 static DataType::Type GetComponentType(DataType::Type value_type,
6080 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006081 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006082 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006083 // be correct, we also check what is the value type. If it is a floating
6084 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006085 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006086 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006087 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006088 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006090 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006091 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006092 }
6093
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006094 static SideEffects ComputeSideEffects(DataType::Type type) {
6095 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006096 }
6097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006098 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6099 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6100 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006101 }
6102
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006103 DECLARE_INSTRUCTION(ArraySet);
6104
Artem Serovcced8ba2017-07-19 18:18:09 +01006105 protected:
6106 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6107
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006108 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006109 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6110 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006111 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006112 static constexpr size_t kFlagNeedsTypeCheck =
6113 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6114 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006115 // Cached information for the reference_type_info_ so that codegen
6116 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006117 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6118 static constexpr size_t kNumberOfArraySetPackedBits =
6119 kFlagStaticTypeOfArrayIsObjectArray + 1;
6120 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6121 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006122 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006123};
6124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006125class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006126 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006127 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306128 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006129 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006130 // Note that arrays do not change length, so the instruction does not
6131 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006132 SetRawInputAt(0, array);
6133 }
6134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006135 bool IsClonable() const override { return true; }
6136 bool CanBeMoved() const override { return true; }
6137 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006138 return true;
6139 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006140 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006141 return obj == InputAt(0);
6142 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006143
Vladimir Markodce016e2016-04-28 13:10:02 +01006144 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6145
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006146 DECLARE_INSTRUCTION(ArrayLength);
6147
Artem Serovcced8ba2017-07-19 18:18:09 +01006148 protected:
6149 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6150
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006151 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006152 // We treat a String as an array, creating the HArrayLength from String.length()
6153 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6154 // determine whether a particular HArrayLength is actually a String.length() by
6155 // looking at the type of the input but that requires holding the mutator lock, so
6156 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006157 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006158 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6159 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6160 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006161};
6162
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006163class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006164 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006165 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006166 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6167 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006168 HBoundsCheck(HInstruction* index,
6169 HInstruction* length,
6170 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006171 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006172 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006173 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006174 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006175 SetRawInputAt(0, index);
6176 SetRawInputAt(1, length);
6177 }
6178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006179 bool IsClonable() const override { return true; }
6180 bool CanBeMoved() const override { return true; }
6181 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006182 return true;
6183 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006185 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006187 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006188
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006189 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006190
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006191 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006192
6193 DECLARE_INSTRUCTION(BoundsCheck);
6194
Artem Serovcced8ba2017-07-19 18:18:09 +01006195 protected:
6196 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006198 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006199 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006200 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6201 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6202 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006203};
6204
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006205class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006206 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006207 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006208 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306209 slow_path_(nullptr) {
6210 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006211
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006212 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006213
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006214 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006215 return true;
6216 }
6217
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006218 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6219 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006220
6221 DECLARE_INSTRUCTION(SuspendCheck);
6222
Artem Serovcced8ba2017-07-19 18:18:09 +01006223 protected:
6224 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6225
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006226 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006227 // Only used for code generation, in order to share the same slow path between back edges
6228 // of a same loop.
6229 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006230};
6231
David Srbecky0cf44932015-12-09 14:09:59 +00006232// Pseudo-instruction which provides the native debugger with mapping information.
6233// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006234class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006235 public:
6236 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006237 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306238 }
David Srbecky0cf44932015-12-09 14:09:59 +00006239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006240 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006241 return true;
6242 }
6243
6244 DECLARE_INSTRUCTION(NativeDebugInfo);
6245
Artem Serovcced8ba2017-07-19 18:18:09 +01006246 protected:
6247 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006248};
6249
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006250/**
6251 * Instruction to load a Class object.
6252 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006253class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006254 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006255 // Determines how to load the Class.
6256 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006257 // We cannot load this class. See HSharpening::SharpenLoadClass.
6258 kInvalid = -1,
6259
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006260 // Use the Class* from the method's own ArtMethod*.
6261 kReferrersClass,
6262
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006263 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006264 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006265 kBootImageLinkTimePcRelative,
6266
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006267 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006268 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006269 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006270
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006271 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006272 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006273 kBssEntry,
6274
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006275 // Use a known boot image Class* address, embedded in the code by the codegen.
6276 // Used for boot image classes referenced by apps in JIT-compiled code.
6277 kJitBootImageAddress,
6278
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006279 // Load from the root table associated with the JIT compiled method.
6280 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006281
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006282 // Load using a simple runtime call. This is the fall-back load kind when
6283 // the codegen is unable to use another appropriate kind.
6284 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006285
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006286 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006287 };
6288
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006289 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006290 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006291 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006292 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006293 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006294 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006295 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006296 : HInstruction(kLoadClass,
6297 DataType::Type::kReference,
6298 SideEffectsForArchRuntimeCalls(),
6299 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006300 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006301 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006302 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006303 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006304 // Referrers class should not need access check. We never inline unverified
6305 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006306 DCHECK(!is_referrers_class || !needs_access_check);
6307
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006308 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006309 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006310 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006311 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006312 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006313 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006314 }
6315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006316 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006317
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006318 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006319
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006320 LoadKind GetLoadKind() const {
6321 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006322 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006323
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006324 bool HasPcRelativeLoadKind() const {
6325 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6326 GetLoadKind() == LoadKind::kBootImageRelRo ||
6327 GetLoadKind() == LoadKind::kBssEntry;
6328 }
6329
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006330 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006331
Yi Kong39402542019-03-24 02:47:16 -07006332 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006334 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006335
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006336 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006337
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006338 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006339 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006340 }
6341
Calin Juravle0ba218d2015-05-19 18:46:01 +01006342 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006343 // The entrypoint the code generator is going to call does not do
6344 // clinit of the class.
6345 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006346 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006347 }
6348
6349 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006350 return NeedsAccessCheck() ||
6351 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006352 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006353 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006354 }
6355
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006356 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006357 return NeedsAccessCheck() ||
6358 MustGenerateClinitCheck() ||
6359 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006360 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006361 GetLoadKind() == LoadKind::kBssEntry) &&
6362 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006363 }
6364
Calin Juravleacf735c2015-02-12 15:25:22 +00006365 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006366 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6367 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006368 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006369 } else {
6370 return ReferenceTypeInfo::CreateInvalid();
6371 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006372 }
6373
Vladimir Marko175e7862018-03-27 09:03:13 +00006374 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6375 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6376 DCHECK(klass_ != nullptr);
6377 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006378 }
6379
Andreas Gampea5b09a62016-11-17 15:21:22 -08006380 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006381 const DexFile& GetDexFile() const { return dex_file_; }
6382
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006383 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006384 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006385 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006386
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006387 static SideEffects SideEffectsForArchRuntimeCalls() {
6388 return SideEffects::CanTriggerGC();
6389 }
6390
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006391 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006392 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006393 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006394 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006395
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006396 bool MustResolveTypeOnSlowPath() const {
6397 // Check that this instruction has a slow path.
6398 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6399 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6400 return GetLoadKind() == LoadKind::kBssEntry;
6401 }
6402
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006403 void MarkInBootImage() {
6404 SetPackedFlag<kFlagIsInBootImage>(true);
6405 }
6406
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006407 void AddSpecialInput(HInstruction* special_input);
6408
6409 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006410 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006411 return ArrayRef<HUserRecord<HInstruction*>>(
6412 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6413 }
6414
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006415 Handle<mirror::Class> GetClass() const {
6416 return klass_;
6417 }
6418
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006419 DECLARE_INSTRUCTION(LoadClass);
6420
Artem Serovcced8ba2017-07-19 18:18:09 +01006421 protected:
6422 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6423
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006424 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006425 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006426 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006427 // Whether this instruction must generate the initialization check.
6428 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006429 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006430 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6431 static constexpr size_t kFieldLoadKindSize =
6432 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006433 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6434 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006435 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006436 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6437
6438 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006439 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006440 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006441 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006442 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006443 }
6444
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006445 void SetLoadKindInternal(LoadKind load_kind);
6446
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006447 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006448 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006449 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006450 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006451
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006452 // A type index and dex file where the class can be accessed. The dex file can be:
6453 // - The compiling method's dex file if the class is defined there too.
6454 // - The compiling method's dex file if the class is referenced there.
6455 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006456 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006457 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006458 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006459
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006460 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006461};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006462std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6463
6464// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006465inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6466 // The load kind should be determined before inserting the instruction to the graph.
6467 DCHECK(GetBlock() == nullptr);
6468 DCHECK(GetEnvironment() == nullptr);
6469 SetPackedField<LoadKindField>(load_kind);
6470 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6471 special_input_ = HUserRecord<HInstruction*>(nullptr);
6472 }
6473 if (!NeedsEnvironment()) {
6474 SetSideEffects(SideEffects::None());
6475 }
6476}
6477
6478// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006479inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006480 // The special input is used for PC-relative loads on some architectures,
6481 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006482 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006483 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006484 GetLoadKind() == LoadKind::kBssEntry ||
6485 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006486 DCHECK(special_input_.GetInstruction() == nullptr);
6487 special_input_ = HUserRecord<HInstruction*>(special_input);
6488 special_input->AddUseAt(this, 0);
6489}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006490
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006491class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006492 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006493 // Determines how to load the String.
6494 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006495 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006496 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006497 kBootImageLinkTimePcRelative,
6498
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006499 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006500 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006501 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006502
Vladimir Markoaad75c62016-10-03 08:46:48 +00006503 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006504 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006505 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006506
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006507 // Use a known boot image String* address, embedded in the code by the codegen.
6508 // Used for boot image strings referenced by apps in JIT-compiled code.
6509 kJitBootImageAddress,
6510
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006511 // Load from the root table associated with the JIT compiled method.
6512 kJitTableAddress,
6513
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006514 // Load using a simple runtime call. This is the fall-back load kind when
6515 // the codegen is unable to use another appropriate kind.
6516 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006517
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006518 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006519 };
6520
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006521 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006522 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006523 const DexFile& dex_file,
6524 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006525 : HInstruction(kLoadString,
6526 DataType::Type::kReference,
6527 SideEffectsForArchRuntimeCalls(),
6528 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006529 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006530 string_index_(string_index),
6531 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006532 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006533 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006534
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006535 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006536
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006537 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006538
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006539 LoadKind GetLoadKind() const {
6540 return GetPackedField<LoadKindField>();
6541 }
6542
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006543 bool HasPcRelativeLoadKind() const {
6544 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6545 GetLoadKind() == LoadKind::kBootImageRelRo ||
6546 GetLoadKind() == LoadKind::kBssEntry;
6547 }
6548
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006549 const DexFile& GetDexFile() const {
6550 return dex_file_;
6551 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006552
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006553 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006554 return string_index_;
6555 }
6556
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006557 Handle<mirror::String> GetString() const {
6558 return string_;
6559 }
6560
6561 void SetString(Handle<mirror::String> str) {
6562 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006563 }
6564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006565 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006566
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006567 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006568
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006569 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006570
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006571 // Will call the runtime if we need to load the string through
6572 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006573 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006574 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006575 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006576 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006577 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006578 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006579 return false;
6580 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006581 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006582 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006583
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006584 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006585 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006586 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006587
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006588 bool CanBeNull() const override { return false; }
6589 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006590
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006591 static SideEffects SideEffectsForArchRuntimeCalls() {
6592 return SideEffects::CanTriggerGC();
6593 }
6594
Vladimir Marko372f10e2016-05-17 16:30:10 +01006595 void AddSpecialInput(HInstruction* special_input);
6596
6597 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006598 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006599 return ArrayRef<HUserRecord<HInstruction*>>(
6600 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006601 }
6602
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006603 DECLARE_INSTRUCTION(LoadString);
6604
Artem Serovcced8ba2017-07-19 18:18:09 +01006605 protected:
6606 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6607
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006608 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006609 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006610 static constexpr size_t kFieldLoadKindSize =
6611 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6612 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006613 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006614 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006615
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006616 void SetLoadKindInternal(LoadKind load_kind);
6617
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006618 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006619 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006620 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006621 HUserRecord<HInstruction*> special_input_;
6622
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006623 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006624 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006625
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006626 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006627};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006628std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6629
6630// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006631inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6632 // The load kind should be determined before inserting the instruction to the graph.
6633 DCHECK(GetBlock() == nullptr);
6634 DCHECK(GetEnvironment() == nullptr);
6635 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6636 SetPackedField<LoadKindField>(load_kind);
6637 if (load_kind != LoadKind::kRuntimeCall) {
6638 special_input_ = HUserRecord<HInstruction*>(nullptr);
6639 }
6640 if (!NeedsEnvironment()) {
6641 SetSideEffects(SideEffects::None());
6642 }
6643}
6644
6645// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006646inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006647 // The special input is used for PC-relative loads on some architectures,
6648 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006649 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006650 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006651 GetLoadKind() == LoadKind::kBssEntry ||
6652 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006653 // HLoadString::GetInputRecords() returns an empty array at this point,
6654 // so use the GetInputRecords() from the base class to set the input record.
6655 DCHECK(special_input_.GetInstruction() == nullptr);
6656 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006657 special_input->AddUseAt(this, 0);
6658}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006659
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006660class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006661 public:
6662 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006663 uint16_t method_handle_idx,
6664 const DexFile& dex_file,
6665 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006666 : HInstruction(kLoadMethodHandle,
6667 DataType::Type::kReference,
6668 SideEffectsForArchRuntimeCalls(),
6669 dex_pc),
6670 special_input_(HUserRecord<HInstruction*>(current_method)),
6671 method_handle_idx_(method_handle_idx),
6672 dex_file_(dex_file) {
6673 }
6674
6675 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006676 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006677 return ArrayRef<HUserRecord<HInstruction*>>(
6678 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6679 }
6680
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006681 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006682
6683 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6684
6685 const DexFile& GetDexFile() const { return dex_file_; }
6686
6687 static SideEffects SideEffectsForArchRuntimeCalls() {
6688 return SideEffects::CanTriggerGC();
6689 }
6690
6691 DECLARE_INSTRUCTION(LoadMethodHandle);
6692
6693 protected:
6694 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6695
6696 private:
6697 // The special input is the HCurrentMethod for kRuntimeCall.
6698 HUserRecord<HInstruction*> special_input_;
6699
6700 const uint16_t method_handle_idx_;
6701 const DexFile& dex_file_;
6702};
6703
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006704class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006705 public:
6706 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006707 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006708 const DexFile& dex_file,
6709 uint32_t dex_pc)
6710 : HInstruction(kLoadMethodType,
6711 DataType::Type::kReference,
6712 SideEffectsForArchRuntimeCalls(),
6713 dex_pc),
6714 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006715 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006716 dex_file_(dex_file) {
6717 }
6718
6719 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006720 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006721 return ArrayRef<HUserRecord<HInstruction*>>(
6722 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6723 }
6724
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006725 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006726
Orion Hodson06d10a72018-05-14 08:53:38 +01006727 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006728
6729 const DexFile& GetDexFile() const { return dex_file_; }
6730
6731 static SideEffects SideEffectsForArchRuntimeCalls() {
6732 return SideEffects::CanTriggerGC();
6733 }
6734
6735 DECLARE_INSTRUCTION(LoadMethodType);
6736
6737 protected:
6738 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6739
6740 private:
6741 // The special input is the HCurrentMethod for kRuntimeCall.
6742 HUserRecord<HInstruction*> special_input_;
6743
Orion Hodson06d10a72018-05-14 08:53:38 +01006744 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006745 const DexFile& dex_file_;
6746};
6747
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006748/**
6749 * Performs an initialization check on its Class object input.
6750 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006751class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006752 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006753 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006754 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306755 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006756 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006757 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006758 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006759 SetRawInputAt(0, constant);
6760 }
Artem Serova6e26142018-06-19 14:55:17 +01006761 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006762 bool CanBeMoved() const override { return true; }
6763 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006764 return true;
6765 }
6766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006767 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006768 // May call runtime to initialize the class.
6769 return true;
6770 }
6771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006772 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006773
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006774 HLoadClass* GetLoadClass() const {
6775 DCHECK(InputAt(0)->IsLoadClass());
6776 return InputAt(0)->AsLoadClass();
6777 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006778
6779 DECLARE_INSTRUCTION(ClinitCheck);
6780
Artem Serovcced8ba2017-07-19 18:18:09 +01006781
6782 protected:
6783 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006784};
6785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006786class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006787 public:
6788 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006789 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006790 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006791 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006792 bool is_volatile,
6793 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006794 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006795 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006796 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306797 : HExpression(kStaticFieldGet,
6798 field_type,
6799 SideEffects::FieldReadOfType(field_type, is_volatile),
6800 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006801 field_info_(field,
6802 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006803 field_type,
6804 is_volatile,
6805 field_idx,
6806 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006807 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006808 SetRawInputAt(0, cls);
6809 }
6810
Calin Juravle52c48962014-12-16 17:02:57 +00006811
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006812 bool IsClonable() const override { return true; }
6813 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006815 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006816 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006817 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006818 }
6819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006820 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006821 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6822 }
6823
Calin Juravle52c48962014-12-16 17:02:57 +00006824 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006825 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006826 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006827 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006828
Vladimir Marko61b92282017-10-11 13:23:17 +01006829 void SetType(DataType::Type new_type) {
6830 DCHECK(DataType::IsIntegralType(GetType()));
6831 DCHECK(DataType::IsIntegralType(new_type));
6832 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6833 SetPackedField<TypeField>(new_type);
6834 }
6835
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006836 DECLARE_INSTRUCTION(StaticFieldGet);
6837
Artem Serovcced8ba2017-07-19 18:18:09 +01006838 protected:
6839 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6840
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006841 private:
6842 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006843};
6844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006845class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006846 public:
6847 HStaticFieldSet(HInstruction* cls,
6848 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006849 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006850 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006851 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006852 bool is_volatile,
6853 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006854 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006855 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006856 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006857 : HExpression(kStaticFieldSet,
6858 SideEffects::FieldWriteOfType(field_type, is_volatile),
6859 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006860 field_info_(field,
6861 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006862 field_type,
6863 is_volatile,
6864 field_idx,
6865 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006866 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006867 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006868 SetRawInputAt(0, cls);
6869 SetRawInputAt(1, value);
6870 }
6871
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006872 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006873 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006874 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006875 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006876 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006877
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006878 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006879 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6880 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006881
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006882 DECLARE_INSTRUCTION(StaticFieldSet);
6883
Artem Serovcced8ba2017-07-19 18:18:09 +01006884 protected:
6885 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6886
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006887 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006888 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6889 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6890 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6891 "Too many packed fields.");
6892
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006893 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006894};
6895
Vladimir Marko552a1342017-10-31 10:56:47 +00006896class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6897 public:
6898 HStringBuilderAppend(HIntConstant* format,
6899 uint32_t number_of_arguments,
6900 ArenaAllocator* allocator,
6901 uint32_t dex_pc)
6902 : HVariableInputSizeInstruction(
6903 kStringBuilderAppend,
6904 DataType::Type::kReference,
6905 // The runtime call may read memory from inputs. It never writes outside
6906 // of the newly allocated result object (or newly allocated helper objects).
6907 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6908 dex_pc,
6909 allocator,
6910 number_of_arguments + /* format */ 1u,
6911 kArenaAllocInvokeInputs) {
6912 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6913 SetRawInputAt(FormatIndex(), format);
6914 }
6915
6916 void SetArgumentAt(size_t index, HInstruction* argument) {
6917 DCHECK_LE(index, GetNumberOfArguments());
6918 SetRawInputAt(index, argument);
6919 }
6920
6921 // Return the number of arguments, excluding the format.
6922 size_t GetNumberOfArguments() const {
6923 DCHECK_GE(InputCount(), 1u);
6924 return InputCount() - 1u;
6925 }
6926
6927 size_t FormatIndex() const {
6928 return GetNumberOfArguments();
6929 }
6930
6931 HIntConstant* GetFormat() {
6932 return InputAt(FormatIndex())->AsIntConstant();
6933 }
6934
6935 bool NeedsEnvironment() const override { return true; }
6936
6937 bool CanThrow() const override { return true; }
6938
6939 DECLARE_INSTRUCTION(StringBuilderAppend);
6940
6941 protected:
6942 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
6943};
6944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006945class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006946 public:
6947 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006948 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006949 uint32_t field_index,
6950 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306951 : HExpression(kUnresolvedInstanceFieldGet,
6952 field_type,
6953 SideEffects::AllExceptGCDependency(),
6954 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006955 field_index_(field_index) {
6956 SetRawInputAt(0, obj);
6957 }
6958
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006959 bool IsClonable() const override { return true; }
6960 bool NeedsEnvironment() const override { return true; }
6961 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006963 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006964 uint32_t GetFieldIndex() const { return field_index_; }
6965
6966 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6967
Artem Serovcced8ba2017-07-19 18:18:09 +01006968 protected:
6969 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6970
Calin Juravlee460d1d2015-09-29 04:52:17 +01006971 private:
6972 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006973};
6974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006975class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006976 public:
6977 HUnresolvedInstanceFieldSet(HInstruction* obj,
6978 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006979 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006980 uint32_t field_index,
6981 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006982 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006983 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006984 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006985 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006986 SetRawInputAt(0, obj);
6987 SetRawInputAt(1, value);
6988 }
6989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006990 bool IsClonable() const override { return true; }
6991 bool NeedsEnvironment() const override { return true; }
6992 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006993
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006994 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006995 uint32_t GetFieldIndex() const { return field_index_; }
6996
6997 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6998
Artem Serovcced8ba2017-07-19 18:18:09 +01006999 protected:
7000 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7001
Calin Juravlee460d1d2015-09-29 04:52:17 +01007002 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007003 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7004 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007005 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007006 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7007 kFieldFieldType + kFieldFieldTypeSize;
7008 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7009 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007010 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007011
Calin Juravlee460d1d2015-09-29 04:52:17 +01007012 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007013};
7014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007015class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007016 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007017 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007018 uint32_t field_index,
7019 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307020 : HExpression(kUnresolvedStaticFieldGet,
7021 field_type,
7022 SideEffects::AllExceptGCDependency(),
7023 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007024 field_index_(field_index) {
7025 }
7026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007027 bool IsClonable() const override { return true; }
7028 bool NeedsEnvironment() const override { return true; }
7029 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007030
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007031 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007032 uint32_t GetFieldIndex() const { return field_index_; }
7033
7034 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7035
Artem Serovcced8ba2017-07-19 18:18:09 +01007036 protected:
7037 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7038
Calin Juravlee460d1d2015-09-29 04:52:17 +01007039 private:
7040 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007041};
7042
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007043class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007044 public:
7045 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007046 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007047 uint32_t field_index,
7048 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007049 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007050 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007051 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007052 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007053 SetRawInputAt(0, value);
7054 }
7055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007056 bool IsClonable() const override { return true; }
7057 bool NeedsEnvironment() const override { return true; }
7058 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007059
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007060 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007061 uint32_t GetFieldIndex() const { return field_index_; }
7062
7063 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7064
Artem Serovcced8ba2017-07-19 18:18:09 +01007065 protected:
7066 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7067
Calin Juravlee460d1d2015-09-29 04:52:17 +01007068 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007069 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7070 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007071 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007072 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7073 kFieldFieldType + kFieldFieldTypeSize;
7074 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7075 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007076 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007077
Calin Juravlee460d1d2015-09-29 04:52:17 +01007078 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007079};
7080
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007081// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007082class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007083 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007084 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307085 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7086 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007087
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007088 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007089
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007090 DECLARE_INSTRUCTION(LoadException);
7091
Artem Serovcced8ba2017-07-19 18:18:09 +01007092 protected:
7093 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007094};
7095
David Brazdilcb1c0552015-08-04 16:22:25 +01007096// Implicit part of move-exception which clears thread-local exception storage.
7097// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007098class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007099 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007100 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007101 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307102 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007103
7104 DECLARE_INSTRUCTION(ClearException);
7105
Artem Serovcced8ba2017-07-19 18:18:09 +01007106 protected:
7107 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007108};
7109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007110class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007111 public:
7112 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007113 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007114 SetRawInputAt(0, exception);
7115 }
7116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007117 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007119 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007121 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007122
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007123 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007124
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007125 DECLARE_INSTRUCTION(Throw);
7126
Artem Serovcced8ba2017-07-19 18:18:09 +01007127 protected:
7128 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007129};
7130
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007131/**
7132 * Implementation strategies for the code generator of a HInstanceOf
7133 * or `HCheckCast`.
7134 */
7135enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007136 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007137 kExactCheck, // Can do a single class compare.
7138 kClassHierarchyCheck, // Can just walk the super class chain.
7139 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7140 kInterfaceCheck, // No optimization yet when checking against an interface.
7141 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007142 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007143 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007144 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007145};
7146
Roland Levillain86503782016-02-11 19:07:30 +00007147std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7148
Vladimir Marko175e7862018-03-27 09:03:13 +00007149// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7150// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7151class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007152 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007153 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007154 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007155 HInstruction* object,
7156 HInstruction* target_class_or_null,
7157 TypeCheckKind check_kind,
7158 Handle<mirror::Class> klass,
7159 uint32_t dex_pc,
7160 ArenaAllocator* allocator,
7161 HIntConstant* bitstring_path_to_root,
7162 HIntConstant* bitstring_mask,
7163 SideEffects side_effects)
7164 : HVariableInputSizeInstruction(
7165 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007166 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007167 side_effects,
7168 dex_pc,
7169 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007170 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007171 kArenaAllocTypeCheckInputs),
7172 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007173 SetPackedField<TypeCheckKindField>(check_kind);
7174 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007175 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007176 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007177 SetRawInputAt(1, target_class_or_null);
7178 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7179 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7180 if (check_kind == TypeCheckKind::kBitstringCheck) {
7181 DCHECK(target_class_or_null->IsNullConstant());
7182 SetRawInputAt(2, bitstring_path_to_root);
7183 SetRawInputAt(3, bitstring_mask);
7184 } else {
7185 DCHECK(target_class_or_null->IsLoadClass());
7186 }
Vladimir Marko87584542017-12-12 17:47:52 +00007187 }
7188
7189 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007190 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007191 HInstruction* load_class = InputAt(1);
7192 DCHECK(load_class->IsLoadClass());
7193 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007194 }
7195
Vladimir Marko175e7862018-03-27 09:03:13 +00007196 uint32_t GetBitstringPathToRoot() const {
7197 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7198 HInstruction* path_to_root = InputAt(2);
7199 DCHECK(path_to_root->IsIntConstant());
7200 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7201 }
7202
7203 uint32_t GetBitstringMask() const {
7204 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7205 HInstruction* mask = InputAt(3);
7206 DCHECK(mask->IsIntConstant());
7207 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7208 }
7209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007210 bool IsClonable() const override { return true; }
7211 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007213 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007214 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7215 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007216 }
7217
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007218 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7219 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7220 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7221 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7222
Vladimir Marko175e7862018-03-27 09:03:13 +00007223 ReferenceTypeInfo GetTargetClassRTI() {
7224 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7225 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007226 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007227 } else {
7228 return ReferenceTypeInfo::CreateInvalid();
7229 }
7230 }
7231
7232 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7233 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7234 DCHECK(klass_ != nullptr);
7235 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7236 }
7237
7238 Handle<mirror::Class> GetClass() const {
7239 return klass_;
7240 }
7241
7242 protected:
7243 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7244
7245 private:
7246 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7247 static constexpr size_t kFieldTypeCheckKindSize =
7248 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7249 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7250 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7251 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7252 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7253 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7254
7255 Handle<mirror::Class> klass_;
7256};
7257
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007258class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007259 public:
7260 HInstanceOf(HInstruction* object,
7261 HInstruction* target_class_or_null,
7262 TypeCheckKind check_kind,
7263 Handle<mirror::Class> klass,
7264 uint32_t dex_pc,
7265 ArenaAllocator* allocator,
7266 HIntConstant* bitstring_path_to_root,
7267 HIntConstant* bitstring_mask)
7268 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007269 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007270 object,
7271 target_class_or_null,
7272 check_kind,
7273 klass,
7274 dex_pc,
7275 allocator,
7276 bitstring_path_to_root,
7277 bitstring_mask,
7278 SideEffectsForArchRuntimeCalls(check_kind)) {}
7279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007280 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007282 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007283 return CanCallRuntime(GetTypeCheckKind());
7284 }
7285
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007286 static bool CanCallRuntime(TypeCheckKind check_kind) {
7287 // Mips currently does runtime calls for any other checks.
7288 return check_kind != TypeCheckKind::kExactCheck;
7289 }
7290
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007291 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007292 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007293 }
7294
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007295 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007296
Artem Serovcced8ba2017-07-19 18:18:09 +01007297 protected:
7298 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007299};
7300
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007301class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007302 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007303 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307304 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007305 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7306 SetPackedFlag<kFlagUpperCanBeNull>(true);
7307 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007308 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007309 SetRawInputAt(0, input);
7310 }
7311
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007312 bool InstructionDataEquals(const HInstruction* other) const override;
7313 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007314
David Brazdilf5552582015-12-27 13:36:12 +00007315 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007316 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007317 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007318 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007319
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007320 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007321 DCHECK(GetUpperCanBeNull() || !can_be_null);
7322 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007323 }
7324
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007325 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007326
Calin Juravleb1498f62015-02-16 13:13:29 +00007327 DECLARE_INSTRUCTION(BoundType);
7328
Artem Serovcced8ba2017-07-19 18:18:09 +01007329 protected:
7330 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7331
Calin Juravleb1498f62015-02-16 13:13:29 +00007332 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007333 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7334 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007335 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007336 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7337 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7338 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7339
Calin Juravleb1498f62015-02-16 13:13:29 +00007340 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007341 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7342 // It is used to bound the type in cases like:
7343 // if (x instanceof ClassX) {
7344 // // uper_bound_ will be ClassX
7345 // }
David Brazdilf5552582015-12-27 13:36:12 +00007346 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007347};
7348
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007349class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007350 public:
7351 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007352 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007353 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007354 Handle<mirror::Class> klass,
7355 uint32_t dex_pc,
7356 ArenaAllocator* allocator,
7357 HIntConstant* bitstring_path_to_root,
7358 HIntConstant* bitstring_mask)
7359 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007360 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007361 object,
7362 target_class_or_null,
7363 check_kind,
7364 klass,
7365 dex_pc,
7366 allocator,
7367 bitstring_path_to_root,
7368 bitstring_mask,
7369 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007370
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007371 bool IsClonable() const override { return true; }
7372 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007373 // Instruction may throw a CheckCastError.
7374 return true;
7375 }
7376
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007377 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007378
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007379 DECLARE_INSTRUCTION(CheckCast);
7380
Artem Serovcced8ba2017-07-19 18:18:09 +01007381 protected:
7382 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007383};
7384
Andreas Gampe26de38b2016-07-27 17:53:11 -07007385/**
7386 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7387 * @details We define the combined barrier types that are actually required
7388 * by the Java Memory Model, rather than using exactly the terminology from
7389 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7390 * primitives. Note that the JSR-133 cookbook generally does not deal with
7391 * store atomicity issues, and the recipes there are not always entirely sufficient.
7392 * The current recipe is as follows:
7393 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7394 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7395 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7396 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7397 * class has final fields.
7398 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7399 * store-to-memory instructions. Only generated together with non-temporal stores.
7400 */
7401enum MemBarrierKind {
7402 kAnyStore,
7403 kLoadAny,
7404 kStoreStore,
7405 kAnyAny,
7406 kNTStoreStore,
7407 kLastBarrierKind = kNTStoreStore
7408};
7409std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7410
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007411class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007412 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007413 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007414 : HExpression(kMemoryBarrier,
7415 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7416 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007417 SetPackedField<BarrierKindField>(barrier_kind);
7418 }
Calin Juravle27df7582015-04-17 19:12:31 +01007419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007420 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007421
Vladimir Markoa1de9182016-02-25 11:37:38 +00007422 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007423
7424 DECLARE_INSTRUCTION(MemoryBarrier);
7425
Artem Serovcced8ba2017-07-19 18:18:09 +01007426 protected:
7427 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7428
Calin Juravle27df7582015-04-17 19:12:31 +01007429 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007430 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7431 static constexpr size_t kFieldBarrierKindSize =
7432 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7433 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7434 kFieldBarrierKind + kFieldBarrierKindSize;
7435 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7436 "Too many packed fields.");
7437 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007438};
7439
Igor Murashkind01745e2017-04-05 16:40:31 -07007440// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7441// the specified object(s), with respect to any subsequent store that might "publish"
7442// (i.e. make visible) the specified object to another thread.
7443//
7444// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7445// for all final fields (that were set) at the end of the invoked constructor.
7446//
7447// The constructor fence models the freeze actions for the final fields of an object
7448// being constructed (semantically at the end of the constructor). Constructor fences
7449// have a per-object affinity; two separate objects being constructed get two separate
7450// constructor fences.
7451//
7452// (Note: that if calling a super-constructor or forwarding to another constructor,
7453// the freezes would happen at the end of *that* constructor being invoked).
7454//
7455// The memory model guarantees that when the object being constructed is "published" after
7456// constructor completion (i.e. escapes the current thread via a store), then any final field
7457// writes must be observable on other threads (once they observe that publication).
7458//
7459// Further, anything written before the freeze, and read by dereferencing through the final field,
7460// must also be visible (so final object field could itself have an object with non-final fields;
7461// yet the freeze must also extend to them).
7462//
7463// Constructor example:
7464//
7465// class HasFinal {
7466// final int field; Optimizing IR for <init>()V:
7467// HasFinal() {
7468// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7469// // freeze(this.field); HConstructorFence(this)
7470// } HReturn
7471// }
7472//
7473// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7474// already-initialized classes; in that case the allocation must act as a "default-initializer"
7475// of the object which effectively writes the class pointer "final field".
7476//
7477// For example, we can model default-initialiation as roughly the equivalent of the following:
7478//
7479// class Object {
7480// private final Class header;
7481// }
7482//
7483// Java code: Optimizing IR:
7484//
7485// T new_instance<T>() {
7486// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7487// obj.header = T.class; // header write is done by above call.
7488// // freeze(obj.header) HConstructorFence(obj)
7489// return (T)obj;
7490// }
7491//
7492// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007493// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007494// * QuasiAtomic::ThreadFenceForConstructor
7495//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007496class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007497 // A fence has variable inputs because the inputs can be removed
7498 // after prepare_for_register_allocation phase.
7499 // (TODO: In the future a fence could freeze multiple objects
7500 // after merging two fences together.)
7501 public:
7502 // `fence_object` is the reference that needs to be protected for correct publication.
7503 //
7504 // It makes sense in the following situations:
7505 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7506 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7507 //
7508 // After construction the `fence_object` becomes the 0th input.
7509 // This is not an input in a real sense, but just a convenient place to stash the information
7510 // about the associated object.
7511 HConstructorFence(HInstruction* fence_object,
7512 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007513 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007514 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7515 // constraints described in the class header. We claim that these SideEffects constraints
7516 // enforce a superset of the real constraints.
7517 //
7518 // The ordering described above is conservatively modeled with SideEffects as follows:
7519 //
7520 // * To prevent reordering of the publication stores:
7521 // ----> "Reads of objects" is the initial SideEffect.
7522 // * For every primitive final field store in the constructor:
7523 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7524 // * If there are any stores to reference final fields in the constructor:
7525 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7526 // that are reachable from `fence_object` also need to be prevented for reordering
7527 // (and we do not want to do alias analysis to figure out what those stores are).
7528 //
7529 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7530 // even more conservative approach than the one described above, and prevents all of the
7531 // above reordering without analyzing any of the instructions in the constructor.
7532 //
7533 // If in a later phase we discover that there are no writes to reference final fields,
7534 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307535 : HVariableInputSizeInstruction(kConstructorFence,
7536 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007537 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007538 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007539 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007540 kArenaAllocConstructorFenceInputs) {
7541 DCHECK(fence_object != nullptr);
7542 SetRawInputAt(0, fence_object);
7543 }
7544
7545 // The object associated with this constructor fence.
7546 //
7547 // (Note: This will be null after the prepare_for_register_allocation phase,
7548 // as all constructor fence inputs are removed there).
7549 HInstruction* GetFenceObject() const {
7550 return InputAt(0);
7551 }
7552
7553 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7554 // - Delete `fence_use` from `this`'s use list.
7555 // - Delete `this` from `fence_use`'s inputs list.
7556 // - If the `fence_use` is dead, remove it from the graph.
7557 //
7558 // A fence is considered dead once it no longer has any uses
7559 // and all of the inputs are dead.
7560 //
7561 // This must *not* be called during/after prepare_for_register_allocation,
7562 // because that removes all the inputs to the fences but the fence is actually
7563 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007564 //
7565 // Returns how many HConstructorFence instructions were removed from graph.
7566 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007567
Igor Murashkindd018df2017-08-09 10:38:31 -07007568 // Combine all inputs of `this` and `other` instruction and remove
7569 // `other` from the graph.
7570 //
7571 // Inputs are unique after the merge.
7572 //
7573 // Requirement: `this` must not be the same as `other.
7574 void Merge(HConstructorFence* other);
7575
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007576 // Check if this constructor fence is protecting
7577 // an HNewInstance or HNewArray that is also the immediate
7578 // predecessor of `this`.
7579 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007580 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7581 // to be one of the inputs of `this`.
7582 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007583 // Returns the associated HNewArray or HNewInstance,
7584 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007585 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007586
Igor Murashkind01745e2017-04-05 16:40:31 -07007587 DECLARE_INSTRUCTION(ConstructorFence);
7588
Artem Serovcced8ba2017-07-19 18:18:09 +01007589 protected:
7590 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007591};
7592
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007593class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007594 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007595 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007596 kEnter,
7597 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007598 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007599 };
7600
7601 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007602 : HExpression(kMonitorOperation,
7603 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7604 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007605 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007606 SetRawInputAt(0, object);
7607 }
7608
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007609 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007610 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007611
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007612 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007613 // Verifier guarantees that monitor-exit cannot throw.
7614 // This is important because it allows the HGraphBuilder to remove
7615 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7616 return IsEnter();
7617 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007618
Vladimir Markoa1de9182016-02-25 11:37:38 +00007619 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7620 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007621
7622 DECLARE_INSTRUCTION(MonitorOperation);
7623
Artem Serovcced8ba2017-07-19 18:18:09 +01007624 protected:
7625 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7626
Calin Juravle52c48962014-12-16 17:02:57 +00007627 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007628 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7629 static constexpr size_t kFieldOperationKindSize =
7630 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7631 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7632 kFieldOperationKind + kFieldOperationKindSize;
7633 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7634 "Too many packed fields.");
7635 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007636};
7637
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007638class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007639 public:
7640 HSelect(HInstruction* condition,
7641 HInstruction* true_value,
7642 HInstruction* false_value,
7643 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307644 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007645 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7646
7647 // First input must be `true_value` or `false_value` to allow codegens to
7648 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7649 // that architectures which implement HSelect as a conditional move also
7650 // will not need to invert the condition.
7651 SetRawInputAt(0, false_value);
7652 SetRawInputAt(1, true_value);
7653 SetRawInputAt(2, condition);
7654 }
7655
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007656 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007657 HInstruction* GetFalseValue() const { return InputAt(0); }
7658 HInstruction* GetTrueValue() const { return InputAt(1); }
7659 HInstruction* GetCondition() const { return InputAt(2); }
7660
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007661 bool CanBeMoved() const override { return true; }
7662 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007663 return true;
7664 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007665
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007666 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007667 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7668 }
7669
7670 DECLARE_INSTRUCTION(Select);
7671
Artem Serovcced8ba2017-07-19 18:18:09 +01007672 protected:
7673 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007674};
7675
Vladimir Markof9f64412015-09-02 14:05:49 +01007676class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007677 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007678 MoveOperands(Location source,
7679 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007680 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007681 HInstruction* instruction)
7682 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007683
7684 Location GetSource() const { return source_; }
7685 Location GetDestination() const { return destination_; }
7686
7687 void SetSource(Location value) { source_ = value; }
7688 void SetDestination(Location value) { destination_ = value; }
7689
7690 // The parallel move resolver marks moves as "in-progress" by clearing the
7691 // destination (but not the source).
7692 Location MarkPending() {
7693 DCHECK(!IsPending());
7694 Location dest = destination_;
7695 destination_ = Location::NoLocation();
7696 return dest;
7697 }
7698
7699 void ClearPending(Location dest) {
7700 DCHECK(IsPending());
7701 destination_ = dest;
7702 }
7703
7704 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007705 DCHECK(source_.IsValid() || destination_.IsInvalid());
7706 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007707 }
7708
7709 // True if this blocks a move from the given location.
7710 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007711 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007712 }
7713
7714 // A move is redundant if it's been eliminated, if its source and
7715 // destination are the same, or if its destination is unneeded.
7716 bool IsRedundant() const {
7717 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7718 }
7719
7720 // We clear both operands to indicate move that's been eliminated.
7721 void Eliminate() {
7722 source_ = destination_ = Location::NoLocation();
7723 }
7724
7725 bool IsEliminated() const {
7726 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7727 return source_.IsInvalid();
7728 }
7729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007730 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007731
Nicolas Geoffray90218252015-04-15 11:56:51 +01007732 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007733 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007734 }
7735
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007736 HInstruction* GetInstruction() const { return instruction_; }
7737
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007738 private:
7739 Location source_;
7740 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007741 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007742 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007743 // The instruction this move is assocatied with. Null when this move is
7744 // for moving an input in the expected locations of user (including a phi user).
7745 // This is only used in debug mode, to ensure we do not connect interval siblings
7746 // in the same parallel move.
7747 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007748};
7749
Roland Levillainc9285912015-12-18 10:38:42 +00007750std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7751
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007752static constexpr size_t kDefaultNumberOfMoves = 4;
7753
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007754class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007755 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007756 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007757 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007758 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007759 moves_.reserve(kDefaultNumberOfMoves);
7760 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007761
Nicolas Geoffray90218252015-04-15 11:56:51 +01007762 void AddMove(Location source,
7763 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007764 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007765 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007766 DCHECK(source.IsValid());
7767 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007768 if (kIsDebugBuild) {
7769 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007770 for (const MoveOperands& move : moves_) {
7771 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007772 // Special case the situation where the move is for the spill slot
7773 // of the instruction.
7774 if ((GetPrevious() == instruction)
7775 || ((GetPrevious() == nullptr)
7776 && instruction->IsPhi()
7777 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007778 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007779 << "Doing parallel moves for the same instruction.";
7780 } else {
7781 DCHECK(false) << "Doing parallel moves for the same instruction.";
7782 }
7783 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007784 }
7785 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007786 for (const MoveOperands& move : moves_) {
7787 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007788 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007789 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007790 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007791 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007792 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007793 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007794 }
7795
Vladimir Marko225b6462015-09-28 12:17:40 +01007796 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007797 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007798 }
7799
Vladimir Marko225b6462015-09-28 12:17:40 +01007800 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007801
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007802 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007803
Artem Serovcced8ba2017-07-19 18:18:09 +01007804 protected:
7805 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7806
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007807 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007808 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007809};
7810
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007811// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7812// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7813// never used across anything that can trigger GC.
7814// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7815// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007816class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007817 public:
7818 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307819 : HExpression(kIntermediateAddress,
7820 DataType::Type::kInt32,
7821 SideEffects::DependsOnGC(),
7822 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007823 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7824 DataType::Size(DataType::Type::kReference))
7825 << "kPrimInt and kPrimNot have different sizes.";
7826 SetRawInputAt(0, base_address);
7827 SetRawInputAt(1, offset);
7828 }
7829
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007830 bool IsClonable() const override { return true; }
7831 bool CanBeMoved() const override { return true; }
7832 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007833 return true;
7834 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007835 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007836
7837 HInstruction* GetBaseAddress() const { return InputAt(0); }
7838 HInstruction* GetOffset() const { return InputAt(1); }
7839
7840 DECLARE_INSTRUCTION(IntermediateAddress);
7841
Artem Serovcced8ba2017-07-19 18:18:09 +01007842 protected:
7843 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007844};
7845
7846
Mark Mendell0616ae02015-04-17 12:49:27 -04007847} // namespace art
7848
Aart Bikf8f5a162017-02-06 15:35:29 -08007849#include "nodes_vector.h"
7850
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007851#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7852#include "nodes_shared.h"
7853#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007854#ifdef ART_ENABLE_CODEGEN_mips
7855#include "nodes_mips.h"
7856#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307857#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007858#include "nodes_x86.h"
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05307859#include "nodes_vector_x86.h"
Mark Mendell0616ae02015-04-17 12:49:27 -04007860#endif
7861
7862namespace art {
7863
Igor Murashkin6ef45672017-08-08 13:59:55 -07007864class OptimizingCompilerStats;
7865
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007866class HGraphVisitor : public ValueObject {
7867 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007868 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7869 : stats_(stats),
7870 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007871 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007872
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007873 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007874 virtual void VisitBasicBlock(HBasicBlock* block);
7875
Roland Levillain633021e2014-10-01 14:12:25 +01007876 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007877 void VisitInsertionOrder();
7878
Roland Levillain633021e2014-10-01 14:12:25 +01007879 // Visit the graph following dominator tree reverse post-order.
7880 void VisitReversePostOrder();
7881
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007882 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007883
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007884 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007885#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007886 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7887
7888 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7889
7890#undef DECLARE_VISIT_INSTRUCTION
7891
Igor Murashkin6ef45672017-08-08 13:59:55 -07007892 protected:
7893 OptimizingCompilerStats* stats_;
7894
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007895 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007896 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007897
7898 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7899};
7900
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007901class HGraphDelegateVisitor : public HGraphVisitor {
7902 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007903 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7904 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007905 virtual ~HGraphDelegateVisitor() {}
7906
7907 // Visit functions that delegate to to super class.
7908#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007909 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007910
7911 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7912
7913#undef DECLARE_VISIT_INSTRUCTION
7914
7915 private:
7916 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7917};
7918
Artem Serovcced8ba2017-07-19 18:18:09 +01007919// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7920// and remove the old instruction.
7921HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7922
7923// Create a clone for each clonable instructions/phis and replace the original with the clone.
7924//
7925// Used for testing individual instruction cloner.
7926class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7927 public:
7928 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007929 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007930
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007931 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007932 if (instruction->IsClonable()) {
7933 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007934 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007935 }
7936 }
7937
Artem Serov7f4aff62017-06-21 17:02:18 +01007938 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007939
7940 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007941 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007942
7943 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7944};
7945
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007946// Iterator over the blocks that art part of the loop. Includes blocks part
7947// of an inner loop. The order in which the blocks are iterated is on their
7948// block id.
7949class HBlocksInLoopIterator : public ValueObject {
7950 public:
7951 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7952 : blocks_in_loop_(info.GetBlocks()),
7953 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7954 index_(0) {
7955 if (!blocks_in_loop_.IsBitSet(index_)) {
7956 Advance();
7957 }
7958 }
7959
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007960 bool Done() const { return index_ == blocks_.size(); }
7961 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007962 void Advance() {
7963 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007964 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007965 if (blocks_in_loop_.IsBitSet(index_)) {
7966 break;
7967 }
7968 }
7969 }
7970
7971 private:
7972 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007973 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007974 size_t index_;
7975
7976 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7977};
7978
Mingyao Yang3584bce2015-05-19 16:01:59 -07007979// Iterator over the blocks that art part of the loop. Includes blocks part
7980// of an inner loop. The order in which the blocks are iterated is reverse
7981// post order.
7982class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7983 public:
7984 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7985 : blocks_in_loop_(info.GetBlocks()),
7986 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7987 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007988 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007989 Advance();
7990 }
7991 }
7992
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007993 bool Done() const { return index_ == blocks_.size(); }
7994 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007995 void Advance() {
7996 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007997 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7998 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007999 break;
8000 }
8001 }
8002 }
8003
8004 private:
8005 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008006 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008007 size_t index_;
8008
8009 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8010};
8011
Aart Bikf3e61ee2017-04-12 17:09:20 -07008012// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008013inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008014 if (constant->IsIntConstant()) {
8015 return constant->AsIntConstant()->GetValue();
8016 } else if (constant->IsLongConstant()) {
8017 return constant->AsLongConstant()->GetValue();
8018 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008019 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008020 return 0;
8021 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008022}
8023
Aart Bikf3e61ee2017-04-12 17:09:20 -07008024// Returns true iff instruction is an integral constant (and sets value on success).
8025inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8026 if (instruction->IsIntConstant()) {
8027 *value = instruction->AsIntConstant()->GetValue();
8028 return true;
8029 } else if (instruction->IsLongConstant()) {
8030 *value = instruction->AsLongConstant()->GetValue();
8031 return true;
8032 } else if (instruction->IsNullConstant()) {
8033 *value = 0;
8034 return true;
8035 }
8036 return false;
8037}
8038
Aart Bik0148de42017-09-05 09:25:01 -07008039// Returns true iff instruction is the given integral constant.
8040inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8041 int64_t val = 0;
8042 return IsInt64AndGet(instruction, &val) && val == value;
8043}
8044
8045// Returns true iff instruction is a zero bit pattern.
8046inline bool IsZeroBitPattern(HInstruction* instruction) {
8047 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8048}
8049
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008050// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008051#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008052 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8053 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8054#undef INSTRUCTION_TYPE_CHECK
8055
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008056// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008057#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8058 std::is_base_of<BaseType, H##type>::value,
8059#define INSTRUCTION_TYPE_CHECK(type, super) \
8060 inline bool HInstruction::Is##type() const { \
8061 DCHECK_LT(GetKind(), kLastInstructionKind); \
8062 using BaseType = H##type; \
8063 static constexpr bool results[] = { \
8064 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8065 }; \
8066 return results[static_cast<size_t>(GetKind())]; \
8067 }
8068
8069 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8070#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008071#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008072
8073#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008074 inline const H##type* HInstruction::As##type() const { \
8075 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8076 } \
8077 inline H##type* HInstruction::As##type() { \
8078 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8079 }
8080
Vladimir Markoa90dd512018-05-04 15:04:45 +01008081 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8082#undef INSTRUCTION_TYPE_CAST
8083
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008084
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008085// Create space in `blocks` for adding `number_of_new_blocks` entries
8086// starting at location `at`. Blocks after `at` are moved accordingly.
8087inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8088 size_t number_of_new_blocks,
8089 size_t after) {
8090 DCHECK_LT(after, blocks->size());
8091 size_t old_size = blocks->size();
8092 size_t new_size = old_size + number_of_new_blocks;
8093 blocks->resize(new_size);
8094 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8095}
8096
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008097/*
8098 * Hunt "under the hood" of array lengths (leading to array references),
8099 * null checks (also leading to array references), and new arrays
8100 * (leading to the actual length). This makes it more likely related
8101 * instructions become actually comparable.
8102 */
8103inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8104 while (instruction->IsArrayLength() ||
8105 instruction->IsNullCheck() ||
8106 instruction->IsNewArray()) {
8107 instruction = instruction->IsNewArray()
8108 ? instruction->AsNewArray()->GetLength()
8109 : instruction->InputAt(0);
8110 }
8111 return instruction;
8112}
8113
Artem Serov21c7e6f2017-07-27 16:04:42 +01008114void RemoveEnvironmentUses(HInstruction* instruction);
8115bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8116void ResetEnvironmentInputRecords(HInstruction* instruction);
8117
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008118} // namespace art
8119
8120#endif // ART_COMPILER_OPTIMIZING_NODES_H_