blob: 759a8e6638f79f0ae47ceb9d88378784403b893c [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 Geoffray05f87212019-06-19 10:00:00 +0100323 bool is_shared_jit_code = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100324 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100325 : allocator_(allocator),
326 arena_stack_(arena_stack),
327 blocks_(allocator->Adapter(kArenaAllocBlockList)),
328 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
329 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700330 entry_block_(nullptr),
331 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100332 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100333 number_of_vregs_(0),
334 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000335 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400336 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000337 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700338 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800339 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000340 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800341 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000342 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000343 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100344 dex_file_(dex_file),
345 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100346 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100347 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800348 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000350 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100351 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
352 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
353 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
354 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000355 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100356 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000357 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700358 osr_(osr),
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100359 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)),
360 is_shared_jit_code_(is_shared_jit_code) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100361 blocks_.reserve(kDefaultNumberOfBlocks);
362 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000363
David Brazdilbadd8262016-02-02 16:28:56 +0000364 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700365 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000366
Vladimir Markoca6fff82017-10-03 14:49:14 +0100367 ArenaAllocator* GetAllocator() const { return allocator_; }
368 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100369 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
370
David Brazdil69ba7b72015-06-23 18:27:30 +0100371 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000372 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100373
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000374 HBasicBlock* GetEntryBlock() const { return entry_block_; }
375 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100376 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000377
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000378 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
379 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000380
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000381 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100382
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100383 void ComputeDominanceInformation();
384 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000385 void ClearLoopInformation();
386 void FindBackEdges(ArenaBitVector* visited);
387 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100388 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100389 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000390
David Brazdil4833f5a2015-12-16 10:37:39 +0000391 // Analyze all natural loops in this graph. Returns a code specifying that it
392 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000393 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000394 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100395
David Brazdilffee3d32015-07-06 11:48:53 +0100396 // Iterate over blocks to compute try block membership. Needs reverse post
397 // order and loop information.
398 void ComputeTryBlockInformation();
399
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000400 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000401 // Returns the instruction to replace the invoke expression or null if the
402 // invoke is for a void method. Note that the caller is responsible for replacing
403 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000404 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000405
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000406 // Update the loop and try membership of `block`, which was spawned from `reference`.
407 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
408 // should be the new back edge.
409 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
410 HBasicBlock* reference,
411 bool replace_if_back_edge);
412
Mingyao Yang3584bce2015-05-19 16:01:59 -0700413 // Need to add a couple of blocks to test if the loop body is entered and
414 // put deoptimization instructions, etc.
415 void TransformLoopHeaderForBCE(HBasicBlock* header);
416
Aart Bikf8f5a162017-02-06 15:35:29 -0800417 // Adds a new loop directly after the loop with the given header and exit.
418 // Returns the new preheader.
419 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
420 HBasicBlock* body,
421 HBasicBlock* exit);
422
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000423 // Removes `block` from the graph. Assumes `block` has been disconnected from
424 // other blocks and has no instructions or phis.
425 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000426
David Brazdilfc6a86a2015-06-26 10:33:45 +0000427 // Splits the edge between `block` and `successor` while preserving the
428 // indices in the predecessor/successor lists. If there are multiple edges
429 // between the blocks, the lowest indices are used.
430 // Returns the new block which is empty and has the same dex pc as `successor`.
431 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
432
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100433 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100434 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000435
436 // Transform a loop into a format with a single preheader.
437 //
438 // Each phi in the header should be split: original one in the header should only hold
439 // inputs reachable from the back edges and a single input from the preheader. The newly created
440 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
441 //
442 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
443 // that no longer have this property.
444 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
445
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100446 void SimplifyLoop(HBasicBlock* header);
447
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000448 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100449 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000450 return current_instruction_id_++;
451 }
452
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000453 int32_t GetCurrentInstructionId() const {
454 return current_instruction_id_;
455 }
456
457 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100458 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 current_instruction_id_ = id;
460 }
461
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100462 uint16_t GetMaximumNumberOfOutVRegs() const {
463 return maximum_number_of_out_vregs_;
464 }
465
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000466 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
467 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100468 }
469
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100470 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
471 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
472 }
473
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000474 void UpdateTemporariesVRegSlots(size_t slots) {
475 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100476 }
477
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000478 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100479 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000480 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100481 }
482
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100483 void SetNumberOfVRegs(uint16_t number_of_vregs) {
484 number_of_vregs_ = number_of_vregs;
485 }
486
487 uint16_t GetNumberOfVRegs() const {
488 return number_of_vregs_;
489 }
490
491 void SetNumberOfInVRegs(uint16_t value) {
492 number_of_in_vregs_ = value;
493 }
494
David Brazdildee58d62016-04-07 09:54:26 +0000495 uint16_t GetNumberOfInVRegs() const {
496 return number_of_in_vregs_;
497 }
498
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100499 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100500 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100501 return number_of_vregs_ - number_of_in_vregs_;
502 }
503
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100504 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100505 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100506 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100507
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100508 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
509 DCHECK(GetReversePostOrder()[0] == entry_block_);
510 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
511 }
512
513 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
514 return ReverseRange(GetReversePostOrder());
515 }
516
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100517 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100518 return linear_order_;
519 }
520
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100521 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
522 return ReverseRange(GetLinearOrder());
523 }
524
Mark Mendell1152c922015-04-24 17:06:35 -0400525 bool HasBoundsChecks() const {
526 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800527 }
528
Mark Mendell1152c922015-04-24 17:06:35 -0400529 void SetHasBoundsChecks(bool value) {
530 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800531 }
532
Hans Boehm206348c2018-12-05 11:11:33 -0800533 // Is the code known to be robust against eliminating dead references
534 // and the effects of early finalization?
535 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
536
537 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
538
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000539 bool IsDebuggable() const { return debuggable_; }
540
David Brazdil8d5b8b22015-03-24 10:51:52 +0000541 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000542 // already, it is created and inserted into the graph. This method is only for
543 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100544 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000545
546 // TODO: This is problematic for the consistency of reference type propagation
547 // because it can be created anytime after the pass and thus it will be left
548 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600549 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000550
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600551 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
552 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000553 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600554 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
555 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000556 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600557 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
558 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000559 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600560 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
561 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000562 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000563
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100564 HCurrentMethod* GetCurrentMethod();
565
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100566 const DexFile& GetDexFile() const {
567 return dex_file_;
568 }
569
570 uint32_t GetMethodIdx() const {
571 return method_idx_;
572 }
573
Igor Murashkind01745e2017-04-05 16:40:31 -0700574 // Get the method name (without the signature), e.g. "<init>"
575 const char* GetMethodName() const;
576
577 // Get the pretty method name (class + name + optionally signature).
578 std::string PrettyMethod(bool with_signature = true) const;
579
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100580 InvokeType GetInvokeType() const {
581 return invoke_type_;
582 }
583
Mark Mendellc4701932015-04-10 13:18:51 -0400584 InstructionSet GetInstructionSet() const {
585 return instruction_set_;
586 }
587
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000588 bool IsCompilingOsr() const { return osr_; }
589
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100590 bool IsCompilingForSharedJitCode() const {
591 return is_shared_jit_code_;
592 }
593
Mingyao Yang063fc772016-08-02 11:02:54 -0700594 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
595 return cha_single_implementation_list_;
596 }
597
598 void AddCHASingleImplementationDependency(ArtMethod* method) {
599 cha_single_implementation_list_.insert(method);
600 }
601
602 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800603 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700604 }
605
David Brazdil77a48ae2015-09-15 12:34:04 +0000606 bool HasTryCatch() const { return has_try_catch_; }
607 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100608
Aart Bikb13c65b2017-03-21 20:14:07 -0700609 bool HasSIMD() const { return has_simd_; }
610 void SetHasSIMD(bool value) { has_simd_ = value; }
611
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800612 bool HasLoops() const { return has_loops_; }
613 void SetHasLoops(bool value) { has_loops_ = value; }
614
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000615 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
616 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
617
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100618 ArtMethod* GetArtMethod() const { return art_method_; }
619 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
620
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100621 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500622 // The instruction has been inserted into the graph, either as a constant, or
623 // before cursor.
624 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
625
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000626 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
627
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800628 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
629 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
630 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
631
David Brazdil2d7352b2015-04-20 14:52:42 +0100632 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000633 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100634 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000635
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000636 template <class InstructionType, typename ValueType>
637 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600638 ArenaSafeMap<ValueType, InstructionType*>* cache,
639 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 // Try to find an existing constant of the given value.
641 InstructionType* constant = nullptr;
642 auto cached_constant = cache->find(value);
643 if (cached_constant != cache->end()) {
644 constant = cached_constant->second;
645 }
646
647 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100648 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000649 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100650 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000651 cache->Overwrite(value, constant);
652 InsertConstant(constant);
653 }
654 return constant;
655 }
656
David Brazdil8d5b8b22015-03-24 10:51:52 +0000657 void InsertConstant(HConstant* instruction);
658
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000659 // Cache a float constant into the graph. This method should only be
660 // called by the SsaBuilder when creating "equivalent" instructions.
661 void CacheFloatConstant(HFloatConstant* constant);
662
663 // See CacheFloatConstant comment.
664 void CacheDoubleConstant(HDoubleConstant* constant);
665
Vladimir Markoca6fff82017-10-03 14:49:14 +0100666 ArenaAllocator* const allocator_;
667 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668
669 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000671
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100672 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100673 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000674
Aart Bik281c6812016-08-26 11:31:48 -0700675 // List of blocks to perform a linear order tree traversal. Unlike the reverse
676 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100677 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100678
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000679 HBasicBlock* entry_block_;
680 HBasicBlock* exit_block_;
681
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100682 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100683 uint16_t maximum_number_of_out_vregs_;
684
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100685 // The number of virtual registers in this method. Contains the parameters.
686 uint16_t number_of_vregs_;
687
688 // The number of virtual registers used by parameters of this method.
689 uint16_t number_of_in_vregs_;
690
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000691 // Number of vreg size slots that the temporaries use (used in baseline compiler).
692 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100693
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800694 // Flag whether there are bounds checks in the graph. We can skip
695 // BCE if it's false. It's only best effort to keep it up to date in
696 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400697 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800698
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800699 // Flag whether there are try/catch blocks in the graph. We will skip
700 // try/catch-related passes if it's false. It's only best effort to keep
701 // it up to date in the presence of code elimination so there might be
702 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000703 bool has_try_catch_;
704
Aart Bikb13c65b2017-03-21 20:14:07 -0700705 // Flag whether SIMD instructions appear in the graph. If true, the
706 // code generators may have to be more careful spilling the wider
707 // contents of SIMD registers.
708 bool has_simd_;
709
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800710 // Flag whether there are any loops in the graph. We can skip loop
711 // optimization if it's false. It's only best effort to keep it up
712 // to date in the presence of code elimination so there might be false
713 // positives.
714 bool has_loops_;
715
716 // Flag whether there are any irreducible loops in the graph. It's only
717 // best effort to keep it up to date in the presence of code elimination
718 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000719 bool has_irreducible_loops_;
720
Hans Boehm206348c2018-12-05 11:11:33 -0800721 // Is the code known to be robust against eliminating dead references
722 // and the effects of early finalization? If false, dead reference variables
723 // are kept if they might be visible to the garbage collector.
724 // Currently this means that the class was declared to be dead-reference-safe,
725 // the method accesses no reachability-sensitive fields or data, and the same
726 // is true for any methods that were inlined into the current one.
727 bool dead_reference_safe_;
728
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000729 // Indicates whether the graph should be compiled in a way that
730 // ensures full debuggability. If false, we can apply more
731 // aggressive optimizations that may limit the level of debugging.
732 const bool debuggable_;
733
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000734 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000735 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000736
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100737 // The dex file from which the method is from.
738 const DexFile& dex_file_;
739
740 // The method index in the dex file.
741 const uint32_t method_idx_;
742
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100743 // If inlined, this encodes how the callee is being invoked.
744 const InvokeType invoke_type_;
745
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100746 // Whether the graph has been transformed to SSA form. Only used
747 // in debug mode to ensure we are not using properties only valid
748 // for non-SSA form (like the number of temporaries).
749 bool in_ssa_form_;
750
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800751 // Number of CHA guards in the graph. Used to short-circuit the
752 // CHA guard optimization pass when there is no CHA guard left.
753 uint32_t number_of_cha_guards_;
754
Mathieu Chartiere401d142015-04-22 13:56:20 -0700755 const InstructionSet instruction_set_;
756
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000757 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000758 HNullConstant* cached_null_constant_;
759 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000760 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000761 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000762 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000763
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100764 HCurrentMethod* cached_current_method_;
765
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100766 // The ArtMethod this graph is for. Note that for AOT, it may be null,
767 // for example for methods whose declaring class could not be resolved
768 // (such as when the superclass could not be found).
769 ArtMethod* art_method_;
770
David Brazdil4833f5a2015-12-16 10:37:39 +0000771 // Keep the RTI of inexact Object to avoid having to pass stack handle
772 // collection pointer to passes which may create NullConstant.
773 ReferenceTypeInfo inexact_object_rti_;
774
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000775 // Whether we are compiling this graph for on stack replacement: this will
776 // make all loops seen as irreducible and emit special stack maps to mark
777 // compiled code entries which the interpreter can directly jump to.
778 const bool osr_;
779
Mingyao Yang063fc772016-08-02 11:02:54 -0700780 // List of methods that are assumed to have single implementation.
781 ArenaSet<ArtMethod*> cha_single_implementation_list_;
782
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100783 // Whether we are JIT compiling in the shared region area, putting
784 // restrictions on, for example, how literals are being generated.
785 bool is_shared_jit_code_;
786
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000787 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100788 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000789 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000790 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791 DISALLOW_COPY_AND_ASSIGN(HGraph);
792};
793
Vladimir Markof9f64412015-09-02 14:05:49 +0100794class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000795 public:
796 HLoopInformation(HBasicBlock* header, HGraph* graph)
797 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100798 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000799 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100800 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100801 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100802 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100803 blocks_(graph->GetAllocator(),
804 graph->GetBlocks().size(),
805 true,
806 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100807 back_edges_.reserve(kDefaultNumberOfBackEdges);
808 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100809
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000810 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100811 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000812
813 void Dump(std::ostream& os);
814
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 HBasicBlock* GetHeader() const {
816 return header_;
817 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000818
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000819 void SetHeader(HBasicBlock* block) {
820 header_ = block;
821 }
822
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100823 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
824 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
825 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
826
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000827 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100828 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000829 }
830
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100831 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100832 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 }
834
David Brazdil46e2a392015-03-16 17:31:52 +0000835 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100836 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100837 }
838
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000839 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100840 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000841 }
842
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100843 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100844
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100845 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100846 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100847 }
848
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100849 // Returns the lifetime position of the back edge that has the
850 // greatest lifetime position.
851 size_t GetLifetimeEnd() const;
852
853 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100854 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100855 }
856
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000857 // Finds blocks that are part of this loop.
858 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100859
Artem Serov7f4aff62017-06-21 17:02:18 +0100860 // Updates blocks population of the loop and all of its outer' ones recursively after the
861 // population of the inner loop is updated.
862 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
863
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100864 // Returns whether this loop information contains `block`.
865 // Note that this loop information *must* be populated before entering this function.
866 bool Contains(const HBasicBlock& block) const;
867
868 // Returns whether this loop information is an inner loop of `other`.
869 // Note that `other` *must* be populated before entering this function.
870 bool IsIn(const HLoopInformation& other) const;
871
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800872 // Returns true if instruction is not defined within this loop.
873 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700874
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100875 const ArenaBitVector& GetBlocks() const { return blocks_; }
876
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000877 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000878 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000879
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000880 void ClearAllBlocks() {
881 blocks_.ClearAllBits();
882 }
883
David Brazdil3f4a5222016-05-06 12:46:21 +0100884 bool HasBackEdgeNotDominatedByHeader() const;
885
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100886 bool IsPopulated() const {
887 return blocks_.GetHighestBitSet() != -1;
888 }
889
Anton Shaminf89381f2016-05-16 16:44:13 +0600890 bool DominatesAllBackEdges(HBasicBlock* block);
891
David Sehrc757dec2016-11-04 15:48:34 -0700892 bool HasExitEdge() const;
893
Artem Serov7f4aff62017-06-21 17:02:18 +0100894 // Resets back edge and blocks-in-loop data.
895 void ResetBasicBlockData() {
896 back_edges_.clear();
897 ClearAllBlocks();
898 }
899
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000900 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100901 // Internal recursive implementation of `Populate`.
902 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100903 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000905 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100906 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000907 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100908 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100909 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100910 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000911
912 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
913};
914
David Brazdilec16f792015-08-19 15:04:01 +0100915// Stores try/catch information for basic blocks.
916// Note that HGraph is constructed so that catch blocks cannot simultaneously
917// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100918class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100919 public:
920 // Try block information constructor.
921 explicit TryCatchInformation(const HTryBoundary& try_entry)
922 : try_entry_(&try_entry),
923 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000924 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100925 DCHECK(try_entry_ != nullptr);
926 }
927
928 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800929 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100930 : try_entry_(nullptr),
931 catch_dex_file_(&dex_file),
932 catch_type_index_(catch_type_index) {}
933
934 bool IsTryBlock() const { return try_entry_ != nullptr; }
935
936 const HTryBoundary& GetTryEntry() const {
937 DCHECK(IsTryBlock());
938 return *try_entry_;
939 }
940
941 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
942
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000943 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100944 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000945 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100946 }
947
Andreas Gampea5b09a62016-11-17 15:21:22 -0800948 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100949 DCHECK(IsCatchBlock());
950 return catch_type_index_;
951 }
952
953 const DexFile& GetCatchDexFile() const {
954 DCHECK(IsCatchBlock());
955 return *catch_dex_file_;
956 }
957
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000958 void SetInvalidTypeIndex() {
959 catch_type_index_ = dex::TypeIndex::Invalid();
960 }
961
David Brazdilec16f792015-08-19 15:04:01 +0100962 private:
963 // One of possibly several TryBoundary instructions entering the block's try.
964 // Only set for try blocks.
965 const HTryBoundary* try_entry_;
966
967 // Exception type information. Only set for catch blocks.
968 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000969 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100970};
971
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100972static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100973static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100974
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000975// A block in a method. Contains the list of instructions represented
976// as a double linked list. Each block knows its predecessors and
977// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100978
Vladimir Markof9f64412015-09-02 14:05:49 +0100979class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700981 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000982 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100983 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
984 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000985 loop_information_(nullptr),
986 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100987 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100988 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100989 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100990 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000991 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000992 try_catch_information_(nullptr) {
993 predecessors_.reserve(kDefaultNumberOfPredecessors);
994 successors_.reserve(kDefaultNumberOfSuccessors);
995 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
996 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997
Vladimir Marko60584552015-09-03 13:35:12 +0000998 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100999 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001000 }
1001
Vladimir Marko60584552015-09-03 13:35:12 +00001002 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001003 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004 }
1005
David Brazdild26a4112015-11-10 11:07:31 +00001006 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1007 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1008
Vladimir Marko60584552015-09-03 13:35:12 +00001009 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1010 return ContainsElement(successors_, block, start_from);
1011 }
1012
1013 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001014 return dominated_blocks_;
1015 }
1016
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001017 bool IsEntryBlock() const {
1018 return graph_->GetEntryBlock() == this;
1019 }
1020
1021 bool IsExitBlock() const {
1022 return graph_->GetExitBlock() == this;
1023 }
1024
David Brazdil46e2a392015-03-16 17:31:52 +00001025 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001026 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001027 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001028 bool IsSingleTryBoundary() const;
1029
1030 // Returns true if this block emits nothing but a jump.
1031 bool IsSingleJump() const {
1032 HLoopInformation* loop_info = GetLoopInformation();
1033 return (IsSingleGoto() || IsSingleTryBoundary())
1034 // Back edges generate a suspend check.
1035 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1036 }
David Brazdil46e2a392015-03-16 17:31:52 +00001037
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001038 void AddBackEdge(HBasicBlock* back_edge) {
1039 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001040 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001041 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001042 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001043 loop_information_->AddBackEdge(back_edge);
1044 }
1045
Artem Serov7f4aff62017-06-21 17:02:18 +01001046 // Registers a back edge; if the block was not a loop header before the call associates a newly
1047 // created loop info with it.
1048 //
1049 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1050 // info for all blocks during back edges recalculation.
1051 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1052 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1053 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1054 }
1055 loop_information_->AddBackEdge(back_edge);
1056 }
1057
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001058 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001059 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001061 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001062 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001063 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001065 HBasicBlock* GetDominator() const { return dominator_; }
1066 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001067 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1068
1069 void RemoveDominatedBlock(HBasicBlock* block) {
1070 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001071 }
Vladimir Marko60584552015-09-03 13:35:12 +00001072
1073 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1074 ReplaceElement(dominated_blocks_, existing, new_block);
1075 }
1076
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001077 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001078
1079 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001080 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001081 }
1082
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001083 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1084 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001085 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001086 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001087 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1088 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001089
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001090 HInstruction* GetFirstInstructionDisregardMoves() const;
1091
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001092 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001093 successors_.push_back(block);
1094 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001095 }
1096
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001097 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1098 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001099 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001100 new_block->predecessors_.push_back(this);
1101 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001102 }
1103
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001104 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1105 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001106 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001107 new_block->successors_.push_back(this);
1108 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001109 }
1110
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001111 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001112 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001113 // `predecessor` and `successor`.
1114 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1115 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001116 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001117 successor->predecessors_[predecessor_index] = this;
1118 predecessor->successors_[successor_index] = this;
1119 successors_.push_back(successor);
1120 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001121 }
1122
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001123 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001124 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001125 }
1126
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001127 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001128 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001129 }
1130
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001131 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001132 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001133 }
1134
1135 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001136 predecessors_.push_back(block);
1137 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001138 }
1139
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001140 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001141 DCHECK_EQ(predecessors_.size(), 2u);
1142 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001143 }
1144
David Brazdil769c9e52015-04-27 13:54:09 +01001145 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001146 DCHECK_EQ(successors_.size(), 2u);
1147 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001148 }
1149
David Brazdilfc6a86a2015-06-26 10:33:45 +00001150 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001151 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001152 }
1153
David Brazdilfc6a86a2015-06-26 10:33:45 +00001154 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001155 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001156 }
1157
David Brazdilfc6a86a2015-06-26 10:33:45 +00001158 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001159 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001160 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001161 }
1162
1163 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001164 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001165 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001166 }
1167
1168 // Returns whether the first occurrence of `predecessor` in the list of
1169 // predecessors is at index `idx`.
1170 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001171 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001172 return GetPredecessorIndexOf(predecessor) == idx;
1173 }
1174
David Brazdild7558da2015-09-22 13:04:14 +01001175 // Create a new block between this block and its predecessors. The new block
1176 // is added to the graph, all predecessor edges are relinked to it and an edge
1177 // is created to `this`. Returns the new empty block. Reverse post order or
1178 // loop and try/catch information are not updated.
1179 HBasicBlock* CreateImmediateDominator();
1180
David Brazdilfc6a86a2015-06-26 10:33:45 +00001181 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001182 // created, latter block. Note that this method will add the block to the
1183 // graph, create a Goto at the end of the former block and will create an edge
1184 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001185 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001186 HBasicBlock* SplitBefore(HInstruction* cursor);
1187
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001188 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001189 // created block. Note that this method just updates raw block information,
1190 // like predecessors, successors, dominators, and instruction list. It does not
1191 // update the graph, reverse post order, loop information, nor make sure the
1192 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001193 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1194
1195 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1196 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001197
1198 // Merge `other` at the end of `this`. Successors and dominated blocks of
1199 // `other` are changed to be successors and dominated blocks of `this`. Note
1200 // that this method does not update the graph, reverse post order, loop
1201 // information, nor make sure the blocks are consistent (for example ending
1202 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001203 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001204
1205 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1206 // of `this` are moved to `other`.
1207 // Note that this method does not update the graph, reverse post order, loop
1208 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001209 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001210 void ReplaceWith(HBasicBlock* other);
1211
Aart Bik6b69e0a2017-01-11 10:20:43 -08001212 // Merges the instructions of `other` at the end of `this`.
1213 void MergeInstructionsWith(HBasicBlock* other);
1214
David Brazdil2d7352b2015-04-20 14:52:42 +01001215 // Merge `other` at the end of `this`. This method updates loops, reverse post
1216 // order, links to predecessors, successors, dominators and deletes the block
1217 // from the graph. The two blocks must be successive, i.e. `this` the only
1218 // predecessor of `other` and vice versa.
1219 void MergeWith(HBasicBlock* other);
1220
1221 // Disconnects `this` from all its predecessors, successors and dominator,
1222 // removes it from all loops it is included in and eventually from the graph.
1223 // The block must not dominate any other block. Predecessors and successors
1224 // are safely updated.
1225 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001226
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001227 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001228 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001229 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001230 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001231 // Replace phi `initial` with `replacement` within this block.
1232 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001233 // Replace instruction `initial` with `replacement` within this block.
1234 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1235 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001236 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001237 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001238 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1239 // instruction list. With 'ensure_safety' set to true, it verifies that the
1240 // instruction is not in use and removes it from the use lists of its inputs.
1241 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1242 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001243 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001244
1245 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001246 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001247 }
1248
Roland Levillain6b879dd2014-09-22 17:13:44 +01001249 bool IsLoopPreHeaderFirstPredecessor() const {
1250 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001251 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001252 }
1253
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001254 bool IsFirstPredecessorBackEdge() const {
1255 DCHECK(IsLoopHeader());
1256 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1257 }
1258
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001259 HLoopInformation* GetLoopInformation() const {
1260 return loop_information_;
1261 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001262
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001263 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001264 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001265 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001266 void SetInLoop(HLoopInformation* info) {
1267 if (IsLoopHeader()) {
1268 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001269 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001270 loop_information_ = info;
1271 } else if (loop_information_->Contains(*info->GetHeader())) {
1272 // Block is currently part of an outer loop. Make it part of this inner loop.
1273 // Note that a non loop header having a loop information means this loop information
1274 // has already been populated
1275 loop_information_ = info;
1276 } else {
1277 // Block is part of an inner loop. Do not update the loop information.
1278 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1279 // at this point, because this method is being called while populating `info`.
1280 }
1281 }
1282
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001283 // Raw update of the loop information.
1284 void SetLoopInformation(HLoopInformation* info) {
1285 loop_information_ = info;
1286 }
1287
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001288 bool IsInLoop() const { return loop_information_ != nullptr; }
1289
David Brazdilec16f792015-08-19 15:04:01 +01001290 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1291
1292 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1293 try_catch_information_ = try_catch_information;
1294 }
1295
1296 bool IsTryBlock() const {
1297 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1298 }
1299
1300 bool IsCatchBlock() const {
1301 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1302 }
David Brazdilffee3d32015-07-06 11:48:53 +01001303
1304 // Returns the try entry that this block's successors should have. They will
1305 // be in the same try, unless the block ends in a try boundary. In that case,
1306 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001307 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001308
Aart Bik75ff2c92018-04-21 01:28:11 +00001309 bool HasThrowingInstructions() const;
1310
David Brazdila4b8c212015-05-07 09:59:30 +01001311 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001312 bool Dominates(HBasicBlock* block) const;
1313
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001314 size_t GetLifetimeStart() const { return lifetime_start_; }
1315 size_t GetLifetimeEnd() const { return lifetime_end_; }
1316
1317 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1318 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1319
David Brazdil8d5b8b22015-03-24 10:51:52 +00001320 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001321 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001322 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001323 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001324 bool HasSinglePhi() const;
1325
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001326 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001327 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001328 ArenaVector<HBasicBlock*> predecessors_;
1329 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001330 HInstructionList instructions_;
1331 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001332 HLoopInformation* loop_information_;
1333 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001334 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001335 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001336 // The dex program counter of the first instruction of this block.
1337 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001338 size_t lifetime_start_;
1339 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001340 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001341
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001342 friend class HGraph;
1343 friend class HInstruction;
1344
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1346};
1347
David Brazdilb2bd1c52015-03-25 11:17:37 +00001348// Iterates over the LoopInformation of all loops which contain 'block'
1349// from the innermost to the outermost.
1350class HLoopInformationOutwardIterator : public ValueObject {
1351 public:
1352 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1353 : current_(block.GetLoopInformation()) {}
1354
1355 bool Done() const { return current_ == nullptr; }
1356
1357 void Advance() {
1358 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001359 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001360 }
1361
1362 HLoopInformation* Current() const {
1363 DCHECK(!Done());
1364 return current_;
1365 }
1366
1367 private:
1368 HLoopInformation* current_;
1369
1370 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1371};
1372
Alexandre Ramesef20f712015-06-09 10:29:30 +01001373#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001374 M(Above, Condition) \
1375 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001376 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001377 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001378 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001379 M(ArrayGet, Instruction) \
1380 M(ArrayLength, Instruction) \
1381 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001382 M(Below, Condition) \
1383 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001384 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001385 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001386 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001387 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001388 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001389 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001390 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001391 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001392 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001393 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001394 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001395 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001397 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001398 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001400 M(Exit, Instruction) \
1401 M(FloatConstant, Constant) \
1402 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001403 M(GreaterThan, Condition) \
1404 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001405 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001406 M(InstanceFieldGet, Instruction) \
1407 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001408 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001409 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001410 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001411 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001412 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001413 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001415 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001416 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001417 M(LessThan, Condition) \
1418 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001419 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001420 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001421 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001422 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001423 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001424 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001425 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001426 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001427 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001428 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001429 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001430 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001431 M(Neg, UnaryOperation) \
1432 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001433 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001434 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001435 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001436 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001437 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001438 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001439 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001440 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001441 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001442 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001443 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001444 M(Return, Instruction) \
1445 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001446 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001447 M(Shl, BinaryOperation) \
1448 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001449 M(StaticFieldGet, Instruction) \
1450 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001451 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001452 M(UnresolvedInstanceFieldGet, Instruction) \
1453 M(UnresolvedInstanceFieldSet, Instruction) \
1454 M(UnresolvedStaticFieldGet, Instruction) \
1455 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001456 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001457 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001458 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001459 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001460 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001461 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001462 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001463 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001464 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001465 M(VecExtractScalar, VecUnaryOperation) \
1466 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001467 M(VecCnv, VecUnaryOperation) \
1468 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001469 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001470 M(VecNot, VecUnaryOperation) \
1471 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001472 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001473 M(VecSub, VecBinaryOperation) \
1474 M(VecMul, VecBinaryOperation) \
1475 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001476 M(VecMin, VecBinaryOperation) \
1477 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001478 M(VecAnd, VecBinaryOperation) \
1479 M(VecAndNot, VecBinaryOperation) \
1480 M(VecOr, VecBinaryOperation) \
1481 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001482 M(VecSaturationAdd, VecBinaryOperation) \
1483 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001484 M(VecShl, VecBinaryOperation) \
1485 M(VecShr, VecBinaryOperation) \
1486 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001487 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001488 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001489 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001490 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001491 M(VecLoad, VecMemoryOperation) \
1492 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001493
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001494/*
1495 * Instructions, shared across several (not all) architectures.
1496 */
1497#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1498#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1499#else
1500#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001501 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001502 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001503 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001504 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001505#endif
1506
Alexandre Ramesef20f712015-06-09 10:29:30 +01001507#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1508
1509#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1510
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001511#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001512#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001513#else
1514#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1515 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001516 M(MipsPackedSwitch, Instruction) \
1517 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001518#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001519
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001520#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1521
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001522#ifndef ART_ENABLE_CODEGEN_x86
1523#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1524#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001525#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1526 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001527 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001528 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001529 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001530#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001531
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301532#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1533#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301534 M(X86AndNot, Instruction) \
1535 M(X86MaskOrResetLeastSetBit, Instruction) \
1536 M(VecAvxSub, VecOperation) \
1537 M(VecAvxAdd, VecOperation)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301538#else
1539#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1540#endif
1541
Alexandre Ramesef20f712015-06-09 10:29:30 +01001542#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1543
1544#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1545 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001546 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001547 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1548 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001549 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001550 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001551 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301552 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1553 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001554
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001555#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1556 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001557 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001558 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001559 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001560 M(Invoke, Instruction) \
1561 M(VecOperation, Instruction) \
1562 M(VecUnaryOperation, VecOperation) \
1563 M(VecBinaryOperation, VecOperation) \
1564 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001565
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001566#define FOR_EACH_INSTRUCTION(M) \
1567 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1568 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1569
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001570#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001571FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1572#undef FORWARD_DECLARATION
1573
Vladimir Marko372f10e2016-05-17 16:30:10 +01001574#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001575 private: \
1576 H##type& operator=(const H##type&) = delete; \
1577 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001578 const char* DebugName() const override { return #type; } \
1579 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001580 DCHECK(IsClonable()); \
1581 return new (arena) H##type(*this->As##type()); \
1582 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001583 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001584
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001585#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001586 private: \
1587 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001588 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001589
Artem Serovcced8ba2017-07-19 18:18:09 +01001590#define DEFAULT_COPY_CONSTRUCTOR(type) \
1591 explicit H##type(const H##type& other) = default;
1592
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001593template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001594class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1595 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001596 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001597 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001598 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001599 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001600 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001601 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001602 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001603
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001604 private:
David Brazdiled596192015-01-23 10:39:45 +00001605 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001606 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001607
1608 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001609 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001610
Vladimir Marko46817b82016-03-29 12:21:58 +01001611 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001612
1613 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1614};
1615
David Brazdiled596192015-01-23 10:39:45 +00001616template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001617using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001618
David Brazdil1abb4192015-02-17 18:33:36 +00001619// This class is used by HEnvironment and HInstruction classes to record the
1620// instructions they use and pointers to the corresponding HUseListNodes kept
1621// by the used instructions.
1622template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001623class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001624 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001625 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1626 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001627
Vladimir Marko46817b82016-03-29 12:21:58 +01001628 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1629 : HUserRecord(old_record.instruction_, before_use_node) {}
1630 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1631 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001632 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001633 }
1634
1635 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001636 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1637 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001638
1639 private:
1640 // Instruction used by the user.
1641 HInstruction* instruction_;
1642
Vladimir Marko46817b82016-03-29 12:21:58 +01001643 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1644 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001645};
1646
Vladimir Markoe9004912016-06-16 16:50:52 +01001647// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1648// This is used for HInstruction::GetInputs() to return a container wrapper providing
1649// HInstruction* values even though the underlying container has HUserRecord<>s.
1650struct HInputExtractor {
1651 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1652 return record.GetInstruction();
1653 }
1654 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1655 return record.GetInstruction();
1656 }
1657};
1658
1659using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1660using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1661
Aart Bik854a02b2015-07-14 16:07:00 -07001662/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001663 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001664 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001665 * For write/read dependences on fields/arrays, the dependence analysis uses
1666 * type disambiguation (e.g. a float field write cannot modify the value of an
1667 * integer field read) and the access type (e.g. a reference array write cannot
1668 * modify the value of a reference field read [although it may modify the
1669 * reference fetch prior to reading the field, which is represented by its own
1670 * write/read dependence]). The analysis makes conservative points-to
1671 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1672 * the same, and any reference read depends on any reference read without
1673 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001674 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001675 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1676 * alive across the point where garbage collection might happen.
1677 *
1678 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1679 *
1680 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1681 * those instructions from the compiler perspective (between this instruction and the next one
1682 * in the IR).
1683 *
1684 * Note: Instructions which can cause GC only on a fatal slow path do not need
1685 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1686 * one. However the execution may return to compiled code if there is a catch block in the
1687 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1688 * set.
1689 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001690 * The internal representation uses 38-bit and is described in the table below.
1691 * The first line indicates the side effect, and for field/array accesses the
1692 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001693 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001694 * The two numbered lines below indicate the bit position in the bitfield (read
1695 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001696 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001697 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1698 * +-------------+---------+---------+--------------+---------+---------+
1699 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1700 * | 3 |333333322|222222221| 1 |111111110|000000000|
1701 * | 7 |654321098|765432109| 8 |765432109|876543210|
1702 *
1703 * Note that, to ease the implementation, 'changes' bits are least significant
1704 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001705 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001706class SideEffects : public ValueObject {
1707 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001708 SideEffects() : flags_(0) {}
1709
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001710 static SideEffects None() {
1711 return SideEffects(0);
1712 }
1713
1714 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001715 return SideEffects(kAllChangeBits | kAllDependOnBits);
1716 }
1717
1718 static SideEffects AllChanges() {
1719 return SideEffects(kAllChangeBits);
1720 }
1721
1722 static SideEffects AllDependencies() {
1723 return SideEffects(kAllDependOnBits);
1724 }
1725
1726 static SideEffects AllExceptGCDependency() {
1727 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1728 }
1729
1730 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001731 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001732 }
1733
Aart Bik34c3ba92015-07-20 14:08:59 -07001734 static SideEffects AllWrites() {
1735 return SideEffects(kAllWrites);
1736 }
1737
1738 static SideEffects AllReads() {
1739 return SideEffects(kAllReads);
1740 }
1741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 static SideEffects FieldWriteOfType(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, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001746 }
1747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001748 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001749 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001750 }
1751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001752 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001753 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001754 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001755 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001756 }
1757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001759 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001760 }
1761
Artem Serovd1aa7d02018-06-22 11:35:46 +01001762 // Returns whether GC might happen across this instruction from the compiler perspective so
1763 // the next instruction in the IR would see that.
1764 //
1765 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001766 static SideEffects CanTriggerGC() {
1767 return SideEffects(1ULL << kCanTriggerGCBit);
1768 }
1769
Artem Serovd1aa7d02018-06-22 11:35:46 +01001770 // Returns whether the instruction must not be alive across a GC point.
1771 //
1772 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001773 static SideEffects DependsOnGC() {
1774 return SideEffects(1ULL << kDependsOnGCBit);
1775 }
1776
Aart Bik854a02b2015-07-14 16:07:00 -07001777 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001778 SideEffects Union(SideEffects other) const {
1779 return SideEffects(flags_ | other.flags_);
1780 }
1781
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001782 SideEffects Exclusion(SideEffects other) const {
1783 return SideEffects(flags_ & ~other.flags_);
1784 }
1785
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001786 void Add(SideEffects other) {
1787 flags_ |= other.flags_;
1788 }
1789
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 bool Includes(SideEffects other) const {
1791 return (other.flags_ & flags_) == other.flags_;
1792 }
1793
1794 bool HasSideEffects() const {
1795 return (flags_ & kAllChangeBits);
1796 }
1797
1798 bool HasDependencies() const {
1799 return (flags_ & kAllDependOnBits);
1800 }
1801
1802 // Returns true if there are no side effects or dependencies.
1803 bool DoesNothing() const {
1804 return flags_ == 0;
1805 }
1806
Aart Bik854a02b2015-07-14 16:07:00 -07001807 // Returns true if something is written.
1808 bool DoesAnyWrite() const {
1809 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001810 }
1811
Aart Bik854a02b2015-07-14 16:07:00 -07001812 // Returns true if something is read.
1813 bool DoesAnyRead() const {
1814 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001815 }
1816
Aart Bik854a02b2015-07-14 16:07:00 -07001817 // Returns true if potentially everything is written and read
1818 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001819 bool DoesAllReadWrite() const {
1820 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1821 }
1822
Aart Bik854a02b2015-07-14 16:07:00 -07001823 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001824 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001825 }
1826
Roland Levillain0d5a2812015-11-13 10:07:31 +00001827 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001828 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001829 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1830 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001831 }
1832
1833 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001834 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001835 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001836 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001837 for (int s = kLastBit; s >= 0; s--) {
1838 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1839 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1840 // This is a bit for the GC side effect.
1841 if (current_bit_is_set) {
1842 flags += "GC";
1843 }
Aart Bik854a02b2015-07-14 16:07:00 -07001844 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001845 } else {
1846 // This is a bit for the array/field analysis.
1847 // The underscore character stands for the 'can trigger GC' bit.
1848 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1849 if (current_bit_is_set) {
1850 flags += kDebug[s];
1851 }
1852 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1853 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1854 flags += "|";
1855 }
1856 }
Aart Bik854a02b2015-07-14 16:07:00 -07001857 }
1858 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001859 }
1860
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001861 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001862
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001863 private:
1864 static constexpr int kFieldArrayAnalysisBits = 9;
1865
1866 static constexpr int kFieldWriteOffset = 0;
1867 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1868 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1869 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1870
1871 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1872
1873 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1874 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1875 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1876 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1877
1878 static constexpr int kLastBit = kDependsOnGCBit;
1879 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1880
1881 // Aliases.
1882
1883 static_assert(kChangeBits == kDependOnBits,
1884 "the 'change' bits should match the 'depend on' bits.");
1885
1886 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1887 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1888 static constexpr uint64_t kAllWrites =
1889 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1890 static constexpr uint64_t kAllReads =
1891 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001892
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001893 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001894 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001895 int shift;
1896 switch (type) {
1897 case DataType::Type::kReference: shift = 0; break;
1898 case DataType::Type::kBool: shift = 1; break;
1899 case DataType::Type::kInt8: shift = 2; break;
1900 case DataType::Type::kUint16: shift = 3; break;
1901 case DataType::Type::kInt16: shift = 4; break;
1902 case DataType::Type::kInt32: shift = 5; break;
1903 case DataType::Type::kInt64: shift = 6; break;
1904 case DataType::Type::kFloat32: shift = 7; break;
1905 case DataType::Type::kFloat64: shift = 8; break;
1906 default:
1907 LOG(FATAL) << "Unexpected data type " << type;
1908 UNREACHABLE();
1909 }
Aart Bik854a02b2015-07-14 16:07:00 -07001910 DCHECK_LE(kFieldWriteOffset, shift);
1911 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001912 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001913 }
1914
1915 // Private constructor on direct flags value.
1916 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1917
1918 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001919};
1920
David Brazdiled596192015-01-23 10:39:45 +00001921// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001922class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001923 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001924 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001925 size_t number_of_vregs,
1926 ArtMethod* method,
1927 uint32_t dex_pc,
1928 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001929 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1930 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001931 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001932 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001933 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001934 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001935 }
1936
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001937 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1938 const HEnvironment& to_copy,
1939 HInstruction* holder)
1940 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001941 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001942 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001943 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001944 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001945
Vladimir Markoec32f642017-06-02 10:51:55 +01001946 void AllocateLocations() {
1947 DCHECK(locations_.empty());
1948 locations_.resize(vregs_.size());
1949 }
1950
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001951 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001952 if (parent_ != nullptr) {
1953 parent_->SetAndCopyParentChain(allocator, parent);
1954 } else {
1955 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1956 parent_->CopyFrom(parent);
1957 if (parent->GetParent() != nullptr) {
1958 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1959 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001960 }
David Brazdiled596192015-01-23 10:39:45 +00001961 }
1962
Vladimir Marko69d310e2017-10-09 14:12:23 +01001963 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001964 void CopyFrom(HEnvironment* environment);
1965
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001966 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1967 // input to the loop phi instead. This is for inserting instructions that
1968 // require an environment (like HDeoptimization) in the loop pre-header.
1969 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001970
1971 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001972 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001973 }
1974
1975 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001976 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001977 }
1978
David Brazdil1abb4192015-02-17 18:33:36 +00001979 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001980
Artem Serovca210e32017-12-15 13:43:20 +00001981 // Replaces the input at the position 'index' with the replacement; the replacement and old
1982 // input instructions' env_uses_ lists are adjusted. The function works similar to
1983 // HInstruction::ReplaceInput.
1984 void ReplaceInput(HInstruction* replacement, size_t index);
1985
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001986 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001987
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001988 HEnvironment* GetParent() const { return parent_; }
1989
1990 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001991 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001992 }
1993
1994 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001995 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001996 }
1997
1998 uint32_t GetDexPc() const {
1999 return dex_pc_;
2000 }
2001
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002002 ArtMethod* GetMethod() const {
2003 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002004 }
2005
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002006 HInstruction* GetHolder() const {
2007 return holder_;
2008 }
2009
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002010
2011 bool IsFromInlinedInvoke() const {
2012 return GetParent() != nullptr;
2013 }
2014
David Brazdiled596192015-01-23 10:39:45 +00002015 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002016 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2017 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002018 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002019 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002020 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002021
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002022 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002023 HInstruction* const holder_;
2024
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002025 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002026
2027 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2028};
2029
Vladimir Markof9f64412015-09-02 14:05:49 +01002030class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002031 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302032#define DECLARE_KIND(type, super) k##type,
2033 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002034 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302035 kLastInstructionKind
2036 };
2037#undef DECLARE_KIND
2038
2039 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002040 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2041
2042 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002043 : previous_(nullptr),
2044 next_(nullptr),
2045 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002046 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002047 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002048 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002049 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002051 locations_(nullptr),
2052 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002053 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002054 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002055 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302056 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002057 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002058 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2059 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002060
Dave Allison20dfc792014-06-16 20:44:29 -07002061 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002062
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002063
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002064 HInstruction* GetNext() const { return next_; }
2065 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002066
Calin Juravle77520bc2015-01-12 18:45:46 +00002067 HInstruction* GetNextDisregardingMoves() const;
2068 HInstruction* GetPreviousDisregardingMoves() const;
2069
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002070 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002071 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002072 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002073 bool IsInBlock() const { return block_ != nullptr; }
2074 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002075 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2076 bool IsIrreducibleLoopHeaderPhi() const {
2077 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2078 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002079
Vladimir Marko372f10e2016-05-17 16:30:10 +01002080 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2081
2082 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2083 // One virtual method is enough, just const_cast<> and then re-add the const.
2084 return ArrayRef<const HUserRecord<HInstruction*>>(
2085 const_cast<HInstruction*>(this)->GetInputRecords());
2086 }
2087
Vladimir Markoe9004912016-06-16 16:50:52 +01002088 HInputsRef GetInputs() {
2089 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002090 }
2091
Vladimir Markoe9004912016-06-16 16:50:52 +01002092 HConstInputsRef GetInputs() const {
2093 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002094 }
2095
2096 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002097 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098
Aart Bik2767f4b2016-10-28 15:03:53 -07002099 bool HasInput(HInstruction* input) const {
2100 for (const HInstruction* i : GetInputs()) {
2101 if (i == input) {
2102 return true;
2103 }
2104 }
2105 return false;
2106 }
2107
Vladimir Marko372f10e2016-05-17 16:30:10 +01002108 void SetRawInputAt(size_t index, HInstruction* input) {
2109 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2110 }
2111
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002112 virtual void Accept(HGraphVisitor* visitor) = 0;
2113 virtual const char* DebugName() const = 0;
2114
Vladimir Markobd785672018-05-03 17:09:09 +01002115 DataType::Type GetType() const {
2116 return TypeField::Decode(GetPackedFields());
2117 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002118
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002119 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002120
2121 uint32_t GetDexPc() const { return dex_pc_; }
2122
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002123 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002124
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002125 // Can the instruction throw?
2126 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2127 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002128 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002129
2130 // Does the instruction always throw an exception unconditionally?
2131 virtual bool AlwaysThrows() const { return false; }
2132
David Brazdilec16f792015-08-19 15:04:01 +01002133 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002134
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002135 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002136 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002137
Calin Juravle10e244f2015-01-26 18:54:32 +00002138 // Does not apply for all instructions, but having this at top level greatly
2139 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002140 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002141 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002142 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002143 return true;
2144 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002145
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002146 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002147 return false;
2148 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002149
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002150 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2151 // with it. Otherwise return null.
2152 HNullCheck* GetImplicitNullCheck() const {
2153 // Find the first previous instruction which is not a move.
2154 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2155 if (first_prev_not_move != nullptr &&
2156 first_prev_not_move->IsNullCheck() &&
2157 first_prev_not_move->IsEmittedAtUseSite()) {
2158 return first_prev_not_move->AsNullCheck();
2159 }
2160 return nullptr;
2161 }
2162
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002163 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002164 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002165 }
2166
Calin Juravle2e768302015-07-28 14:41:11 +00002167 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002168
Calin Juravle61d544b2015-02-23 16:46:57 +00002169 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002170 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002171 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002172 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002173 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002174
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002175 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002176 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002177 // Note: fixup_end remains valid across push_front().
2178 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2179 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002180 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002181 uses_.push_front(*new_node);
2182 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002183 }
2184
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002186 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002187 // Note: env_fixup_end remains valid across push_front().
2188 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2189 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002190 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002191 env_uses_.push_front(*new_node);
2192 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193 }
2194
David Brazdil1abb4192015-02-17 18:33:36 +00002195 void RemoveAsUserOfInput(size_t input) {
2196 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002197 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2198 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2199 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002200 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002201
Vladimir Marko372f10e2016-05-17 16:30:10 +01002202 void RemoveAsUserOfAllInputs() {
2203 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2204 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2205 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2206 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2207 }
2208 }
2209
David Brazdil1abb4192015-02-17 18:33:36 +00002210 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2211 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002212
Vladimir Marko46817b82016-03-29 12:21:58 +01002213 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2214 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2215 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002216 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002217 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002218 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002219
Aart Bikcc42be02016-10-20 16:14:16 -07002220 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002221 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002222 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002223 !CanThrow() &&
2224 !IsSuspendCheck() &&
2225 !IsControlFlow() &&
2226 !IsNativeDebugInfo() &&
2227 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002228 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002229 !IsMemoryBarrier() &&
2230 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002231 }
2232
Aart Bikcc42be02016-10-20 16:14:16 -07002233 bool IsDeadAndRemovable() const {
2234 return IsRemovable() && !HasUses();
2235 }
2236
Roland Levillain6c82d402014-10-13 16:10:27 +01002237 // Does this instruction strictly dominate `other_instruction`?
2238 // Returns false if this instruction and `other_instruction` are the same.
2239 // Aborts if this instruction and `other_instruction` are both phis.
2240 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002241
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002242 int GetId() const { return id_; }
2243 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002244
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002245 int GetSsaIndex() const { return ssa_index_; }
2246 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2247 bool HasSsaIndex() const { return ssa_index_ != -1; }
2248
2249 bool HasEnvironment() const { return environment_ != nullptr; }
2250 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002251 // Set the `environment_` field. Raw because this method does not
2252 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002253 void SetRawEnvironment(HEnvironment* environment) {
2254 DCHECK(environment_ == nullptr);
2255 DCHECK_EQ(environment->GetHolder(), this);
2256 environment_ = environment;
2257 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002258
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002259 void InsertRawEnvironment(HEnvironment* environment) {
2260 DCHECK(environment_ != nullptr);
2261 DCHECK_EQ(environment->GetHolder(), this);
2262 DCHECK(environment->GetParent() == nullptr);
2263 environment->parent_ = environment_;
2264 environment_ = environment;
2265 }
2266
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002267 void RemoveEnvironment();
2268
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002269 // Set the environment of this instruction, copying it from `environment`. While
2270 // copying, the uses lists are being updated.
2271 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002272 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002273 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002274 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002275 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002276 if (environment->GetParent() != nullptr) {
2277 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2278 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002279 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002280
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002281 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2282 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002283 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002284 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002285 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002286 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002287 if (environment->GetParent() != nullptr) {
2288 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2289 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002290 }
2291
Nicolas Geoffray39468442014-09-02 15:17:15 +01002292 // Returns the number of entries in the environment. Typically, that is the
2293 // number of dex registers in a method. It could be more in case of inlining.
2294 size_t EnvironmentSize() const;
2295
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002296 LocationSummary* GetLocations() const { return locations_; }
2297 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002298
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002299 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002300 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002301 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002302 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002303
Alexandre Rames188d4312015-04-09 18:30:21 +01002304 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2305 // uses of this instruction by `other` are *not* updated.
2306 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2307 ReplaceWith(other);
2308 other->ReplaceInput(this, use_index);
2309 }
2310
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002311 // Move `this` instruction before `cursor`
2312 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002313
Vladimir Markofb337ea2015-11-25 15:25:10 +00002314 // Move `this` before its first user and out of any loops. If there is no
2315 // out-of-loop user that dominates all other users, move the instruction
2316 // to the end of the out-of-loop common dominator of the user's blocks.
2317 //
2318 // This can be used only on non-throwing instructions with no side effects that
2319 // have at least one use but no environment uses.
2320 void MoveBeforeFirstUserAndOutOfLoops();
2321
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002322#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002323 bool Is##type() const;
2324
2325 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2326#undef INSTRUCTION_TYPE_CHECK
2327
2328#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002329 const H##type* As##type() const; \
2330 H##type* As##type();
2331
Vladimir Markoa90dd512018-05-04 15:04:45 +01002332 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2333#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002334
Artem Serovcced8ba2017-07-19 18:18:09 +01002335 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2336 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2337 // the instruction then the behaviour of this function is undefined.
2338 //
2339 // Note: It is semantically valid to create a clone of the instruction only until
2340 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2341 // copied.
2342 //
2343 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2344 // 'explicit HInstruction(const HInstruction& other)' for details.
2345 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2346 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2347 DebugName() << " " << GetId();
2348 UNREACHABLE();
2349 }
2350
2351 // Return whether instruction can be cloned (copied).
2352 virtual bool IsClonable() const { return false; }
2353
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002354 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002355 // TODO: this method is used by LICM and GVN with possibly different
2356 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002357 virtual bool CanBeMoved() const { return false; }
2358
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002359 // Returns whether any data encoded in the two instructions is equal.
2360 // This method does not look at the inputs. Both instructions must be
2361 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002362 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002363 return false;
2364 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002365
2366 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002367 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002368 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002369 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002370
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302371 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002372
2373 virtual size_t ComputeHashCode() const {
2374 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002375 for (const HInstruction* input : GetInputs()) {
2376 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002377 }
2378 return result;
2379 }
2380
2381 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002382 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002383 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002384
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002385 size_t GetLifetimePosition() const { return lifetime_position_; }
2386 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2387 LiveInterval* GetLiveInterval() const { return live_interval_; }
2388 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2389 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2390
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002391 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2392
2393 // Returns whether the code generation of the instruction will require to have access
2394 // to the current method. Such instructions are:
2395 // (1): Instructions that require an environment, as calling the runtime requires
2396 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002397 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2398 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002399 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002400 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002401 }
2402
Vladimir Markodc151b22015-10-15 18:02:30 +01002403 // Returns whether the code generation of the instruction will require to have access
2404 // to the dex cache of the current method's declaring class via the current method.
2405 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002406
Mark Mendellc4701932015-04-10 13:18:51 -04002407 // Does this instruction have any use in an environment before
2408 // control flow hits 'other'?
2409 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2410
2411 // Remove all references to environment uses of this instruction.
2412 // The caller must ensure that this is safe to do.
2413 void RemoveEnvironmentUsers();
2414
Vladimir Markoa1de9182016-02-25 11:37:38 +00002415 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2416 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002417
David Brazdil1abb4192015-02-17 18:33:36 +00002418 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002419 // If set, the machine code for this instruction is assumed to be generated by
2420 // its users. Used by liveness analysis to compute use positions accordingly.
2421 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2422 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302423 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2424 static constexpr size_t kFieldInstructionKindSize =
2425 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002426 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302427 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002428 static constexpr size_t kFieldTypeSize =
2429 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2430 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002431 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2432
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302433 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2434 "Too many generic packed fields");
2435
Vladimir Markobd785672018-05-03 17:09:09 +01002436 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2437
Vladimir Marko372f10e2016-05-17 16:30:10 +01002438 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2439 return GetInputRecords()[i];
2440 }
2441
2442 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2443 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2444 input_records[index] = input;
2445 }
David Brazdil1abb4192015-02-17 18:33:36 +00002446
Vladimir Markoa1de9182016-02-25 11:37:38 +00002447 uint32_t GetPackedFields() const {
2448 return packed_fields_;
2449 }
2450
2451 template <size_t flag>
2452 bool GetPackedFlag() const {
2453 return (packed_fields_ & (1u << flag)) != 0u;
2454 }
2455
2456 template <size_t flag>
2457 void SetPackedFlag(bool value = true) {
2458 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2459 }
2460
2461 template <typename BitFieldType>
2462 typename BitFieldType::value_type GetPackedField() const {
2463 return BitFieldType::Decode(packed_fields_);
2464 }
2465
2466 template <typename BitFieldType>
2467 void SetPackedField(typename BitFieldType::value_type value) {
2468 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2469 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2470 }
2471
Artem Serovcced8ba2017-07-19 18:18:09 +01002472 // Copy construction for the instruction (used for Clone function).
2473 //
2474 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2475 // prepare_for_register_allocator are not copied (set to default values).
2476 //
2477 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2478 // fine for most of them. However for some of the instructions a custom copy constructor must be
2479 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2480 // for copying them).
2481 explicit HInstruction(const HInstruction& other)
2482 : previous_(nullptr),
2483 next_(nullptr),
2484 block_(nullptr),
2485 dex_pc_(other.dex_pc_),
2486 id_(-1),
2487 ssa_index_(-1),
2488 packed_fields_(other.packed_fields_),
2489 environment_(nullptr),
2490 locations_(nullptr),
2491 live_interval_(nullptr),
2492 lifetime_position_(kNoLifetime),
2493 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302494 reference_type_handle_(other.reference_type_handle_) {
2495 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002496
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002497 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302498 using InstructionKindField =
2499 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2500
Vladimir Marko46817b82016-03-29 12:21:58 +01002501 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2502 auto before_use_node = uses_.before_begin();
2503 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2504 HInstruction* user = use_node->GetUser();
2505 size_t input_index = use_node->GetIndex();
2506 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2507 before_use_node = use_node;
2508 }
2509 }
2510
2511 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2512 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2513 if (next != uses_.end()) {
2514 HInstruction* next_user = next->GetUser();
2515 size_t next_index = next->GetIndex();
2516 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2517 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2518 }
2519 }
2520
2521 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2522 auto before_env_use_node = env_uses_.before_begin();
2523 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2524 HEnvironment* user = env_use_node->GetUser();
2525 size_t input_index = env_use_node->GetIndex();
2526 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2527 before_env_use_node = env_use_node;
2528 }
2529 }
2530
2531 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2532 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2533 if (next != env_uses_.end()) {
2534 HEnvironment* next_user = next->GetUser();
2535 size_t next_index = next->GetIndex();
2536 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2537 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2538 }
2539 }
David Brazdil1abb4192015-02-17 18:33:36 +00002540
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002541 HInstruction* previous_;
2542 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002543 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002544 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002545
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002546 // An instruction gets an id when it is added to the graph.
2547 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002548 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002549 int id_;
2550
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002551 // When doing liveness analysis, instructions that have uses get an SSA index.
2552 int ssa_index_;
2553
Vladimir Markoa1de9182016-02-25 11:37:38 +00002554 // Packed fields.
2555 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002556
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002557 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002558 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002559
2560 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002561 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002562
Nicolas Geoffray39468442014-09-02 15:17:15 +01002563 // The environment associated with this instruction. Not null if the instruction
2564 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002565 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002566
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002567 // Set by the code generator.
2568 LocationSummary* locations_;
2569
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002570 // Set by the liveness analysis.
2571 LiveInterval* live_interval_;
2572
2573 // Set by the liveness analysis, this is the position in a linear
2574 // order of blocks where this instruction's live interval start.
2575 size_t lifetime_position_;
2576
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002577 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002578
Vladimir Markoa1de9182016-02-25 11:37:38 +00002579 // The reference handle part of the reference type info.
2580 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002581 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002582 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002583
David Brazdil1abb4192015-02-17 18:33:36 +00002584 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002585 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002586 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002587 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002588 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002589};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002590std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002591
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002592// Iterates over the instructions, while preserving the next instruction
2593// in case the current instruction gets removed from the list by the user
2594// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002595class HInstructionIterator : public ValueObject {
2596 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002597 explicit HInstructionIterator(const HInstructionList& instructions)
2598 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002599 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002600 }
2601
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002602 bool Done() const { return instruction_ == nullptr; }
2603 HInstruction* Current() const { return instruction_; }
2604 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002605 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002606 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002607 }
2608
2609 private:
2610 HInstruction* instruction_;
2611 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002612
2613 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002614};
2615
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002616// Iterates over the instructions without saving the next instruction,
2617// therefore handling changes in the graph potentially made by the user
2618// of this iterator.
2619class HInstructionIteratorHandleChanges : public ValueObject {
2620 public:
2621 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2622 : instruction_(instructions.first_instruction_) {
2623 }
2624
2625 bool Done() const { return instruction_ == nullptr; }
2626 HInstruction* Current() const { return instruction_; }
2627 void Advance() {
2628 instruction_ = instruction_->GetNext();
2629 }
2630
2631 private:
2632 HInstruction* instruction_;
2633
2634 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2635};
2636
2637
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002638class HBackwardInstructionIterator : public ValueObject {
2639 public:
2640 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2641 : instruction_(instructions.last_instruction_) {
2642 next_ = Done() ? nullptr : instruction_->GetPrevious();
2643 }
2644
2645 bool Done() const { return instruction_ == nullptr; }
2646 HInstruction* Current() const { return instruction_; }
2647 void Advance() {
2648 instruction_ = next_;
2649 next_ = Done() ? nullptr : instruction_->GetPrevious();
2650 }
2651
2652 private:
2653 HInstruction* instruction_;
2654 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002655
2656 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002657};
2658
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002659class HVariableInputSizeInstruction : public HInstruction {
2660 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002661 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002662 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002663 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2664 }
2665
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002666 void AddInput(HInstruction* input);
2667 void InsertInputAt(size_t index, HInstruction* input);
2668 void RemoveInputAt(size_t index);
2669
Igor Murashkind01745e2017-04-05 16:40:31 -07002670 // Removes all the inputs.
2671 // Also removes this instructions from each input's use list
2672 // (for non-environment uses only).
2673 void RemoveAllInputs();
2674
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002675 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302676 HVariableInputSizeInstruction(InstructionKind inst_kind,
2677 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002678 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002679 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002680 size_t number_of_inputs,
2681 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302682 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002683 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002684 HVariableInputSizeInstruction(InstructionKind inst_kind,
2685 DataType::Type type,
2686 SideEffects side_effects,
2687 uint32_t dex_pc,
2688 ArenaAllocator* allocator,
2689 size_t number_of_inputs,
2690 ArenaAllocKind kind)
2691 : HInstruction(inst_kind, type, side_effects, dex_pc),
2692 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002693
Artem Serovcced8ba2017-07-19 18:18:09 +01002694 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002695
Artem Serovcced8ba2017-07-19 18:18:09 +01002696 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002697};
2698
Vladimir Markof9f64412015-09-02 14:05:49 +01002699template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002700class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002701 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002702 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302703 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002704 HExpression<N>(InstructionKind kind,
2705 DataType::Type type,
2706 SideEffects side_effects,
2707 uint32_t dex_pc)
2708 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2709 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002710
Vladimir Marko372f10e2016-05-17 16:30:10 +01002711 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002712 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002713 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002714 }
2715
Artem Serovcced8ba2017-07-19 18:18:09 +01002716 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002717 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002718
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002719 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002720 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002721
2722 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002723};
2724
Vladimir Markobd785672018-05-03 17:09:09 +01002725// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002726template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002727class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002728 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002729 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730
Vladimir Markobd785672018-05-03 17:09:09 +01002731 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002732
Vladimir Marko372f10e2016-05-17 16:30:10 +01002733 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002734 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002735 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002736 }
2737
Artem Serovcced8ba2017-07-19 18:18:09 +01002738 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002739 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002740
Vladimir Markof9f64412015-09-02 14:05:49 +01002741 private:
2742 friend class SsaBuilder;
2743};
2744
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002745// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2746// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002747class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002748 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002749 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002750 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302751 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002752
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002753 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002754
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002755 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002756
Artem Serovcced8ba2017-07-19 18:18:09 +01002757 protected:
2758 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002759};
2760
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002761// Represents dex's RETURN opcodes. A HReturn is a control flow
2762// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002763class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002764 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002765 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002766 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002767 SetRawInputAt(0, value);
2768 }
2769
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002770 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002771
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002772 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002773
Artem Serovcced8ba2017-07-19 18:18:09 +01002774 protected:
2775 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002776};
2777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002778class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002779 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002780 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002781 uint32_t reg_number,
2782 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002784 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002785 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302786 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002787 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002788 SideEffects::None(),
2789 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002790 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002791 number_of_inputs,
2792 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002793 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002794 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002795 // Phis are constructed live and marked dead if conflicting or unused.
2796 // Individual steps of SsaBuilder should assume that if a phi has been
2797 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2798 SetPackedFlag<kFlagIsLive>(true);
2799 SetPackedFlag<kFlagCanBeNull>(true);
2800 }
2801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002802 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002803
David Brazdildee58d62016-04-07 09:54:26 +00002804 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 static DataType::Type ToPhiType(DataType::Type type) {
2806 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002807 }
2808
2809 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002812 // Make sure that only valid type changes occur. The following are allowed:
2813 // (1) int -> float/ref (primitive type propagation),
2814 // (2) long -> double (primitive type propagation).
2815 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2817 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2818 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002819 SetPackedField<TypeField>(new_type);
2820 }
2821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002822 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002823 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2824
2825 uint32_t GetRegNumber() const { return reg_number_; }
2826
2827 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2828 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2829 bool IsDead() const { return !IsLive(); }
2830 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2831
Vladimir Markoe9004912016-06-16 16:50:52 +01002832 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002833 return other != nullptr
2834 && other->IsPhi()
2835 && other->AsPhi()->GetBlock() == GetBlock()
2836 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2837 }
2838
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002839 bool HasEquivalentPhi() const {
2840 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2841 return true;
2842 }
2843 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2844 return true;
2845 }
2846 return false;
2847 }
2848
David Brazdildee58d62016-04-07 09:54:26 +00002849 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2850 // An equivalent phi is a phi having the same dex register and type.
2851 // It assumes that phis with the same dex register are adjacent.
2852 HPhi* GetNextEquivalentPhiWithSameType() {
2853 HInstruction* next = GetNext();
2854 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2855 if (next->GetType() == GetType()) {
2856 return next->AsPhi();
2857 }
2858 next = next->GetNext();
2859 }
2860 return nullptr;
2861 }
2862
2863 DECLARE_INSTRUCTION(Phi);
2864
Artem Serovcced8ba2017-07-19 18:18:09 +01002865 protected:
2866 DEFAULT_COPY_CONSTRUCTOR(Phi);
2867
David Brazdildee58d62016-04-07 09:54:26 +00002868 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002869 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002870 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2871 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2872 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002873
David Brazdildee58d62016-04-07 09:54:26 +00002874 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002875};
2876
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002877// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002878// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002879// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002880class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002881 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302882 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002883 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302884 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002886 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002887
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002888 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002889
Artem Serovcced8ba2017-07-19 18:18:09 +01002890 protected:
2891 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002892};
2893
2894// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002895class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002896 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302897 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002898 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302899 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002900
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002901 bool IsClonable() const override { return true; }
2902 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002903
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002904 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002905 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002906 }
2907
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002908 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002909
Artem Serovcced8ba2017-07-19 18:18:09 +01002910 protected:
2911 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002912};
2913
Roland Levillain9867bc72015-08-05 10:21:34 +01002914class HConstant : public HExpression<0> {
2915 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302916 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2917 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2918 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002919
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002920 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002921
Roland Levillain1a653882016-03-18 18:05:57 +00002922 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002923 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002924 // Is this constant 0 in the arithmetic sense?
2925 virtual bool IsArithmeticZero() const { return false; }
2926 // Is this constant a 0-bit pattern?
2927 virtual bool IsZeroBitPattern() const { return false; }
2928 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 virtual bool IsOne() const { return false; }
2930
David Brazdil77a48ae2015-09-15 12:34:04 +00002931 virtual uint64_t GetValueAsUint64() const = 0;
2932
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002933 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002934
Artem Serovcced8ba2017-07-19 18:18:09 +01002935 protected:
2936 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002937};
2938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002939class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002940 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002941 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002942 return true;
2943 }
2944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002945 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002947 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002948
Roland Levillain1a653882016-03-18 18:05:57 +00002949 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002950 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002951
Roland Levillain9867bc72015-08-05 10:21:34 +01002952 DECLARE_INSTRUCTION(NullConstant);
2953
Artem Serovcced8ba2017-07-19 18:18:09 +01002954 protected:
2955 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2956
Roland Levillain9867bc72015-08-05 10:21:34 +01002957 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002958 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302959 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2960 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002961
2962 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002963};
2964
2965// Constants of the type int. Those can be from Dex instructions, or
2966// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002967class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002968 public:
2969 int32_t GetValue() const { return value_; }
2970
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002971 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002972 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2973 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002975 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002976 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002977 return other->AsIntConstant()->value_ == value_;
2978 }
2979
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002980 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002982 bool IsMinusOne() const override { return GetValue() == -1; }
2983 bool IsArithmeticZero() const override { return GetValue() == 0; }
2984 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2985 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002986
Roland Levillain1a653882016-03-18 18:05:57 +00002987 // Integer constants are used to encode Boolean values as well,
2988 // where 1 means true and 0 means false.
2989 bool IsTrue() const { return GetValue() == 1; }
2990 bool IsFalse() const { return GetValue() == 0; }
2991
Roland Levillain9867bc72015-08-05 10:21:34 +01002992 DECLARE_INSTRUCTION(IntConstant);
2993
Artem Serovcced8ba2017-07-19 18:18:09 +01002994 protected:
2995 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2996
Roland Levillain9867bc72015-08-05 10:21:34 +01002997 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002998 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302999 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3000 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003001 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303002 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3003 value_(value ? 1 : 0) {
3004 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003005
3006 const int32_t value_;
3007
3008 friend class HGraph;
3009 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3010 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003011};
3012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003013class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003014 public:
3015 int64_t GetValue() const { return value_; }
3016
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003017 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003019 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003020 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003021 return other->AsLongConstant()->value_ == value_;
3022 }
3023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003024 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003025
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003026 bool IsMinusOne() const override { return GetValue() == -1; }
3027 bool IsArithmeticZero() const override { return GetValue() == 0; }
3028 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3029 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003030
3031 DECLARE_INSTRUCTION(LongConstant);
3032
Artem Serovcced8ba2017-07-19 18:18:09 +01003033 protected:
3034 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3035
Roland Levillain9867bc72015-08-05 10:21:34 +01003036 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003037 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303038 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3039 value_(value) {
3040 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003041
3042 const int64_t value_;
3043
3044 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003045};
Dave Allison20dfc792014-06-16 20:44:29 -07003046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003047class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003048 public:
3049 float GetValue() const { return value_; }
3050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003051 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003052 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3053 }
3054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003055 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003056 DCHECK(other->IsFloatConstant()) << other->DebugName();
3057 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3058 }
3059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003060 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003062 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3064 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003065 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003066 return std::fpclassify(value_) == FP_ZERO;
3067 }
3068 bool IsArithmeticPositiveZero() const {
3069 return IsArithmeticZero() && !std::signbit(value_);
3070 }
3071 bool IsArithmeticNegativeZero() const {
3072 return IsArithmeticZero() && std::signbit(value_);
3073 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003074 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003075 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003076 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003077 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3079 }
3080 bool IsNaN() const {
3081 return std::isnan(value_);
3082 }
3083
3084 DECLARE_INSTRUCTION(FloatConstant);
3085
Artem Serovcced8ba2017-07-19 18:18:09 +01003086 protected:
3087 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3088
Roland Levillain31dd3d62016-02-16 12:21:02 +00003089 private:
3090 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303091 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3092 value_(value) {
3093 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303095 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3096 value_(bit_cast<float, int32_t>(value)) {
3097 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003098
3099 const float value_;
3100
3101 // Only the SsaBuilder and HGraph can create floating-point constants.
3102 friend class SsaBuilder;
3103 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104};
3105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003106class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003107 public:
3108 double GetValue() const { return value_; }
3109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003110 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003112 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3114 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3115 }
3116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003117 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003119 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3121 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003122 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003123 return std::fpclassify(value_) == FP_ZERO;
3124 }
3125 bool IsArithmeticPositiveZero() const {
3126 return IsArithmeticZero() && !std::signbit(value_);
3127 }
3128 bool IsArithmeticNegativeZero() const {
3129 return IsArithmeticZero() && std::signbit(value_);
3130 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003131 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003132 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003133 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003134 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003135 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3136 }
3137 bool IsNaN() const {
3138 return std::isnan(value_);
3139 }
3140
3141 DECLARE_INSTRUCTION(DoubleConstant);
3142
Artem Serovcced8ba2017-07-19 18:18:09 +01003143 protected:
3144 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3145
Roland Levillain31dd3d62016-02-16 12:21:02 +00003146 private:
3147 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303148 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3149 value_(value) {
3150 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003151 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303152 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3153 value_(bit_cast<double, int64_t>(value)) {
3154 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003155
3156 const double value_;
3157
3158 // Only the SsaBuilder and HGraph can create floating-point constants.
3159 friend class SsaBuilder;
3160 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003161};
3162
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003163// Conditional branch. A block ending with an HIf instruction must have
3164// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003165class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003166 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003167 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003168 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003169 SetRawInputAt(0, input);
3170 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003172 bool IsClonable() const override { return true; }
3173 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003174
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003175 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003176 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003177 }
3178
3179 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003180 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003181 }
3182
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003183 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003184
Artem Serovcced8ba2017-07-19 18:18:09 +01003185 protected:
3186 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003187};
3188
David Brazdilfc6a86a2015-06-26 10:33:45 +00003189
3190// Abstract instruction which marks the beginning and/or end of a try block and
3191// links it to the respective exception handlers. Behaves the same as a Goto in
3192// non-exceptional control flow.
3193// Normal-flow successor is stored at index zero, exception handlers under
3194// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003195class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003196 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003197 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003198 kEntry,
3199 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003200 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003201 };
3202
Artem Serovd1aa7d02018-06-22 11:35:46 +01003203 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3204 // across the catch block entering edges as GC might happen during throwing an exception.
3205 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3206 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003207 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003208 : HExpression(kTryBoundary,
3209 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3210 : SideEffects::None(),
3211 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003212 SetPackedField<BoundaryKindField>(kind);
3213 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003214
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003215 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003216
3217 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003218 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003219
David Brazdild26a4112015-11-10 11:07:31 +00003220 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3221 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3222 }
3223
David Brazdilfc6a86a2015-06-26 10:33:45 +00003224 // Returns whether `handler` is among its exception handlers (non-zero index
3225 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003226 bool HasExceptionHandler(const HBasicBlock& handler) const {
3227 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003228 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003229 }
3230
3231 // If not present already, adds `handler` to its block's list of exception
3232 // handlers.
3233 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003234 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003235 GetBlock()->AddSuccessor(handler);
3236 }
3237 }
3238
Vladimir Markoa1de9182016-02-25 11:37:38 +00003239 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3240 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003241
David Brazdilffee3d32015-07-06 11:48:53 +01003242 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3243
David Brazdilfc6a86a2015-06-26 10:33:45 +00003244 DECLARE_INSTRUCTION(TryBoundary);
3245
Artem Serovcced8ba2017-07-19 18:18:09 +01003246 protected:
3247 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3248
David Brazdilfc6a86a2015-06-26 10:33:45 +00003249 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003250 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3251 static constexpr size_t kFieldBoundaryKindSize =
3252 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3253 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3254 kFieldBoundaryKind + kFieldBoundaryKindSize;
3255 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3256 "Too many packed fields.");
3257 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003258};
3259
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003260// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003261class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003262 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003263 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3264 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003265 HDeoptimize(ArenaAllocator* allocator,
3266 HInstruction* cond,
3267 DeoptimizationKind kind,
3268 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003269 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303270 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003271 SideEffects::All(),
3272 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003273 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003274 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003275 kArenaAllocMisc) {
3276 SetPackedFlag<kFieldCanBeMoved>(false);
3277 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003278 SetRawInputAt(0, cond);
3279 }
3280
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003281 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003282
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003283 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3284 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3285 // instead of `guard`.
3286 // We set CanTriggerGC to prevent any intermediate address to be live
3287 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003288 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003289 HInstruction* cond,
3290 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003291 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003292 uint32_t dex_pc)
3293 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303294 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003295 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003296 SideEffects::CanTriggerGC(),
3297 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003298 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003299 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003300 kArenaAllocMisc) {
3301 SetPackedFlag<kFieldCanBeMoved>(true);
3302 SetPackedField<DeoptimizeKindField>(kind);
3303 SetRawInputAt(0, cond);
3304 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003305 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003307 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003309 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003310 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3311 }
3312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003313 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003314
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003315 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003316
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003317 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003318
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003319 bool GuardsAnInput() const {
3320 return InputCount() == 2;
3321 }
3322
3323 HInstruction* GuardedInput() const {
3324 DCHECK(GuardsAnInput());
3325 return InputAt(1);
3326 }
3327
3328 void RemoveGuard() {
3329 RemoveInputAt(1);
3330 }
3331
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003332 DECLARE_INSTRUCTION(Deoptimize);
3333
Artem Serovcced8ba2017-07-19 18:18:09 +01003334 protected:
3335 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3336
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003337 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003338 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3339 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3340 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003341 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003342 static constexpr size_t kNumberOfDeoptimizePackedBits =
3343 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3344 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3345 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003346 using DeoptimizeKindField =
3347 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003348};
3349
Mingyao Yang063fc772016-08-02 11:02:54 -07003350// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3351// The compiled code checks this flag value in a guard before devirtualized call and
3352// if it's true, starts to do deoptimization.
3353// It has a 4-byte slot on stack.
3354// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003355class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003356 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003357 // CHA guards are only optimized in a separate pass and it has no side effects
3358 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003359 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303360 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003361 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303362 SideEffects::None(),
3363 dex_pc,
3364 allocator,
3365 0,
3366 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003367 }
3368
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003369 // We do all CHA guard elimination/motion in a single pass, after which there is no
3370 // further guard elimination/motion since a guard might have been used for justification
3371 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3372 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003373 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003374
3375 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3376
Artem Serovcced8ba2017-07-19 18:18:09 +01003377 protected:
3378 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003379};
3380
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003381// Represents the ArtMethod that was passed as a first argument to
3382// the method. It is used by instructions that depend on it, like
3383// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003384class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003385 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003386 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303387 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3388 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003389
3390 DECLARE_INSTRUCTION(CurrentMethod);
3391
Artem Serovcced8ba2017-07-19 18:18:09 +01003392 protected:
3393 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003394};
3395
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003396// Fetches an ArtMethod from the virtual table or the interface method table
3397// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003398class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003399 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003400 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003401 kVTable,
3402 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003403 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003404 };
3405 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003406 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003407 TableKind kind,
3408 size_t index,
3409 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303410 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003411 index_(index) {
3412 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003413 SetRawInputAt(0, cls);
3414 }
3415
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003416 bool IsClonable() const override { return true; }
3417 bool CanBeMoved() const override { return true; }
3418 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003419 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003420 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003421 }
3422
Vladimir Markoa1de9182016-02-25 11:37:38 +00003423 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003424 size_t GetIndex() const { return index_; }
3425
3426 DECLARE_INSTRUCTION(ClassTableGet);
3427
Artem Serovcced8ba2017-07-19 18:18:09 +01003428 protected:
3429 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3430
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003431 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003432 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003433 static constexpr size_t kFieldTableKindSize =
3434 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3435 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3436 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3437 "Too many packed fields.");
3438 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3439
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003440 // The index of the ArtMethod in the table.
3441 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003442};
3443
Mark Mendellfe57faa2015-09-18 09:26:15 -04003444// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3445// have one successor for each entry in the switch table, and the final successor
3446// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003447class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003448 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003449 HPackedSwitch(int32_t start_value,
3450 uint32_t num_entries,
3451 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003452 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003453 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003454 start_value_(start_value),
3455 num_entries_(num_entries) {
3456 SetRawInputAt(0, input);
3457 }
3458
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003459 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003461 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003462
3463 int32_t GetStartValue() const { return start_value_; }
3464
Vladimir Marko211c2112015-09-24 16:52:33 +01003465 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003466
3467 HBasicBlock* GetDefaultBlock() const {
3468 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003469 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003470 }
3471 DECLARE_INSTRUCTION(PackedSwitch);
3472
Artem Serovcced8ba2017-07-19 18:18:09 +01003473 protected:
3474 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3475
Mark Mendellfe57faa2015-09-18 09:26:15 -04003476 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003477 const int32_t start_value_;
3478 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003479};
3480
Roland Levillain88cb1752014-10-20 16:36:47 +01003481class HUnaryOperation : public HExpression<1> {
3482 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303483 HUnaryOperation(InstructionKind kind,
3484 DataType::Type result_type,
3485 HInstruction* input,
3486 uint32_t dex_pc = kNoDexPc)
3487 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003488 SetRawInputAt(0, input);
3489 }
3490
Artem Serovcced8ba2017-07-19 18:18:09 +01003491 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003492 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003493
Roland Levillain88cb1752014-10-20 16:36:47 +01003494 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003495 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003496
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003497 bool CanBeMoved() const override { return true; }
3498 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003499 return true;
3500 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003501
Roland Levillain31dd3d62016-02-16 12:21:02 +00003502 // Try to statically evaluate `this` and return a HConstant
3503 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003504 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003505 HConstant* TryStaticEvaluation() const;
3506
3507 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003508 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3509 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3511 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003512
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003513 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003514
Artem Serovcced8ba2017-07-19 18:18:09 +01003515 protected:
3516 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003517};
3518
Dave Allison20dfc792014-06-16 20:44:29 -07003519class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003520 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303521 HBinaryOperation(InstructionKind kind,
3522 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003523 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003524 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003525 SideEffects side_effects = SideEffects::None(),
3526 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303527 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003528 SetRawInputAt(0, left);
3529 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003530 }
3531
Artem Serovcced8ba2017-07-19 18:18:09 +01003532 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003533 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003534
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003535 HInstruction* GetLeft() const { return InputAt(0); }
3536 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003537 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003538
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003539 virtual bool IsCommutative() const { return false; }
3540
3541 // Put constant on the right.
3542 // Returns whether order is changed.
3543 bool OrderInputsWithConstantOnTheRight() {
3544 HInstruction* left = InputAt(0);
3545 HInstruction* right = InputAt(1);
3546 if (left->IsConstant() && !right->IsConstant()) {
3547 ReplaceInput(right, 0);
3548 ReplaceInput(left, 1);
3549 return true;
3550 }
3551 return false;
3552 }
3553
3554 // Order inputs by instruction id, but favor constant on the right side.
3555 // This helps GVN for commutative ops.
3556 void OrderInputs() {
3557 DCHECK(IsCommutative());
3558 HInstruction* left = InputAt(0);
3559 HInstruction* right = InputAt(1);
3560 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3561 return;
3562 }
3563 if (OrderInputsWithConstantOnTheRight()) {
3564 return;
3565 }
3566 // Order according to instruction id.
3567 if (left->GetId() > right->GetId()) {
3568 ReplaceInput(right, 0);
3569 ReplaceInput(left, 1);
3570 }
3571 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003572
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003573 bool CanBeMoved() const override { return true; }
3574 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003575 return true;
3576 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003577
Roland Levillain31dd3d62016-02-16 12:21:02 +00003578 // Try to statically evaluate `this` and return a HConstant
3579 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003580 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003581 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003582
3583 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003584 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3585 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003586 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3587 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003589 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3590 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003591 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3592 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003593 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3594 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003595 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003596 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3597 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003598
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003599 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003600 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003601 HConstant* GetConstantRight() const;
3602
3603 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003604 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003605 HInstruction* GetLeastConstantLeft() const;
3606
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003607 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003608
Artem Serovcced8ba2017-07-19 18:18:09 +01003609 protected:
3610 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003611};
3612
Mark Mendellc4701932015-04-10 13:18:51 -04003613// The comparison bias applies for floating point operations and indicates how NaN
3614// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003615enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003616 kNoBias, // bias is not applicable (i.e. for long operation)
3617 kGtBias, // return 1 for NaN comparisons
3618 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003619 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003620};
3621
Roland Levillain31dd3d62016-02-16 12:21:02 +00003622std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3623
Dave Allison20dfc792014-06-16 20:44:29 -07003624class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003625 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303626 HCondition(InstructionKind kind,
3627 HInstruction* first,
3628 HInstruction* second,
3629 uint32_t dex_pc = kNoDexPc)
3630 : HBinaryOperation(kind,
3631 DataType::Type::kBool,
3632 first,
3633 second,
3634 SideEffects::None(),
3635 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003636 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3637 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003638
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003639 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003640 // `instruction`, and disregard moves in between.
3641 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003642
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003643 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003644
3645 virtual IfCondition GetCondition() const = 0;
3646
Mark Mendellc4701932015-04-10 13:18:51 -04003647 virtual IfCondition GetOppositeCondition() const = 0;
3648
Vladimir Markoa1de9182016-02-25 11:37:38 +00003649 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003650 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3651
Vladimir Markoa1de9182016-02-25 11:37:38 +00003652 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3653 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003655 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003656 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003657 }
3658
Roland Levillain4fa13f62015-07-06 18:11:54 +01003659 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003660 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003661 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003662 if (if_cond == kCondNE) {
3663 return true;
3664 } else if (if_cond == kCondEQ) {
3665 return false;
3666 }
3667 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003668 }
3669
3670 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003671 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003672 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003673 if (if_cond == kCondEQ) {
3674 return true;
3675 } else if (if_cond == kCondNE) {
3676 return false;
3677 }
3678 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003679 }
3680
Roland Levillain31dd3d62016-02-16 12:21:02 +00003681 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003682 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003683 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003684 static constexpr size_t kFieldComparisonBiasSize =
3685 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3686 static constexpr size_t kNumberOfConditionPackedBits =
3687 kFieldComparisonBias + kFieldComparisonBiasSize;
3688 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3689 using ComparisonBiasField =
3690 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3691
Roland Levillain31dd3d62016-02-16 12:21:02 +00003692 template <typename T>
3693 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3694
3695 template <typename T>
3696 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003697 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003698 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3699 // Handle the bias.
3700 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3701 }
3702
3703 // Return an integer constant containing the result of a condition evaluated at compile time.
3704 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3705 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3706 }
3707
Artem Serovcced8ba2017-07-19 18:18:09 +01003708 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003709};
3710
3711// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003712class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003713 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003714 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303715 : HCondition(kEqual, first, second, dex_pc) {
3716 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003717
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003718 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003719
Vladimir Marko9e23df52015-11-10 17:14:35 +00003720 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003721 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003722 return MakeConstantCondition(true, GetDexPc());
3723 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003724 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003725 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3726 }
3727 // In the following Evaluate methods, a HCompare instruction has
3728 // been merged into this HEqual instruction; evaluate it as
3729 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003730 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003731 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3732 GetDexPc());
3733 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003734 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003735 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3736 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003737 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003738 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003739 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003740
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003741 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003742
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003743 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003744 return kCondEQ;
3745 }
3746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003747 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003748 return kCondNE;
3749 }
3750
Artem Serovcced8ba2017-07-19 18:18:09 +01003751 protected:
3752 DEFAULT_COPY_CONSTRUCTOR(Equal);
3753
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003754 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003755 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003756};
3757
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003758class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003759 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303760 HNotEqual(HInstruction* first, HInstruction* second,
3761 uint32_t dex_pc = kNoDexPc)
3762 : HCondition(kNotEqual, first, second, dex_pc) {
3763 }
Dave Allison20dfc792014-06-16 20:44:29 -07003764
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003765 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003766
Vladimir Marko9e23df52015-11-10 17:14:35 +00003767 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003768 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003769 return MakeConstantCondition(false, GetDexPc());
3770 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003771 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003772 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3773 }
3774 // In the following Evaluate methods, a HCompare instruction has
3775 // been merged into this HNotEqual instruction; evaluate it as
3776 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003777 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003778 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3779 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003780 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003781 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3782 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003783 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003784 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003785 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003786
Dave Allison20dfc792014-06-16 20:44:29 -07003787 DECLARE_INSTRUCTION(NotEqual);
3788
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003789 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003790 return kCondNE;
3791 }
3792
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003793 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003794 return kCondEQ;
3795 }
3796
Artem Serovcced8ba2017-07-19 18:18:09 +01003797 protected:
3798 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3799
Dave Allison20dfc792014-06-16 20:44:29 -07003800 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003801 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003802};
3803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003804class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003805 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303806 HLessThan(HInstruction* first, HInstruction* second,
3807 uint32_t dex_pc = kNoDexPc)
3808 : HCondition(kLessThan, first, second, dex_pc) {
3809 }
Dave Allison20dfc792014-06-16 20:44:29 -07003810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003812 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003813 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003814 // In the following Evaluate methods, a HCompare instruction has
3815 // been merged into this HLessThan instruction; evaluate it as
3816 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003817 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003818 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3819 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003820 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003821 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3822 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003823 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003824 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003825 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003826
Dave Allison20dfc792014-06-16 20:44:29 -07003827 DECLARE_INSTRUCTION(LessThan);
3828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003829 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003830 return kCondLT;
3831 }
3832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003833 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003834 return kCondGE;
3835 }
3836
Artem Serovcced8ba2017-07-19 18:18:09 +01003837 protected:
3838 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3839
Dave Allison20dfc792014-06-16 20:44:29 -07003840 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003841 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003842};
3843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003844class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003845 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303846 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3847 uint32_t dex_pc = kNoDexPc)
3848 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3849 }
Dave Allison20dfc792014-06-16 20:44:29 -07003850
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003851 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003852 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003853 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003854 // In the following Evaluate methods, a HCompare instruction has
3855 // been merged into this HLessThanOrEqual instruction; evaluate it as
3856 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003857 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003858 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3859 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3862 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003863 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003864 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003865 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003866
Dave Allison20dfc792014-06-16 20:44:29 -07003867 DECLARE_INSTRUCTION(LessThanOrEqual);
3868
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003869 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003870 return kCondLE;
3871 }
3872
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003873 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003874 return kCondGT;
3875 }
3876
Artem Serovcced8ba2017-07-19 18:18:09 +01003877 protected:
3878 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3879
Dave Allison20dfc792014-06-16 20:44:29 -07003880 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003881 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003882};
3883
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003884class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003885 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003886 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303887 : HCondition(kGreaterThan, first, second, dex_pc) {
3888 }
Dave Allison20dfc792014-06-16 20:44:29 -07003889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003890 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003891 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003892 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003893 // In the following Evaluate methods, a HCompare instruction has
3894 // been merged into this HGreaterThan instruction; evaluate it as
3895 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003896 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003897 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3898 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003899 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003900 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3901 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003902 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003903 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003904 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003905
Dave Allison20dfc792014-06-16 20:44:29 -07003906 DECLARE_INSTRUCTION(GreaterThan);
3907
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003908 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003909 return kCondGT;
3910 }
3911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003912 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003913 return kCondLE;
3914 }
3915
Artem Serovcced8ba2017-07-19 18:18:09 +01003916 protected:
3917 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3918
Dave Allison20dfc792014-06-16 20:44:29 -07003919 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003920 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003921};
3922
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003923class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003924 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003925 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303926 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3927 }
Dave Allison20dfc792014-06-16 20:44:29 -07003928
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003929 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003930 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003931 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003932 // In the following Evaluate methods, a HCompare instruction has
3933 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3934 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003935 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003936 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3937 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003938 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003939 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3940 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003941 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003942 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003943 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003944
Dave Allison20dfc792014-06-16 20:44:29 -07003945 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003947 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003948 return kCondGE;
3949 }
3950
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003951 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003952 return kCondLT;
3953 }
3954
Artem Serovcced8ba2017-07-19 18:18:09 +01003955 protected:
3956 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3957
Dave Allison20dfc792014-06-16 20:44:29 -07003958 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003959 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003960};
3961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003962class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003963 public:
3964 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303965 : HCondition(kBelow, first, second, dex_pc) {
3966 }
Aart Bike9f37602015-10-09 11:15:55 -07003967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003968 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003969 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003970 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003971 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003972 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3973 }
3974 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003975 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003976 LOG(FATAL) << DebugName() << " is not defined for float values";
3977 UNREACHABLE();
3978 }
3979 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003980 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003981 LOG(FATAL) << DebugName() << " is not defined for double values";
3982 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003983 }
3984
3985 DECLARE_INSTRUCTION(Below);
3986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003987 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003988 return kCondB;
3989 }
3990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003991 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003992 return kCondAE;
3993 }
3994
Artem Serovcced8ba2017-07-19 18:18:09 +01003995 protected:
3996 DEFAULT_COPY_CONSTRUCTOR(Below);
3997
Aart Bike9f37602015-10-09 11:15:55 -07003998 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003999 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004000 return MakeUnsigned(x) < MakeUnsigned(y);
4001 }
Aart Bike9f37602015-10-09 11:15:55 -07004002};
4003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004004class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004005 public:
4006 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304007 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4008 }
Aart Bike9f37602015-10-09 11:15:55 -07004009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004010 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004011 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004012 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004013 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004014 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4015 }
4016 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004017 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004018 LOG(FATAL) << DebugName() << " is not defined for float values";
4019 UNREACHABLE();
4020 }
4021 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004022 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004023 LOG(FATAL) << DebugName() << " is not defined for double values";
4024 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004025 }
4026
4027 DECLARE_INSTRUCTION(BelowOrEqual);
4028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004029 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004030 return kCondBE;
4031 }
4032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004033 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004034 return kCondA;
4035 }
4036
Artem Serovcced8ba2017-07-19 18:18:09 +01004037 protected:
4038 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4039
Aart Bike9f37602015-10-09 11:15:55 -07004040 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004041 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004042 return MakeUnsigned(x) <= MakeUnsigned(y);
4043 }
Aart Bike9f37602015-10-09 11:15:55 -07004044};
4045
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004046class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004047 public:
4048 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304049 : HCondition(kAbove, first, second, dex_pc) {
4050 }
Aart Bike9f37602015-10-09 11:15:55 -07004051
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004052 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004053 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004054 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004055 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004056 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4057 }
4058 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004059 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004060 LOG(FATAL) << DebugName() << " is not defined for float values";
4061 UNREACHABLE();
4062 }
4063 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004064 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004065 LOG(FATAL) << DebugName() << " is not defined for double values";
4066 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004067 }
4068
4069 DECLARE_INSTRUCTION(Above);
4070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004071 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004072 return kCondA;
4073 }
4074
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004075 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004076 return kCondBE;
4077 }
4078
Artem Serovcced8ba2017-07-19 18:18:09 +01004079 protected:
4080 DEFAULT_COPY_CONSTRUCTOR(Above);
4081
Aart Bike9f37602015-10-09 11:15:55 -07004082 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004083 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004084 return MakeUnsigned(x) > MakeUnsigned(y);
4085 }
Aart Bike9f37602015-10-09 11:15:55 -07004086};
4087
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004088class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004089 public:
4090 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304091 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4092 }
Aart Bike9f37602015-10-09 11:15:55 -07004093
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004094 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004095 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004096 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004097 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004098 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4099 }
4100 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004101 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004102 LOG(FATAL) << DebugName() << " is not defined for float values";
4103 UNREACHABLE();
4104 }
4105 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004106 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004107 LOG(FATAL) << DebugName() << " is not defined for double values";
4108 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004109 }
4110
4111 DECLARE_INSTRUCTION(AboveOrEqual);
4112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004113 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004114 return kCondAE;
4115 }
4116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004117 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004118 return kCondB;
4119 }
4120
Artem Serovcced8ba2017-07-19 18:18:09 +01004121 protected:
4122 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4123
Aart Bike9f37602015-10-09 11:15:55 -07004124 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004125 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004126 return MakeUnsigned(x) >= MakeUnsigned(y);
4127 }
Aart Bike9f37602015-10-09 11:15:55 -07004128};
Dave Allison20dfc792014-06-16 20:44:29 -07004129
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004130// Instruction to check how two inputs compare to each other.
4131// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004132class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004133 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004134 // Note that `comparison_type` is the type of comparison performed
4135 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004136 // HCompare instruction (which is always DataType::Type::kInt).
4137 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004138 HInstruction* first,
4139 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004140 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004141 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304142 : HBinaryOperation(kCompare,
4143 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004144 first,
4145 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004146 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004147 dex_pc) {
4148 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4150 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004151 }
4152
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004154 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4155
4156 template <typename T>
4157 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004158 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004159 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4160 // Handle the bias.
4161 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4162 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004163
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004164 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004165 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4166 // reach this code path when processing a freshly built HIR
4167 // graph. However HCompare integer instructions can be synthesized
4168 // by the instruction simplifier to implement IntegerCompare and
4169 // IntegerSignum intrinsics, so we have to handle this case.
4170 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004171 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004172 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004173 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4174 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004175 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004176 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4177 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004178 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004179 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004180 }
4181
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004182 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004183 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004184 }
4185
Vladimir Markoa1de9182016-02-25 11:37:38 +00004186 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004187
Roland Levillain31dd3d62016-02-16 12:21:02 +00004188 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004189 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004190 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004191 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004192 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004193 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004194
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004195 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004196 // Comparisons do not require a runtime call in any back end.
4197 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004198 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004199
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004200 DECLARE_INSTRUCTION(Compare);
4201
Roland Levillain31dd3d62016-02-16 12:21:02 +00004202 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004203 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004204 static constexpr size_t kFieldComparisonBiasSize =
4205 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4206 static constexpr size_t kNumberOfComparePackedBits =
4207 kFieldComparisonBias + kFieldComparisonBiasSize;
4208 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4209 using ComparisonBiasField =
4210 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4211
Roland Levillain31dd3d62016-02-16 12:21:02 +00004212 // Return an integer constant containing the result of a comparison evaluated at compile time.
4213 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4214 DCHECK(value == -1 || value == 0 || value == 1) << value;
4215 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4216 }
4217
Artem Serovcced8ba2017-07-19 18:18:09 +01004218 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004219};
4220
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004221class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004222 public:
4223 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004224 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004225 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004226 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004227 bool finalizable,
4228 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304229 : HExpression(kNewInstance,
4230 DataType::Type::kReference,
4231 SideEffects::CanTriggerGC(),
4232 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004233 type_index_(type_index),
4234 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004235 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004236 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004237 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004238 }
4239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004240 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004241
Andreas Gampea5b09a62016-11-17 15:21:22 -08004242 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004243 const DexFile& GetDexFile() const { return dex_file_; }
4244
4245 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004246 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004247
Mingyao Yang062157f2016-03-02 10:15:36 -08004248 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004249 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004250
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004251 bool NeedsChecks() const {
4252 return entrypoint_ == kQuickAllocObjectWithChecks;
4253 }
David Brazdil6de19382016-01-08 17:37:10 +00004254
Vladimir Markoa1de9182016-02-25 11:37:38 +00004255 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004257 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004258
4259 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4260
4261 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4262 entrypoint_ = entrypoint;
4263 }
4264
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004265 HLoadClass* GetLoadClass() const {
4266 HInstruction* input = InputAt(0);
4267 if (input->IsClinitCheck()) {
4268 input = input->InputAt(0);
4269 }
4270 DCHECK(input->IsLoadClass());
4271 return input->AsLoadClass();
4272 }
4273
David Brazdil6de19382016-01-08 17:37:10 +00004274 bool IsStringAlloc() const;
4275
4276 DECLARE_INSTRUCTION(NewInstance);
4277
Artem Serovcced8ba2017-07-19 18:18:09 +01004278 protected:
4279 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4280
David Brazdil6de19382016-01-08 17:37:10 +00004281 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004282 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004283 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4284 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4285 "Too many packed fields.");
4286
Andreas Gampea5b09a62016-11-17 15:21:22 -08004287 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004288 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004289 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004290};
4291
Agi Csaki05f20562015-08-19 14:58:14 -07004292enum IntrinsicNeedsEnvironmentOrCache {
4293 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4294 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004295};
4296
Aart Bik5d75afe2015-12-14 11:57:01 -08004297enum IntrinsicSideEffects {
4298 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4299 kReadSideEffects, // Intrinsic may read heap memory.
4300 kWriteSideEffects, // Intrinsic may write heap memory.
4301 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4302};
4303
4304enum IntrinsicExceptions {
4305 kNoThrow, // Intrinsic does not throw any exceptions.
4306 kCanThrow // Intrinsic may throw exceptions.
4307};
4308
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004309class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004310 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004311 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004312
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004313 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004314 SetRawInputAt(index, argument);
4315 }
4316
Roland Levillain3e3d7332015-04-28 11:00:54 +01004317 // Return the number of arguments. This number can be lower than
4318 // the number of inputs returned by InputCount(), as some invoke
4319 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4320 // inputs at the end of their list of inputs.
4321 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4322
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004323 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4324
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004325 InvokeType GetInvokeType() const {
4326 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004327 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004328
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004329 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004330 return intrinsic_;
4331 }
4332
Aart Bik5d75afe2015-12-14 11:57:01 -08004333 void SetIntrinsic(Intrinsics intrinsic,
4334 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4335 IntrinsicSideEffects side_effects,
4336 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004337
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004338 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004339 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004340 }
4341
Aart Bikff7d89c2016-11-07 08:49:28 -08004342 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4343
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004344 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004345
Aart Bika8b8e9b2018-01-09 11:01:02 -08004346 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4347
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004348 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004349
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004350 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004351
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004352 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004353 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4354 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004355
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004356 uint32_t* GetIntrinsicOptimizations() {
4357 return &intrinsic_optimizations_;
4358 }
4359
4360 const uint32_t* GetIntrinsicOptimizations() const {
4361 return &intrinsic_optimizations_;
4362 }
4363
4364 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4365
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004366 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004367 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004368
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004369 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004370
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004371 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004372 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4373 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004374 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004375 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004376 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4377 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004378 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004379 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004380
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304381 HInvoke(InstructionKind kind,
4382 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004383 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004384 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004385 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004386 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004387 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004388 ArtMethod* resolved_method,
4389 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004390 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304391 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004392 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004393 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4394 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004395 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004396 number_of_arguments + number_of_other_inputs,
4397 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004398 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004399 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004400 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004401 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004402 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004403 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004404 // Check mutator lock, constructors lack annotalysis support.
4405 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4406 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004407 }
4408
Artem Serovcced8ba2017-07-19 18:18:09 +01004409 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4410
Roland Levillain3e3d7332015-04-28 11:00:54 +01004411 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004412 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004413 const uint32_t dex_method_index_;
4414 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004415
4416 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4417 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004418};
4419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004420class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004421 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004422 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004423 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004424 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004425 uint32_t dex_pc,
4426 uint32_t dex_method_index,
4427 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304428 : HInvoke(kInvokeUnresolved,
4429 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004430 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004431 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004432 return_type,
4433 dex_pc,
4434 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004435 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004436 invoke_type) {
4437 }
4438
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004439 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004440
Calin Juravle175dc732015-08-25 15:42:32 +01004441 DECLARE_INSTRUCTION(InvokeUnresolved);
4442
Artem Serovcced8ba2017-07-19 18:18:09 +01004443 protected:
4444 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004445};
4446
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004447class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004448 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004449 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004450 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004451 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004452 uint32_t dex_pc,
4453 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304454 : HInvoke(kInvokePolymorphic,
4455 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004456 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004457 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004458 return_type,
4459 dex_pc,
4460 dex_method_index,
4461 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304462 kVirtual) {
4463 }
Orion Hodsonac141392017-01-13 11:53:47 +00004464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004465 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004466
Orion Hodsonac141392017-01-13 11:53:47 +00004467 DECLARE_INSTRUCTION(InvokePolymorphic);
4468
Artem Serovcced8ba2017-07-19 18:18:09 +01004469 protected:
4470 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004471};
4472
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004473class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004474 public:
4475 HInvokeCustom(ArenaAllocator* allocator,
4476 uint32_t number_of_arguments,
4477 uint32_t call_site_index,
4478 DataType::Type return_type,
4479 uint32_t dex_pc)
4480 : HInvoke(kInvokeCustom,
4481 allocator,
4482 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004483 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004484 return_type,
4485 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004486 /* dex_method_index= */ dex::kDexNoIndex,
4487 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004488 kStatic),
4489 call_site_index_(call_site_index) {
4490 }
4491
4492 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4493
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004494 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004495
4496 DECLARE_INSTRUCTION(InvokeCustom);
4497
4498 protected:
4499 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4500
4501 private:
4502 uint32_t call_site_index_;
4503};
4504
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004505class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004506 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004507 // Requirements of this method call regarding the class
4508 // initialization (clinit) check of its declaring class.
4509 enum class ClinitCheckRequirement {
4510 kNone, // Class already initialized.
4511 kExplicit, // Static call having explicit clinit check as last input.
4512 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004513 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004514 };
4515
Vladimir Marko58155012015-08-19 12:49:41 +00004516 // Determines how to load the target ArtMethod*.
4517 enum class MethodLoadKind {
4518 // Use a String init ArtMethod* loaded from Thread entrypoints.
4519 kStringInit,
4520
4521 // Use the method's own ArtMethod* loaded by the register allocator.
4522 kRecursive,
4523
Vladimir Marko65979462017-05-19 17:25:12 +01004524 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4525 // Used for boot image methods referenced by boot image code.
4526 kBootImageLinkTimePcRelative,
4527
Vladimir Markob066d432018-01-03 13:14:37 +00004528 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4529 // Used for app->boot calls with relocatable image.
4530 kBootImageRelRo,
4531
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004532 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004533 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004534 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004535
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004536 // Use ArtMethod* at a known address, embed the direct address in the code.
4537 // Used for for JIT-compiled calls.
4538 kJitDirectAddress,
4539
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004540 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4541 // used when other kinds are unimplemented on a particular architecture.
4542 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004543 };
4544
4545 // Determines the location of the code pointer.
4546 enum class CodePtrLocation {
4547 // Recursive call, use local PC-relative call instruction.
4548 kCallSelf,
4549
Vladimir Marko58155012015-08-19 12:49:41 +00004550 // Use code pointer from the ArtMethod*.
4551 // Used when we don't know the target code. This is also the last-resort-kind used when
4552 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4553 kCallArtMethod,
4554 };
4555
4556 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004557 MethodLoadKind method_load_kind;
4558 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004559 // The method load data holds
4560 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4561 // Note that there are multiple string init methods, each having its own offset.
4562 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004563 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004564 };
4565
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004566 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004567 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004568 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004569 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004570 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004571 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004572 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004573 InvokeType invoke_type,
4574 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004575 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304576 : HInvoke(kInvokeStaticOrDirect,
4577 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004578 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004579 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004580 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004581 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004582 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004583 return_type,
4584 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004585 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004586 resolved_method,
4587 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004588 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004589 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004590 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4591 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004592
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004593 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004594
Vladimir Markodc151b22015-10-15 18:02:30 +01004595 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004596 bool had_current_method_input = HasCurrentMethodInput();
4597 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4598
4599 // Using the current method is the default and once we find a better
4600 // method load kind, we should not go back to using the current method.
4601 DCHECK(had_current_method_input || !needs_current_method_input);
4602
4603 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004604 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4605 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004606 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004607 dispatch_info_ = dispatch_info;
4608 }
4609
Aart Bik6daebeb2017-04-03 14:35:41 -07004610 DispatchInfo GetDispatchInfo() const {
4611 return dispatch_info_;
4612 }
4613
Vladimir Markoc53c0792015-11-19 15:48:33 +00004614 void AddSpecialInput(HInstruction* input) {
4615 // We allow only one special input.
4616 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4617 DCHECK(InputCount() == GetSpecialInputIndex() ||
4618 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4619 InsertInputAt(GetSpecialInputIndex(), input);
4620 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004621
Vladimir Marko372f10e2016-05-17 16:30:10 +01004622 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004623 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004624 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4625 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4626 DCHECK(!input_records.empty());
4627 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4628 HInstruction* last_input = input_records.back().GetInstruction();
4629 // Note: `last_input` may be null during arguments setup.
4630 if (last_input != nullptr) {
4631 // `last_input` is the last input of a static invoke marked as having
4632 // an explicit clinit check. It must either be:
4633 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4634 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4635 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4636 }
4637 }
4638 return input_records;
4639 }
4640
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004641 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004642 // We access the method via the dex cache so we can't do an implicit null check.
4643 // TODO: for intrinsics we can generate implicit null checks.
4644 return false;
4645 }
4646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004647 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004648 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004649 }
4650
Vladimir Markoc53c0792015-11-19 15:48:33 +00004651 // Get the index of the special input, if any.
4652 //
David Brazdil6de19382016-01-08 17:37:10 +00004653 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4654 // method pointer; otherwise there may be one platform-specific special input,
4655 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004656 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004657 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004658
Vladimir Marko58155012015-08-19 12:49:41 +00004659 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4660 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4661 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004662 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004663 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004664 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004665 bool HasPcRelativeMethodLoadKind() const {
4666 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004667 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004668 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004669 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004670 bool HasCurrentMethodInput() const {
4671 // This function can be called only after the invoke has been fully initialized by the builder.
4672 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004673 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004674 return true;
4675 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004676 DCHECK(InputCount() == GetSpecialInputIndex() ||
4677 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004678 return false;
4679 }
4680 }
Vladimir Marko58155012015-08-19 12:49:41 +00004681
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004682 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004683 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004684 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004685 }
4686
4687 uint64_t GetMethodAddress() const {
4688 DCHECK(HasMethodAddress());
4689 return dispatch_info_.method_load_data;
4690 }
4691
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004692 const DexFile& GetDexFileForPcRelativeDexCache() const;
4693
Vladimir Markoa1de9182016-02-25 11:37:38 +00004694 ClinitCheckRequirement GetClinitCheckRequirement() const {
4695 return GetPackedField<ClinitCheckRequirementField>();
4696 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004697
Roland Levillain4c0eb422015-04-24 16:43:49 +01004698 // Is this instruction a call to a static method?
4699 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004700 return GetInvokeType() == kStatic;
4701 }
4702
4703 MethodReference GetTargetMethod() const {
4704 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004705 }
4706
Vladimir Markofbb184a2015-11-13 14:47:00 +00004707 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4708 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4709 // instruction; only relevant for static calls with explicit clinit check.
4710 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004711 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004712 size_t last_input_index = inputs_.size() - 1u;
4713 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004714 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004715 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004716 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004717 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004718 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004719 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004720 }
4721
4722 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004723 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004724 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004725 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004726 }
4727
4728 // Is this a call to a static method whose declaring class has an
4729 // implicit intialization check requirement?
4730 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004731 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004732 }
4733
Vladimir Markob554b5a2015-11-06 12:57:55 +00004734 // Does this method load kind need the current method as an input?
4735 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004736 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004737 }
4738
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004739 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004740
Artem Serovcced8ba2017-07-19 18:18:09 +01004741 protected:
4742 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4743
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004744 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004745 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004746 static constexpr size_t kFieldClinitCheckRequirementSize =
4747 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4748 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4749 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4750 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4751 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004752 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4753 kFieldClinitCheckRequirement,
4754 kFieldClinitCheckRequirementSize>;
4755
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004756 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004757 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004758 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004759};
Vladimir Markof64242a2015-12-01 14:58:23 +00004760std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004761std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004763class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004764 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004765 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004766 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004767 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004768 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004769 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004770 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004771 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304772 : HInvoke(kInvokeVirtual,
4773 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004774 number_of_arguments,
4775 0u,
4776 return_type,
4777 dex_pc,
4778 dex_method_index,
4779 resolved_method,
4780 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304781 vtable_index_(vtable_index) {
4782 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004783
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004784 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004786 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004787 switch (GetIntrinsic()) {
4788 case Intrinsics::kThreadCurrentThread:
4789 case Intrinsics::kStringBufferAppend:
4790 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004791 case Intrinsics::kStringBuilderAppendObject:
4792 case Intrinsics::kStringBuilderAppendString:
4793 case Intrinsics::kStringBuilderAppendCharSequence:
4794 case Intrinsics::kStringBuilderAppendCharArray:
4795 case Intrinsics::kStringBuilderAppendBoolean:
4796 case Intrinsics::kStringBuilderAppendChar:
4797 case Intrinsics::kStringBuilderAppendInt:
4798 case Intrinsics::kStringBuilderAppendLong:
4799 case Intrinsics::kStringBuilderAppendFloat:
4800 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004801 case Intrinsics::kStringBuilderToString:
4802 return false;
4803 default:
4804 return HInvoke::CanBeNull();
4805 }
4806 }
4807
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004808 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004809 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004810 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004811 }
4812
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004813 uint32_t GetVTableIndex() const { return vtable_index_; }
4814
4815 DECLARE_INSTRUCTION(InvokeVirtual);
4816
Artem Serovcced8ba2017-07-19 18:18:09 +01004817 protected:
4818 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4819
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004820 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004821 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004822 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004823};
4824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004825class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004826 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004827 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004828 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004829 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004830 uint32_t dex_pc,
4831 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004832 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004833 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304834 : HInvoke(kInvokeInterface,
4835 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004836 number_of_arguments,
4837 0u,
4838 return_type,
4839 dex_pc,
4840 dex_method_index,
4841 resolved_method,
4842 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304843 imt_index_(imt_index) {
4844 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004846 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004848 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004849 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004850 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004851 }
4852
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004853 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004854 // The assembly stub currently needs it.
4855 return true;
4856 }
4857
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004858 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004859
4860 DECLARE_INSTRUCTION(InvokeInterface);
4861
Artem Serovcced8ba2017-07-19 18:18:09 +01004862 protected:
4863 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4864
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004865 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004866 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004867 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004868};
4869
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004870class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004871 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004872 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304873 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004874 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004875 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004876
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004877 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004879 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004880 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004881 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004882 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004883 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004884 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004885 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004886 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4887 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004888 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004889 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4890 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004891
Roland Levillain88cb1752014-10-20 16:36:47 +01004892 DECLARE_INSTRUCTION(Neg);
4893
Artem Serovcced8ba2017-07-19 18:18:09 +01004894 protected:
4895 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004896};
4897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004898class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004899 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004900 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304901 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004902 SetRawInputAt(0, cls);
4903 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004904 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004905 }
4906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004907 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004908
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004909 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004910 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004911
Mingyao Yang0c365e62015-03-31 15:09:29 -07004912 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004913 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004915 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004916
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004917 HLoadClass* GetLoadClass() const {
4918 DCHECK(InputAt(0)->IsLoadClass());
4919 return InputAt(0)->AsLoadClass();
4920 }
4921
4922 HInstruction* GetLength() const {
4923 return InputAt(1);
4924 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004925
Vladimir Markob5461632018-10-15 14:24:21 +01004926 size_t GetComponentSizeShift() {
4927 return GetPackedField<ComponentSizeShiftField>();
4928 }
4929
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004930 DECLARE_INSTRUCTION(NewArray);
4931
Artem Serovcced8ba2017-07-19 18:18:09 +01004932 protected:
4933 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004934
4935 private:
4936 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4937 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4938 static constexpr size_t kNumberOfNewArrayPackedBits =
4939 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4940 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4941 using ComponentSizeShiftField =
4942 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004943};
4944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004945class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004946 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004947 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004948 HInstruction* left,
4949 HInstruction* right,
4950 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304951 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4952 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004954 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004955
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004956 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004958 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004959 return GetBlock()->GetGraph()->GetIntConstant(
4960 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004961 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004962 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004963 return GetBlock()->GetGraph()->GetLongConstant(
4964 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004965 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004966 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004967 return GetBlock()->GetGraph()->GetFloatConstant(
4968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004971 return GetBlock()->GetGraph()->GetDoubleConstant(
4972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4973 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004974
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004975 DECLARE_INSTRUCTION(Add);
4976
Artem Serovcced8ba2017-07-19 18:18:09 +01004977 protected:
4978 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004979};
4980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004981class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004982 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004983 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004984 HInstruction* left,
4985 HInstruction* right,
4986 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304987 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4988 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004989
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004990 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004991
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004992 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004993 return GetBlock()->GetGraph()->GetIntConstant(
4994 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004995 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004996 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004997 return GetBlock()->GetGraph()->GetLongConstant(
4998 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004999 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005000 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005001 return GetBlock()->GetGraph()->GetFloatConstant(
5002 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5003 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005004 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005005 return GetBlock()->GetGraph()->GetDoubleConstant(
5006 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5007 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005008
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005009 DECLARE_INSTRUCTION(Sub);
5010
Artem Serovcced8ba2017-07-19 18:18:09 +01005011 protected:
5012 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005013};
5014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005015class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005016 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005018 HInstruction* left,
5019 HInstruction* right,
5020 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305021 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5022 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005024 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005025
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005026 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005027
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005028 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005029 return GetBlock()->GetGraph()->GetIntConstant(
5030 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005031 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005032 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005033 return GetBlock()->GetGraph()->GetLongConstant(
5034 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005035 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005036 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005037 return GetBlock()->GetGraph()->GetFloatConstant(
5038 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5039 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005040 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005041 return GetBlock()->GetGraph()->GetDoubleConstant(
5042 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5043 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005044
5045 DECLARE_INSTRUCTION(Mul);
5046
Artem Serovcced8ba2017-07-19 18:18:09 +01005047 protected:
5048 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005049};
5050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005051class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005052 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005053 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005054 HInstruction* left,
5055 HInstruction* right,
5056 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305057 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5058 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005059
Roland Levillain9867bc72015-08-05 10:21:34 +01005060 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005061 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005062 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005063 // Our graph structure ensures we never have 0 for `y` during
5064 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005065 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005066 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005067 return (y == -1) ? -x : x / y;
5068 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005069
Roland Levillain31dd3d62016-02-16 12:21:02 +00005070 template <typename T>
5071 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005072 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005073 return x / y;
5074 }
5075
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005076 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005077 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005078 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005079 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005080 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005081 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005082 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5083 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005084 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005085 return GetBlock()->GetGraph()->GetFloatConstant(
5086 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5087 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005088 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005089 return GetBlock()->GetGraph()->GetDoubleConstant(
5090 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005091 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005092
5093 DECLARE_INSTRUCTION(Div);
5094
Artem Serovcced8ba2017-07-19 18:18:09 +01005095 protected:
5096 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005097};
5098
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005099class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005100 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005101 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005102 HInstruction* left,
5103 HInstruction* right,
5104 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305105 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5106 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005107
Roland Levillain9867bc72015-08-05 10:21:34 +01005108 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005109 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005110 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005111 // Our graph structure ensures we never have 0 for `y` during
5112 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005113 DCHECK_NE(y, 0);
5114 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5115 return (y == -1) ? 0 : x % y;
5116 }
5117
Roland Levillain31dd3d62016-02-16 12:21:02 +00005118 template <typename T>
5119 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005120 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005121 return std::fmod(x, y);
5122 }
5123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005124 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005125 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005126 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005127 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005128 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005129 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005130 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005131 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005132 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005133 return GetBlock()->GetGraph()->GetFloatConstant(
5134 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5135 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005136 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005137 return GetBlock()->GetGraph()->GetDoubleConstant(
5138 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5139 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005140
5141 DECLARE_INSTRUCTION(Rem);
5142
Artem Serovcced8ba2017-07-19 18:18:09 +01005143 protected:
5144 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005145};
5146
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005147class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005148 public:
5149 HMin(DataType::Type result_type,
5150 HInstruction* left,
5151 HInstruction* right,
5152 uint32_t dex_pc)
5153 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005155 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005156
5157 // Evaluation for integral values.
5158 template <typename T> static T ComputeIntegral(T x, T y) {
5159 return (x <= y) ? x : y;
5160 }
5161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005162 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005163 return GetBlock()->GetGraph()->GetIntConstant(
5164 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5165 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005166 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005167 return GetBlock()->GetGraph()->GetLongConstant(
5168 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5169 }
5170 // TODO: Evaluation for floating-point values.
5171 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005172 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005173 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005174 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005175
5176 DECLARE_INSTRUCTION(Min);
5177
5178 protected:
5179 DEFAULT_COPY_CONSTRUCTOR(Min);
5180};
5181
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005182class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005183 public:
5184 HMax(DataType::Type result_type,
5185 HInstruction* left,
5186 HInstruction* right,
5187 uint32_t dex_pc)
5188 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5189
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005190 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005191
5192 // Evaluation for integral values.
5193 template <typename T> static T ComputeIntegral(T x, T y) {
5194 return (x >= y) ? x : y;
5195 }
5196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005197 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005198 return GetBlock()->GetGraph()->GetIntConstant(
5199 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5200 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005201 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005202 return GetBlock()->GetGraph()->GetLongConstant(
5203 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5204 }
5205 // TODO: Evaluation for floating-point values.
5206 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005207 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005208 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005210
5211 DECLARE_INSTRUCTION(Max);
5212
5213 protected:
5214 DEFAULT_COPY_CONSTRUCTOR(Max);
5215};
5216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005217class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005218 public:
5219 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5220 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5221
5222 // Evaluation for integral values.
5223 template <typename T> static T ComputeIntegral(T x) {
5224 return x < 0 ? -x : x;
5225 }
5226
5227 // Evaluation for floating-point values.
5228 // Note, as a "quality of implementation", rather than pure "spec compliance",
5229 // we require that Math.abs() clears the sign bit (but changes nothing else)
5230 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5231 // http://b/30758343
5232 template <typename T, typename S> static T ComputeFP(T x) {
5233 S bits = bit_cast<S, T>(x);
5234 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5235 }
5236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005237 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005238 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5239 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005240 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005241 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5242 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005243 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005244 return GetBlock()->GetGraph()->GetFloatConstant(
5245 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5246 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005247 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005248 return GetBlock()->GetGraph()->GetDoubleConstant(
5249 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5250 }
5251
5252 DECLARE_INSTRUCTION(Abs);
5253
5254 protected:
5255 DEFAULT_COPY_CONSTRUCTOR(Abs);
5256};
5257
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005258class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005259 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005260 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005261 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5262 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005263 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005264 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005265 SetRawInputAt(0, value);
5266 }
5267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005268 bool IsClonable() const override { return true; }
5269 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005271 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005272 return true;
5273 }
5274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005275 bool NeedsEnvironment() const override { return true; }
5276 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005277
Calin Juravled0d48522014-11-04 16:40:20 +00005278 DECLARE_INSTRUCTION(DivZeroCheck);
5279
Artem Serovcced8ba2017-07-19 18:18:09 +01005280 protected:
5281 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005282};
5283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005285 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005287 HInstruction* value,
5288 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005289 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305290 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5292 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005293 }
5294
Roland Levillain5b5b9312016-03-22 14:57:31 +00005295 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005296 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005297 return value << (distance & max_shift_distance);
5298 }
5299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005300 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005301 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005302 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005303 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005304 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005305 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005306 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005307 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005308 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005309 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005310 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5311 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005312 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005313 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005314 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005315 LOG(FATAL) << DebugName() << " is not defined for float values";
5316 UNREACHABLE();
5317 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005318 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005319 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005320 LOG(FATAL) << DebugName() << " is not defined for double values";
5321 UNREACHABLE();
5322 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005323
5324 DECLARE_INSTRUCTION(Shl);
5325
Artem Serovcced8ba2017-07-19 18:18:09 +01005326 protected:
5327 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005328};
5329
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005330class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005331 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005332 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005333 HInstruction* value,
5334 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005335 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305336 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005337 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5338 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005339 }
5340
Roland Levillain5b5b9312016-03-22 14:57:31 +00005341 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005342 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005343 return value >> (distance & max_shift_distance);
5344 }
5345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005346 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005347 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005348 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005349 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005350 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005351 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005352 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005353 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005354 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005355 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005356 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5357 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005358 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005359 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005360 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005361 LOG(FATAL) << DebugName() << " is not defined for float values";
5362 UNREACHABLE();
5363 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005364 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005365 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005366 LOG(FATAL) << DebugName() << " is not defined for double values";
5367 UNREACHABLE();
5368 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005369
5370 DECLARE_INSTRUCTION(Shr);
5371
Artem Serovcced8ba2017-07-19 18:18:09 +01005372 protected:
5373 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005374};
5375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005376class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005377 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005378 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005379 HInstruction* value,
5380 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005381 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305382 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005383 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5384 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005385 }
5386
Roland Levillain5b5b9312016-03-22 14:57:31 +00005387 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005388 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005389 typedef typename std::make_unsigned<T>::type V;
5390 V ux = static_cast<V>(value);
5391 return static_cast<T>(ux >> (distance & max_shift_distance));
5392 }
5393
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005394 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005395 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005396 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005397 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005398 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005399 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005400 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005401 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005402 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005403 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005404 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5405 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005406 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005407 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005408 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005409 LOG(FATAL) << DebugName() << " is not defined for float values";
5410 UNREACHABLE();
5411 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005412 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005413 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005414 LOG(FATAL) << DebugName() << " is not defined for double values";
5415 UNREACHABLE();
5416 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005417
5418 DECLARE_INSTRUCTION(UShr);
5419
Artem Serovcced8ba2017-07-19 18:18:09 +01005420 protected:
5421 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005422};
5423
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005424class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005425 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005426 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005427 HInstruction* left,
5428 HInstruction* right,
5429 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305430 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5431 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005433 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005434
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005435 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005436
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005437 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005438 return GetBlock()->GetGraph()->GetIntConstant(
5439 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005440 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005441 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005442 return GetBlock()->GetGraph()->GetLongConstant(
5443 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005444 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005445 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005446 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005447 LOG(FATAL) << DebugName() << " is not defined for float values";
5448 UNREACHABLE();
5449 }
5450 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005451 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005452 LOG(FATAL) << DebugName() << " is not defined for double values";
5453 UNREACHABLE();
5454 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005455
5456 DECLARE_INSTRUCTION(And);
5457
Artem Serovcced8ba2017-07-19 18:18:09 +01005458 protected:
5459 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005460};
5461
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005463 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005464 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005465 HInstruction* left,
5466 HInstruction* right,
5467 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305468 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5469 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005470
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005471 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005472
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005473 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005475 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005476 return GetBlock()->GetGraph()->GetIntConstant(
5477 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005478 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005480 return GetBlock()->GetGraph()->GetLongConstant(
5481 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005482 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005483 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005484 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005485 LOG(FATAL) << DebugName() << " is not defined for float values";
5486 UNREACHABLE();
5487 }
5488 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005489 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005490 LOG(FATAL) << DebugName() << " is not defined for double values";
5491 UNREACHABLE();
5492 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005493
5494 DECLARE_INSTRUCTION(Or);
5495
Artem Serovcced8ba2017-07-19 18:18:09 +01005496 protected:
5497 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005498};
5499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005500class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005501 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 HInstruction* left,
5504 HInstruction* right,
5505 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305506 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5507 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005509 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005510
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005511 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005512
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005513 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005514 return GetBlock()->GetGraph()->GetIntConstant(
5515 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005516 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005517 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005518 return GetBlock()->GetGraph()->GetLongConstant(
5519 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005520 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005521 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005522 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005523 LOG(FATAL) << DebugName() << " is not defined for float values";
5524 UNREACHABLE();
5525 }
5526 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005527 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005528 LOG(FATAL) << DebugName() << " is not defined for double values";
5529 UNREACHABLE();
5530 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005531
5532 DECLARE_INSTRUCTION(Xor);
5533
Artem Serovcced8ba2017-07-19 18:18:09 +01005534 protected:
5535 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005536};
5537
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005538class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005539 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005540 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305541 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005542 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5543 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005544 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005545
Roland Levillain5b5b9312016-03-22 14:57:31 +00005546 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005547 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005548 typedef typename std::make_unsigned<T>::type V;
5549 V ux = static_cast<V>(value);
5550 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005551 return static_cast<T>(ux);
5552 } else {
5553 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005554 return static_cast<T>(ux >> (distance & max_shift_value)) |
5555 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005556 }
5557 }
5558
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005559 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005560 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005561 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005562 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005563 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005564 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005565 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005566 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005567 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005568 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005569 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5570 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005571 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005572 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005573 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005574 LOG(FATAL) << DebugName() << " is not defined for float values";
5575 UNREACHABLE();
5576 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005577 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005578 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005579 LOG(FATAL) << DebugName() << " is not defined for double values";
5580 UNREACHABLE();
5581 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005582
5583 DECLARE_INSTRUCTION(Ror);
5584
Artem Serovcced8ba2017-07-19 18:18:09 +01005585 protected:
5586 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005587};
5588
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005589// The value of a parameter in this method. Its location depends on
5590// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005591class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005592 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005593 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005594 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005595 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005597 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305598 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005599 dex_file_(dex_file),
5600 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005601 index_(index) {
5602 SetPackedFlag<kFlagIsThis>(is_this);
5603 SetPackedFlag<kFlagCanBeNull>(!is_this);
5604 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005605
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005606 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005607 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005608 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005609 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005610
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005611 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005612 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005613
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005614 DECLARE_INSTRUCTION(ParameterValue);
5615
Artem Serovcced8ba2017-07-19 18:18:09 +01005616 protected:
5617 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5618
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005619 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005620 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005621 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005622 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5623 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5624 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5625 "Too many packed fields.");
5626
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005627 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005628 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005629 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005630 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005631 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005632};
5633
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005634class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005635 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005636 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305637 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5638 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005640 bool CanBeMoved() const override { return true; }
5641 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005642 return true;
5643 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005644
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005645 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005647 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005648 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005649 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005650 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005651 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005652 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005653 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005654 LOG(FATAL) << DebugName() << " is not defined for float values";
5655 UNREACHABLE();
5656 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005657 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005658 LOG(FATAL) << DebugName() << " is not defined for double values";
5659 UNREACHABLE();
5660 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005661
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005662 DECLARE_INSTRUCTION(Not);
5663
Artem Serovcced8ba2017-07-19 18:18:09 +01005664 protected:
5665 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005666};
5667
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005668class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005669 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005670 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305671 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5672 }
David Brazdil66d126e2015-04-03 16:02:44 +01005673
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005674 bool CanBeMoved() const override { return true; }
5675 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005676 return true;
5677 }
5678
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005679 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005680 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005681 return !x;
5682 }
5683
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005684 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005685 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005686 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005687 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005688 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005689 UNREACHABLE();
5690 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005691 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005692 LOG(FATAL) << DebugName() << " is not defined for float values";
5693 UNREACHABLE();
5694 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005695 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005696 LOG(FATAL) << DebugName() << " is not defined for double values";
5697 UNREACHABLE();
5698 }
David Brazdil66d126e2015-04-03 16:02:44 +01005699
5700 DECLARE_INSTRUCTION(BooleanNot);
5701
Artem Serovcced8ba2017-07-19 18:18:09 +01005702 protected:
5703 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005704};
5705
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005706class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005707 public:
5708 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005709 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305710 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005711 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005712 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005713 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005714 }
5715
5716 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005717 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5718 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005719
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005720 bool IsClonable() const override { return true; }
5721 bool CanBeMoved() const override { return true; }
5722 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005723 return true;
5724 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005725 // Return whether the conversion is implicit. This includes conversion to the same type.
5726 bool IsImplicitConversion() const {
5727 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5728 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005729
Mark Mendelle82549b2015-05-06 10:55:34 -04005730 // Try to statically evaluate the conversion and return a HConstant
5731 // containing the result. If the input cannot be converted, return nullptr.
5732 HConstant* TryStaticEvaluation() const;
5733
Roland Levillaindff1f282014-11-05 14:15:05 +00005734 DECLARE_INSTRUCTION(TypeConversion);
5735
Artem Serovcced8ba2017-07-19 18:18:09 +01005736 protected:
5737 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005738};
5739
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005740static constexpr uint32_t kNoRegNumber = -1;
5741
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005742class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005743 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005744 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005745 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5746 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005747 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005748 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005749 SetRawInputAt(0, value);
5750 }
5751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005752 bool IsClonable() const override { return true; }
5753 bool CanBeMoved() const override { return true; }
5754 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005755 return true;
5756 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005757
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005758 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005760 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005761
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005762 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005763
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005764 DECLARE_INSTRUCTION(NullCheck);
5765
Artem Serovcced8ba2017-07-19 18:18:09 +01005766 protected:
5767 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005768};
5769
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005770// Embeds an ArtField and all the information required by the compiler. We cache
5771// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005772class FieldInfo : public ValueObject {
5773 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005774 FieldInfo(ArtField* field,
5775 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005776 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005777 bool is_volatile,
5778 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005779 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005780 const DexFile& dex_file)
5781 : field_(field),
5782 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005783 field_type_(field_type),
5784 is_volatile_(is_volatile),
5785 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005786 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005787 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005788
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005789 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005790 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005791 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005792 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005793 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005794 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005795 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005796
5797 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005798 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005799 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005801 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005802 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005803 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005804 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005805};
5806
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005807class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005808 public:
5809 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005810 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005811 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005812 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005813 bool is_volatile,
5814 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005815 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005816 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005817 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305818 : HExpression(kInstanceFieldGet,
5819 field_type,
5820 SideEffects::FieldReadOfType(field_type, is_volatile),
5821 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005822 field_info_(field,
5823 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005824 field_type,
5825 is_volatile,
5826 field_idx,
5827 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005828 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005829 SetRawInputAt(0, value);
5830 }
5831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005832 bool IsClonable() const override { return true; }
5833 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005834
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005835 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005836 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005837 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005838 }
5839
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005840 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005841 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005842 }
5843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005844 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005845 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5846 }
5847
Calin Juravle52c48962014-12-16 17:02:57 +00005848 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005849 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005850 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005851 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005852
Vladimir Marko61b92282017-10-11 13:23:17 +01005853 void SetType(DataType::Type new_type) {
5854 DCHECK(DataType::IsIntegralType(GetType()));
5855 DCHECK(DataType::IsIntegralType(new_type));
5856 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5857 SetPackedField<TypeField>(new_type);
5858 }
5859
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005860 DECLARE_INSTRUCTION(InstanceFieldGet);
5861
Artem Serovcced8ba2017-07-19 18:18:09 +01005862 protected:
5863 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5864
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005865 private:
5866 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005867};
5868
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005869class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005870 public:
5871 HInstanceFieldSet(HInstruction* object,
5872 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005873 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005874 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005875 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005876 bool is_volatile,
5877 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005878 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005879 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005880 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005881 : HExpression(kInstanceFieldSet,
5882 SideEffects::FieldWriteOfType(field_type, is_volatile),
5883 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005884 field_info_(field,
5885 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005886 field_type,
5887 is_volatile,
5888 field_idx,
5889 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005890 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005891 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005892 SetRawInputAt(0, object);
5893 SetRawInputAt(1, value);
5894 }
5895
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005896 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005898 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005899 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005900 }
5901
Calin Juravle52c48962014-12-16 17:02:57 +00005902 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005903 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005904 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005905 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005906 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005907 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5908 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005909
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005910 DECLARE_INSTRUCTION(InstanceFieldSet);
5911
Artem Serovcced8ba2017-07-19 18:18:09 +01005912 protected:
5913 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5914
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005915 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005916 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5917 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5918 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5919 "Too many packed fields.");
5920
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005921 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005922};
5923
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005924class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005925 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005926 HArrayGet(HInstruction* array,
5927 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005929 uint32_t dex_pc)
5930 : HArrayGet(array,
5931 index,
5932 type,
5933 SideEffects::ArrayReadOfType(type),
5934 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005935 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305936 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005937
5938 HArrayGet(HInstruction* array,
5939 HInstruction* index,
5940 DataType::Type type,
5941 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005942 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005943 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305944 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005945 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005946 SetRawInputAt(0, array);
5947 SetRawInputAt(1, index);
5948 }
5949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005950 bool IsClonable() const override { return true; }
5951 bool CanBeMoved() const override { return true; }
5952 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005953 return true;
5954 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005955 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005956 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005957 // Currently, unless the array is the result of NewArray, the array access is always
5958 // preceded by some form of null NullCheck necessary for the bounds check, usually
5959 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5960 // dynamic BCE. There are cases when these could be removed to produce better code.
5961 // If we ever add optimizations to do so we should allow an implicit check here
5962 // (as long as the address falls in the first page).
5963 //
5964 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5965 // a = cond ? new int[1] : null;
5966 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005967 return false;
5968 }
5969
David Brazdil4833f5a2015-12-16 10:37:39 +00005970 bool IsEquivalentOf(HArrayGet* other) const {
5971 bool result = (GetDexPc() == other->GetDexPc());
5972 if (kIsDebugBuild && result) {
5973 DCHECK_EQ(GetBlock(), other->GetBlock());
5974 DCHECK_EQ(GetArray(), other->GetArray());
5975 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 if (DataType::IsIntOrLongType(GetType())) {
5977 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005978 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005979 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5980 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005981 }
5982 }
5983 return result;
5984 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005985
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005986 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5987
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005988 HInstruction* GetArray() const { return InputAt(0); }
5989 HInstruction* GetIndex() const { return InputAt(1); }
5990
Vladimir Marko61b92282017-10-11 13:23:17 +01005991 void SetType(DataType::Type new_type) {
5992 DCHECK(DataType::IsIntegralType(GetType()));
5993 DCHECK(DataType::IsIntegralType(new_type));
5994 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5995 SetPackedField<TypeField>(new_type);
5996 }
5997
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005998 DECLARE_INSTRUCTION(ArrayGet);
5999
Artem Serovcced8ba2017-07-19 18:18:09 +01006000 protected:
6001 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006003 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006004 // We treat a String as an array, creating the HArrayGet from String.charAt()
6005 // intrinsic in the instruction simplifier. We can always determine whether
6006 // a particular HArrayGet is actually a String.charAt() by looking at the type
6007 // of the input but that requires holding the mutator lock, so we prefer to use
6008 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006009 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006010 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6011 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6012 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006013};
6014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006015class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006016 public:
6017 HArraySet(HInstruction* array,
6018 HInstruction* index,
6019 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006020 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006021 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006022 : HArraySet(array,
6023 index,
6024 value,
6025 expected_component_type,
6026 // Make a best guess for side effects now, may be refined during SSA building.
6027 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306028 dex_pc) {
6029 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006030
6031 HArraySet(HInstruction* array,
6032 HInstruction* index,
6033 HInstruction* value,
6034 DataType::Type expected_component_type,
6035 SideEffects side_effects,
6036 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006037 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006038 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006039 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006040 SetPackedFlag<kFlagValueCanBeNull>(true);
6041 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006042 SetRawInputAt(0, array);
6043 SetRawInputAt(1, index);
6044 SetRawInputAt(2, value);
6045 }
6046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006047 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006049 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006050 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006051 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006052 }
6053
Mingyao Yang81014cb2015-06-02 03:16:27 -07006054 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006055 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006056
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006057 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006058 // TODO: Same as for ArrayGet.
6059 return false;
6060 }
6061
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006062 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006063 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006064 }
6065
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006066 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006067 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006068 }
6069
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006070 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006071 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006072 }
6073
Vladimir Markoa1de9182016-02-25 11:37:38 +00006074 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6075 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6076 bool StaticTypeOfArrayIsObjectArray() const {
6077 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6078 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006079
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006080 HInstruction* GetArray() const { return InputAt(0); }
6081 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006082 HInstruction* GetValue() const { return InputAt(2); }
6083
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006084 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006085 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6086 }
6087
6088 static DataType::Type GetComponentType(DataType::Type value_type,
6089 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006090 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006091 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006092 // be correct, we also check what is the value type. If it is a floating
6093 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006094 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006095 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006096 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006097 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006099 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006100 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006101 }
6102
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006103 static SideEffects ComputeSideEffects(DataType::Type type) {
6104 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006105 }
6106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006107 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6108 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6109 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006110 }
6111
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006112 DECLARE_INSTRUCTION(ArraySet);
6113
Artem Serovcced8ba2017-07-19 18:18:09 +01006114 protected:
6115 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6116
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006117 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006118 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6119 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006120 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006121 static constexpr size_t kFlagNeedsTypeCheck =
6122 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6123 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006124 // Cached information for the reference_type_info_ so that codegen
6125 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006126 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6127 static constexpr size_t kNumberOfArraySetPackedBits =
6128 kFlagStaticTypeOfArrayIsObjectArray + 1;
6129 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6130 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006131 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006132};
6133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006134class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006135 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006136 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306137 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006138 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006139 // Note that arrays do not change length, so the instruction does not
6140 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006141 SetRawInputAt(0, array);
6142 }
6143
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006144 bool IsClonable() const override { return true; }
6145 bool CanBeMoved() const override { return true; }
6146 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006147 return true;
6148 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006149 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006150 return obj == InputAt(0);
6151 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006152
Vladimir Markodce016e2016-04-28 13:10:02 +01006153 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6154
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006155 DECLARE_INSTRUCTION(ArrayLength);
6156
Artem Serovcced8ba2017-07-19 18:18:09 +01006157 protected:
6158 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006160 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006161 // We treat a String as an array, creating the HArrayLength from String.length()
6162 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6163 // determine whether a particular HArrayLength is actually a String.length() by
6164 // looking at the type of the input but that requires holding the mutator lock, so
6165 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006166 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006167 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6168 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6169 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006170};
6171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006172class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006173 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006174 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006175 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6176 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006177 HBoundsCheck(HInstruction* index,
6178 HInstruction* length,
6179 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006180 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006181 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006182 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006183 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006184 SetRawInputAt(0, index);
6185 SetRawInputAt(1, length);
6186 }
6187
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006188 bool IsClonable() const override { return true; }
6189 bool CanBeMoved() const override { return true; }
6190 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006191 return true;
6192 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006193
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006194 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006195
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006196 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006197
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006198 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006199
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006200 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006201
6202 DECLARE_INSTRUCTION(BoundsCheck);
6203
Artem Serovcced8ba2017-07-19 18:18:09 +01006204 protected:
6205 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6206
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006207 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006208 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006209 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6210 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6211 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006212};
6213
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006214class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006215 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006216 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006217 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306218 slow_path_(nullptr) {
6219 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006220
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006221 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006222
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006223 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006224 return true;
6225 }
6226
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006227 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6228 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006229
6230 DECLARE_INSTRUCTION(SuspendCheck);
6231
Artem Serovcced8ba2017-07-19 18:18:09 +01006232 protected:
6233 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6234
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006235 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006236 // Only used for code generation, in order to share the same slow path between back edges
6237 // of a same loop.
6238 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006239};
6240
David Srbecky0cf44932015-12-09 14:09:59 +00006241// Pseudo-instruction which provides the native debugger with mapping information.
6242// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006243class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006244 public:
6245 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006246 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306247 }
David Srbecky0cf44932015-12-09 14:09:59 +00006248
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006249 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006250 return true;
6251 }
6252
6253 DECLARE_INSTRUCTION(NativeDebugInfo);
6254
Artem Serovcced8ba2017-07-19 18:18:09 +01006255 protected:
6256 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006257};
6258
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006259/**
6260 * Instruction to load a Class object.
6261 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006262class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006263 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006264 // Determines how to load the Class.
6265 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006266 // We cannot load this class. See HSharpening::SharpenLoadClass.
6267 kInvalid = -1,
6268
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006269 // Use the Class* from the method's own ArtMethod*.
6270 kReferrersClass,
6271
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006272 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006273 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006274 kBootImageLinkTimePcRelative,
6275
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006276 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006277 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006278 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006279
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006280 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006281 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006282 kBssEntry,
6283
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006284 // Use a known boot image Class* address, embedded in the code by the codegen.
6285 // Used for boot image classes referenced by apps in JIT-compiled code.
6286 kJitBootImageAddress,
6287
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006288 // Load from the root table associated with the JIT compiled method.
6289 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006290
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006291 // Load using a simple runtime call. This is the fall-back load kind when
6292 // the codegen is unable to use another appropriate kind.
6293 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006294
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006295 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006296 };
6297
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006298 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006299 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006300 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006301 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006302 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006303 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006304 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006305 : HInstruction(kLoadClass,
6306 DataType::Type::kReference,
6307 SideEffectsForArchRuntimeCalls(),
6308 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006309 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006310 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006311 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006312 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006313 // Referrers class should not need access check. We never inline unverified
6314 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006315 DCHECK(!is_referrers_class || !needs_access_check);
6316
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006317 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006318 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006319 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006320 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006321 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006322 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006323 }
6324
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006325 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006326
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006327 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006328
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006329 LoadKind GetLoadKind() const {
6330 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006331 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006332
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006333 bool HasPcRelativeLoadKind() const {
6334 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6335 GetLoadKind() == LoadKind::kBootImageRelRo ||
6336 GetLoadKind() == LoadKind::kBssEntry;
6337 }
6338
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006339 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006340
Yi Kong39402542019-03-24 02:47:16 -07006341 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006343 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006344
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006345 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006346
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006347 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006348 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006349 }
6350
Calin Juravle0ba218d2015-05-19 18:46:01 +01006351 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006352 // The entrypoint the code generator is going to call does not do
6353 // clinit of the class.
6354 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006355 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006356 }
6357
6358 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006359 return NeedsAccessCheck() ||
6360 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006361 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006362 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006363 }
6364
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006365 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006366 return NeedsAccessCheck() ||
6367 MustGenerateClinitCheck() ||
6368 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006369 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006370 GetLoadKind() == LoadKind::kBssEntry) &&
6371 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006372 }
6373
Calin Juravleacf735c2015-02-12 15:25:22 +00006374 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006375 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6376 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006377 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006378 } else {
6379 return ReferenceTypeInfo::CreateInvalid();
6380 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006381 }
6382
Vladimir Marko175e7862018-03-27 09:03:13 +00006383 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6384 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6385 DCHECK(klass_ != nullptr);
6386 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006387 }
6388
Andreas Gampea5b09a62016-11-17 15:21:22 -08006389 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006390 const DexFile& GetDexFile() const { return dex_file_; }
6391
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006392 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006393 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006394 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006395
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006396 static SideEffects SideEffectsForArchRuntimeCalls() {
6397 return SideEffects::CanTriggerGC();
6398 }
6399
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006400 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006401 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006402 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006403 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006404
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006405 bool MustResolveTypeOnSlowPath() const {
6406 // Check that this instruction has a slow path.
6407 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6408 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6409 return GetLoadKind() == LoadKind::kBssEntry;
6410 }
6411
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006412 void MarkInBootImage() {
6413 SetPackedFlag<kFlagIsInBootImage>(true);
6414 }
6415
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006416 void AddSpecialInput(HInstruction* special_input);
6417
6418 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006419 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006420 return ArrayRef<HUserRecord<HInstruction*>>(
6421 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6422 }
6423
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006424 Handle<mirror::Class> GetClass() const {
6425 return klass_;
6426 }
6427
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006428 DECLARE_INSTRUCTION(LoadClass);
6429
Artem Serovcced8ba2017-07-19 18:18:09 +01006430 protected:
6431 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6432
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006433 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006434 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006435 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006436 // Whether this instruction must generate the initialization check.
6437 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006438 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006439 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6440 static constexpr size_t kFieldLoadKindSize =
6441 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006442 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6443 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006444 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006445 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6446
6447 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006448 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006449 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006450 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006451 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006452 }
6453
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006454 void SetLoadKindInternal(LoadKind load_kind);
6455
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006456 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006457 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006458 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006459 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006460
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006461 // A type index and dex file where the class can be accessed. The dex file can be:
6462 // - The compiling method's dex file if the class is defined there too.
6463 // - The compiling method's dex file if the class is referenced there.
6464 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006465 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006466 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006467 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006468
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006469 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006470};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006471std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6472
6473// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006474inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6475 // The load kind should be determined before inserting the instruction to the graph.
6476 DCHECK(GetBlock() == nullptr);
6477 DCHECK(GetEnvironment() == nullptr);
6478 SetPackedField<LoadKindField>(load_kind);
6479 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6480 special_input_ = HUserRecord<HInstruction*>(nullptr);
6481 }
6482 if (!NeedsEnvironment()) {
6483 SetSideEffects(SideEffects::None());
6484 }
6485}
6486
6487// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006488inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006489 // The special input is used for PC-relative loads on some architectures,
6490 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006491 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006492 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006493 GetLoadKind() == LoadKind::kBssEntry ||
6494 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006495 DCHECK(special_input_.GetInstruction() == nullptr);
6496 special_input_ = HUserRecord<HInstruction*>(special_input);
6497 special_input->AddUseAt(this, 0);
6498}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006500class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006501 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006502 // Determines how to load the String.
6503 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006504 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006505 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006506 kBootImageLinkTimePcRelative,
6507
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006508 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006509 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006510 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006511
Vladimir Markoaad75c62016-10-03 08:46:48 +00006512 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006513 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006514 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006515
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006516 // Use a known boot image String* address, embedded in the code by the codegen.
6517 // Used for boot image strings referenced by apps in JIT-compiled code.
6518 kJitBootImageAddress,
6519
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006520 // Load from the root table associated with the JIT compiled method.
6521 kJitTableAddress,
6522
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006523 // Load using a simple runtime call. This is the fall-back load kind when
6524 // the codegen is unable to use another appropriate kind.
6525 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006526
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006527 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006528 };
6529
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006530 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006531 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006532 const DexFile& dex_file,
6533 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006534 : HInstruction(kLoadString,
6535 DataType::Type::kReference,
6536 SideEffectsForArchRuntimeCalls(),
6537 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006538 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006539 string_index_(string_index),
6540 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006541 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006542 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006543
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006544 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006545
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006546 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006547
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006548 LoadKind GetLoadKind() const {
6549 return GetPackedField<LoadKindField>();
6550 }
6551
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006552 bool HasPcRelativeLoadKind() const {
6553 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6554 GetLoadKind() == LoadKind::kBootImageRelRo ||
6555 GetLoadKind() == LoadKind::kBssEntry;
6556 }
6557
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006558 const DexFile& GetDexFile() const {
6559 return dex_file_;
6560 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006561
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006562 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006563 return string_index_;
6564 }
6565
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006566 Handle<mirror::String> GetString() const {
6567 return string_;
6568 }
6569
6570 void SetString(Handle<mirror::String> str) {
6571 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006572 }
6573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006574 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006575
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006576 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006577
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006578 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006579
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006580 // Will call the runtime if we need to load the string through
6581 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006582 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006583 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006584 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006585 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006586 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006587 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006588 return false;
6589 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006590 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006591 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006592
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006593 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006594 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006595 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006596
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006597 bool CanBeNull() const override { return false; }
6598 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006599
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006600 static SideEffects SideEffectsForArchRuntimeCalls() {
6601 return SideEffects::CanTriggerGC();
6602 }
6603
Vladimir Marko372f10e2016-05-17 16:30:10 +01006604 void AddSpecialInput(HInstruction* special_input);
6605
6606 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006607 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006608 return ArrayRef<HUserRecord<HInstruction*>>(
6609 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006610 }
6611
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006612 DECLARE_INSTRUCTION(LoadString);
6613
Artem Serovcced8ba2017-07-19 18:18:09 +01006614 protected:
6615 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6616
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006617 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006618 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006619 static constexpr size_t kFieldLoadKindSize =
6620 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6621 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006622 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006623 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006624
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006625 void SetLoadKindInternal(LoadKind load_kind);
6626
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006627 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006628 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006629 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006630 HUserRecord<HInstruction*> special_input_;
6631
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006632 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006633 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006634
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006635 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006636};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006637std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6638
6639// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006640inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6641 // The load kind should be determined before inserting the instruction to the graph.
6642 DCHECK(GetBlock() == nullptr);
6643 DCHECK(GetEnvironment() == nullptr);
6644 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6645 SetPackedField<LoadKindField>(load_kind);
6646 if (load_kind != LoadKind::kRuntimeCall) {
6647 special_input_ = HUserRecord<HInstruction*>(nullptr);
6648 }
6649 if (!NeedsEnvironment()) {
6650 SetSideEffects(SideEffects::None());
6651 }
6652}
6653
6654// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006655inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006656 // The special input is used for PC-relative loads on some architectures,
6657 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006658 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006659 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006660 GetLoadKind() == LoadKind::kBssEntry ||
6661 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006662 // HLoadString::GetInputRecords() returns an empty array at this point,
6663 // so use the GetInputRecords() from the base class to set the input record.
6664 DCHECK(special_input_.GetInstruction() == nullptr);
6665 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006666 special_input->AddUseAt(this, 0);
6667}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006668
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006669class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006670 public:
6671 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006672 uint16_t method_handle_idx,
6673 const DexFile& dex_file,
6674 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006675 : HInstruction(kLoadMethodHandle,
6676 DataType::Type::kReference,
6677 SideEffectsForArchRuntimeCalls(),
6678 dex_pc),
6679 special_input_(HUserRecord<HInstruction*>(current_method)),
6680 method_handle_idx_(method_handle_idx),
6681 dex_file_(dex_file) {
6682 }
6683
6684 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006685 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006686 return ArrayRef<HUserRecord<HInstruction*>>(
6687 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6688 }
6689
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006690 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006691
6692 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6693
6694 const DexFile& GetDexFile() const { return dex_file_; }
6695
6696 static SideEffects SideEffectsForArchRuntimeCalls() {
6697 return SideEffects::CanTriggerGC();
6698 }
6699
6700 DECLARE_INSTRUCTION(LoadMethodHandle);
6701
6702 protected:
6703 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6704
6705 private:
6706 // The special input is the HCurrentMethod for kRuntimeCall.
6707 HUserRecord<HInstruction*> special_input_;
6708
6709 const uint16_t method_handle_idx_;
6710 const DexFile& dex_file_;
6711};
6712
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006713class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006714 public:
6715 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006716 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006717 const DexFile& dex_file,
6718 uint32_t dex_pc)
6719 : HInstruction(kLoadMethodType,
6720 DataType::Type::kReference,
6721 SideEffectsForArchRuntimeCalls(),
6722 dex_pc),
6723 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006724 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006725 dex_file_(dex_file) {
6726 }
6727
6728 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006729 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006730 return ArrayRef<HUserRecord<HInstruction*>>(
6731 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6732 }
6733
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006734 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006735
Orion Hodson06d10a72018-05-14 08:53:38 +01006736 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006737
6738 const DexFile& GetDexFile() const { return dex_file_; }
6739
6740 static SideEffects SideEffectsForArchRuntimeCalls() {
6741 return SideEffects::CanTriggerGC();
6742 }
6743
6744 DECLARE_INSTRUCTION(LoadMethodType);
6745
6746 protected:
6747 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6748
6749 private:
6750 // The special input is the HCurrentMethod for kRuntimeCall.
6751 HUserRecord<HInstruction*> special_input_;
6752
Orion Hodson06d10a72018-05-14 08:53:38 +01006753 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006754 const DexFile& dex_file_;
6755};
6756
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006757/**
6758 * Performs an initialization check on its Class object input.
6759 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006760class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006761 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006762 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006763 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306764 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006765 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006766 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006767 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006768 SetRawInputAt(0, constant);
6769 }
Artem Serova6e26142018-06-19 14:55:17 +01006770 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006771 bool CanBeMoved() const override { return true; }
6772 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006773 return true;
6774 }
6775
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006776 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006777 // May call runtime to initialize the class.
6778 return true;
6779 }
6780
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006781 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006782
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006783 HLoadClass* GetLoadClass() const {
6784 DCHECK(InputAt(0)->IsLoadClass());
6785 return InputAt(0)->AsLoadClass();
6786 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006787
6788 DECLARE_INSTRUCTION(ClinitCheck);
6789
Artem Serovcced8ba2017-07-19 18:18:09 +01006790
6791 protected:
6792 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006793};
6794
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006795class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006796 public:
6797 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006798 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006799 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006800 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006801 bool is_volatile,
6802 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006803 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006804 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006805 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306806 : HExpression(kStaticFieldGet,
6807 field_type,
6808 SideEffects::FieldReadOfType(field_type, is_volatile),
6809 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006810 field_info_(field,
6811 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006812 field_type,
6813 is_volatile,
6814 field_idx,
6815 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006816 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006817 SetRawInputAt(0, cls);
6818 }
6819
Calin Juravle52c48962014-12-16 17:02:57 +00006820
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006821 bool IsClonable() const override { return true; }
6822 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006823
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006824 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006825 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006826 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006827 }
6828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006829 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006830 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6831 }
6832
Calin Juravle52c48962014-12-16 17:02:57 +00006833 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006834 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006835 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006836 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006837
Vladimir Marko61b92282017-10-11 13:23:17 +01006838 void SetType(DataType::Type new_type) {
6839 DCHECK(DataType::IsIntegralType(GetType()));
6840 DCHECK(DataType::IsIntegralType(new_type));
6841 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6842 SetPackedField<TypeField>(new_type);
6843 }
6844
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006845 DECLARE_INSTRUCTION(StaticFieldGet);
6846
Artem Serovcced8ba2017-07-19 18:18:09 +01006847 protected:
6848 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6849
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006850 private:
6851 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006852};
6853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006854class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006855 public:
6856 HStaticFieldSet(HInstruction* cls,
6857 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006858 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006859 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006860 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006861 bool is_volatile,
6862 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006863 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006864 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006865 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006866 : HExpression(kStaticFieldSet,
6867 SideEffects::FieldWriteOfType(field_type, is_volatile),
6868 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006869 field_info_(field,
6870 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006871 field_type,
6872 is_volatile,
6873 field_idx,
6874 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006875 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006876 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006877 SetRawInputAt(0, cls);
6878 SetRawInputAt(1, value);
6879 }
6880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006881 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006882 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006883 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006884 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006885 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006886
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006887 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006888 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6889 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006890
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006891 DECLARE_INSTRUCTION(StaticFieldSet);
6892
Artem Serovcced8ba2017-07-19 18:18:09 +01006893 protected:
6894 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6895
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006896 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006897 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6898 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6899 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6900 "Too many packed fields.");
6901
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006902 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006903};
6904
Vladimir Marko552a1342017-10-31 10:56:47 +00006905class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6906 public:
6907 HStringBuilderAppend(HIntConstant* format,
6908 uint32_t number_of_arguments,
6909 ArenaAllocator* allocator,
6910 uint32_t dex_pc)
6911 : HVariableInputSizeInstruction(
6912 kStringBuilderAppend,
6913 DataType::Type::kReference,
6914 // The runtime call may read memory from inputs. It never writes outside
6915 // of the newly allocated result object (or newly allocated helper objects).
6916 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6917 dex_pc,
6918 allocator,
6919 number_of_arguments + /* format */ 1u,
6920 kArenaAllocInvokeInputs) {
6921 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6922 SetRawInputAt(FormatIndex(), format);
6923 }
6924
6925 void SetArgumentAt(size_t index, HInstruction* argument) {
6926 DCHECK_LE(index, GetNumberOfArguments());
6927 SetRawInputAt(index, argument);
6928 }
6929
6930 // Return the number of arguments, excluding the format.
6931 size_t GetNumberOfArguments() const {
6932 DCHECK_GE(InputCount(), 1u);
6933 return InputCount() - 1u;
6934 }
6935
6936 size_t FormatIndex() const {
6937 return GetNumberOfArguments();
6938 }
6939
6940 HIntConstant* GetFormat() {
6941 return InputAt(FormatIndex())->AsIntConstant();
6942 }
6943
6944 bool NeedsEnvironment() const override { return true; }
6945
6946 bool CanThrow() const override { return true; }
6947
6948 DECLARE_INSTRUCTION(StringBuilderAppend);
6949
6950 protected:
6951 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
6952};
6953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006954class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006955 public:
6956 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006957 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006958 uint32_t field_index,
6959 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306960 : HExpression(kUnresolvedInstanceFieldGet,
6961 field_type,
6962 SideEffects::AllExceptGCDependency(),
6963 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006964 field_index_(field_index) {
6965 SetRawInputAt(0, obj);
6966 }
6967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006968 bool IsClonable() const override { return true; }
6969 bool NeedsEnvironment() const override { return true; }
6970 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006972 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006973 uint32_t GetFieldIndex() const { return field_index_; }
6974
6975 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6976
Artem Serovcced8ba2017-07-19 18:18:09 +01006977 protected:
6978 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6979
Calin Juravlee460d1d2015-09-29 04:52:17 +01006980 private:
6981 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006982};
6983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006984class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006985 public:
6986 HUnresolvedInstanceFieldSet(HInstruction* obj,
6987 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006988 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006989 uint32_t field_index,
6990 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006991 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006992 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006993 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006994 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006995 SetRawInputAt(0, obj);
6996 SetRawInputAt(1, value);
6997 }
6998
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006999 bool IsClonable() const override { return true; }
7000 bool NeedsEnvironment() const override { return true; }
7001 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007002
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007003 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007004 uint32_t GetFieldIndex() const { return field_index_; }
7005
7006 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7007
Artem Serovcced8ba2017-07-19 18:18:09 +01007008 protected:
7009 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7010
Calin Juravlee460d1d2015-09-29 04:52:17 +01007011 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007012 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7013 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007014 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007015 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7016 kFieldFieldType + kFieldFieldTypeSize;
7017 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7018 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007019 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007020
Calin Juravlee460d1d2015-09-29 04:52:17 +01007021 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007022};
7023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007024class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007025 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007026 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007027 uint32_t field_index,
7028 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307029 : HExpression(kUnresolvedStaticFieldGet,
7030 field_type,
7031 SideEffects::AllExceptGCDependency(),
7032 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007033 field_index_(field_index) {
7034 }
7035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007036 bool IsClonable() const override { return true; }
7037 bool NeedsEnvironment() const override { return true; }
7038 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007040 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007041 uint32_t GetFieldIndex() const { return field_index_; }
7042
7043 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7044
Artem Serovcced8ba2017-07-19 18:18:09 +01007045 protected:
7046 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7047
Calin Juravlee460d1d2015-09-29 04:52:17 +01007048 private:
7049 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007050};
7051
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007052class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007053 public:
7054 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007055 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007056 uint32_t field_index,
7057 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007058 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007059 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007060 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007061 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007062 SetRawInputAt(0, value);
7063 }
7064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007065 bool IsClonable() const override { return true; }
7066 bool NeedsEnvironment() const override { return true; }
7067 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007069 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007070 uint32_t GetFieldIndex() const { return field_index_; }
7071
7072 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7073
Artem Serovcced8ba2017-07-19 18:18:09 +01007074 protected:
7075 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7076
Calin Juravlee460d1d2015-09-29 04:52:17 +01007077 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007078 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7079 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007080 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007081 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7082 kFieldFieldType + kFieldFieldTypeSize;
7083 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7084 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007085 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007086
Calin Juravlee460d1d2015-09-29 04:52:17 +01007087 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007088};
7089
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007090// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007091class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007092 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007093 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307094 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7095 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007097 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007098
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007099 DECLARE_INSTRUCTION(LoadException);
7100
Artem Serovcced8ba2017-07-19 18:18:09 +01007101 protected:
7102 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007103};
7104
David Brazdilcb1c0552015-08-04 16:22:25 +01007105// Implicit part of move-exception which clears thread-local exception storage.
7106// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007107class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007108 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007109 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007110 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307111 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007112
7113 DECLARE_INSTRUCTION(ClearException);
7114
Artem Serovcced8ba2017-07-19 18:18:09 +01007115 protected:
7116 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007117};
7118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007119class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007120 public:
7121 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007122 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007123 SetRawInputAt(0, exception);
7124 }
7125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007126 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007128 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007130 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007132 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007133
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007134 DECLARE_INSTRUCTION(Throw);
7135
Artem Serovcced8ba2017-07-19 18:18:09 +01007136 protected:
7137 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007138};
7139
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007140/**
7141 * Implementation strategies for the code generator of a HInstanceOf
7142 * or `HCheckCast`.
7143 */
7144enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007145 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007146 kExactCheck, // Can do a single class compare.
7147 kClassHierarchyCheck, // Can just walk the super class chain.
7148 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7149 kInterfaceCheck, // No optimization yet when checking against an interface.
7150 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007151 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007152 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007153 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007154};
7155
Roland Levillain86503782016-02-11 19:07:30 +00007156std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7157
Vladimir Marko175e7862018-03-27 09:03:13 +00007158// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7159// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7160class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007161 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007162 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007163 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007164 HInstruction* object,
7165 HInstruction* target_class_or_null,
7166 TypeCheckKind check_kind,
7167 Handle<mirror::Class> klass,
7168 uint32_t dex_pc,
7169 ArenaAllocator* allocator,
7170 HIntConstant* bitstring_path_to_root,
7171 HIntConstant* bitstring_mask,
7172 SideEffects side_effects)
7173 : HVariableInputSizeInstruction(
7174 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007175 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007176 side_effects,
7177 dex_pc,
7178 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007179 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007180 kArenaAllocTypeCheckInputs),
7181 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007182 SetPackedField<TypeCheckKindField>(check_kind);
7183 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007184 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007185 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007186 SetRawInputAt(1, target_class_or_null);
7187 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7188 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7189 if (check_kind == TypeCheckKind::kBitstringCheck) {
7190 DCHECK(target_class_or_null->IsNullConstant());
7191 SetRawInputAt(2, bitstring_path_to_root);
7192 SetRawInputAt(3, bitstring_mask);
7193 } else {
7194 DCHECK(target_class_or_null->IsLoadClass());
7195 }
Vladimir Marko87584542017-12-12 17:47:52 +00007196 }
7197
7198 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007199 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007200 HInstruction* load_class = InputAt(1);
7201 DCHECK(load_class->IsLoadClass());
7202 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007203 }
7204
Vladimir Marko175e7862018-03-27 09:03:13 +00007205 uint32_t GetBitstringPathToRoot() const {
7206 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7207 HInstruction* path_to_root = InputAt(2);
7208 DCHECK(path_to_root->IsIntConstant());
7209 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7210 }
7211
7212 uint32_t GetBitstringMask() const {
7213 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7214 HInstruction* mask = InputAt(3);
7215 DCHECK(mask->IsIntConstant());
7216 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7217 }
7218
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007219 bool IsClonable() const override { return true; }
7220 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007222 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007223 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7224 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007225 }
7226
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007227 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7228 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7229 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7230 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7231
Vladimir Marko175e7862018-03-27 09:03:13 +00007232 ReferenceTypeInfo GetTargetClassRTI() {
7233 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7234 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007235 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007236 } else {
7237 return ReferenceTypeInfo::CreateInvalid();
7238 }
7239 }
7240
7241 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7242 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7243 DCHECK(klass_ != nullptr);
7244 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7245 }
7246
7247 Handle<mirror::Class> GetClass() const {
7248 return klass_;
7249 }
7250
7251 protected:
7252 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7253
7254 private:
7255 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7256 static constexpr size_t kFieldTypeCheckKindSize =
7257 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7258 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7259 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7260 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7261 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7262 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7263
7264 Handle<mirror::Class> klass_;
7265};
7266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007267class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007268 public:
7269 HInstanceOf(HInstruction* object,
7270 HInstruction* target_class_or_null,
7271 TypeCheckKind check_kind,
7272 Handle<mirror::Class> klass,
7273 uint32_t dex_pc,
7274 ArenaAllocator* allocator,
7275 HIntConstant* bitstring_path_to_root,
7276 HIntConstant* bitstring_mask)
7277 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007278 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007279 object,
7280 target_class_or_null,
7281 check_kind,
7282 klass,
7283 dex_pc,
7284 allocator,
7285 bitstring_path_to_root,
7286 bitstring_mask,
7287 SideEffectsForArchRuntimeCalls(check_kind)) {}
7288
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007289 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007291 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007292 return CanCallRuntime(GetTypeCheckKind());
7293 }
7294
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007295 static bool CanCallRuntime(TypeCheckKind check_kind) {
7296 // Mips currently does runtime calls for any other checks.
7297 return check_kind != TypeCheckKind::kExactCheck;
7298 }
7299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007300 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007301 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007302 }
7303
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007304 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007305
Artem Serovcced8ba2017-07-19 18:18:09 +01007306 protected:
7307 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007308};
7309
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007310class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007311 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007312 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307313 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007314 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7315 SetPackedFlag<kFlagUpperCanBeNull>(true);
7316 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007317 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007318 SetRawInputAt(0, input);
7319 }
7320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007321 bool InstructionDataEquals(const HInstruction* other) const override;
7322 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007323
David Brazdilf5552582015-12-27 13:36:12 +00007324 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007325 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007326 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007327 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007328
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007329 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007330 DCHECK(GetUpperCanBeNull() || !can_be_null);
7331 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007332 }
7333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007334 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007335
Calin Juravleb1498f62015-02-16 13:13:29 +00007336 DECLARE_INSTRUCTION(BoundType);
7337
Artem Serovcced8ba2017-07-19 18:18:09 +01007338 protected:
7339 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7340
Calin Juravleb1498f62015-02-16 13:13:29 +00007341 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007342 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7343 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007344 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007345 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7346 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7347 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7348
Calin Juravleb1498f62015-02-16 13:13:29 +00007349 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007350 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7351 // It is used to bound the type in cases like:
7352 // if (x instanceof ClassX) {
7353 // // uper_bound_ will be ClassX
7354 // }
David Brazdilf5552582015-12-27 13:36:12 +00007355 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007356};
7357
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007358class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007359 public:
7360 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007361 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007362 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007363 Handle<mirror::Class> klass,
7364 uint32_t dex_pc,
7365 ArenaAllocator* allocator,
7366 HIntConstant* bitstring_path_to_root,
7367 HIntConstant* bitstring_mask)
7368 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007369 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007370 object,
7371 target_class_or_null,
7372 check_kind,
7373 klass,
7374 dex_pc,
7375 allocator,
7376 bitstring_path_to_root,
7377 bitstring_mask,
7378 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007380 bool IsClonable() const override { return true; }
7381 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007382 // Instruction may throw a CheckCastError.
7383 return true;
7384 }
7385
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007386 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007387
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007388 DECLARE_INSTRUCTION(CheckCast);
7389
Artem Serovcced8ba2017-07-19 18:18:09 +01007390 protected:
7391 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007392};
7393
Andreas Gampe26de38b2016-07-27 17:53:11 -07007394/**
7395 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7396 * @details We define the combined barrier types that are actually required
7397 * by the Java Memory Model, rather than using exactly the terminology from
7398 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7399 * primitives. Note that the JSR-133 cookbook generally does not deal with
7400 * store atomicity issues, and the recipes there are not always entirely sufficient.
7401 * The current recipe is as follows:
7402 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7403 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7404 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7405 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7406 * class has final fields.
7407 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7408 * store-to-memory instructions. Only generated together with non-temporal stores.
7409 */
7410enum MemBarrierKind {
7411 kAnyStore,
7412 kLoadAny,
7413 kStoreStore,
7414 kAnyAny,
7415 kNTStoreStore,
7416 kLastBarrierKind = kNTStoreStore
7417};
7418std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007420class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007421 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007422 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007423 : HExpression(kMemoryBarrier,
7424 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7425 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007426 SetPackedField<BarrierKindField>(barrier_kind);
7427 }
Calin Juravle27df7582015-04-17 19:12:31 +01007428
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007429 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007430
Vladimir Markoa1de9182016-02-25 11:37:38 +00007431 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007432
7433 DECLARE_INSTRUCTION(MemoryBarrier);
7434
Artem Serovcced8ba2017-07-19 18:18:09 +01007435 protected:
7436 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7437
Calin Juravle27df7582015-04-17 19:12:31 +01007438 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007439 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7440 static constexpr size_t kFieldBarrierKindSize =
7441 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7442 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7443 kFieldBarrierKind + kFieldBarrierKindSize;
7444 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7445 "Too many packed fields.");
7446 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007447};
7448
Igor Murashkind01745e2017-04-05 16:40:31 -07007449// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7450// the specified object(s), with respect to any subsequent store that might "publish"
7451// (i.e. make visible) the specified object to another thread.
7452//
7453// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7454// for all final fields (that were set) at the end of the invoked constructor.
7455//
7456// The constructor fence models the freeze actions for the final fields of an object
7457// being constructed (semantically at the end of the constructor). Constructor fences
7458// have a per-object affinity; two separate objects being constructed get two separate
7459// constructor fences.
7460//
7461// (Note: that if calling a super-constructor or forwarding to another constructor,
7462// the freezes would happen at the end of *that* constructor being invoked).
7463//
7464// The memory model guarantees that when the object being constructed is "published" after
7465// constructor completion (i.e. escapes the current thread via a store), then any final field
7466// writes must be observable on other threads (once they observe that publication).
7467//
7468// Further, anything written before the freeze, and read by dereferencing through the final field,
7469// must also be visible (so final object field could itself have an object with non-final fields;
7470// yet the freeze must also extend to them).
7471//
7472// Constructor example:
7473//
7474// class HasFinal {
7475// final int field; Optimizing IR for <init>()V:
7476// HasFinal() {
7477// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7478// // freeze(this.field); HConstructorFence(this)
7479// } HReturn
7480// }
7481//
7482// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7483// already-initialized classes; in that case the allocation must act as a "default-initializer"
7484// of the object which effectively writes the class pointer "final field".
7485//
7486// For example, we can model default-initialiation as roughly the equivalent of the following:
7487//
7488// class Object {
7489// private final Class header;
7490// }
7491//
7492// Java code: Optimizing IR:
7493//
7494// T new_instance<T>() {
7495// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7496// obj.header = T.class; // header write is done by above call.
7497// // freeze(obj.header) HConstructorFence(obj)
7498// return (T)obj;
7499// }
7500//
7501// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007502// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007503// * QuasiAtomic::ThreadFenceForConstructor
7504//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007505class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007506 // A fence has variable inputs because the inputs can be removed
7507 // after prepare_for_register_allocation phase.
7508 // (TODO: In the future a fence could freeze multiple objects
7509 // after merging two fences together.)
7510 public:
7511 // `fence_object` is the reference that needs to be protected for correct publication.
7512 //
7513 // It makes sense in the following situations:
7514 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7515 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7516 //
7517 // After construction the `fence_object` becomes the 0th input.
7518 // This is not an input in a real sense, but just a convenient place to stash the information
7519 // about the associated object.
7520 HConstructorFence(HInstruction* fence_object,
7521 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007522 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007523 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7524 // constraints described in the class header. We claim that these SideEffects constraints
7525 // enforce a superset of the real constraints.
7526 //
7527 // The ordering described above is conservatively modeled with SideEffects as follows:
7528 //
7529 // * To prevent reordering of the publication stores:
7530 // ----> "Reads of objects" is the initial SideEffect.
7531 // * For every primitive final field store in the constructor:
7532 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7533 // * If there are any stores to reference final fields in the constructor:
7534 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7535 // that are reachable from `fence_object` also need to be prevented for reordering
7536 // (and we do not want to do alias analysis to figure out what those stores are).
7537 //
7538 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7539 // even more conservative approach than the one described above, and prevents all of the
7540 // above reordering without analyzing any of the instructions in the constructor.
7541 //
7542 // If in a later phase we discover that there are no writes to reference final fields,
7543 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307544 : HVariableInputSizeInstruction(kConstructorFence,
7545 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007546 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007547 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007548 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007549 kArenaAllocConstructorFenceInputs) {
7550 DCHECK(fence_object != nullptr);
7551 SetRawInputAt(0, fence_object);
7552 }
7553
7554 // The object associated with this constructor fence.
7555 //
7556 // (Note: This will be null after the prepare_for_register_allocation phase,
7557 // as all constructor fence inputs are removed there).
7558 HInstruction* GetFenceObject() const {
7559 return InputAt(0);
7560 }
7561
7562 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7563 // - Delete `fence_use` from `this`'s use list.
7564 // - Delete `this` from `fence_use`'s inputs list.
7565 // - If the `fence_use` is dead, remove it from the graph.
7566 //
7567 // A fence is considered dead once it no longer has any uses
7568 // and all of the inputs are dead.
7569 //
7570 // This must *not* be called during/after prepare_for_register_allocation,
7571 // because that removes all the inputs to the fences but the fence is actually
7572 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007573 //
7574 // Returns how many HConstructorFence instructions were removed from graph.
7575 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007576
Igor Murashkindd018df2017-08-09 10:38:31 -07007577 // Combine all inputs of `this` and `other` instruction and remove
7578 // `other` from the graph.
7579 //
7580 // Inputs are unique after the merge.
7581 //
7582 // Requirement: `this` must not be the same as `other.
7583 void Merge(HConstructorFence* other);
7584
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007585 // Check if this constructor fence is protecting
7586 // an HNewInstance or HNewArray that is also the immediate
7587 // predecessor of `this`.
7588 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007589 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7590 // to be one of the inputs of `this`.
7591 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007592 // Returns the associated HNewArray or HNewInstance,
7593 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007594 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007595
Igor Murashkind01745e2017-04-05 16:40:31 -07007596 DECLARE_INSTRUCTION(ConstructorFence);
7597
Artem Serovcced8ba2017-07-19 18:18:09 +01007598 protected:
7599 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007600};
7601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007602class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007603 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007604 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007605 kEnter,
7606 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007607 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007608 };
7609
7610 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007611 : HExpression(kMonitorOperation,
7612 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7613 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007614 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007615 SetRawInputAt(0, object);
7616 }
7617
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007618 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007619 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007620
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007621 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007622 // Verifier guarantees that monitor-exit cannot throw.
7623 // This is important because it allows the HGraphBuilder to remove
7624 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7625 return IsEnter();
7626 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007627
Vladimir Markoa1de9182016-02-25 11:37:38 +00007628 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7629 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007630
7631 DECLARE_INSTRUCTION(MonitorOperation);
7632
Artem Serovcced8ba2017-07-19 18:18:09 +01007633 protected:
7634 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7635
Calin Juravle52c48962014-12-16 17:02:57 +00007636 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007637 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7638 static constexpr size_t kFieldOperationKindSize =
7639 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7640 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7641 kFieldOperationKind + kFieldOperationKindSize;
7642 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7643 "Too many packed fields.");
7644 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007645};
7646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007647class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007648 public:
7649 HSelect(HInstruction* condition,
7650 HInstruction* true_value,
7651 HInstruction* false_value,
7652 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307653 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007654 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7655
7656 // First input must be `true_value` or `false_value` to allow codegens to
7657 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7658 // that architectures which implement HSelect as a conditional move also
7659 // will not need to invert the condition.
7660 SetRawInputAt(0, false_value);
7661 SetRawInputAt(1, true_value);
7662 SetRawInputAt(2, condition);
7663 }
7664
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007665 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007666 HInstruction* GetFalseValue() const { return InputAt(0); }
7667 HInstruction* GetTrueValue() const { return InputAt(1); }
7668 HInstruction* GetCondition() const { return InputAt(2); }
7669
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007670 bool CanBeMoved() const override { return true; }
7671 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007672 return true;
7673 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007674
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007675 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007676 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7677 }
7678
7679 DECLARE_INSTRUCTION(Select);
7680
Artem Serovcced8ba2017-07-19 18:18:09 +01007681 protected:
7682 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007683};
7684
Vladimir Markof9f64412015-09-02 14:05:49 +01007685class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007686 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007687 MoveOperands(Location source,
7688 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007689 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007690 HInstruction* instruction)
7691 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007692
7693 Location GetSource() const { return source_; }
7694 Location GetDestination() const { return destination_; }
7695
7696 void SetSource(Location value) { source_ = value; }
7697 void SetDestination(Location value) { destination_ = value; }
7698
7699 // The parallel move resolver marks moves as "in-progress" by clearing the
7700 // destination (but not the source).
7701 Location MarkPending() {
7702 DCHECK(!IsPending());
7703 Location dest = destination_;
7704 destination_ = Location::NoLocation();
7705 return dest;
7706 }
7707
7708 void ClearPending(Location dest) {
7709 DCHECK(IsPending());
7710 destination_ = dest;
7711 }
7712
7713 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007714 DCHECK(source_.IsValid() || destination_.IsInvalid());
7715 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007716 }
7717
7718 // True if this blocks a move from the given location.
7719 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007720 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007721 }
7722
7723 // A move is redundant if it's been eliminated, if its source and
7724 // destination are the same, or if its destination is unneeded.
7725 bool IsRedundant() const {
7726 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7727 }
7728
7729 // We clear both operands to indicate move that's been eliminated.
7730 void Eliminate() {
7731 source_ = destination_ = Location::NoLocation();
7732 }
7733
7734 bool IsEliminated() const {
7735 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7736 return source_.IsInvalid();
7737 }
7738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007739 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007740
Nicolas Geoffray90218252015-04-15 11:56:51 +01007741 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007742 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007743 }
7744
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007745 HInstruction* GetInstruction() const { return instruction_; }
7746
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007747 private:
7748 Location source_;
7749 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007750 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007751 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007752 // The instruction this move is assocatied with. Null when this move is
7753 // for moving an input in the expected locations of user (including a phi user).
7754 // This is only used in debug mode, to ensure we do not connect interval siblings
7755 // in the same parallel move.
7756 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007757};
7758
Roland Levillainc9285912015-12-18 10:38:42 +00007759std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7760
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007761static constexpr size_t kDefaultNumberOfMoves = 4;
7762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007763class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007764 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007765 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007766 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007767 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007768 moves_.reserve(kDefaultNumberOfMoves);
7769 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007770
Nicolas Geoffray90218252015-04-15 11:56:51 +01007771 void AddMove(Location source,
7772 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007773 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007774 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007775 DCHECK(source.IsValid());
7776 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007777 if (kIsDebugBuild) {
7778 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007779 for (const MoveOperands& move : moves_) {
7780 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007781 // Special case the situation where the move is for the spill slot
7782 // of the instruction.
7783 if ((GetPrevious() == instruction)
7784 || ((GetPrevious() == nullptr)
7785 && instruction->IsPhi()
7786 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007787 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007788 << "Doing parallel moves for the same instruction.";
7789 } else {
7790 DCHECK(false) << "Doing parallel moves for the same instruction.";
7791 }
7792 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007793 }
7794 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007795 for (const MoveOperands& move : moves_) {
7796 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007797 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007798 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007799 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007800 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007801 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007802 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007803 }
7804
Vladimir Marko225b6462015-09-28 12:17:40 +01007805 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007806 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007807 }
7808
Vladimir Marko225b6462015-09-28 12:17:40 +01007809 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007810
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007811 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007812
Artem Serovcced8ba2017-07-19 18:18:09 +01007813 protected:
7814 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7815
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007816 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007817 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007818};
7819
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007820// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7821// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7822// never used across anything that can trigger GC.
7823// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7824// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007825class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007826 public:
7827 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307828 : HExpression(kIntermediateAddress,
7829 DataType::Type::kInt32,
7830 SideEffects::DependsOnGC(),
7831 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007832 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7833 DataType::Size(DataType::Type::kReference))
7834 << "kPrimInt and kPrimNot have different sizes.";
7835 SetRawInputAt(0, base_address);
7836 SetRawInputAt(1, offset);
7837 }
7838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007839 bool IsClonable() const override { return true; }
7840 bool CanBeMoved() const override { return true; }
7841 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007842 return true;
7843 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007844 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007845
7846 HInstruction* GetBaseAddress() const { return InputAt(0); }
7847 HInstruction* GetOffset() const { return InputAt(1); }
7848
7849 DECLARE_INSTRUCTION(IntermediateAddress);
7850
Artem Serovcced8ba2017-07-19 18:18:09 +01007851 protected:
7852 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007853};
7854
7855
Mark Mendell0616ae02015-04-17 12:49:27 -04007856} // namespace art
7857
Aart Bikf8f5a162017-02-06 15:35:29 -08007858#include "nodes_vector.h"
7859
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007860#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7861#include "nodes_shared.h"
7862#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007863#ifdef ART_ENABLE_CODEGEN_mips
7864#include "nodes_mips.h"
7865#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307866#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007867#include "nodes_x86.h"
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05307868#include "nodes_vector_x86.h"
Mark Mendell0616ae02015-04-17 12:49:27 -04007869#endif
7870
7871namespace art {
7872
Igor Murashkin6ef45672017-08-08 13:59:55 -07007873class OptimizingCompilerStats;
7874
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007875class HGraphVisitor : public ValueObject {
7876 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007877 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7878 : stats_(stats),
7879 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007880 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007881
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007882 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007883 virtual void VisitBasicBlock(HBasicBlock* block);
7884
Roland Levillain633021e2014-10-01 14:12:25 +01007885 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007886 void VisitInsertionOrder();
7887
Roland Levillain633021e2014-10-01 14:12:25 +01007888 // Visit the graph following dominator tree reverse post-order.
7889 void VisitReversePostOrder();
7890
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007891 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007892
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007893 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007894#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007895 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7896
7897 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7898
7899#undef DECLARE_VISIT_INSTRUCTION
7900
Igor Murashkin6ef45672017-08-08 13:59:55 -07007901 protected:
7902 OptimizingCompilerStats* stats_;
7903
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007904 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007905 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007906
7907 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7908};
7909
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007910class HGraphDelegateVisitor : public HGraphVisitor {
7911 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007912 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7913 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007914 virtual ~HGraphDelegateVisitor() {}
7915
7916 // Visit functions that delegate to to super class.
7917#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007918 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007919
7920 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7921
7922#undef DECLARE_VISIT_INSTRUCTION
7923
7924 private:
7925 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7926};
7927
Artem Serovcced8ba2017-07-19 18:18:09 +01007928// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7929// and remove the old instruction.
7930HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7931
7932// Create a clone for each clonable instructions/phis and replace the original with the clone.
7933//
7934// Used for testing individual instruction cloner.
7935class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7936 public:
7937 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007938 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007939
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007940 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007941 if (instruction->IsClonable()) {
7942 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007943 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007944 }
7945 }
7946
Artem Serov7f4aff62017-06-21 17:02:18 +01007947 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007948
7949 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007950 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007951
7952 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7953};
7954
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007955// Iterator over the blocks that art part of the loop. Includes blocks part
7956// of an inner loop. The order in which the blocks are iterated is on their
7957// block id.
7958class HBlocksInLoopIterator : public ValueObject {
7959 public:
7960 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7961 : blocks_in_loop_(info.GetBlocks()),
7962 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7963 index_(0) {
7964 if (!blocks_in_loop_.IsBitSet(index_)) {
7965 Advance();
7966 }
7967 }
7968
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007969 bool Done() const { return index_ == blocks_.size(); }
7970 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007971 void Advance() {
7972 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007973 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007974 if (blocks_in_loop_.IsBitSet(index_)) {
7975 break;
7976 }
7977 }
7978 }
7979
7980 private:
7981 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007982 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007983 size_t index_;
7984
7985 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7986};
7987
Mingyao Yang3584bce2015-05-19 16:01:59 -07007988// Iterator over the blocks that art part of the loop. Includes blocks part
7989// of an inner loop. The order in which the blocks are iterated is reverse
7990// post order.
7991class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7992 public:
7993 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7994 : blocks_in_loop_(info.GetBlocks()),
7995 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7996 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007997 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007998 Advance();
7999 }
8000 }
8001
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008002 bool Done() const { return index_ == blocks_.size(); }
8003 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008004 void Advance() {
8005 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008006 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8007 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008008 break;
8009 }
8010 }
8011 }
8012
8013 private:
8014 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008015 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008016 size_t index_;
8017
8018 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8019};
8020
Aart Bikf3e61ee2017-04-12 17:09:20 -07008021// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008022inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008023 if (constant->IsIntConstant()) {
8024 return constant->AsIntConstant()->GetValue();
8025 } else if (constant->IsLongConstant()) {
8026 return constant->AsLongConstant()->GetValue();
8027 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008028 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008029 return 0;
8030 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008031}
8032
Aart Bikf3e61ee2017-04-12 17:09:20 -07008033// Returns true iff instruction is an integral constant (and sets value on success).
8034inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8035 if (instruction->IsIntConstant()) {
8036 *value = instruction->AsIntConstant()->GetValue();
8037 return true;
8038 } else if (instruction->IsLongConstant()) {
8039 *value = instruction->AsLongConstant()->GetValue();
8040 return true;
8041 } else if (instruction->IsNullConstant()) {
8042 *value = 0;
8043 return true;
8044 }
8045 return false;
8046}
8047
Aart Bik0148de42017-09-05 09:25:01 -07008048// Returns true iff instruction is the given integral constant.
8049inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8050 int64_t val = 0;
8051 return IsInt64AndGet(instruction, &val) && val == value;
8052}
8053
8054// Returns true iff instruction is a zero bit pattern.
8055inline bool IsZeroBitPattern(HInstruction* instruction) {
8056 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8057}
8058
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008059// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008060#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008061 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8062 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8063#undef INSTRUCTION_TYPE_CHECK
8064
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008065// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008066#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8067 std::is_base_of<BaseType, H##type>::value,
8068#define INSTRUCTION_TYPE_CHECK(type, super) \
8069 inline bool HInstruction::Is##type() const { \
8070 DCHECK_LT(GetKind(), kLastInstructionKind); \
8071 using BaseType = H##type; \
8072 static constexpr bool results[] = { \
8073 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8074 }; \
8075 return results[static_cast<size_t>(GetKind())]; \
8076 }
8077
8078 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8079#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008080#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008081
8082#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008083 inline const H##type* HInstruction::As##type() const { \
8084 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8085 } \
8086 inline H##type* HInstruction::As##type() { \
8087 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8088 }
8089
Vladimir Markoa90dd512018-05-04 15:04:45 +01008090 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8091#undef INSTRUCTION_TYPE_CAST
8092
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008093
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008094// Create space in `blocks` for adding `number_of_new_blocks` entries
8095// starting at location `at`. Blocks after `at` are moved accordingly.
8096inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8097 size_t number_of_new_blocks,
8098 size_t after) {
8099 DCHECK_LT(after, blocks->size());
8100 size_t old_size = blocks->size();
8101 size_t new_size = old_size + number_of_new_blocks;
8102 blocks->resize(new_size);
8103 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8104}
8105
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008106/*
8107 * Hunt "under the hood" of array lengths (leading to array references),
8108 * null checks (also leading to array references), and new arrays
8109 * (leading to the actual length). This makes it more likely related
8110 * instructions become actually comparable.
8111 */
8112inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8113 while (instruction->IsArrayLength() ||
8114 instruction->IsNullCheck() ||
8115 instruction->IsNewArray()) {
8116 instruction = instruction->IsNewArray()
8117 ? instruction->AsNewArray()->GetLength()
8118 : instruction->InputAt(0);
8119 }
8120 return instruction;
8121}
8122
Artem Serov21c7e6f2017-07-27 16:04:42 +01008123void RemoveEnvironmentUses(HInstruction* instruction);
8124bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8125void ResetEnvironmentInputRecords(HInstruction* instruction);
8126
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008127} // namespace art
8128
8129#endif // ART_COMPILER_OPTIMIZING_NODES_H_