blob: 25f9e3cb7396d76dc288041286c05ab6827e09dc [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 Geoffraya48c3df2019-06-27 13:11:12 +0000323 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),
Artem Serov2808be82018-12-20 19:15:11 +0000338 has_monitor_operations_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700339 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800340 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000341 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800342 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000343 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000344 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100345 dex_file_(dex_file),
346 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100347 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100348 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800349 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000351 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100352 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
353 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
354 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
355 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000356 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100357 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000358 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700359 osr_(osr),
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000360 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)),
361 is_shared_jit_code_(is_shared_jit_code) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100362 blocks_.reserve(kDefaultNumberOfBlocks);
363 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364
David Brazdilbadd8262016-02-02 16:28:56 +0000365 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700366 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000367
Vladimir Markoca6fff82017-10-03 14:49:14 +0100368 ArenaAllocator* GetAllocator() const { return allocator_; }
369 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100370 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
371
David Brazdil69ba7b72015-06-23 18:27:30 +0100372 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000373 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100374
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000375 HBasicBlock* GetEntryBlock() const { return entry_block_; }
376 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100377 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000378
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000379 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
380 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000381
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000382 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100383
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100384 void ComputeDominanceInformation();
385 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000386 void ClearLoopInformation();
387 void FindBackEdges(ArenaBitVector* visited);
388 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100389 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100390 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000391
David Brazdil4833f5a2015-12-16 10:37:39 +0000392 // Analyze all natural loops in this graph. Returns a code specifying that it
393 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000394 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000395 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100396
David Brazdilffee3d32015-07-06 11:48:53 +0100397 // Iterate over blocks to compute try block membership. Needs reverse post
398 // order and loop information.
399 void ComputeTryBlockInformation();
400
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000401 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000402 // Returns the instruction to replace the invoke expression or null if the
403 // invoke is for a void method. Note that the caller is responsible for replacing
404 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000405 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000406
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000407 // Update the loop and try membership of `block`, which was spawned from `reference`.
408 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
409 // should be the new back edge.
410 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
411 HBasicBlock* reference,
412 bool replace_if_back_edge);
413
Mingyao Yang3584bce2015-05-19 16:01:59 -0700414 // Need to add a couple of blocks to test if the loop body is entered and
415 // put deoptimization instructions, etc.
416 void TransformLoopHeaderForBCE(HBasicBlock* header);
417
Aart Bikf8f5a162017-02-06 15:35:29 -0800418 // Adds a new loop directly after the loop with the given header and exit.
419 // Returns the new preheader.
420 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
421 HBasicBlock* body,
422 HBasicBlock* exit);
423
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000424 // Removes `block` from the graph. Assumes `block` has been disconnected from
425 // other blocks and has no instructions or phis.
426 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000427
David Brazdilfc6a86a2015-06-26 10:33:45 +0000428 // Splits the edge between `block` and `successor` while preserving the
429 // indices in the predecessor/successor lists. If there are multiple edges
430 // between the blocks, the lowest indices are used.
431 // Returns the new block which is empty and has the same dex pc as `successor`.
432 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
433
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100434 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100435 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000436
437 // Transform a loop into a format with a single preheader.
438 //
439 // Each phi in the header should be split: original one in the header should only hold
440 // inputs reachable from the back edges and a single input from the preheader. The newly created
441 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
442 //
443 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
444 // that no longer have this property.
445 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
446
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100447 void SimplifyLoop(HBasicBlock* header);
448
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000449 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100450 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000451 return current_instruction_id_++;
452 }
453
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000454 int32_t GetCurrentInstructionId() const {
455 return current_instruction_id_;
456 }
457
458 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100459 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000460 current_instruction_id_ = id;
461 }
462
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100463 uint16_t GetMaximumNumberOfOutVRegs() const {
464 return maximum_number_of_out_vregs_;
465 }
466
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000467 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
468 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100469 }
470
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100471 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
472 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
473 }
474
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000475 void UpdateTemporariesVRegSlots(size_t slots) {
476 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100477 }
478
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000479 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100480 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000481 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100482 }
483
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100484 void SetNumberOfVRegs(uint16_t number_of_vregs) {
485 number_of_vregs_ = number_of_vregs;
486 }
487
488 uint16_t GetNumberOfVRegs() const {
489 return number_of_vregs_;
490 }
491
492 void SetNumberOfInVRegs(uint16_t value) {
493 number_of_in_vregs_ = value;
494 }
495
David Brazdildee58d62016-04-07 09:54:26 +0000496 uint16_t GetNumberOfInVRegs() const {
497 return number_of_in_vregs_;
498 }
499
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100500 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100501 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100502 return number_of_vregs_ - number_of_in_vregs_;
503 }
504
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100505 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100507 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100508
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100509 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
510 DCHECK(GetReversePostOrder()[0] == entry_block_);
511 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
512 }
513
514 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
515 return ReverseRange(GetReversePostOrder());
516 }
517
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100518 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100519 return linear_order_;
520 }
521
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100522 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
523 return ReverseRange(GetLinearOrder());
524 }
525
Mark Mendell1152c922015-04-24 17:06:35 -0400526 bool HasBoundsChecks() const {
527 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800528 }
529
Mark Mendell1152c922015-04-24 17:06:35 -0400530 void SetHasBoundsChecks(bool value) {
531 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800532 }
533
Hans Boehm206348c2018-12-05 11:11:33 -0800534 // Is the code known to be robust against eliminating dead references
535 // and the effects of early finalization?
536 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
537
538 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
539
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000540 bool IsDebuggable() const { return debuggable_; }
541
David Brazdil8d5b8b22015-03-24 10:51:52 +0000542 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000543 // already, it is created and inserted into the graph. This method is only for
544 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100545 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000546
547 // TODO: This is problematic for the consistency of reference type propagation
548 // because it can be created anytime after the pass and thus it will be left
549 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600550 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000551
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600552 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
553 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000554 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600555 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
556 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000557 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600558 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
559 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000560 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600561 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
562 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000563 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000564
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100565 HCurrentMethod* GetCurrentMethod();
566
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100567 const DexFile& GetDexFile() const {
568 return dex_file_;
569 }
570
571 uint32_t GetMethodIdx() const {
572 return method_idx_;
573 }
574
Igor Murashkind01745e2017-04-05 16:40:31 -0700575 // Get the method name (without the signature), e.g. "<init>"
576 const char* GetMethodName() const;
577
578 // Get the pretty method name (class + name + optionally signature).
579 std::string PrettyMethod(bool with_signature = true) const;
580
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100581 InvokeType GetInvokeType() const {
582 return invoke_type_;
583 }
584
Mark Mendellc4701932015-04-10 13:18:51 -0400585 InstructionSet GetInstructionSet() const {
586 return instruction_set_;
587 }
588
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000589 bool IsCompilingOsr() const { return osr_; }
590
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000591 bool IsCompilingForSharedJitCode() const {
592 return is_shared_jit_code_;
593 }
594
Mingyao Yang063fc772016-08-02 11:02:54 -0700595 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
596 return cha_single_implementation_list_;
597 }
598
599 void AddCHASingleImplementationDependency(ArtMethod* method) {
600 cha_single_implementation_list_.insert(method);
601 }
602
603 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800604 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700605 }
606
David Brazdil77a48ae2015-09-15 12:34:04 +0000607 bool HasTryCatch() const { return has_try_catch_; }
608 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100609
Artem Serov2808be82018-12-20 19:15:11 +0000610 bool HasMonitorOperations() const { return has_monitor_operations_; }
611 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
612
Aart Bikb13c65b2017-03-21 20:14:07 -0700613 bool HasSIMD() const { return has_simd_; }
614 void SetHasSIMD(bool value) { has_simd_ = value; }
615
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800616 bool HasLoops() const { return has_loops_; }
617 void SetHasLoops(bool value) { has_loops_ = value; }
618
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000619 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
620 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
621
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100622 ArtMethod* GetArtMethod() const { return art_method_; }
623 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
624
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100625 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500626 // The instruction has been inserted into the graph, either as a constant, or
627 // before cursor.
628 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
629
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000630 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
631
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800632 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
633 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
634 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
635
David Brazdil2d7352b2015-04-20 14:52:42 +0100636 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000637 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100638 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 template <class InstructionType, typename ValueType>
641 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600642 ArenaSafeMap<ValueType, InstructionType*>* cache,
643 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000644 // Try to find an existing constant of the given value.
645 InstructionType* constant = nullptr;
646 auto cached_constant = cache->find(value);
647 if (cached_constant != cache->end()) {
648 constant = cached_constant->second;
649 }
650
651 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100652 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000653 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100654 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000655 cache->Overwrite(value, constant);
656 InsertConstant(constant);
657 }
658 return constant;
659 }
660
David Brazdil8d5b8b22015-03-24 10:51:52 +0000661 void InsertConstant(HConstant* instruction);
662
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000663 // Cache a float constant into the graph. This method should only be
664 // called by the SsaBuilder when creating "equivalent" instructions.
665 void CacheFloatConstant(HFloatConstant* constant);
666
667 // See CacheFloatConstant comment.
668 void CacheDoubleConstant(HDoubleConstant* constant);
669
Vladimir Markoca6fff82017-10-03 14:49:14 +0100670 ArenaAllocator* const allocator_;
671 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000672
673 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100674 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000675
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100677 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000678
Aart Bik281c6812016-08-26 11:31:48 -0700679 // List of blocks to perform a linear order tree traversal. Unlike the reverse
680 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100681 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100682
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000683 HBasicBlock* entry_block_;
684 HBasicBlock* exit_block_;
685
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100686 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100687 uint16_t maximum_number_of_out_vregs_;
688
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100689 // The number of virtual registers in this method. Contains the parameters.
690 uint16_t number_of_vregs_;
691
692 // The number of virtual registers used by parameters of this method.
693 uint16_t number_of_in_vregs_;
694
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000695 // Number of vreg size slots that the temporaries use (used in baseline compiler).
696 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100697
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800698 // Flag whether there are bounds checks in the graph. We can skip
699 // BCE if it's false. It's only best effort to keep it up to date in
700 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400701 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800702
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800703 // Flag whether there are try/catch blocks in the graph. We will skip
704 // try/catch-related passes if it's false. It's only best effort to keep
705 // it up to date in the presence of code elimination so there might be
706 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000707 bool has_try_catch_;
708
Artem Serov2808be82018-12-20 19:15:11 +0000709 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
710 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
711 bool has_monitor_operations_;
712
Aart Bikb13c65b2017-03-21 20:14:07 -0700713 // Flag whether SIMD instructions appear in the graph. If true, the
714 // code generators may have to be more careful spilling the wider
715 // contents of SIMD registers.
716 bool has_simd_;
717
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800718 // Flag whether there are any loops in the graph. We can skip loop
719 // optimization if it's false. It's only best effort to keep it up
720 // to date in the presence of code elimination so there might be false
721 // positives.
722 bool has_loops_;
723
724 // Flag whether there are any irreducible loops in the graph. It's only
725 // best effort to keep it up to date in the presence of code elimination
726 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000727 bool has_irreducible_loops_;
728
Hans Boehm206348c2018-12-05 11:11:33 -0800729 // Is the code known to be robust against eliminating dead references
730 // and the effects of early finalization? If false, dead reference variables
731 // are kept if they might be visible to the garbage collector.
732 // Currently this means that the class was declared to be dead-reference-safe,
733 // the method accesses no reachability-sensitive fields or data, and the same
734 // is true for any methods that were inlined into the current one.
735 bool dead_reference_safe_;
736
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000737 // Indicates whether the graph should be compiled in a way that
738 // ensures full debuggability. If false, we can apply more
739 // aggressive optimizations that may limit the level of debugging.
740 const bool debuggable_;
741
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000742 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000743 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000744
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100745 // The dex file from which the method is from.
746 const DexFile& dex_file_;
747
748 // The method index in the dex file.
749 const uint32_t method_idx_;
750
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100751 // If inlined, this encodes how the callee is being invoked.
752 const InvokeType invoke_type_;
753
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100754 // Whether the graph has been transformed to SSA form. Only used
755 // in debug mode to ensure we are not using properties only valid
756 // for non-SSA form (like the number of temporaries).
757 bool in_ssa_form_;
758
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800759 // Number of CHA guards in the graph. Used to short-circuit the
760 // CHA guard optimization pass when there is no CHA guard left.
761 uint32_t number_of_cha_guards_;
762
Mathieu Chartiere401d142015-04-22 13:56:20 -0700763 const InstructionSet instruction_set_;
764
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000765 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000766 HNullConstant* cached_null_constant_;
767 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000768 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000769 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000770 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000771
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100772 HCurrentMethod* cached_current_method_;
773
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100774 // The ArtMethod this graph is for. Note that for AOT, it may be null,
775 // for example for methods whose declaring class could not be resolved
776 // (such as when the superclass could not be found).
777 ArtMethod* art_method_;
778
David Brazdil4833f5a2015-12-16 10:37:39 +0000779 // Keep the RTI of inexact Object to avoid having to pass stack handle
780 // collection pointer to passes which may create NullConstant.
781 ReferenceTypeInfo inexact_object_rti_;
782
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000783 // Whether we are compiling this graph for on stack replacement: this will
784 // make all loops seen as irreducible and emit special stack maps to mark
785 // compiled code entries which the interpreter can directly jump to.
786 const bool osr_;
787
Mingyao Yang063fc772016-08-02 11:02:54 -0700788 // List of methods that are assumed to have single implementation.
789 ArenaSet<ArtMethod*> cha_single_implementation_list_;
790
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000791 // Whether we are JIT compiling in the shared region area, putting
792 // restrictions on, for example, how literals are being generated.
793 bool is_shared_jit_code_;
794
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000795 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100796 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000797 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000798 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799 DISALLOW_COPY_AND_ASSIGN(HGraph);
800};
801
Vladimir Markof9f64412015-09-02 14:05:49 +0100802class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000803 public:
804 HLoopInformation(HBasicBlock* header, HGraph* graph)
805 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100806 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000807 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100808 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100809 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100810 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100811 blocks_(graph->GetAllocator(),
812 graph->GetBlocks().size(),
813 true,
814 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100815 back_edges_.reserve(kDefaultNumberOfBackEdges);
816 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000818 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100819 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000820
821 void Dump(std::ostream& os);
822
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100823 HBasicBlock* GetHeader() const {
824 return header_;
825 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000826
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000827 void SetHeader(HBasicBlock* block) {
828 header_ = block;
829 }
830
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100831 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
832 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
833 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
834
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000835 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100836 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000837 }
838
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100839 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100840 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100841 }
842
David Brazdil46e2a392015-03-16 17:31:52 +0000843 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100844 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100845 }
846
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000847 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100848 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000849 }
850
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100851 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100853 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100854 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100855 }
856
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100857 // Returns the lifetime position of the back edge that has the
858 // greatest lifetime position.
859 size_t GetLifetimeEnd() const;
860
861 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100862 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100863 }
864
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000865 // Finds blocks that are part of this loop.
866 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100867
Artem Serov7f4aff62017-06-21 17:02:18 +0100868 // Updates blocks population of the loop and all of its outer' ones recursively after the
869 // population of the inner loop is updated.
870 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
871
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872 // Returns whether this loop information contains `block`.
873 // Note that this loop information *must* be populated before entering this function.
874 bool Contains(const HBasicBlock& block) const;
875
876 // Returns whether this loop information is an inner loop of `other`.
877 // Note that `other` *must* be populated before entering this function.
878 bool IsIn(const HLoopInformation& other) const;
879
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800880 // Returns true if instruction is not defined within this loop.
881 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700882
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100883 const ArenaBitVector& GetBlocks() const { return blocks_; }
884
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000885 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000886 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000887
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000888 void ClearAllBlocks() {
889 blocks_.ClearAllBits();
890 }
891
David Brazdil3f4a5222016-05-06 12:46:21 +0100892 bool HasBackEdgeNotDominatedByHeader() const;
893
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100894 bool IsPopulated() const {
895 return blocks_.GetHighestBitSet() != -1;
896 }
897
Anton Shaminf89381f2016-05-16 16:44:13 +0600898 bool DominatesAllBackEdges(HBasicBlock* block);
899
David Sehrc757dec2016-11-04 15:48:34 -0700900 bool HasExitEdge() const;
901
Artem Serov7f4aff62017-06-21 17:02:18 +0100902 // Resets back edge and blocks-in-loop data.
903 void ResetBasicBlockData() {
904 back_edges_.clear();
905 ClearAllBlocks();
906 }
907
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000908 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100909 // Internal recursive implementation of `Populate`.
910 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100911 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100912
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000913 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100914 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000915 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100916 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100917 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100918 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000919
920 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
921};
922
David Brazdilec16f792015-08-19 15:04:01 +0100923// Stores try/catch information for basic blocks.
924// Note that HGraph is constructed so that catch blocks cannot simultaneously
925// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100926class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100927 public:
928 // Try block information constructor.
929 explicit TryCatchInformation(const HTryBoundary& try_entry)
930 : try_entry_(&try_entry),
931 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000932 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100933 DCHECK(try_entry_ != nullptr);
934 }
935
936 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800937 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100938 : try_entry_(nullptr),
939 catch_dex_file_(&dex_file),
940 catch_type_index_(catch_type_index) {}
941
942 bool IsTryBlock() const { return try_entry_ != nullptr; }
943
944 const HTryBoundary& GetTryEntry() const {
945 DCHECK(IsTryBlock());
946 return *try_entry_;
947 }
948
949 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
950
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000951 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100952 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000953 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100954 }
955
Andreas Gampea5b09a62016-11-17 15:21:22 -0800956 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100957 DCHECK(IsCatchBlock());
958 return catch_type_index_;
959 }
960
961 const DexFile& GetCatchDexFile() const {
962 DCHECK(IsCatchBlock());
963 return *catch_dex_file_;
964 }
965
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000966 void SetInvalidTypeIndex() {
967 catch_type_index_ = dex::TypeIndex::Invalid();
968 }
969
David Brazdilec16f792015-08-19 15:04:01 +0100970 private:
971 // One of possibly several TryBoundary instructions entering the block's try.
972 // Only set for try blocks.
973 const HTryBoundary* try_entry_;
974
975 // Exception type information. Only set for catch blocks.
976 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000977 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100978};
979
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100980static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100981static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100982
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983// A block in a method. Contains the list of instructions represented
984// as a double linked list. Each block knows its predecessors and
985// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100986
Vladimir Markof9f64412015-09-02 14:05:49 +0100987class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000988 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700989 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000990 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100991 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
992 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000993 loop_information_(nullptr),
994 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100995 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100996 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100997 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100998 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000999 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001000 try_catch_information_(nullptr) {
1001 predecessors_.reserve(kDefaultNumberOfPredecessors);
1002 successors_.reserve(kDefaultNumberOfSuccessors);
1003 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1004 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001005
Vladimir Marko60584552015-09-03 13:35:12 +00001006 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001007 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008 }
1009
Vladimir Marko60584552015-09-03 13:35:12 +00001010 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001011 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012 }
1013
David Brazdild26a4112015-11-10 11:07:31 +00001014 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1015 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1016
Vladimir Marko60584552015-09-03 13:35:12 +00001017 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1018 return ContainsElement(successors_, block, start_from);
1019 }
1020
1021 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001022 return dominated_blocks_;
1023 }
1024
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001025 bool IsEntryBlock() const {
1026 return graph_->GetEntryBlock() == this;
1027 }
1028
1029 bool IsExitBlock() const {
1030 return graph_->GetExitBlock() == this;
1031 }
1032
David Brazdil46e2a392015-03-16 17:31:52 +00001033 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001034 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001035 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001036 bool IsSingleTryBoundary() const;
1037
1038 // Returns true if this block emits nothing but a jump.
1039 bool IsSingleJump() const {
1040 HLoopInformation* loop_info = GetLoopInformation();
1041 return (IsSingleGoto() || IsSingleTryBoundary())
1042 // Back edges generate a suspend check.
1043 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1044 }
David Brazdil46e2a392015-03-16 17:31:52 +00001045
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001046 void AddBackEdge(HBasicBlock* back_edge) {
1047 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001048 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001049 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001050 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001051 loop_information_->AddBackEdge(back_edge);
1052 }
1053
Artem Serov7f4aff62017-06-21 17:02:18 +01001054 // Registers a back edge; if the block was not a loop header before the call associates a newly
1055 // created loop info with it.
1056 //
1057 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1058 // info for all blocks during back edges recalculation.
1059 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1060 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1061 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1062 }
1063 loop_information_->AddBackEdge(back_edge);
1064 }
1065
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001066 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001067 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001068
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001069 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001070 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001071 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001072
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001073 HBasicBlock* GetDominator() const { return dominator_; }
1074 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001075 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1076
1077 void RemoveDominatedBlock(HBasicBlock* block) {
1078 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001079 }
Vladimir Marko60584552015-09-03 13:35:12 +00001080
1081 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1082 ReplaceElement(dominated_blocks_, existing, new_block);
1083 }
1084
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001085 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001086
1087 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001088 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001089 }
1090
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001091 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1092 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001093 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001094 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001095 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1096 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001098 HInstruction* GetFirstInstructionDisregardMoves() const;
1099
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001100 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001101 successors_.push_back(block);
1102 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001103 }
1104
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001105 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1106 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001107 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001108 new_block->predecessors_.push_back(this);
1109 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001110 }
1111
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001112 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1113 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001114 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001115 new_block->successors_.push_back(this);
1116 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001117 }
1118
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001119 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001120 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001121 // `predecessor` and `successor`.
1122 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1123 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001124 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001125 successor->predecessors_[predecessor_index] = this;
1126 predecessor->successors_[successor_index] = this;
1127 successors_.push_back(successor);
1128 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001129 }
1130
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001131 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001132 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001133 }
1134
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001135 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001136 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001137 }
1138
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001139 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001140 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001141 }
1142
1143 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001144 predecessors_.push_back(block);
1145 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001146 }
1147
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001148 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001149 DCHECK_EQ(predecessors_.size(), 2u);
1150 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001151 }
1152
David Brazdil769c9e52015-04-27 13:54:09 +01001153 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001154 DCHECK_EQ(successors_.size(), 2u);
1155 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001156 }
1157
David Brazdilfc6a86a2015-06-26 10:33:45 +00001158 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001159 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001160 }
1161
David Brazdilfc6a86a2015-06-26 10:33:45 +00001162 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001163 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001164 }
1165
David Brazdilfc6a86a2015-06-26 10:33:45 +00001166 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001167 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001168 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001169 }
1170
1171 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001172 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001173 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001174 }
1175
1176 // Returns whether the first occurrence of `predecessor` in the list of
1177 // predecessors is at index `idx`.
1178 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001179 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001180 return GetPredecessorIndexOf(predecessor) == idx;
1181 }
1182
David Brazdild7558da2015-09-22 13:04:14 +01001183 // Create a new block between this block and its predecessors. The new block
1184 // is added to the graph, all predecessor edges are relinked to it and an edge
1185 // is created to `this`. Returns the new empty block. Reverse post order or
1186 // loop and try/catch information are not updated.
1187 HBasicBlock* CreateImmediateDominator();
1188
David Brazdilfc6a86a2015-06-26 10:33:45 +00001189 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001190 // created, latter block. Note that this method will add the block to the
1191 // graph, create a Goto at the end of the former block and will create an edge
1192 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001193 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001194 HBasicBlock* SplitBefore(HInstruction* cursor);
1195
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001196 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001197 // created block. Note that this method just updates raw block information,
1198 // like predecessors, successors, dominators, and instruction list. It does not
1199 // update the graph, reverse post order, loop information, nor make sure the
1200 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001201 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1202
1203 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1204 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001205
1206 // Merge `other` at the end of `this`. Successors and dominated blocks of
1207 // `other` are changed to be successors and dominated blocks of `this`. Note
1208 // that this method does not update the graph, reverse post order, loop
1209 // information, nor make sure the blocks are consistent (for example ending
1210 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001211 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001212
1213 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1214 // of `this` are moved to `other`.
1215 // Note that this method does not update the graph, reverse post order, loop
1216 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001217 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001218 void ReplaceWith(HBasicBlock* other);
1219
Aart Bik6b69e0a2017-01-11 10:20:43 -08001220 // Merges the instructions of `other` at the end of `this`.
1221 void MergeInstructionsWith(HBasicBlock* other);
1222
David Brazdil2d7352b2015-04-20 14:52:42 +01001223 // Merge `other` at the end of `this`. This method updates loops, reverse post
1224 // order, links to predecessors, successors, dominators and deletes the block
1225 // from the graph. The two blocks must be successive, i.e. `this` the only
1226 // predecessor of `other` and vice versa.
1227 void MergeWith(HBasicBlock* other);
1228
1229 // Disconnects `this` from all its predecessors, successors and dominator,
1230 // removes it from all loops it is included in and eventually from the graph.
1231 // The block must not dominate any other block. Predecessors and successors
1232 // are safely updated.
1233 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001234
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001235 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001236 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001237 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001238 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001239 // Replace phi `initial` with `replacement` within this block.
1240 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001241 // Replace instruction `initial` with `replacement` within this block.
1242 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1243 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001244 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001245 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001246 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1247 // instruction list. With 'ensure_safety' set to true, it verifies that the
1248 // instruction is not in use and removes it from the use lists of its inputs.
1249 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1250 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001251 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001252
1253 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001254 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001255 }
1256
Roland Levillain6b879dd2014-09-22 17:13:44 +01001257 bool IsLoopPreHeaderFirstPredecessor() const {
1258 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001259 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001260 }
1261
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001262 bool IsFirstPredecessorBackEdge() const {
1263 DCHECK(IsLoopHeader());
1264 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1265 }
1266
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001267 HLoopInformation* GetLoopInformation() const {
1268 return loop_information_;
1269 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001270
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001271 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001272 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001273 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001274 void SetInLoop(HLoopInformation* info) {
1275 if (IsLoopHeader()) {
1276 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001277 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001278 loop_information_ = info;
1279 } else if (loop_information_->Contains(*info->GetHeader())) {
1280 // Block is currently part of an outer loop. Make it part of this inner loop.
1281 // Note that a non loop header having a loop information means this loop information
1282 // has already been populated
1283 loop_information_ = info;
1284 } else {
1285 // Block is part of an inner loop. Do not update the loop information.
1286 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1287 // at this point, because this method is being called while populating `info`.
1288 }
1289 }
1290
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001291 // Raw update of the loop information.
1292 void SetLoopInformation(HLoopInformation* info) {
1293 loop_information_ = info;
1294 }
1295
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001296 bool IsInLoop() const { return loop_information_ != nullptr; }
1297
David Brazdilec16f792015-08-19 15:04:01 +01001298 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1299
1300 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1301 try_catch_information_ = try_catch_information;
1302 }
1303
1304 bool IsTryBlock() const {
1305 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1306 }
1307
1308 bool IsCatchBlock() const {
1309 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1310 }
David Brazdilffee3d32015-07-06 11:48:53 +01001311
1312 // Returns the try entry that this block's successors should have. They will
1313 // be in the same try, unless the block ends in a try boundary. In that case,
1314 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001315 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001316
Aart Bik75ff2c92018-04-21 01:28:11 +00001317 bool HasThrowingInstructions() const;
1318
David Brazdila4b8c212015-05-07 09:59:30 +01001319 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001320 bool Dominates(HBasicBlock* block) const;
1321
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001322 size_t GetLifetimeStart() const { return lifetime_start_; }
1323 size_t GetLifetimeEnd() const { return lifetime_end_; }
1324
1325 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1326 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1327
David Brazdil8d5b8b22015-03-24 10:51:52 +00001328 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001329 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001330 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001331 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001332 bool HasSinglePhi() const;
1333
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001334 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001335 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001336 ArenaVector<HBasicBlock*> predecessors_;
1337 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001338 HInstructionList instructions_;
1339 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001340 HLoopInformation* loop_information_;
1341 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001342 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001343 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001344 // The dex program counter of the first instruction of this block.
1345 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001346 size_t lifetime_start_;
1347 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001348 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001349
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001350 friend class HGraph;
1351 friend class HInstruction;
1352
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001353 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1354};
1355
David Brazdilb2bd1c52015-03-25 11:17:37 +00001356// Iterates over the LoopInformation of all loops which contain 'block'
1357// from the innermost to the outermost.
1358class HLoopInformationOutwardIterator : public ValueObject {
1359 public:
1360 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1361 : current_(block.GetLoopInformation()) {}
1362
1363 bool Done() const { return current_ == nullptr; }
1364
1365 void Advance() {
1366 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001367 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001368 }
1369
1370 HLoopInformation* Current() const {
1371 DCHECK(!Done());
1372 return current_;
1373 }
1374
1375 private:
1376 HLoopInformation* current_;
1377
1378 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1379};
1380
Alexandre Ramesef20f712015-06-09 10:29:30 +01001381#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001382 M(Above, Condition) \
1383 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001384 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001385 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001386 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001387 M(ArrayGet, Instruction) \
1388 M(ArrayLength, Instruction) \
1389 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001390 M(Below, Condition) \
1391 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001392 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001393 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001394 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001395 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001396 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001397 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001398 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001400 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001401 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001402 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001403 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001404 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001405 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001406 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001407 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001408 M(Exit, Instruction) \
1409 M(FloatConstant, Constant) \
1410 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001411 M(GreaterThan, Condition) \
1412 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001413 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001414 M(InstanceFieldGet, Instruction) \
1415 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001416 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001417 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001418 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001419 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001420 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001421 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001422 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001423 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001424 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001425 M(LessThan, Condition) \
1426 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001427 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001428 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001429 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001430 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001431 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001432 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001433 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001434 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001435 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001436 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001437 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001438 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001439 M(Neg, UnaryOperation) \
1440 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001441 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001442 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001443 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001444 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001445 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001446 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001447 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001448 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001449 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001450 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001451 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001452 M(Return, Instruction) \
1453 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001454 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001455 M(Shl, BinaryOperation) \
1456 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001457 M(StaticFieldGet, Instruction) \
1458 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001459 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001460 M(UnresolvedInstanceFieldGet, Instruction) \
1461 M(UnresolvedInstanceFieldSet, Instruction) \
1462 M(UnresolvedStaticFieldGet, Instruction) \
1463 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001464 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001465 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001466 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001467 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001468 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001469 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001470 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001471 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001472 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001473 M(VecExtractScalar, VecUnaryOperation) \
1474 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001475 M(VecCnv, VecUnaryOperation) \
1476 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001477 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001478 M(VecNot, VecUnaryOperation) \
1479 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001480 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001481 M(VecSub, VecBinaryOperation) \
1482 M(VecMul, VecBinaryOperation) \
1483 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001484 M(VecMin, VecBinaryOperation) \
1485 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001486 M(VecAnd, VecBinaryOperation) \
1487 M(VecAndNot, VecBinaryOperation) \
1488 M(VecOr, VecBinaryOperation) \
1489 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001490 M(VecSaturationAdd, VecBinaryOperation) \
1491 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001492 M(VecShl, VecBinaryOperation) \
1493 M(VecShr, VecBinaryOperation) \
1494 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001495 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001496 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001497 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001498 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001499 M(VecLoad, VecMemoryOperation) \
1500 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001501
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001502/*
1503 * Instructions, shared across several (not all) architectures.
1504 */
1505#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1506#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1507#else
1508#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001509 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001510 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001511 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001512 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001513#endif
1514
Alexandre Ramesef20f712015-06-09 10:29:30 +01001515#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1516
1517#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1518
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001519#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001520#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001521#else
1522#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1523 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001524 M(MipsPackedSwitch, Instruction) \
1525 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001526#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001527
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001528#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1529
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001530#ifndef ART_ENABLE_CODEGEN_x86
1531#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1532#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001533#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1534 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001535 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001536 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001537 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001538#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001539
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301540#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1541#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301542 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301543 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301544#else
1545#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1546#endif
1547
Alexandre Ramesef20f712015-06-09 10:29:30 +01001548#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1549
1550#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1551 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001552 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001553 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1554 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001555 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001556 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001557 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301558 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1559 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001560
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001561#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1562 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001563 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001564 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001565 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001566 M(Invoke, Instruction) \
1567 M(VecOperation, Instruction) \
1568 M(VecUnaryOperation, VecOperation) \
1569 M(VecBinaryOperation, VecOperation) \
1570 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001571
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001572#define FOR_EACH_INSTRUCTION(M) \
1573 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1574 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1575
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001576#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001577FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1578#undef FORWARD_DECLARATION
1579
Vladimir Marko372f10e2016-05-17 16:30:10 +01001580#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001581 private: \
1582 H##type& operator=(const H##type&) = delete; \
1583 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001584 const char* DebugName() const override { return #type; } \
1585 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001586 DCHECK(IsClonable()); \
1587 return new (arena) H##type(*this->As##type()); \
1588 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001589 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001590
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001591#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001592 private: \
1593 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001594 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001595
Artem Serovcced8ba2017-07-19 18:18:09 +01001596#define DEFAULT_COPY_CONSTRUCTOR(type) \
1597 explicit H##type(const H##type& other) = default;
1598
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001599template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001600class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1601 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001602 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001603 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001604 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001605 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001606 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001607 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001608 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001609
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001610 private:
David Brazdiled596192015-01-23 10:39:45 +00001611 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001612 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001613
1614 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001615 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001616
Vladimir Marko46817b82016-03-29 12:21:58 +01001617 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001618
1619 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1620};
1621
David Brazdiled596192015-01-23 10:39:45 +00001622template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001623using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001624
David Brazdil1abb4192015-02-17 18:33:36 +00001625// This class is used by HEnvironment and HInstruction classes to record the
1626// instructions they use and pointers to the corresponding HUseListNodes kept
1627// by the used instructions.
1628template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001629class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001630 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001631 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1632 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001633
Vladimir Marko46817b82016-03-29 12:21:58 +01001634 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1635 : HUserRecord(old_record.instruction_, before_use_node) {}
1636 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1637 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001638 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001639 }
1640
1641 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001642 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1643 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001644
1645 private:
1646 // Instruction used by the user.
1647 HInstruction* instruction_;
1648
Vladimir Marko46817b82016-03-29 12:21:58 +01001649 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1650 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001651};
1652
Vladimir Markoe9004912016-06-16 16:50:52 +01001653// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1654// This is used for HInstruction::GetInputs() to return a container wrapper providing
1655// HInstruction* values even though the underlying container has HUserRecord<>s.
1656struct HInputExtractor {
1657 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1658 return record.GetInstruction();
1659 }
1660 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1661 return record.GetInstruction();
1662 }
1663};
1664
1665using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1666using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1667
Aart Bik854a02b2015-07-14 16:07:00 -07001668/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001669 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001670 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001671 * For write/read dependences on fields/arrays, the dependence analysis uses
1672 * type disambiguation (e.g. a float field write cannot modify the value of an
1673 * integer field read) and the access type (e.g. a reference array write cannot
1674 * modify the value of a reference field read [although it may modify the
1675 * reference fetch prior to reading the field, which is represented by its own
1676 * write/read dependence]). The analysis makes conservative points-to
1677 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1678 * the same, and any reference read depends on any reference read without
1679 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001680 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001681 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1682 * alive across the point where garbage collection might happen.
1683 *
1684 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1685 *
1686 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1687 * those instructions from the compiler perspective (between this instruction and the next one
1688 * in the IR).
1689 *
1690 * Note: Instructions which can cause GC only on a fatal slow path do not need
1691 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1692 * one. However the execution may return to compiled code if there is a catch block in the
1693 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1694 * set.
1695 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001696 * The internal representation uses 38-bit and is described in the table below.
1697 * The first line indicates the side effect, and for field/array accesses the
1698 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001699 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001700 * The two numbered lines below indicate the bit position in the bitfield (read
1701 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001702 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001703 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1704 * +-------------+---------+---------+--------------+---------+---------+
1705 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1706 * | 3 |333333322|222222221| 1 |111111110|000000000|
1707 * | 7 |654321098|765432109| 8 |765432109|876543210|
1708 *
1709 * Note that, to ease the implementation, 'changes' bits are least significant
1710 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001711 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001712class SideEffects : public ValueObject {
1713 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001714 SideEffects() : flags_(0) {}
1715
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001716 static SideEffects None() {
1717 return SideEffects(0);
1718 }
1719
1720 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 return SideEffects(kAllChangeBits | kAllDependOnBits);
1722 }
1723
1724 static SideEffects AllChanges() {
1725 return SideEffects(kAllChangeBits);
1726 }
1727
1728 static SideEffects AllDependencies() {
1729 return SideEffects(kAllDependOnBits);
1730 }
1731
1732 static SideEffects AllExceptGCDependency() {
1733 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1734 }
1735
1736 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001737 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001738 }
1739
Aart Bik34c3ba92015-07-20 14:08:59 -07001740 static SideEffects AllWrites() {
1741 return SideEffects(kAllWrites);
1742 }
1743
1744 static SideEffects AllReads() {
1745 return SideEffects(kAllReads);
1746 }
1747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001748 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001749 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001750 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001751 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001752 }
1753
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001755 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001756 }
1757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001759 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001760 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001761 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001762 }
1763
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001764 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001765 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001766 }
1767
Artem Serovd1aa7d02018-06-22 11:35:46 +01001768 // Returns whether GC might happen across this instruction from the compiler perspective so
1769 // the next instruction in the IR would see that.
1770 //
1771 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001772 static SideEffects CanTriggerGC() {
1773 return SideEffects(1ULL << kCanTriggerGCBit);
1774 }
1775
Artem Serovd1aa7d02018-06-22 11:35:46 +01001776 // Returns whether the instruction must not be alive across a GC point.
1777 //
1778 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001779 static SideEffects DependsOnGC() {
1780 return SideEffects(1ULL << kDependsOnGCBit);
1781 }
1782
Aart Bik854a02b2015-07-14 16:07:00 -07001783 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001784 SideEffects Union(SideEffects other) const {
1785 return SideEffects(flags_ | other.flags_);
1786 }
1787
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001788 SideEffects Exclusion(SideEffects other) const {
1789 return SideEffects(flags_ & ~other.flags_);
1790 }
1791
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001792 void Add(SideEffects other) {
1793 flags_ |= other.flags_;
1794 }
1795
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001796 bool Includes(SideEffects other) const {
1797 return (other.flags_ & flags_) == other.flags_;
1798 }
1799
1800 bool HasSideEffects() const {
1801 return (flags_ & kAllChangeBits);
1802 }
1803
1804 bool HasDependencies() const {
1805 return (flags_ & kAllDependOnBits);
1806 }
1807
1808 // Returns true if there are no side effects or dependencies.
1809 bool DoesNothing() const {
1810 return flags_ == 0;
1811 }
1812
Aart Bik854a02b2015-07-14 16:07:00 -07001813 // Returns true if something is written.
1814 bool DoesAnyWrite() const {
1815 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001816 }
1817
Aart Bik854a02b2015-07-14 16:07:00 -07001818 // Returns true if something is read.
1819 bool DoesAnyRead() const {
1820 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001821 }
1822
Aart Bik854a02b2015-07-14 16:07:00 -07001823 // Returns true if potentially everything is written and read
1824 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001825 bool DoesAllReadWrite() const {
1826 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1827 }
1828
Aart Bik854a02b2015-07-14 16:07:00 -07001829 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001830 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001831 }
1832
Roland Levillain0d5a2812015-11-13 10:07:31 +00001833 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001834 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001835 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1836 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001837 }
1838
1839 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001840 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001841 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001842 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001843 for (int s = kLastBit; s >= 0; s--) {
1844 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1845 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1846 // This is a bit for the GC side effect.
1847 if (current_bit_is_set) {
1848 flags += "GC";
1849 }
Aart Bik854a02b2015-07-14 16:07:00 -07001850 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001851 } else {
1852 // This is a bit for the array/field analysis.
1853 // The underscore character stands for the 'can trigger GC' bit.
1854 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1855 if (current_bit_is_set) {
1856 flags += kDebug[s];
1857 }
1858 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1859 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1860 flags += "|";
1861 }
1862 }
Aart Bik854a02b2015-07-14 16:07:00 -07001863 }
1864 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001865 }
1866
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001867 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001868
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001869 private:
1870 static constexpr int kFieldArrayAnalysisBits = 9;
1871
1872 static constexpr int kFieldWriteOffset = 0;
1873 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1874 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1875 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1876
1877 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1878
1879 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1880 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1881 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1882 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1883
1884 static constexpr int kLastBit = kDependsOnGCBit;
1885 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1886
1887 // Aliases.
1888
1889 static_assert(kChangeBits == kDependOnBits,
1890 "the 'change' bits should match the 'depend on' bits.");
1891
1892 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1893 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1894 static constexpr uint64_t kAllWrites =
1895 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1896 static constexpr uint64_t kAllReads =
1897 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001898
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001899 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001900 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001901 int shift;
1902 switch (type) {
1903 case DataType::Type::kReference: shift = 0; break;
1904 case DataType::Type::kBool: shift = 1; break;
1905 case DataType::Type::kInt8: shift = 2; break;
1906 case DataType::Type::kUint16: shift = 3; break;
1907 case DataType::Type::kInt16: shift = 4; break;
1908 case DataType::Type::kInt32: shift = 5; break;
1909 case DataType::Type::kInt64: shift = 6; break;
1910 case DataType::Type::kFloat32: shift = 7; break;
1911 case DataType::Type::kFloat64: shift = 8; break;
1912 default:
1913 LOG(FATAL) << "Unexpected data type " << type;
1914 UNREACHABLE();
1915 }
Aart Bik854a02b2015-07-14 16:07:00 -07001916 DCHECK_LE(kFieldWriteOffset, shift);
1917 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001918 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001919 }
1920
1921 // Private constructor on direct flags value.
1922 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1923
1924 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001925};
1926
David Brazdiled596192015-01-23 10:39:45 +00001927// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001928class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001929 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001930 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001931 size_t number_of_vregs,
1932 ArtMethod* method,
1933 uint32_t dex_pc,
1934 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001935 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1936 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001937 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001938 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001939 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001940 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001941 }
1942
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001943 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1944 const HEnvironment& to_copy,
1945 HInstruction* holder)
1946 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001947 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001948 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001949 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001950 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001951
Vladimir Markoec32f642017-06-02 10:51:55 +01001952 void AllocateLocations() {
1953 DCHECK(locations_.empty());
1954 locations_.resize(vregs_.size());
1955 }
1956
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001957 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001958 if (parent_ != nullptr) {
1959 parent_->SetAndCopyParentChain(allocator, parent);
1960 } else {
1961 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1962 parent_->CopyFrom(parent);
1963 if (parent->GetParent() != nullptr) {
1964 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1965 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001966 }
David Brazdiled596192015-01-23 10:39:45 +00001967 }
1968
Vladimir Marko69d310e2017-10-09 14:12:23 +01001969 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001970 void CopyFrom(HEnvironment* environment);
1971
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001972 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1973 // input to the loop phi instead. This is for inserting instructions that
1974 // require an environment (like HDeoptimization) in the loop pre-header.
1975 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001976
1977 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001978 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001979 }
1980
1981 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001982 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001983 }
1984
David Brazdil1abb4192015-02-17 18:33:36 +00001985 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001986
Artem Serovca210e32017-12-15 13:43:20 +00001987 // Replaces the input at the position 'index' with the replacement; the replacement and old
1988 // input instructions' env_uses_ lists are adjusted. The function works similar to
1989 // HInstruction::ReplaceInput.
1990 void ReplaceInput(HInstruction* replacement, size_t index);
1991
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001992 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001993
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001994 HEnvironment* GetParent() const { return parent_; }
1995
1996 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001997 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001998 }
1999
2000 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002001 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002002 }
2003
2004 uint32_t GetDexPc() const {
2005 return dex_pc_;
2006 }
2007
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002008 ArtMethod* GetMethod() const {
2009 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002010 }
2011
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002012 HInstruction* GetHolder() const {
2013 return holder_;
2014 }
2015
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002016
2017 bool IsFromInlinedInvoke() const {
2018 return GetParent() != nullptr;
2019 }
2020
David Brazdiled596192015-01-23 10:39:45 +00002021 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002022 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2023 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002024 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002025 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002026 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002027
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002028 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002029 HInstruction* const holder_;
2030
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002031 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002032
2033 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2034};
2035
Vladimir Markof9f64412015-09-02 14:05:49 +01002036class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002037 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302038#define DECLARE_KIND(type, super) k##type,
2039 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002040 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302041 kLastInstructionKind
2042 };
2043#undef DECLARE_KIND
2044
2045 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002046 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2047
2048 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002049 : previous_(nullptr),
2050 next_(nullptr),
2051 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002052 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002053 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002054 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002055 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002056 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002057 locations_(nullptr),
2058 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002059 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002060 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002061 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302062 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002063 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002064 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2065 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002066
Dave Allison20dfc792014-06-16 20:44:29 -07002067 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002068
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002069
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002070 HInstruction* GetNext() const { return next_; }
2071 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002072
Calin Juravle77520bc2015-01-12 18:45:46 +00002073 HInstruction* GetNextDisregardingMoves() const;
2074 HInstruction* GetPreviousDisregardingMoves() const;
2075
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002076 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002077 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002078 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002079 bool IsInBlock() const { return block_ != nullptr; }
2080 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002081 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2082 bool IsIrreducibleLoopHeaderPhi() const {
2083 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2084 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002085
Vladimir Marko372f10e2016-05-17 16:30:10 +01002086 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2087
2088 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2089 // One virtual method is enough, just const_cast<> and then re-add the const.
2090 return ArrayRef<const HUserRecord<HInstruction*>>(
2091 const_cast<HInstruction*>(this)->GetInputRecords());
2092 }
2093
Vladimir Markoe9004912016-06-16 16:50:52 +01002094 HInputsRef GetInputs() {
2095 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002096 }
2097
Vladimir Markoe9004912016-06-16 16:50:52 +01002098 HConstInputsRef GetInputs() const {
2099 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002100 }
2101
2102 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002103 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002104
Aart Bik2767f4b2016-10-28 15:03:53 -07002105 bool HasInput(HInstruction* input) const {
2106 for (const HInstruction* i : GetInputs()) {
2107 if (i == input) {
2108 return true;
2109 }
2110 }
2111 return false;
2112 }
2113
Vladimir Marko372f10e2016-05-17 16:30:10 +01002114 void SetRawInputAt(size_t index, HInstruction* input) {
2115 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2116 }
2117
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002118 virtual void Accept(HGraphVisitor* visitor) = 0;
2119 virtual const char* DebugName() const = 0;
2120
Vladimir Markobd785672018-05-03 17:09:09 +01002121 DataType::Type GetType() const {
2122 return TypeField::Decode(GetPackedFields());
2123 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002124
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002125 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002126
2127 uint32_t GetDexPc() const { return dex_pc_; }
2128
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002129 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002130
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002131 // Can the instruction throw?
2132 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2133 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002134 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002135
2136 // Does the instruction always throw an exception unconditionally?
2137 virtual bool AlwaysThrows() const { return false; }
2138
David Brazdilec16f792015-08-19 15:04:01 +01002139 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002140
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002141 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002142 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002143
Calin Juravle10e244f2015-01-26 18:54:32 +00002144 // Does not apply for all instructions, but having this at top level greatly
2145 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002146 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002147 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002148 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002149 return true;
2150 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002151
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002152 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002153 return false;
2154 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002155
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002156 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2157 // with it. Otherwise return null.
2158 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002159 // Go over previous non-move instructions that are emitted at use site.
2160 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2161 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2162 if (prev_not_move->IsNullCheck()) {
2163 return prev_not_move->AsNullCheck();
2164 }
2165 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002166 }
2167 return nullptr;
2168 }
2169
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002170 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002171 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002172 }
2173
Calin Juravle2e768302015-07-28 14:41:11 +00002174 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002175
Calin Juravle61d544b2015-02-23 16:46:57 +00002176 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002177 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002178 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002179 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002180 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002181
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002182 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002183 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002184 // Note: fixup_end remains valid across push_front().
2185 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2186 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002187 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002188 uses_.push_front(*new_node);
2189 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002190 }
2191
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002193 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002194 // Note: env_fixup_end remains valid across push_front().
2195 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2196 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002197 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002198 env_uses_.push_front(*new_node);
2199 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002200 }
2201
David Brazdil1abb4192015-02-17 18:33:36 +00002202 void RemoveAsUserOfInput(size_t input) {
2203 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002204 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);
David Brazdil1abb4192015-02-17 18:33:36 +00002207 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002208
Vladimir Marko372f10e2016-05-17 16:30:10 +01002209 void RemoveAsUserOfAllInputs() {
2210 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2211 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2212 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2213 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2214 }
2215 }
2216
David Brazdil1abb4192015-02-17 18:33:36 +00002217 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2218 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002219
Vladimir Marko46817b82016-03-29 12:21:58 +01002220 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2221 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2222 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002223 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002224 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002225 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226
Aart Bikcc42be02016-10-20 16:14:16 -07002227 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002228 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002229 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002230 !CanThrow() &&
2231 !IsSuspendCheck() &&
2232 !IsControlFlow() &&
2233 !IsNativeDebugInfo() &&
2234 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002235 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002236 !IsMemoryBarrier() &&
2237 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002238 }
2239
Aart Bikcc42be02016-10-20 16:14:16 -07002240 bool IsDeadAndRemovable() const {
2241 return IsRemovable() && !HasUses();
2242 }
2243
Roland Levillain6c82d402014-10-13 16:10:27 +01002244 // Does this instruction strictly dominate `other_instruction`?
2245 // Returns false if this instruction and `other_instruction` are the same.
2246 // Aborts if this instruction and `other_instruction` are both phis.
2247 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002248
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002249 int GetId() const { return id_; }
2250 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002251
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002252 int GetSsaIndex() const { return ssa_index_; }
2253 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2254 bool HasSsaIndex() const { return ssa_index_ != -1; }
2255
2256 bool HasEnvironment() const { return environment_ != nullptr; }
2257 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002258 // Set the `environment_` field. Raw because this method does not
2259 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002260 void SetRawEnvironment(HEnvironment* environment) {
2261 DCHECK(environment_ == nullptr);
2262 DCHECK_EQ(environment->GetHolder(), this);
2263 environment_ = environment;
2264 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002265
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002266 void InsertRawEnvironment(HEnvironment* environment) {
2267 DCHECK(environment_ != nullptr);
2268 DCHECK_EQ(environment->GetHolder(), this);
2269 DCHECK(environment->GetParent() == nullptr);
2270 environment->parent_ = environment_;
2271 environment_ = environment;
2272 }
2273
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002274 void RemoveEnvironment();
2275
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002276 // Set the environment of this instruction, copying it from `environment`. While
2277 // copying, the uses lists are being updated.
2278 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002279 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002280 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002281 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002282 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002283 if (environment->GetParent() != nullptr) {
2284 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2285 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002286 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002287
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002288 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2289 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002290 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002291 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002292 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002293 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002294 if (environment->GetParent() != nullptr) {
2295 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2296 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002297 }
2298
Nicolas Geoffray39468442014-09-02 15:17:15 +01002299 // Returns the number of entries in the environment. Typically, that is the
2300 // number of dex registers in a method. It could be more in case of inlining.
2301 size_t EnvironmentSize() const;
2302
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002303 LocationSummary* GetLocations() const { return locations_; }
2304 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002305
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002306 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002307 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002308 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002309 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002310
Alexandre Rames188d4312015-04-09 18:30:21 +01002311 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2312 // uses of this instruction by `other` are *not* updated.
2313 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2314 ReplaceWith(other);
2315 other->ReplaceInput(this, use_index);
2316 }
2317
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002318 // Move `this` instruction before `cursor`
2319 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002320
Vladimir Markofb337ea2015-11-25 15:25:10 +00002321 // Move `this` before its first user and out of any loops. If there is no
2322 // out-of-loop user that dominates all other users, move the instruction
2323 // to the end of the out-of-loop common dominator of the user's blocks.
2324 //
2325 // This can be used only on non-throwing instructions with no side effects that
2326 // have at least one use but no environment uses.
2327 void MoveBeforeFirstUserAndOutOfLoops();
2328
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002329#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002330 bool Is##type() const;
2331
2332 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2333#undef INSTRUCTION_TYPE_CHECK
2334
2335#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002336 const H##type* As##type() const; \
2337 H##type* As##type();
2338
Vladimir Markoa90dd512018-05-04 15:04:45 +01002339 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2340#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002341
Artem Serovcced8ba2017-07-19 18:18:09 +01002342 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2343 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2344 // the instruction then the behaviour of this function is undefined.
2345 //
2346 // Note: It is semantically valid to create a clone of the instruction only until
2347 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2348 // copied.
2349 //
2350 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2351 // 'explicit HInstruction(const HInstruction& other)' for details.
2352 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2353 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2354 DebugName() << " " << GetId();
2355 UNREACHABLE();
2356 }
2357
2358 // Return whether instruction can be cloned (copied).
2359 virtual bool IsClonable() const { return false; }
2360
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002361 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002362 // TODO: this method is used by LICM and GVN with possibly different
2363 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002364 virtual bool CanBeMoved() const { return false; }
2365
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002366 // Returns whether any data encoded in the two instructions is equal.
2367 // This method does not look at the inputs. Both instructions must be
2368 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002369 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002370 return false;
2371 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002372
2373 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002374 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002375 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002376 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002377
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302378 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002379
2380 virtual size_t ComputeHashCode() const {
2381 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002382 for (const HInstruction* input : GetInputs()) {
2383 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002384 }
2385 return result;
2386 }
2387
2388 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002389 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002390 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002391
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002392 size_t GetLifetimePosition() const { return lifetime_position_; }
2393 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2394 LiveInterval* GetLiveInterval() const { return live_interval_; }
2395 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2396 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2397
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002398 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2399
2400 // Returns whether the code generation of the instruction will require to have access
2401 // to the current method. Such instructions are:
2402 // (1): Instructions that require an environment, as calling the runtime requires
2403 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002404 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2405 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002406 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002407 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002408 }
2409
Vladimir Markodc151b22015-10-15 18:02:30 +01002410 // Returns whether the code generation of the instruction will require to have access
2411 // to the dex cache of the current method's declaring class via the current method.
2412 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002413
Mark Mendellc4701932015-04-10 13:18:51 -04002414 // Does this instruction have any use in an environment before
2415 // control flow hits 'other'?
2416 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2417
2418 // Remove all references to environment uses of this instruction.
2419 // The caller must ensure that this is safe to do.
2420 void RemoveEnvironmentUsers();
2421
Vladimir Markoa1de9182016-02-25 11:37:38 +00002422 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2423 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002424
David Brazdil1abb4192015-02-17 18:33:36 +00002425 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002426 // If set, the machine code for this instruction is assumed to be generated by
2427 // its users. Used by liveness analysis to compute use positions accordingly.
2428 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2429 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302430 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2431 static constexpr size_t kFieldInstructionKindSize =
2432 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002433 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302434 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002435 static constexpr size_t kFieldTypeSize =
2436 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2437 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002438 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2439
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302440 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2441 "Too many generic packed fields");
2442
Vladimir Markobd785672018-05-03 17:09:09 +01002443 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2444
Vladimir Marko372f10e2016-05-17 16:30:10 +01002445 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2446 return GetInputRecords()[i];
2447 }
2448
2449 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2450 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2451 input_records[index] = input;
2452 }
David Brazdil1abb4192015-02-17 18:33:36 +00002453
Vladimir Markoa1de9182016-02-25 11:37:38 +00002454 uint32_t GetPackedFields() const {
2455 return packed_fields_;
2456 }
2457
2458 template <size_t flag>
2459 bool GetPackedFlag() const {
2460 return (packed_fields_ & (1u << flag)) != 0u;
2461 }
2462
2463 template <size_t flag>
2464 void SetPackedFlag(bool value = true) {
2465 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2466 }
2467
2468 template <typename BitFieldType>
2469 typename BitFieldType::value_type GetPackedField() const {
2470 return BitFieldType::Decode(packed_fields_);
2471 }
2472
2473 template <typename BitFieldType>
2474 void SetPackedField(typename BitFieldType::value_type value) {
2475 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2476 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2477 }
2478
Artem Serovcced8ba2017-07-19 18:18:09 +01002479 // Copy construction for the instruction (used for Clone function).
2480 //
2481 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2482 // prepare_for_register_allocator are not copied (set to default values).
2483 //
2484 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2485 // fine for most of them. However for some of the instructions a custom copy constructor must be
2486 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2487 // for copying them).
2488 explicit HInstruction(const HInstruction& other)
2489 : previous_(nullptr),
2490 next_(nullptr),
2491 block_(nullptr),
2492 dex_pc_(other.dex_pc_),
2493 id_(-1),
2494 ssa_index_(-1),
2495 packed_fields_(other.packed_fields_),
2496 environment_(nullptr),
2497 locations_(nullptr),
2498 live_interval_(nullptr),
2499 lifetime_position_(kNoLifetime),
2500 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302501 reference_type_handle_(other.reference_type_handle_) {
2502 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002503
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002504 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302505 using InstructionKindField =
2506 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2507
Vladimir Marko46817b82016-03-29 12:21:58 +01002508 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2509 auto before_use_node = uses_.before_begin();
2510 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2511 HInstruction* user = use_node->GetUser();
2512 size_t input_index = use_node->GetIndex();
2513 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2514 before_use_node = use_node;
2515 }
2516 }
2517
2518 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2519 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2520 if (next != uses_.end()) {
2521 HInstruction* next_user = next->GetUser();
2522 size_t next_index = next->GetIndex();
2523 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2524 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2525 }
2526 }
2527
2528 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2529 auto before_env_use_node = env_uses_.before_begin();
2530 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2531 HEnvironment* user = env_use_node->GetUser();
2532 size_t input_index = env_use_node->GetIndex();
2533 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2534 before_env_use_node = env_use_node;
2535 }
2536 }
2537
2538 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2539 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2540 if (next != env_uses_.end()) {
2541 HEnvironment* next_user = next->GetUser();
2542 size_t next_index = next->GetIndex();
2543 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2544 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2545 }
2546 }
David Brazdil1abb4192015-02-17 18:33:36 +00002547
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002548 HInstruction* previous_;
2549 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002550 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002551 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002552
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002553 // An instruction gets an id when it is added to the graph.
2554 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002555 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002556 int id_;
2557
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002558 // When doing liveness analysis, instructions that have uses get an SSA index.
2559 int ssa_index_;
2560
Vladimir Markoa1de9182016-02-25 11:37:38 +00002561 // Packed fields.
2562 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002563
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002564 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002565 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002566
2567 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002568 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002569
Nicolas Geoffray39468442014-09-02 15:17:15 +01002570 // The environment associated with this instruction. Not null if the instruction
2571 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002572 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002573
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002574 // Set by the code generator.
2575 LocationSummary* locations_;
2576
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002577 // Set by the liveness analysis.
2578 LiveInterval* live_interval_;
2579
2580 // Set by the liveness analysis, this is the position in a linear
2581 // order of blocks where this instruction's live interval start.
2582 size_t lifetime_position_;
2583
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002584 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002585
Vladimir Markoa1de9182016-02-25 11:37:38 +00002586 // The reference handle part of the reference type info.
2587 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002588 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002589 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002590
David Brazdil1abb4192015-02-17 18:33:36 +00002591 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002592 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002593 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002594 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002595 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002596};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002597std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002598
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002599// Iterates over the instructions, while preserving the next instruction
2600// in case the current instruction gets removed from the list by the user
2601// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002602class HInstructionIterator : public ValueObject {
2603 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002604 explicit HInstructionIterator(const HInstructionList& instructions)
2605 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002606 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002607 }
2608
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002609 bool Done() const { return instruction_ == nullptr; }
2610 HInstruction* Current() const { return instruction_; }
2611 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002612 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002613 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002614 }
2615
2616 private:
2617 HInstruction* instruction_;
2618 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002619
2620 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002621};
2622
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002623// Iterates over the instructions without saving the next instruction,
2624// therefore handling changes in the graph potentially made by the user
2625// of this iterator.
2626class HInstructionIteratorHandleChanges : public ValueObject {
2627 public:
2628 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2629 : instruction_(instructions.first_instruction_) {
2630 }
2631
2632 bool Done() const { return instruction_ == nullptr; }
2633 HInstruction* Current() const { return instruction_; }
2634 void Advance() {
2635 instruction_ = instruction_->GetNext();
2636 }
2637
2638 private:
2639 HInstruction* instruction_;
2640
2641 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2642};
2643
2644
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002645class HBackwardInstructionIterator : public ValueObject {
2646 public:
2647 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2648 : instruction_(instructions.last_instruction_) {
2649 next_ = Done() ? nullptr : instruction_->GetPrevious();
2650 }
2651
2652 bool Done() const { return instruction_ == nullptr; }
2653 HInstruction* Current() const { return instruction_; }
2654 void Advance() {
2655 instruction_ = next_;
2656 next_ = Done() ? nullptr : instruction_->GetPrevious();
2657 }
2658
2659 private:
2660 HInstruction* instruction_;
2661 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002662
2663 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002664};
2665
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002666class HVariableInputSizeInstruction : public HInstruction {
2667 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002668 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002669 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002670 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2671 }
2672
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002673 void AddInput(HInstruction* input);
2674 void InsertInputAt(size_t index, HInstruction* input);
2675 void RemoveInputAt(size_t index);
2676
Igor Murashkind01745e2017-04-05 16:40:31 -07002677 // Removes all the inputs.
2678 // Also removes this instructions from each input's use list
2679 // (for non-environment uses only).
2680 void RemoveAllInputs();
2681
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002682 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302683 HVariableInputSizeInstruction(InstructionKind inst_kind,
2684 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002685 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002686 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002687 size_t number_of_inputs,
2688 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302689 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002690 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002691 HVariableInputSizeInstruction(InstructionKind inst_kind,
2692 DataType::Type type,
2693 SideEffects side_effects,
2694 uint32_t dex_pc,
2695 ArenaAllocator* allocator,
2696 size_t number_of_inputs,
2697 ArenaAllocKind kind)
2698 : HInstruction(inst_kind, type, side_effects, dex_pc),
2699 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002700
Artem Serovcced8ba2017-07-19 18:18:09 +01002701 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002702
Artem Serovcced8ba2017-07-19 18:18:09 +01002703 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002704};
2705
Vladimir Markof9f64412015-09-02 14:05:49 +01002706template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002707class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002708 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002709 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302710 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002711 HExpression<N>(InstructionKind kind,
2712 DataType::Type type,
2713 SideEffects side_effects,
2714 uint32_t dex_pc)
2715 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2716 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002717
Vladimir Marko372f10e2016-05-17 16:30:10 +01002718 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002719 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002720 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002721 }
2722
Artem Serovcced8ba2017-07-19 18:18:09 +01002723 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002724 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002725
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002726 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002727 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002728
2729 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002730};
2731
Vladimir Markobd785672018-05-03 17:09:09 +01002732// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002733template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002734class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002735 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002736 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002737
Vladimir Markobd785672018-05-03 17:09:09 +01002738 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002739
Vladimir Marko372f10e2016-05-17 16:30:10 +01002740 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002741 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002742 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002743 }
2744
Artem Serovcced8ba2017-07-19 18:18:09 +01002745 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002746 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002747
Vladimir Markof9f64412015-09-02 14:05:49 +01002748 private:
2749 friend class SsaBuilder;
2750};
2751
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002752// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2753// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002754class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002755 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002757 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302758 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002760 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002761
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002762 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002763
Artem Serovcced8ba2017-07-19 18:18:09 +01002764 protected:
2765 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002766};
2767
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002768// Represents dex's RETURN opcodes. A HReturn is a control flow
2769// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002770class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002771 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002772 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002773 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002774 SetRawInputAt(0, value);
2775 }
2776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002777 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002778
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002779 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002780
Artem Serovcced8ba2017-07-19 18:18:09 +01002781 protected:
2782 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002783};
2784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002785class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002786 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002787 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002788 uint32_t reg_number,
2789 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002790 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002791 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002792 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302793 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002794 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002795 SideEffects::None(),
2796 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002797 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002798 number_of_inputs,
2799 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002800 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002802 // Phis are constructed live and marked dead if conflicting or unused.
2803 // Individual steps of SsaBuilder should assume that if a phi has been
2804 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2805 SetPackedFlag<kFlagIsLive>(true);
2806 SetPackedFlag<kFlagCanBeNull>(true);
2807 }
2808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002809 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002810
David Brazdildee58d62016-04-07 09:54:26 +00002811 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002812 static DataType::Type ToPhiType(DataType::Type type) {
2813 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002814 }
2815
2816 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002819 // Make sure that only valid type changes occur. The following are allowed:
2820 // (1) int -> float/ref (primitive type propagation),
2821 // (2) long -> double (primitive type propagation).
2822 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2824 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2825 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002826 SetPackedField<TypeField>(new_type);
2827 }
2828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002829 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002830 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2831
2832 uint32_t GetRegNumber() const { return reg_number_; }
2833
2834 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2835 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2836 bool IsDead() const { return !IsLive(); }
2837 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2838
Vladimir Markoe9004912016-06-16 16:50:52 +01002839 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002840 return other != nullptr
2841 && other->IsPhi()
2842 && other->AsPhi()->GetBlock() == GetBlock()
2843 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2844 }
2845
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002846 bool HasEquivalentPhi() const {
2847 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2848 return true;
2849 }
2850 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2851 return true;
2852 }
2853 return false;
2854 }
2855
David Brazdildee58d62016-04-07 09:54:26 +00002856 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2857 // An equivalent phi is a phi having the same dex register and type.
2858 // It assumes that phis with the same dex register are adjacent.
2859 HPhi* GetNextEquivalentPhiWithSameType() {
2860 HInstruction* next = GetNext();
2861 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2862 if (next->GetType() == GetType()) {
2863 return next->AsPhi();
2864 }
2865 next = next->GetNext();
2866 }
2867 return nullptr;
2868 }
2869
2870 DECLARE_INSTRUCTION(Phi);
2871
Artem Serovcced8ba2017-07-19 18:18:09 +01002872 protected:
2873 DEFAULT_COPY_CONSTRUCTOR(Phi);
2874
David Brazdildee58d62016-04-07 09:54:26 +00002875 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002876 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002877 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2878 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2879 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002880
David Brazdildee58d62016-04-07 09:54:26 +00002881 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002882};
2883
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002884// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002885// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002886// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002887class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002888 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302889 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002890 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302891 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002892
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002893 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002894
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002895 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002896
Artem Serovcced8ba2017-07-19 18:18:09 +01002897 protected:
2898 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002899};
2900
2901// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002902class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002903 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302904 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002905 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302906 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002907
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002908 bool IsClonable() const override { return true; }
2909 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002910
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002911 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002912 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002913 }
2914
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002915 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002916
Artem Serovcced8ba2017-07-19 18:18:09 +01002917 protected:
2918 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002919};
2920
Roland Levillain9867bc72015-08-05 10:21:34 +01002921class HConstant : public HExpression<0> {
2922 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302923 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2924 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2925 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002926
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002927 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002928
Roland Levillain1a653882016-03-18 18:05:57 +00002929 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002930 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002931 // Is this constant 0 in the arithmetic sense?
2932 virtual bool IsArithmeticZero() const { return false; }
2933 // Is this constant a 0-bit pattern?
2934 virtual bool IsZeroBitPattern() const { return false; }
2935 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002936 virtual bool IsOne() const { return false; }
2937
David Brazdil77a48ae2015-09-15 12:34:04 +00002938 virtual uint64_t GetValueAsUint64() const = 0;
2939
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002940 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002941
Artem Serovcced8ba2017-07-19 18:18:09 +01002942 protected:
2943 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002944};
2945
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002946class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002947 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002948 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002949 return true;
2950 }
2951
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002952 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002954 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002955
Roland Levillain1a653882016-03-18 18:05:57 +00002956 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002957 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002958
Roland Levillain9867bc72015-08-05 10:21:34 +01002959 DECLARE_INSTRUCTION(NullConstant);
2960
Artem Serovcced8ba2017-07-19 18:18:09 +01002961 protected:
2962 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2963
Roland Levillain9867bc72015-08-05 10:21:34 +01002964 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302966 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2967 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002968
2969 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002970};
2971
2972// Constants of the type int. Those can be from Dex instructions, or
2973// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002974class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002975 public:
2976 int32_t GetValue() const { return value_; }
2977
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002978 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002979 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2980 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002982 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002983 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002984 return other->AsIntConstant()->value_ == value_;
2985 }
2986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002987 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002989 bool IsMinusOne() const override { return GetValue() == -1; }
2990 bool IsArithmeticZero() const override { return GetValue() == 0; }
2991 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2992 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002993
Roland Levillain1a653882016-03-18 18:05:57 +00002994 // Integer constants are used to encode Boolean values as well,
2995 // where 1 means true and 0 means false.
2996 bool IsTrue() const { return GetValue() == 1; }
2997 bool IsFalse() const { return GetValue() == 0; }
2998
Roland Levillain9867bc72015-08-05 10:21:34 +01002999 DECLARE_INSTRUCTION(IntConstant);
3000
Artem Serovcced8ba2017-07-19 18:18:09 +01003001 protected:
3002 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3003
Roland Levillain9867bc72015-08-05 10:21:34 +01003004 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003005 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303006 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3007 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003008 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303009 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3010 value_(value ? 1 : 0) {
3011 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003012
3013 const int32_t value_;
3014
3015 friend class HGraph;
3016 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3017 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003018};
3019
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003020class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003021 public:
3022 int64_t GetValue() const { return value_; }
3023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003024 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003025
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003026 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003028 return other->AsLongConstant()->value_ == value_;
3029 }
3030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003031 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003033 bool IsMinusOne() const override { return GetValue() == -1; }
3034 bool IsArithmeticZero() const override { return GetValue() == 0; }
3035 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3036 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003037
3038 DECLARE_INSTRUCTION(LongConstant);
3039
Artem Serovcced8ba2017-07-19 18:18:09 +01003040 protected:
3041 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3042
Roland Levillain9867bc72015-08-05 10:21:34 +01003043 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003044 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303045 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3046 value_(value) {
3047 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003048
3049 const int64_t value_;
3050
3051 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003052};
Dave Allison20dfc792014-06-16 20:44:29 -07003053
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003054class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003055 public:
3056 float GetValue() const { return value_; }
3057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003058 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003059 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3060 }
3061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003062 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 DCHECK(other->IsFloatConstant()) << other->DebugName();
3064 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3065 }
3066
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003067 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003069 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003070 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3071 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003072 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003073 return std::fpclassify(value_) == FP_ZERO;
3074 }
3075 bool IsArithmeticPositiveZero() const {
3076 return IsArithmeticZero() && !std::signbit(value_);
3077 }
3078 bool IsArithmeticNegativeZero() const {
3079 return IsArithmeticZero() && std::signbit(value_);
3080 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003081 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003082 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003083 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003084 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003085 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3086 }
3087 bool IsNaN() const {
3088 return std::isnan(value_);
3089 }
3090
3091 DECLARE_INSTRUCTION(FloatConstant);
3092
Artem Serovcced8ba2017-07-19 18:18:09 +01003093 protected:
3094 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3095
Roland Levillain31dd3d62016-02-16 12:21:02 +00003096 private:
3097 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303098 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3099 value_(value) {
3100 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003101 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303102 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3103 value_(bit_cast<float, int32_t>(value)) {
3104 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105
3106 const float value_;
3107
3108 // Only the SsaBuilder and HGraph can create floating-point constants.
3109 friend class SsaBuilder;
3110 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003111};
3112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003113class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003114 public:
3115 double GetValue() const { return value_; }
3116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003117 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003119 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3121 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3122 }
3123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003124 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003126 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003127 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3128 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003129 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003130 return std::fpclassify(value_) == FP_ZERO;
3131 }
3132 bool IsArithmeticPositiveZero() const {
3133 return IsArithmeticZero() && !std::signbit(value_);
3134 }
3135 bool IsArithmeticNegativeZero() const {
3136 return IsArithmeticZero() && std::signbit(value_);
3137 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003138 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003139 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003140 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003141 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003142 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3143 }
3144 bool IsNaN() const {
3145 return std::isnan(value_);
3146 }
3147
3148 DECLARE_INSTRUCTION(DoubleConstant);
3149
Artem Serovcced8ba2017-07-19 18:18:09 +01003150 protected:
3151 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3152
Roland Levillain31dd3d62016-02-16 12:21:02 +00003153 private:
3154 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303155 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3156 value_(value) {
3157 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003158 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303159 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3160 value_(bit_cast<double, int64_t>(value)) {
3161 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003162
3163 const double value_;
3164
3165 // Only the SsaBuilder and HGraph can create floating-point constants.
3166 friend class SsaBuilder;
3167 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003168};
3169
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003170// Conditional branch. A block ending with an HIf instruction must have
3171// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003172class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003173 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003174 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003175 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003176 SetRawInputAt(0, input);
3177 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003179 bool IsClonable() const override { return true; }
3180 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003181
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003182 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003183 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003184 }
3185
3186 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003187 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003188 }
3189
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003190 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003191
Artem Serovcced8ba2017-07-19 18:18:09 +01003192 protected:
3193 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003194};
3195
David Brazdilfc6a86a2015-06-26 10:33:45 +00003196
3197// Abstract instruction which marks the beginning and/or end of a try block and
3198// links it to the respective exception handlers. Behaves the same as a Goto in
3199// non-exceptional control flow.
3200// Normal-flow successor is stored at index zero, exception handlers under
3201// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003202class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003203 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003204 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003205 kEntry,
3206 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003207 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003208 };
3209
Artem Serovd1aa7d02018-06-22 11:35:46 +01003210 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3211 // across the catch block entering edges as GC might happen during throwing an exception.
3212 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3213 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003214 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003215 : HExpression(kTryBoundary,
3216 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3217 : SideEffects::None(),
3218 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003219 SetPackedField<BoundaryKindField>(kind);
3220 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003222 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003223
3224 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003225 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003226
David Brazdild26a4112015-11-10 11:07:31 +00003227 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3228 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3229 }
3230
David Brazdilfc6a86a2015-06-26 10:33:45 +00003231 // Returns whether `handler` is among its exception handlers (non-zero index
3232 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003233 bool HasExceptionHandler(const HBasicBlock& handler) const {
3234 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003235 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003236 }
3237
3238 // If not present already, adds `handler` to its block's list of exception
3239 // handlers.
3240 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003241 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003242 GetBlock()->AddSuccessor(handler);
3243 }
3244 }
3245
Vladimir Markoa1de9182016-02-25 11:37:38 +00003246 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3247 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003248
David Brazdilffee3d32015-07-06 11:48:53 +01003249 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3250
David Brazdilfc6a86a2015-06-26 10:33:45 +00003251 DECLARE_INSTRUCTION(TryBoundary);
3252
Artem Serovcced8ba2017-07-19 18:18:09 +01003253 protected:
3254 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3255
David Brazdilfc6a86a2015-06-26 10:33:45 +00003256 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003257 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3258 static constexpr size_t kFieldBoundaryKindSize =
3259 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3260 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3261 kFieldBoundaryKind + kFieldBoundaryKindSize;
3262 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3263 "Too many packed fields.");
3264 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003265};
3266
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003267// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003268class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003269 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003270 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3271 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003272 HDeoptimize(ArenaAllocator* allocator,
3273 HInstruction* cond,
3274 DeoptimizationKind kind,
3275 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003276 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303277 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003278 SideEffects::All(),
3279 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003280 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003281 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003282 kArenaAllocMisc) {
3283 SetPackedFlag<kFieldCanBeMoved>(false);
3284 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003285 SetRawInputAt(0, cond);
3286 }
3287
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003288 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003289
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003290 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3291 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3292 // instead of `guard`.
3293 // We set CanTriggerGC to prevent any intermediate address to be live
3294 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003295 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003296 HInstruction* cond,
3297 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003298 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003299 uint32_t dex_pc)
3300 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303301 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003302 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003303 SideEffects::CanTriggerGC(),
3304 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003305 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003306 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003307 kArenaAllocMisc) {
3308 SetPackedFlag<kFieldCanBeMoved>(true);
3309 SetPackedField<DeoptimizeKindField>(kind);
3310 SetRawInputAt(0, cond);
3311 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003312 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003314 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003316 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003317 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3318 }
3319
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003320 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003322 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003323
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003324 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003325
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003326 bool GuardsAnInput() const {
3327 return InputCount() == 2;
3328 }
3329
3330 HInstruction* GuardedInput() const {
3331 DCHECK(GuardsAnInput());
3332 return InputAt(1);
3333 }
3334
3335 void RemoveGuard() {
3336 RemoveInputAt(1);
3337 }
3338
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003339 DECLARE_INSTRUCTION(Deoptimize);
3340
Artem Serovcced8ba2017-07-19 18:18:09 +01003341 protected:
3342 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3343
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003344 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003345 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3346 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3347 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003348 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003349 static constexpr size_t kNumberOfDeoptimizePackedBits =
3350 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3351 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3352 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003353 using DeoptimizeKindField =
3354 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003355};
3356
Mingyao Yang063fc772016-08-02 11:02:54 -07003357// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3358// The compiled code checks this flag value in a guard before devirtualized call and
3359// if it's true, starts to do deoptimization.
3360// It has a 4-byte slot on stack.
3361// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003362class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003363 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003364 // CHA guards are only optimized in a separate pass and it has no side effects
3365 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003366 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303367 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003368 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303369 SideEffects::None(),
3370 dex_pc,
3371 allocator,
3372 0,
3373 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003374 }
3375
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003376 // We do all CHA guard elimination/motion in a single pass, after which there is no
3377 // further guard elimination/motion since a guard might have been used for justification
3378 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3379 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003380 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003381
3382 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3383
Artem Serovcced8ba2017-07-19 18:18:09 +01003384 protected:
3385 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003386};
3387
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003388// Represents the ArtMethod that was passed as a first argument to
3389// the method. It is used by instructions that depend on it, like
3390// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003391class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003392 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003393 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303394 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3395 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003396
3397 DECLARE_INSTRUCTION(CurrentMethod);
3398
Artem Serovcced8ba2017-07-19 18:18:09 +01003399 protected:
3400 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003401};
3402
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003403// Fetches an ArtMethod from the virtual table or the interface method table
3404// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003405class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003406 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003407 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003408 kVTable,
3409 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003410 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003411 };
3412 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003413 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003414 TableKind kind,
3415 size_t index,
3416 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303417 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003418 index_(index) {
3419 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003420 SetRawInputAt(0, cls);
3421 }
3422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003423 bool IsClonable() const override { return true; }
3424 bool CanBeMoved() const override { return true; }
3425 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003426 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003427 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003428 }
3429
Vladimir Markoa1de9182016-02-25 11:37:38 +00003430 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003431 size_t GetIndex() const { return index_; }
3432
3433 DECLARE_INSTRUCTION(ClassTableGet);
3434
Artem Serovcced8ba2017-07-19 18:18:09 +01003435 protected:
3436 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3437
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003438 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003439 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003440 static constexpr size_t kFieldTableKindSize =
3441 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3442 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3443 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3444 "Too many packed fields.");
3445 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3446
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003447 // The index of the ArtMethod in the table.
3448 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003449};
3450
Mark Mendellfe57faa2015-09-18 09:26:15 -04003451// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3452// have one successor for each entry in the switch table, and the final successor
3453// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003454class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003455 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003456 HPackedSwitch(int32_t start_value,
3457 uint32_t num_entries,
3458 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003459 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003460 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003461 start_value_(start_value),
3462 num_entries_(num_entries) {
3463 SetRawInputAt(0, input);
3464 }
3465
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003466 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003467
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003468 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003469
3470 int32_t GetStartValue() const { return start_value_; }
3471
Vladimir Marko211c2112015-09-24 16:52:33 +01003472 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003473
3474 HBasicBlock* GetDefaultBlock() const {
3475 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003476 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003477 }
3478 DECLARE_INSTRUCTION(PackedSwitch);
3479
Artem Serovcced8ba2017-07-19 18:18:09 +01003480 protected:
3481 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3482
Mark Mendellfe57faa2015-09-18 09:26:15 -04003483 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003484 const int32_t start_value_;
3485 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003486};
3487
Roland Levillain88cb1752014-10-20 16:36:47 +01003488class HUnaryOperation : public HExpression<1> {
3489 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303490 HUnaryOperation(InstructionKind kind,
3491 DataType::Type result_type,
3492 HInstruction* input,
3493 uint32_t dex_pc = kNoDexPc)
3494 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003495 SetRawInputAt(0, input);
3496 }
3497
Artem Serovcced8ba2017-07-19 18:18:09 +01003498 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003499 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003500
Roland Levillain88cb1752014-10-20 16:36:47 +01003501 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003502 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003503
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003504 bool CanBeMoved() const override { return true; }
3505 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003506 return true;
3507 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003508
Roland Levillain31dd3d62016-02-16 12:21:02 +00003509 // Try to statically evaluate `this` and return a HConstant
3510 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003511 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003512 HConstant* TryStaticEvaluation() const;
3513
3514 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003515 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3516 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003517 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3518 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003519
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003520 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003521
Artem Serovcced8ba2017-07-19 18:18:09 +01003522 protected:
3523 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003524};
3525
Dave Allison20dfc792014-06-16 20:44:29 -07003526class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003527 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303528 HBinaryOperation(InstructionKind kind,
3529 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003530 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003531 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003532 SideEffects side_effects = SideEffects::None(),
3533 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303534 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003535 SetRawInputAt(0, left);
3536 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003537 }
3538
Artem Serovcced8ba2017-07-19 18:18:09 +01003539 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003540 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003541
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003542 HInstruction* GetLeft() const { return InputAt(0); }
3543 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003544 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003545
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003546 virtual bool IsCommutative() const { return false; }
3547
3548 // Put constant on the right.
3549 // Returns whether order is changed.
3550 bool OrderInputsWithConstantOnTheRight() {
3551 HInstruction* left = InputAt(0);
3552 HInstruction* right = InputAt(1);
3553 if (left->IsConstant() && !right->IsConstant()) {
3554 ReplaceInput(right, 0);
3555 ReplaceInput(left, 1);
3556 return true;
3557 }
3558 return false;
3559 }
3560
3561 // Order inputs by instruction id, but favor constant on the right side.
3562 // This helps GVN for commutative ops.
3563 void OrderInputs() {
3564 DCHECK(IsCommutative());
3565 HInstruction* left = InputAt(0);
3566 HInstruction* right = InputAt(1);
3567 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3568 return;
3569 }
3570 if (OrderInputsWithConstantOnTheRight()) {
3571 return;
3572 }
3573 // Order according to instruction id.
3574 if (left->GetId() > right->GetId()) {
3575 ReplaceInput(right, 0);
3576 ReplaceInput(left, 1);
3577 }
3578 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003579
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003580 bool CanBeMoved() const override { return true; }
3581 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003582 return true;
3583 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003584
Roland Levillain31dd3d62016-02-16 12:21:02 +00003585 // Try to statically evaluate `this` and return a HConstant
3586 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003587 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003588 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003589
3590 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3592 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003593 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3594 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003595 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003596 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3597 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003598 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3599 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003600 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3601 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003602 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003603 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3604 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003605
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003606 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003607 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003608 HConstant* GetConstantRight() const;
3609
3610 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003611 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003612 HInstruction* GetLeastConstantLeft() const;
3613
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003614 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003615
Artem Serovcced8ba2017-07-19 18:18:09 +01003616 protected:
3617 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003618};
3619
Mark Mendellc4701932015-04-10 13:18:51 -04003620// The comparison bias applies for floating point operations and indicates how NaN
3621// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003622enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003623 kNoBias, // bias is not applicable (i.e. for long operation)
3624 kGtBias, // return 1 for NaN comparisons
3625 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003626 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003627};
3628
Roland Levillain31dd3d62016-02-16 12:21:02 +00003629std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3630
Dave Allison20dfc792014-06-16 20:44:29 -07003631class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003632 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303633 HCondition(InstructionKind kind,
3634 HInstruction* first,
3635 HInstruction* second,
3636 uint32_t dex_pc = kNoDexPc)
3637 : HBinaryOperation(kind,
3638 DataType::Type::kBool,
3639 first,
3640 second,
3641 SideEffects::None(),
3642 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003643 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3644 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003645
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003646 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003647 // `instruction`, and disregard moves in between.
3648 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003649
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003650 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003651
3652 virtual IfCondition GetCondition() const = 0;
3653
Mark Mendellc4701932015-04-10 13:18:51 -04003654 virtual IfCondition GetOppositeCondition() const = 0;
3655
Vladimir Markoa1de9182016-02-25 11:37:38 +00003656 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003657 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3658
Vladimir Markoa1de9182016-02-25 11:37:38 +00003659 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3660 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003661
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003662 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003663 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003664 }
3665
Roland Levillain4fa13f62015-07-06 18:11:54 +01003666 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003668 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003669 if (if_cond == kCondNE) {
3670 return true;
3671 } else if (if_cond == kCondEQ) {
3672 return false;
3673 }
3674 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003675 }
3676
3677 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003678 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003679 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003680 if (if_cond == kCondEQ) {
3681 return true;
3682 } else if (if_cond == kCondNE) {
3683 return false;
3684 }
3685 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003686 }
3687
Roland Levillain31dd3d62016-02-16 12:21:02 +00003688 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003689 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003690 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003691 static constexpr size_t kFieldComparisonBiasSize =
3692 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3693 static constexpr size_t kNumberOfConditionPackedBits =
3694 kFieldComparisonBias + kFieldComparisonBiasSize;
3695 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3696 using ComparisonBiasField =
3697 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3698
Roland Levillain31dd3d62016-02-16 12:21:02 +00003699 template <typename T>
3700 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3701
3702 template <typename T>
3703 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003704 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003705 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3706 // Handle the bias.
3707 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3708 }
3709
3710 // Return an integer constant containing the result of a condition evaluated at compile time.
3711 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3712 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3713 }
3714
Artem Serovcced8ba2017-07-19 18:18:09 +01003715 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003716};
3717
3718// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003719class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003720 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003721 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303722 : HCondition(kEqual, first, second, dex_pc) {
3723 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003724
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003725 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003726
Vladimir Marko9e23df52015-11-10 17:14:35 +00003727 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003728 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003729 return MakeConstantCondition(true, GetDexPc());
3730 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003731 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003732 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3733 }
3734 // In the following Evaluate methods, a HCompare instruction has
3735 // been merged into this HEqual instruction; evaluate it as
3736 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003737 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003738 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3739 GetDexPc());
3740 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003741 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003742 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3743 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003744 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003745 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003746 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003747
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003748 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003750 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003751 return kCondEQ;
3752 }
3753
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003754 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003755 return kCondNE;
3756 }
3757
Artem Serovcced8ba2017-07-19 18:18:09 +01003758 protected:
3759 DEFAULT_COPY_CONSTRUCTOR(Equal);
3760
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003761 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003762 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003763};
3764
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003765class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003766 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303767 HNotEqual(HInstruction* first, HInstruction* second,
3768 uint32_t dex_pc = kNoDexPc)
3769 : HCondition(kNotEqual, first, second, dex_pc) {
3770 }
Dave Allison20dfc792014-06-16 20:44:29 -07003771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003772 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003773
Vladimir Marko9e23df52015-11-10 17:14:35 +00003774 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003775 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003776 return MakeConstantCondition(false, GetDexPc());
3777 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003778 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003779 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3780 }
3781 // In the following Evaluate methods, a HCompare instruction has
3782 // been merged into this HNotEqual instruction; evaluate it as
3783 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003784 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003785 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3786 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003787 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003788 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3789 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003790 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003791 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003792 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003793
Dave Allison20dfc792014-06-16 20:44:29 -07003794 DECLARE_INSTRUCTION(NotEqual);
3795
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003796 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003797 return kCondNE;
3798 }
3799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003800 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003801 return kCondEQ;
3802 }
3803
Artem Serovcced8ba2017-07-19 18:18:09 +01003804 protected:
3805 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3806
Dave Allison20dfc792014-06-16 20:44:29 -07003807 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003808 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003809};
3810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003811class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003812 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303813 HLessThan(HInstruction* first, HInstruction* second,
3814 uint32_t dex_pc = kNoDexPc)
3815 : HCondition(kLessThan, first, second, dex_pc) {
3816 }
Dave Allison20dfc792014-06-16 20:44:29 -07003817
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003818 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003819 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003820 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003821 // In the following Evaluate methods, a HCompare instruction has
3822 // been merged into this HLessThan instruction; evaluate it as
3823 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003824 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003825 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3826 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003827 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003828 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3829 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003830 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003831 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003832 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003833
Dave Allison20dfc792014-06-16 20:44:29 -07003834 DECLARE_INSTRUCTION(LessThan);
3835
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003836 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003837 return kCondLT;
3838 }
3839
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003840 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003841 return kCondGE;
3842 }
3843
Artem Serovcced8ba2017-07-19 18:18:09 +01003844 protected:
3845 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3846
Dave Allison20dfc792014-06-16 20:44:29 -07003847 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003848 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003849};
3850
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003851class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003852 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303853 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3854 uint32_t dex_pc = kNoDexPc)
3855 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3856 }
Dave Allison20dfc792014-06-16 20:44:29 -07003857
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003858 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003859 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003860 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 // In the following Evaluate methods, a HCompare instruction has
3862 // been merged into this HLessThanOrEqual instruction; evaluate it as
3863 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003864 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003865 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3866 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003867 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003868 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3869 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003870 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003871 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003872 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003873
Dave Allison20dfc792014-06-16 20:44:29 -07003874 DECLARE_INSTRUCTION(LessThanOrEqual);
3875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003876 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003877 return kCondLE;
3878 }
3879
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003880 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003881 return kCondGT;
3882 }
3883
Artem Serovcced8ba2017-07-19 18:18:09 +01003884 protected:
3885 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3886
Dave Allison20dfc792014-06-16 20:44:29 -07003887 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003888 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003889};
3890
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003891class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003892 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003893 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303894 : HCondition(kGreaterThan, first, second, dex_pc) {
3895 }
Dave Allison20dfc792014-06-16 20:44:29 -07003896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003897 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003898 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003899 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003900 // In the following Evaluate methods, a HCompare instruction has
3901 // been merged into this HGreaterThan instruction; evaluate it as
3902 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003903 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003904 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3905 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003906 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003907 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3908 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003909 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003910 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003911 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003912
Dave Allison20dfc792014-06-16 20:44:29 -07003913 DECLARE_INSTRUCTION(GreaterThan);
3914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003915 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003916 return kCondGT;
3917 }
3918
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003919 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003920 return kCondLE;
3921 }
3922
Artem Serovcced8ba2017-07-19 18:18:09 +01003923 protected:
3924 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3925
Dave Allison20dfc792014-06-16 20:44:29 -07003926 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003927 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003928};
3929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003930class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003931 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003932 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303933 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3934 }
Dave Allison20dfc792014-06-16 20:44:29 -07003935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003936 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003937 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003938 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003939 // In the following Evaluate methods, a HCompare instruction has
3940 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3941 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003943 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3944 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003945 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003946 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3947 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003948 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003949 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003950 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003951
Dave Allison20dfc792014-06-16 20:44:29 -07003952 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003954 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003955 return kCondGE;
3956 }
3957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003958 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003959 return kCondLT;
3960 }
3961
Artem Serovcced8ba2017-07-19 18:18:09 +01003962 protected:
3963 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3964
Dave Allison20dfc792014-06-16 20:44:29 -07003965 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003966 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003967};
3968
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003969class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003970 public:
3971 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303972 : HCondition(kBelow, first, second, dex_pc) {
3973 }
Aart Bike9f37602015-10-09 11:15:55 -07003974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003975 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003976 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003977 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003978 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003979 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3980 }
3981 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003982 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003983 LOG(FATAL) << DebugName() << " is not defined for float values";
3984 UNREACHABLE();
3985 }
3986 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003987 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003988 LOG(FATAL) << DebugName() << " is not defined for double values";
3989 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003990 }
3991
3992 DECLARE_INSTRUCTION(Below);
3993
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003994 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003995 return kCondB;
3996 }
3997
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003998 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003999 return kCondAE;
4000 }
4001
Artem Serovcced8ba2017-07-19 18:18:09 +01004002 protected:
4003 DEFAULT_COPY_CONSTRUCTOR(Below);
4004
Aart Bike9f37602015-10-09 11:15:55 -07004005 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004006 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004007 return MakeUnsigned(x) < MakeUnsigned(y);
4008 }
Aart Bike9f37602015-10-09 11:15:55 -07004009};
4010
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004011class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004012 public:
4013 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304014 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4015 }
Aart Bike9f37602015-10-09 11:15:55 -07004016
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004017 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004018 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004019 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004021 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4022 }
4023 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004024 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004025 LOG(FATAL) << DebugName() << " is not defined for float values";
4026 UNREACHABLE();
4027 }
4028 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004029 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004030 LOG(FATAL) << DebugName() << " is not defined for double values";
4031 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004032 }
4033
4034 DECLARE_INSTRUCTION(BelowOrEqual);
4035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004036 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004037 return kCondBE;
4038 }
4039
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004040 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004041 return kCondA;
4042 }
4043
Artem Serovcced8ba2017-07-19 18:18:09 +01004044 protected:
4045 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4046
Aart Bike9f37602015-10-09 11:15:55 -07004047 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004048 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004049 return MakeUnsigned(x) <= MakeUnsigned(y);
4050 }
Aart Bike9f37602015-10-09 11:15:55 -07004051};
4052
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004053class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004054 public:
4055 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304056 : HCondition(kAbove, first, second, dex_pc) {
4057 }
Aart Bike9f37602015-10-09 11:15:55 -07004058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004059 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004060 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004061 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004062 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004063 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4064 }
4065 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004066 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004067 LOG(FATAL) << DebugName() << " is not defined for float values";
4068 UNREACHABLE();
4069 }
4070 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004071 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004072 LOG(FATAL) << DebugName() << " is not defined for double values";
4073 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004074 }
4075
4076 DECLARE_INSTRUCTION(Above);
4077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004078 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004079 return kCondA;
4080 }
4081
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004082 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004083 return kCondBE;
4084 }
4085
Artem Serovcced8ba2017-07-19 18:18:09 +01004086 protected:
4087 DEFAULT_COPY_CONSTRUCTOR(Above);
4088
Aart Bike9f37602015-10-09 11:15:55 -07004089 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004090 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004091 return MakeUnsigned(x) > MakeUnsigned(y);
4092 }
Aart Bike9f37602015-10-09 11:15:55 -07004093};
4094
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004095class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004096 public:
4097 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304098 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4099 }
Aart Bike9f37602015-10-09 11:15:55 -07004100
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004101 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004102 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004103 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004104 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004105 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4106 }
4107 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004108 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004109 LOG(FATAL) << DebugName() << " is not defined for float values";
4110 UNREACHABLE();
4111 }
4112 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004113 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004114 LOG(FATAL) << DebugName() << " is not defined for double values";
4115 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004116 }
4117
4118 DECLARE_INSTRUCTION(AboveOrEqual);
4119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004120 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004121 return kCondAE;
4122 }
4123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004124 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004125 return kCondB;
4126 }
4127
Artem Serovcced8ba2017-07-19 18:18:09 +01004128 protected:
4129 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4130
Aart Bike9f37602015-10-09 11:15:55 -07004131 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004132 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004133 return MakeUnsigned(x) >= MakeUnsigned(y);
4134 }
Aart Bike9f37602015-10-09 11:15:55 -07004135};
Dave Allison20dfc792014-06-16 20:44:29 -07004136
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004137// Instruction to check how two inputs compare to each other.
4138// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004139class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004140 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004141 // Note that `comparison_type` is the type of comparison performed
4142 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004143 // HCompare instruction (which is always DataType::Type::kInt).
4144 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004145 HInstruction* first,
4146 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004147 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004148 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304149 : HBinaryOperation(kCompare,
4150 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004151 first,
4152 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004153 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004154 dex_pc) {
4155 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004156 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4157 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004158 }
4159
Roland Levillain9867bc72015-08-05 10:21:34 +01004160 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004161 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4162
4163 template <typename T>
4164 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004165 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004166 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4167 // Handle the bias.
4168 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4169 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004170
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004171 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004172 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4173 // reach this code path when processing a freshly built HIR
4174 // graph. However HCompare integer instructions can be synthesized
4175 // by the instruction simplifier to implement IntegerCompare and
4176 // IntegerSignum intrinsics, so we have to handle this case.
4177 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004178 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004179 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004180 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4181 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004182 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004183 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4184 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004185 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004186 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004187 }
4188
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004189 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004190 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004191 }
4192
Vladimir Markoa1de9182016-02-25 11:37:38 +00004193 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004194
Roland Levillain31dd3d62016-02-16 12:21:02 +00004195 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004196 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004197 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004198 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004199 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004200 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004202 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004203 // Comparisons do not require a runtime call in any back end.
4204 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004205 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004206
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004207 DECLARE_INSTRUCTION(Compare);
4208
Roland Levillain31dd3d62016-02-16 12:21:02 +00004209 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004210 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004211 static constexpr size_t kFieldComparisonBiasSize =
4212 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4213 static constexpr size_t kNumberOfComparePackedBits =
4214 kFieldComparisonBias + kFieldComparisonBiasSize;
4215 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4216 using ComparisonBiasField =
4217 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4218
Roland Levillain31dd3d62016-02-16 12:21:02 +00004219 // Return an integer constant containing the result of a comparison evaluated at compile time.
4220 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4221 DCHECK(value == -1 || value == 0 || value == 1) << value;
4222 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4223 }
4224
Artem Serovcced8ba2017-07-19 18:18:09 +01004225 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004226};
4227
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004228class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004229 public:
4230 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004231 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004232 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004233 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004234 bool finalizable,
4235 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304236 : HExpression(kNewInstance,
4237 DataType::Type::kReference,
4238 SideEffects::CanTriggerGC(),
4239 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004240 type_index_(type_index),
4241 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004242 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004243 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004244 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004245 }
4246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004247 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004248
Andreas Gampea5b09a62016-11-17 15:21:22 -08004249 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004250 const DexFile& GetDexFile() const { return dex_file_; }
4251
4252 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004253 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004254
Mingyao Yang062157f2016-03-02 10:15:36 -08004255 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004256 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004257
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004258 bool NeedsChecks() const {
4259 return entrypoint_ == kQuickAllocObjectWithChecks;
4260 }
David Brazdil6de19382016-01-08 17:37:10 +00004261
Vladimir Markoa1de9182016-02-25 11:37:38 +00004262 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004263
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004264 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004265
4266 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4267
4268 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4269 entrypoint_ = entrypoint;
4270 }
4271
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004272 HLoadClass* GetLoadClass() const {
4273 HInstruction* input = InputAt(0);
4274 if (input->IsClinitCheck()) {
4275 input = input->InputAt(0);
4276 }
4277 DCHECK(input->IsLoadClass());
4278 return input->AsLoadClass();
4279 }
4280
David Brazdil6de19382016-01-08 17:37:10 +00004281 bool IsStringAlloc() const;
4282
4283 DECLARE_INSTRUCTION(NewInstance);
4284
Artem Serovcced8ba2017-07-19 18:18:09 +01004285 protected:
4286 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4287
David Brazdil6de19382016-01-08 17:37:10 +00004288 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004289 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004290 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4291 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4292 "Too many packed fields.");
4293
Andreas Gampea5b09a62016-11-17 15:21:22 -08004294 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004295 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004296 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004297};
4298
Agi Csaki05f20562015-08-19 14:58:14 -07004299enum IntrinsicNeedsEnvironmentOrCache {
4300 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4301 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004302};
4303
Aart Bik5d75afe2015-12-14 11:57:01 -08004304enum IntrinsicSideEffects {
4305 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4306 kReadSideEffects, // Intrinsic may read heap memory.
4307 kWriteSideEffects, // Intrinsic may write heap memory.
4308 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4309};
4310
4311enum IntrinsicExceptions {
4312 kNoThrow, // Intrinsic does not throw any exceptions.
4313 kCanThrow // Intrinsic may throw exceptions.
4314};
4315
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004316class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004317 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004318 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004319
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004320 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004321 SetRawInputAt(index, argument);
4322 }
4323
Roland Levillain3e3d7332015-04-28 11:00:54 +01004324 // Return the number of arguments. This number can be lower than
4325 // the number of inputs returned by InputCount(), as some invoke
4326 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4327 // inputs at the end of their list of inputs.
4328 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4329
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004330 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4331
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004332 InvokeType GetInvokeType() const {
4333 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004334 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004335
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004336 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004337 return intrinsic_;
4338 }
4339
Aart Bik5d75afe2015-12-14 11:57:01 -08004340 void SetIntrinsic(Intrinsics intrinsic,
4341 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4342 IntrinsicSideEffects side_effects,
4343 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004344
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004345 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004346 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004347 }
4348
Aart Bikff7d89c2016-11-07 08:49:28 -08004349 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4350
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004351 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004352
Aart Bika8b8e9b2018-01-09 11:01:02 -08004353 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004355 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004356
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004357 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004358
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004359 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004360 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4361 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004362
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004363 uint32_t* GetIntrinsicOptimizations() {
4364 return &intrinsic_optimizations_;
4365 }
4366
4367 const uint32_t* GetIntrinsicOptimizations() const {
4368 return &intrinsic_optimizations_;
4369 }
4370
4371 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4372
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004373 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004374 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004375
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004376 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004377
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004378 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004379 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4380 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004381 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004382 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004383 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4384 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004385 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004386 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004387
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304388 HInvoke(InstructionKind kind,
4389 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004390 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004391 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004392 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004393 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004394 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004395 ArtMethod* resolved_method,
4396 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004397 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304398 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004399 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004400 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4401 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004402 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004403 number_of_arguments + number_of_other_inputs,
4404 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004405 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004406 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004407 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004408 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004409 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004410 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004411 // Check mutator lock, constructors lack annotalysis support.
4412 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4413 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004414 }
4415
Artem Serovcced8ba2017-07-19 18:18:09 +01004416 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4417
Roland Levillain3e3d7332015-04-28 11:00:54 +01004418 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004419 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004420 const uint32_t dex_method_index_;
4421 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004422
4423 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4424 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004425};
4426
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004427class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004428 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004429 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004430 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004431 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004432 uint32_t dex_pc,
4433 uint32_t dex_method_index,
4434 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304435 : HInvoke(kInvokeUnresolved,
4436 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004437 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004438 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004439 return_type,
4440 dex_pc,
4441 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004442 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004443 invoke_type) {
4444 }
4445
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004446 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004447
Calin Juravle175dc732015-08-25 15:42:32 +01004448 DECLARE_INSTRUCTION(InvokeUnresolved);
4449
Artem Serovcced8ba2017-07-19 18:18:09 +01004450 protected:
4451 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004452};
4453
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004454class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004455 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004456 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004457 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004458 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004459 uint32_t dex_pc,
4460 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304461 : HInvoke(kInvokePolymorphic,
4462 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004463 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004464 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004465 return_type,
4466 dex_pc,
4467 dex_method_index,
4468 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304469 kVirtual) {
4470 }
Orion Hodsonac141392017-01-13 11:53:47 +00004471
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004472 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004473
Orion Hodsonac141392017-01-13 11:53:47 +00004474 DECLARE_INSTRUCTION(InvokePolymorphic);
4475
Artem Serovcced8ba2017-07-19 18:18:09 +01004476 protected:
4477 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004478};
4479
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004480class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004481 public:
4482 HInvokeCustom(ArenaAllocator* allocator,
4483 uint32_t number_of_arguments,
4484 uint32_t call_site_index,
4485 DataType::Type return_type,
4486 uint32_t dex_pc)
4487 : HInvoke(kInvokeCustom,
4488 allocator,
4489 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004490 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004491 return_type,
4492 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004493 /* dex_method_index= */ dex::kDexNoIndex,
4494 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004495 kStatic),
4496 call_site_index_(call_site_index) {
4497 }
4498
4499 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4500
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004501 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004502
4503 DECLARE_INSTRUCTION(InvokeCustom);
4504
4505 protected:
4506 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4507
4508 private:
4509 uint32_t call_site_index_;
4510};
4511
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004512class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004513 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004514 // Requirements of this method call regarding the class
4515 // initialization (clinit) check of its declaring class.
4516 enum class ClinitCheckRequirement {
4517 kNone, // Class already initialized.
4518 kExplicit, // Static call having explicit clinit check as last input.
4519 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004520 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004521 };
4522
Vladimir Marko58155012015-08-19 12:49:41 +00004523 // Determines how to load the target ArtMethod*.
4524 enum class MethodLoadKind {
4525 // Use a String init ArtMethod* loaded from Thread entrypoints.
4526 kStringInit,
4527
4528 // Use the method's own ArtMethod* loaded by the register allocator.
4529 kRecursive,
4530
Vladimir Marko65979462017-05-19 17:25:12 +01004531 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4532 // Used for boot image methods referenced by boot image code.
4533 kBootImageLinkTimePcRelative,
4534
Vladimir Markob066d432018-01-03 13:14:37 +00004535 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4536 // Used for app->boot calls with relocatable image.
4537 kBootImageRelRo,
4538
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004539 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004540 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004541 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004542
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004543 // Use ArtMethod* at a known address, embed the direct address in the code.
4544 // Used for for JIT-compiled calls.
4545 kJitDirectAddress,
4546
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004547 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4548 // used when other kinds are unimplemented on a particular architecture.
4549 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004550 };
4551
4552 // Determines the location of the code pointer.
4553 enum class CodePtrLocation {
4554 // Recursive call, use local PC-relative call instruction.
4555 kCallSelf,
4556
Vladimir Marko58155012015-08-19 12:49:41 +00004557 // Use code pointer from the ArtMethod*.
4558 // Used when we don't know the target code. This is also the last-resort-kind used when
4559 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4560 kCallArtMethod,
4561 };
4562
4563 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004564 MethodLoadKind method_load_kind;
4565 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004566 // The method load data holds
4567 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4568 // Note that there are multiple string init methods, each having its own offset.
4569 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004570 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004571 };
4572
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004573 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004574 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004575 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004576 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004577 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004578 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004579 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004580 InvokeType invoke_type,
4581 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004582 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304583 : HInvoke(kInvokeStaticOrDirect,
4584 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004585 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004586 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004587 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004588 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004589 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004590 return_type,
4591 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004592 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004593 resolved_method,
4594 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004595 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004596 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004597 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4598 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004599
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004600 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004601
Vladimir Markodc151b22015-10-15 18:02:30 +01004602 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004603 bool had_current_method_input = HasCurrentMethodInput();
4604 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4605
4606 // Using the current method is the default and once we find a better
4607 // method load kind, we should not go back to using the current method.
4608 DCHECK(had_current_method_input || !needs_current_method_input);
4609
4610 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004611 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4612 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004613 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004614 dispatch_info_ = dispatch_info;
4615 }
4616
Aart Bik6daebeb2017-04-03 14:35:41 -07004617 DispatchInfo GetDispatchInfo() const {
4618 return dispatch_info_;
4619 }
4620
Vladimir Markoc53c0792015-11-19 15:48:33 +00004621 void AddSpecialInput(HInstruction* input) {
4622 // We allow only one special input.
4623 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4624 DCHECK(InputCount() == GetSpecialInputIndex() ||
4625 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4626 InsertInputAt(GetSpecialInputIndex(), input);
4627 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004628
Vladimir Marko372f10e2016-05-17 16:30:10 +01004629 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004630 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004631 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4632 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4633 DCHECK(!input_records.empty());
4634 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4635 HInstruction* last_input = input_records.back().GetInstruction();
4636 // Note: `last_input` may be null during arguments setup.
4637 if (last_input != nullptr) {
4638 // `last_input` is the last input of a static invoke marked as having
4639 // an explicit clinit check. It must either be:
4640 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4641 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4642 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4643 }
4644 }
4645 return input_records;
4646 }
4647
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004648 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004649 // We access the method via the dex cache so we can't do an implicit null check.
4650 // TODO: for intrinsics we can generate implicit null checks.
4651 return false;
4652 }
4653
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004654 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004655 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004656 }
4657
Vladimir Markoc53c0792015-11-19 15:48:33 +00004658 // Get the index of the special input, if any.
4659 //
David Brazdil6de19382016-01-08 17:37:10 +00004660 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4661 // method pointer; otherwise there may be one platform-specific special input,
4662 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004663 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004664 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004665
Vladimir Marko58155012015-08-19 12:49:41 +00004666 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4667 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4668 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004669 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004670 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004671 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004672 bool HasPcRelativeMethodLoadKind() const {
4673 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004674 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004675 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004676 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004677 bool HasCurrentMethodInput() const {
4678 // This function can be called only after the invoke has been fully initialized by the builder.
4679 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004680 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004681 return true;
4682 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004683 DCHECK(InputCount() == GetSpecialInputIndex() ||
4684 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004685 return false;
4686 }
4687 }
Vladimir Marko58155012015-08-19 12:49:41 +00004688
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004689 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004690 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004691 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004692 }
4693
4694 uint64_t GetMethodAddress() const {
4695 DCHECK(HasMethodAddress());
4696 return dispatch_info_.method_load_data;
4697 }
4698
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004699 const DexFile& GetDexFileForPcRelativeDexCache() const;
4700
Vladimir Markoa1de9182016-02-25 11:37:38 +00004701 ClinitCheckRequirement GetClinitCheckRequirement() const {
4702 return GetPackedField<ClinitCheckRequirementField>();
4703 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004704
Roland Levillain4c0eb422015-04-24 16:43:49 +01004705 // Is this instruction a call to a static method?
4706 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004707 return GetInvokeType() == kStatic;
4708 }
4709
4710 MethodReference GetTargetMethod() const {
4711 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004712 }
4713
Vladimir Markofbb184a2015-11-13 14:47:00 +00004714 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4715 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4716 // instruction; only relevant for static calls with explicit clinit check.
4717 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004718 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004719 size_t last_input_index = inputs_.size() - 1u;
4720 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004721 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004722 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004723 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004724 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004725 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004726 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004727 }
4728
4729 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004730 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004731 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004732 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004733 }
4734
4735 // Is this a call to a static method whose declaring class has an
4736 // implicit intialization check requirement?
4737 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004738 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004739 }
4740
Vladimir Markob554b5a2015-11-06 12:57:55 +00004741 // Does this method load kind need the current method as an input?
4742 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004743 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004744 }
4745
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004746 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004747
Artem Serovcced8ba2017-07-19 18:18:09 +01004748 protected:
4749 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4750
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004751 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004752 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004753 static constexpr size_t kFieldClinitCheckRequirementSize =
4754 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4755 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4756 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4757 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4758 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004759 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4760 kFieldClinitCheckRequirement,
4761 kFieldClinitCheckRequirementSize>;
4762
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004763 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004764 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004765 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004766};
Vladimir Markof64242a2015-12-01 14:58:23 +00004767std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004768std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004769
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004770class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004771 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004772 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004773 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004774 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004775 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004776 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004777 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004778 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304779 : HInvoke(kInvokeVirtual,
4780 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004781 number_of_arguments,
4782 0u,
4783 return_type,
4784 dex_pc,
4785 dex_method_index,
4786 resolved_method,
4787 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304788 vtable_index_(vtable_index) {
4789 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004790
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004791 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004792
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004793 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004794 switch (GetIntrinsic()) {
4795 case Intrinsics::kThreadCurrentThread:
4796 case Intrinsics::kStringBufferAppend:
4797 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004798 case Intrinsics::kStringBuilderAppendObject:
4799 case Intrinsics::kStringBuilderAppendString:
4800 case Intrinsics::kStringBuilderAppendCharSequence:
4801 case Intrinsics::kStringBuilderAppendCharArray:
4802 case Intrinsics::kStringBuilderAppendBoolean:
4803 case Intrinsics::kStringBuilderAppendChar:
4804 case Intrinsics::kStringBuilderAppendInt:
4805 case Intrinsics::kStringBuilderAppendLong:
4806 case Intrinsics::kStringBuilderAppendFloat:
4807 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004808 case Intrinsics::kStringBuilderToString:
4809 return false;
4810 default:
4811 return HInvoke::CanBeNull();
4812 }
4813 }
4814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004815 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004816 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004817 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004818 }
4819
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004820 uint32_t GetVTableIndex() const { return vtable_index_; }
4821
4822 DECLARE_INSTRUCTION(InvokeVirtual);
4823
Artem Serovcced8ba2017-07-19 18:18:09 +01004824 protected:
4825 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4826
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004827 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004828 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004829 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004830};
4831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004832class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004833 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004834 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004835 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004836 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004837 uint32_t dex_pc,
4838 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004839 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004840 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304841 : HInvoke(kInvokeInterface,
4842 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004843 number_of_arguments,
4844 0u,
4845 return_type,
4846 dex_pc,
4847 dex_method_index,
4848 resolved_method,
4849 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304850 imt_index_(imt_index) {
4851 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004852
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004853 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004854
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004855 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004856 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004857 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004858 }
4859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004860 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004861 // The assembly stub currently needs it.
4862 return true;
4863 }
4864
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004865 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004866
4867 DECLARE_INSTRUCTION(InvokeInterface);
4868
Artem Serovcced8ba2017-07-19 18:18:09 +01004869 protected:
4870 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4871
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004872 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004873 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004874 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004875};
4876
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004877class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004878 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004879 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304880 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004881 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004882 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004883
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004884 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004886 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004887 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004888 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004889 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004890 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004891 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004892 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004893 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4894 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004895 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004896 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4897 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004898
Roland Levillain88cb1752014-10-20 16:36:47 +01004899 DECLARE_INSTRUCTION(Neg);
4900
Artem Serovcced8ba2017-07-19 18:18:09 +01004901 protected:
4902 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004903};
4904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004905class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004906 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004907 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304908 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004909 SetRawInputAt(0, cls);
4910 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004911 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004912 }
4913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004914 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004915
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004916 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004917 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004918
Mingyao Yang0c365e62015-03-31 15:09:29 -07004919 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004920 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004922 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004923
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004924 HLoadClass* GetLoadClass() const {
4925 DCHECK(InputAt(0)->IsLoadClass());
4926 return InputAt(0)->AsLoadClass();
4927 }
4928
4929 HInstruction* GetLength() const {
4930 return InputAt(1);
4931 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004932
Vladimir Markob5461632018-10-15 14:24:21 +01004933 size_t GetComponentSizeShift() {
4934 return GetPackedField<ComponentSizeShiftField>();
4935 }
4936
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004937 DECLARE_INSTRUCTION(NewArray);
4938
Artem Serovcced8ba2017-07-19 18:18:09 +01004939 protected:
4940 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004941
4942 private:
4943 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4944 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4945 static constexpr size_t kNumberOfNewArrayPackedBits =
4946 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4947 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4948 using ComponentSizeShiftField =
4949 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004950};
4951
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004952class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004953 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004954 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004955 HInstruction* left,
4956 HInstruction* right,
4957 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304958 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4959 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004960
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004961 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004962
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004963 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004964
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004965 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004966 return GetBlock()->GetGraph()->GetIntConstant(
4967 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004968 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004969 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004970 return GetBlock()->GetGraph()->GetLongConstant(
4971 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004972 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004973 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004974 return GetBlock()->GetGraph()->GetFloatConstant(
4975 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4976 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004977 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004978 return GetBlock()->GetGraph()->GetDoubleConstant(
4979 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4980 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004981
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004982 DECLARE_INSTRUCTION(Add);
4983
Artem Serovcced8ba2017-07-19 18:18:09 +01004984 protected:
4985 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004986};
4987
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004988class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004989 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004990 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004991 HInstruction* left,
4992 HInstruction* right,
4993 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304994 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4995 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004996
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004997 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004998
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004999 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005000 return GetBlock()->GetGraph()->GetIntConstant(
5001 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005002 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005003 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005004 return GetBlock()->GetGraph()->GetLongConstant(
5005 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005006 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005007 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005008 return GetBlock()->GetGraph()->GetFloatConstant(
5009 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5010 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005011 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005012 return GetBlock()->GetGraph()->GetDoubleConstant(
5013 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5014 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005015
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005016 DECLARE_INSTRUCTION(Sub);
5017
Artem Serovcced8ba2017-07-19 18:18:09 +01005018 protected:
5019 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005020};
5021
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005022class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005023 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005024 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005025 HInstruction* left,
5026 HInstruction* right,
5027 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305028 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5029 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005031 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005032
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005033 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005035 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005036 return GetBlock()->GetGraph()->GetIntConstant(
5037 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005038 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005039 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005040 return GetBlock()->GetGraph()->GetLongConstant(
5041 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005042 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005043 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005044 return GetBlock()->GetGraph()->GetFloatConstant(
5045 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5046 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005047 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005048 return GetBlock()->GetGraph()->GetDoubleConstant(
5049 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5050 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005051
5052 DECLARE_INSTRUCTION(Mul);
5053
Artem Serovcced8ba2017-07-19 18:18:09 +01005054 protected:
5055 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005056};
5057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005058class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005059 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005060 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005061 HInstruction* left,
5062 HInstruction* right,
5063 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305064 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5065 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005066
Roland Levillain9867bc72015-08-05 10:21:34 +01005067 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005068 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005069 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005070 // Our graph structure ensures we never have 0 for `y` during
5071 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005072 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005073 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005074 return (y == -1) ? -x : x / y;
5075 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005076
Roland Levillain31dd3d62016-02-16 12:21:02 +00005077 template <typename T>
5078 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005079 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005080 return x / y;
5081 }
5082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005083 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005084 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005085 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005086 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005087 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005088 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005089 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5090 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005091 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005092 return GetBlock()->GetGraph()->GetFloatConstant(
5093 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5094 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005095 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005096 return GetBlock()->GetGraph()->GetDoubleConstant(
5097 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005098 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005099
5100 DECLARE_INSTRUCTION(Div);
5101
Artem Serovcced8ba2017-07-19 18:18:09 +01005102 protected:
5103 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005104};
5105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005106class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005107 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005108 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005109 HInstruction* left,
5110 HInstruction* right,
5111 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305112 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5113 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005114
Roland Levillain9867bc72015-08-05 10:21:34 +01005115 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005116 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005117 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005118 // Our graph structure ensures we never have 0 for `y` during
5119 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005120 DCHECK_NE(y, 0);
5121 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5122 return (y == -1) ? 0 : x % y;
5123 }
5124
Roland Levillain31dd3d62016-02-16 12:21:02 +00005125 template <typename T>
5126 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005127 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005128 return std::fmod(x, y);
5129 }
5130
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005131 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005132 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005133 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005134 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005135 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005136 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005137 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005138 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005139 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005140 return GetBlock()->GetGraph()->GetFloatConstant(
5141 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5142 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005143 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005144 return GetBlock()->GetGraph()->GetDoubleConstant(
5145 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5146 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005147
5148 DECLARE_INSTRUCTION(Rem);
5149
Artem Serovcced8ba2017-07-19 18:18:09 +01005150 protected:
5151 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005152};
5153
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005154class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005155 public:
5156 HMin(DataType::Type result_type,
5157 HInstruction* left,
5158 HInstruction* right,
5159 uint32_t dex_pc)
5160 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005162 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005163
5164 // Evaluation for integral values.
5165 template <typename T> static T ComputeIntegral(T x, T y) {
5166 return (x <= y) ? x : y;
5167 }
5168
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005169 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005170 return GetBlock()->GetGraph()->GetIntConstant(
5171 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5172 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005173 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005174 return GetBlock()->GetGraph()->GetLongConstant(
5175 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5176 }
5177 // TODO: Evaluation for floating-point values.
5178 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005179 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005180 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005181 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005182
5183 DECLARE_INSTRUCTION(Min);
5184
5185 protected:
5186 DEFAULT_COPY_CONSTRUCTOR(Min);
5187};
5188
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005189class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005190 public:
5191 HMax(DataType::Type result_type,
5192 HInstruction* left,
5193 HInstruction* right,
5194 uint32_t dex_pc)
5195 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005197 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005198
5199 // Evaluation for integral values.
5200 template <typename T> static T ComputeIntegral(T x, T y) {
5201 return (x >= y) ? x : y;
5202 }
5203
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005204 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005205 return GetBlock()->GetGraph()->GetIntConstant(
5206 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5207 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005208 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005209 return GetBlock()->GetGraph()->GetLongConstant(
5210 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5211 }
5212 // TODO: Evaluation for floating-point values.
5213 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005214 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005215 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005216 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005217
5218 DECLARE_INSTRUCTION(Max);
5219
5220 protected:
5221 DEFAULT_COPY_CONSTRUCTOR(Max);
5222};
5223
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005224class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005225 public:
5226 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5227 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5228
5229 // Evaluation for integral values.
5230 template <typename T> static T ComputeIntegral(T x) {
5231 return x < 0 ? -x : x;
5232 }
5233
5234 // Evaluation for floating-point values.
5235 // Note, as a "quality of implementation", rather than pure "spec compliance",
5236 // we require that Math.abs() clears the sign bit (but changes nothing else)
5237 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5238 // http://b/30758343
5239 template <typename T, typename S> static T ComputeFP(T x) {
5240 S bits = bit_cast<S, T>(x);
5241 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5242 }
5243
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005244 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005245 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5246 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005247 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005248 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5249 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005250 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005251 return GetBlock()->GetGraph()->GetFloatConstant(
5252 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5253 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005254 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005255 return GetBlock()->GetGraph()->GetDoubleConstant(
5256 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5257 }
5258
5259 DECLARE_INSTRUCTION(Abs);
5260
5261 protected:
5262 DEFAULT_COPY_CONSTRUCTOR(Abs);
5263};
5264
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005265class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005266 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005267 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005268 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5269 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005270 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005271 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005272 SetRawInputAt(0, value);
5273 }
5274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005275 bool IsClonable() const override { return true; }
5276 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005278 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005279 return true;
5280 }
5281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005282 bool NeedsEnvironment() const override { return true; }
5283 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005284
Calin Juravled0d48522014-11-04 16:40:20 +00005285 DECLARE_INSTRUCTION(DivZeroCheck);
5286
Artem Serovcced8ba2017-07-19 18:18:09 +01005287 protected:
5288 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005289};
5290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005291class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005292 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005293 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005294 HInstruction* value,
5295 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005296 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305297 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005298 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5299 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005300 }
5301
Roland Levillain5b5b9312016-03-22 14:57:31 +00005302 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005303 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005304 return value << (distance & max_shift_distance);
5305 }
5306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005307 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005308 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005309 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005310 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005311 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005312 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005313 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005314 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005315 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005316 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005317 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5318 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005319 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005320 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005321 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005322 LOG(FATAL) << DebugName() << " is not defined for float values";
5323 UNREACHABLE();
5324 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005325 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005326 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005327 LOG(FATAL) << DebugName() << " is not defined for double values";
5328 UNREACHABLE();
5329 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005330
5331 DECLARE_INSTRUCTION(Shl);
5332
Artem Serovcced8ba2017-07-19 18:18:09 +01005333 protected:
5334 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005335};
5336
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005337class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005338 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005339 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005340 HInstruction* value,
5341 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005342 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305343 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005344 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5345 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005346 }
5347
Roland Levillain5b5b9312016-03-22 14:57:31 +00005348 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005349 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005350 return value >> (distance & max_shift_distance);
5351 }
5352
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005353 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005354 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005355 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005356 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005357 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005358 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005359 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005360 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005361 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005362 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005363 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5364 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005365 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005366 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005367 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005368 LOG(FATAL) << DebugName() << " is not defined for float values";
5369 UNREACHABLE();
5370 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005371 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005372 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005373 LOG(FATAL) << DebugName() << " is not defined for double values";
5374 UNREACHABLE();
5375 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005376
5377 DECLARE_INSTRUCTION(Shr);
5378
Artem Serovcced8ba2017-07-19 18:18:09 +01005379 protected:
5380 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005381};
5382
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005383class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005384 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005385 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005386 HInstruction* value,
5387 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005388 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305389 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005390 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5391 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005392 }
5393
Roland Levillain5b5b9312016-03-22 14:57:31 +00005394 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005395 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005396 typedef typename std::make_unsigned<T>::type V;
5397 V ux = static_cast<V>(value);
5398 return static_cast<T>(ux >> (distance & max_shift_distance));
5399 }
5400
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005401 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005402 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005403 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005404 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005405 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005406 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005407 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005408 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005409 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005410 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005411 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5412 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005413 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005414 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005415 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005416 LOG(FATAL) << DebugName() << " is not defined for float values";
5417 UNREACHABLE();
5418 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005419 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005420 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005421 LOG(FATAL) << DebugName() << " is not defined for double values";
5422 UNREACHABLE();
5423 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005424
5425 DECLARE_INSTRUCTION(UShr);
5426
Artem Serovcced8ba2017-07-19 18:18:09 +01005427 protected:
5428 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005429};
5430
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005431class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005432 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005433 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005434 HInstruction* left,
5435 HInstruction* right,
5436 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305437 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5438 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005439
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005440 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005441
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005442 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005443
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005444 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005445 return GetBlock()->GetGraph()->GetIntConstant(
5446 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005447 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005448 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005449 return GetBlock()->GetGraph()->GetLongConstant(
5450 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005451 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005452 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005453 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005454 LOG(FATAL) << DebugName() << " is not defined for float values";
5455 UNREACHABLE();
5456 }
5457 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005458 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005459 LOG(FATAL) << DebugName() << " is not defined for double values";
5460 UNREACHABLE();
5461 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005462
5463 DECLARE_INSTRUCTION(And);
5464
Artem Serovcced8ba2017-07-19 18:18:09 +01005465 protected:
5466 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005467};
5468
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005469class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005470 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005471 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005472 HInstruction* left,
5473 HInstruction* right,
5474 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305475 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5476 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005477
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005478 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005479
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005480 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005481
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005482 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005483 return GetBlock()->GetGraph()->GetIntConstant(
5484 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005485 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005486 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005487 return GetBlock()->GetGraph()->GetLongConstant(
5488 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005489 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005490 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005491 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005492 LOG(FATAL) << DebugName() << " is not defined for float values";
5493 UNREACHABLE();
5494 }
5495 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005496 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005497 LOG(FATAL) << DebugName() << " is not defined for double values";
5498 UNREACHABLE();
5499 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005500
5501 DECLARE_INSTRUCTION(Or);
5502
Artem Serovcced8ba2017-07-19 18:18:09 +01005503 protected:
5504 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005505};
5506
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005507class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005508 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005509 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005510 HInstruction* left,
5511 HInstruction* right,
5512 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305513 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5514 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005515
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005516 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005517
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005518 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005520 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005521 return GetBlock()->GetGraph()->GetIntConstant(
5522 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005523 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005524 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005525 return GetBlock()->GetGraph()->GetLongConstant(
5526 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005527 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005528 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005529 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005530 LOG(FATAL) << DebugName() << " is not defined for float values";
5531 UNREACHABLE();
5532 }
5533 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005534 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005535 LOG(FATAL) << DebugName() << " is not defined for double values";
5536 UNREACHABLE();
5537 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005538
5539 DECLARE_INSTRUCTION(Xor);
5540
Artem Serovcced8ba2017-07-19 18:18:09 +01005541 protected:
5542 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005543};
5544
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005545class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005546 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005547 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305548 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005549 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5550 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005551 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005552
Roland Levillain5b5b9312016-03-22 14:57:31 +00005553 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005554 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005555 typedef typename std::make_unsigned<T>::type V;
5556 V ux = static_cast<V>(value);
5557 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005558 return static_cast<T>(ux);
5559 } else {
5560 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005561 return static_cast<T>(ux >> (distance & max_shift_value)) |
5562 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005563 }
5564 }
5565
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005566 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005567 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005568 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005569 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005570 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005571 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005572 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005573 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005574 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005575 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005576 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5577 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005578 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005579 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005580 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005581 LOG(FATAL) << DebugName() << " is not defined for float values";
5582 UNREACHABLE();
5583 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005584 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005585 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005586 LOG(FATAL) << DebugName() << " is not defined for double values";
5587 UNREACHABLE();
5588 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005589
5590 DECLARE_INSTRUCTION(Ror);
5591
Artem Serovcced8ba2017-07-19 18:18:09 +01005592 protected:
5593 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005594};
5595
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005596// The value of a parameter in this method. Its location depends on
5597// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005598class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005599 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005600 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005601 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005602 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005603 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005604 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305605 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005606 dex_file_(dex_file),
5607 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005608 index_(index) {
5609 SetPackedFlag<kFlagIsThis>(is_this);
5610 SetPackedFlag<kFlagCanBeNull>(!is_this);
5611 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005612
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005613 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005614 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005615 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005616 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005617
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005618 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005619 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005620
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005621 DECLARE_INSTRUCTION(ParameterValue);
5622
Artem Serovcced8ba2017-07-19 18:18:09 +01005623 protected:
5624 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5625
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005626 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005627 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005628 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005629 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5630 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5631 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5632 "Too many packed fields.");
5633
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005634 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005635 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005636 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005637 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005638 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005639};
5640
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005641class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005642 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005643 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305644 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5645 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005647 bool CanBeMoved() const override { return true; }
5648 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005649 return true;
5650 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005651
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005652 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005653
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005654 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005655 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005656 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005657 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005658 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005659 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005660 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005661 LOG(FATAL) << DebugName() << " is not defined for float values";
5662 UNREACHABLE();
5663 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005664 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005665 LOG(FATAL) << DebugName() << " is not defined for double values";
5666 UNREACHABLE();
5667 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005668
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005669 DECLARE_INSTRUCTION(Not);
5670
Artem Serovcced8ba2017-07-19 18:18:09 +01005671 protected:
5672 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005673};
5674
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005675class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005676 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005677 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305678 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5679 }
David Brazdil66d126e2015-04-03 16:02:44 +01005680
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005681 bool CanBeMoved() const override { return true; }
5682 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005683 return true;
5684 }
5685
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005686 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005687 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005688 return !x;
5689 }
5690
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005691 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005692 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005693 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005694 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005695 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005696 UNREACHABLE();
5697 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005698 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005699 LOG(FATAL) << DebugName() << " is not defined for float values";
5700 UNREACHABLE();
5701 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005702 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005703 LOG(FATAL) << DebugName() << " is not defined for double values";
5704 UNREACHABLE();
5705 }
David Brazdil66d126e2015-04-03 16:02:44 +01005706
5707 DECLARE_INSTRUCTION(BooleanNot);
5708
Artem Serovcced8ba2017-07-19 18:18:09 +01005709 protected:
5710 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005711};
5712
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005713class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005714 public:
5715 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005716 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305717 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005718 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005719 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005720 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005721 }
5722
5723 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005724 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5725 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005726
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005727 bool IsClonable() const override { return true; }
5728 bool CanBeMoved() const override { return true; }
5729 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005730 return true;
5731 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005732 // Return whether the conversion is implicit. This includes conversion to the same type.
5733 bool IsImplicitConversion() const {
5734 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5735 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005736
Mark Mendelle82549b2015-05-06 10:55:34 -04005737 // Try to statically evaluate the conversion and return a HConstant
5738 // containing the result. If the input cannot be converted, return nullptr.
5739 HConstant* TryStaticEvaluation() const;
5740
Roland Levillaindff1f282014-11-05 14:15:05 +00005741 DECLARE_INSTRUCTION(TypeConversion);
5742
Artem Serovcced8ba2017-07-19 18:18:09 +01005743 protected:
5744 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005745};
5746
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005747static constexpr uint32_t kNoRegNumber = -1;
5748
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005749class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005750 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005751 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005752 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5753 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005754 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005755 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005756 SetRawInputAt(0, value);
5757 }
5758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005759 bool IsClonable() const override { return true; }
5760 bool CanBeMoved() const override { return true; }
5761 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005762 return true;
5763 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005764
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005765 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005767 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005769 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005770
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005771 DECLARE_INSTRUCTION(NullCheck);
5772
Artem Serovcced8ba2017-07-19 18:18:09 +01005773 protected:
5774 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005775};
5776
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005777// Embeds an ArtField and all the information required by the compiler. We cache
5778// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005779class FieldInfo : public ValueObject {
5780 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005781 FieldInfo(ArtField* field,
5782 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005783 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005784 bool is_volatile,
5785 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005786 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005787 const DexFile& dex_file)
5788 : field_(field),
5789 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005790 field_type_(field_type),
5791 is_volatile_(is_volatile),
5792 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005793 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005794 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005795
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005796 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005797 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005798 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005799 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005800 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005801 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005802 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005803
5804 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005805 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005806 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005807 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005808 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005809 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005810 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005811 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005812};
5813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005814class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005815 public:
5816 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005817 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005818 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005819 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005820 bool is_volatile,
5821 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005822 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005823 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005824 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305825 : HExpression(kInstanceFieldGet,
5826 field_type,
5827 SideEffects::FieldReadOfType(field_type, is_volatile),
5828 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005829 field_info_(field,
5830 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005831 field_type,
5832 is_volatile,
5833 field_idx,
5834 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005835 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005836 SetRawInputAt(0, value);
5837 }
5838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005839 bool IsClonable() const override { return true; }
5840 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005841
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005842 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005843 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005844 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005845 }
5846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005847 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005848 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005849 }
5850
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005851 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005852 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5853 }
5854
Calin Juravle52c48962014-12-16 17:02:57 +00005855 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005856 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005857 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005858 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005859
Vladimir Marko61b92282017-10-11 13:23:17 +01005860 void SetType(DataType::Type new_type) {
5861 DCHECK(DataType::IsIntegralType(GetType()));
5862 DCHECK(DataType::IsIntegralType(new_type));
5863 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5864 SetPackedField<TypeField>(new_type);
5865 }
5866
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005867 DECLARE_INSTRUCTION(InstanceFieldGet);
5868
Artem Serovcced8ba2017-07-19 18:18:09 +01005869 protected:
5870 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5871
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005872 private:
5873 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005874};
5875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005876class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005877 public:
5878 HInstanceFieldSet(HInstruction* object,
5879 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005880 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005881 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005882 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005883 bool is_volatile,
5884 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005885 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005886 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005887 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005888 : HExpression(kInstanceFieldSet,
5889 SideEffects::FieldWriteOfType(field_type, is_volatile),
5890 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005891 field_info_(field,
5892 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005893 field_type,
5894 is_volatile,
5895 field_idx,
5896 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005897 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005898 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005899 SetRawInputAt(0, object);
5900 SetRawInputAt(1, value);
5901 }
5902
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005903 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005905 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005906 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005907 }
5908
Calin Juravle52c48962014-12-16 17:02:57 +00005909 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005910 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005911 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005912 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005913 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005914 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5915 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005916
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005917 DECLARE_INSTRUCTION(InstanceFieldSet);
5918
Artem Serovcced8ba2017-07-19 18:18:09 +01005919 protected:
5920 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5921
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005922 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005923 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5924 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5925 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5926 "Too many packed fields.");
5927
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005928 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005929};
5930
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005931class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005932 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005933 HArrayGet(HInstruction* array,
5934 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005935 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005936 uint32_t dex_pc)
5937 : HArrayGet(array,
5938 index,
5939 type,
5940 SideEffects::ArrayReadOfType(type),
5941 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005942 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305943 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005944
5945 HArrayGet(HInstruction* array,
5946 HInstruction* index,
5947 DataType::Type type,
5948 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005949 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005950 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305951 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005952 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005953 SetRawInputAt(0, array);
5954 SetRawInputAt(1, index);
5955 }
5956
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005957 bool IsClonable() const override { return true; }
5958 bool CanBeMoved() const override { return true; }
5959 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005960 return true;
5961 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005962 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005963 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005964 // Currently, unless the array is the result of NewArray, the array access is always
5965 // preceded by some form of null NullCheck necessary for the bounds check, usually
5966 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5967 // dynamic BCE. There are cases when these could be removed to produce better code.
5968 // If we ever add optimizations to do so we should allow an implicit check here
5969 // (as long as the address falls in the first page).
5970 //
5971 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5972 // a = cond ? new int[1] : null;
5973 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005974 return false;
5975 }
5976
David Brazdil4833f5a2015-12-16 10:37:39 +00005977 bool IsEquivalentOf(HArrayGet* other) const {
5978 bool result = (GetDexPc() == other->GetDexPc());
5979 if (kIsDebugBuild && result) {
5980 DCHECK_EQ(GetBlock(), other->GetBlock());
5981 DCHECK_EQ(GetArray(), other->GetArray());
5982 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005983 if (DataType::IsIntOrLongType(GetType())) {
5984 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005985 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005986 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5987 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005988 }
5989 }
5990 return result;
5991 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005992
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005993 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5994
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005995 HInstruction* GetArray() const { return InputAt(0); }
5996 HInstruction* GetIndex() const { return InputAt(1); }
5997
Vladimir Marko61b92282017-10-11 13:23:17 +01005998 void SetType(DataType::Type new_type) {
5999 DCHECK(DataType::IsIntegralType(GetType()));
6000 DCHECK(DataType::IsIntegralType(new_type));
6001 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6002 SetPackedField<TypeField>(new_type);
6003 }
6004
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006005 DECLARE_INSTRUCTION(ArrayGet);
6006
Artem Serovcced8ba2017-07-19 18:18:09 +01006007 protected:
6008 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6009
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006010 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006011 // We treat a String as an array, creating the HArrayGet from String.charAt()
6012 // intrinsic in the instruction simplifier. We can always determine whether
6013 // a particular HArrayGet is actually a String.charAt() by looking at the type
6014 // of the input but that requires holding the mutator lock, so we prefer to use
6015 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006016 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006017 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6018 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6019 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006020};
6021
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006022class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006023 public:
6024 HArraySet(HInstruction* array,
6025 HInstruction* index,
6026 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006027 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006028 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006029 : HArraySet(array,
6030 index,
6031 value,
6032 expected_component_type,
6033 // Make a best guess for side effects now, may be refined during SSA building.
6034 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306035 dex_pc) {
6036 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006037
6038 HArraySet(HInstruction* array,
6039 HInstruction* index,
6040 HInstruction* value,
6041 DataType::Type expected_component_type,
6042 SideEffects side_effects,
6043 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006044 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006045 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006046 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006047 SetPackedFlag<kFlagValueCanBeNull>(true);
6048 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006049 SetRawInputAt(0, array);
6050 SetRawInputAt(1, index);
6051 SetRawInputAt(2, value);
6052 }
6053
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006054 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006056 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006057 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006058 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006059 }
6060
Mingyao Yang81014cb2015-06-02 03:16:27 -07006061 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006062 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006063
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006064 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006065 // TODO: Same as for ArrayGet.
6066 return false;
6067 }
6068
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006069 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006071 }
6072
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006073 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006074 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006075 }
6076
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006077 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006078 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006079 }
6080
Vladimir Markoa1de9182016-02-25 11:37:38 +00006081 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6082 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6083 bool StaticTypeOfArrayIsObjectArray() const {
6084 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6085 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006086
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006087 HInstruction* GetArray() const { return InputAt(0); }
6088 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006089 HInstruction* GetValue() const { return InputAt(2); }
6090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006091 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006092 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6093 }
6094
6095 static DataType::Type GetComponentType(DataType::Type value_type,
6096 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006097 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006098 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006099 // be correct, we also check what is the value type. If it is a floating
6100 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006101 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006102 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006103 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006104 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006106 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006107 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006108 }
6109
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006110 static SideEffects ComputeSideEffects(DataType::Type type) {
6111 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006112 }
6113
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006114 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6115 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6116 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006117 }
6118
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006119 DECLARE_INSTRUCTION(ArraySet);
6120
Artem Serovcced8ba2017-07-19 18:18:09 +01006121 protected:
6122 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6123
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006124 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006125 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6126 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006127 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006128 static constexpr size_t kFlagNeedsTypeCheck =
6129 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6130 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006131 // Cached information for the reference_type_info_ so that codegen
6132 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006133 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6134 static constexpr size_t kNumberOfArraySetPackedBits =
6135 kFlagStaticTypeOfArrayIsObjectArray + 1;
6136 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6137 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006138 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006139};
6140
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006141class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006142 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006143 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306144 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006145 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006146 // Note that arrays do not change length, so the instruction does not
6147 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006148 SetRawInputAt(0, array);
6149 }
6150
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006151 bool IsClonable() const override { return true; }
6152 bool CanBeMoved() const override { return true; }
6153 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006154 return true;
6155 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006156 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006157 return obj == InputAt(0);
6158 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006159
Vladimir Markodce016e2016-04-28 13:10:02 +01006160 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006162 DECLARE_INSTRUCTION(ArrayLength);
6163
Artem Serovcced8ba2017-07-19 18:18:09 +01006164 protected:
6165 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6166
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006167 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006168 // We treat a String as an array, creating the HArrayLength from String.length()
6169 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6170 // determine whether a particular HArrayLength is actually a String.length() by
6171 // looking at the type of the input but that requires holding the mutator lock, so
6172 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006173 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006174 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6175 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6176 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006177};
6178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006179class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006180 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006181 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006182 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6183 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006184 HBoundsCheck(HInstruction* index,
6185 HInstruction* length,
6186 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006187 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006188 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006189 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006190 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006191 SetRawInputAt(0, index);
6192 SetRawInputAt(1, length);
6193 }
6194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006195 bool IsClonable() const override { return true; }
6196 bool CanBeMoved() const override { return true; }
6197 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006198 return true;
6199 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006200
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006201 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006202
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006203 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006204
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006205 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006206
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006207 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006208
6209 DECLARE_INSTRUCTION(BoundsCheck);
6210
Artem Serovcced8ba2017-07-19 18:18:09 +01006211 protected:
6212 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6213
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006214 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006215 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006216 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6217 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6218 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006219};
6220
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006221class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006222 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006223 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006224 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306225 slow_path_(nullptr) {
6226 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006227
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006228 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006230 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006231 return true;
6232 }
6233
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006234 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6235 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006236
6237 DECLARE_INSTRUCTION(SuspendCheck);
6238
Artem Serovcced8ba2017-07-19 18:18:09 +01006239 protected:
6240 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6241
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006242 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006243 // Only used for code generation, in order to share the same slow path between back edges
6244 // of a same loop.
6245 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006246};
6247
David Srbecky0cf44932015-12-09 14:09:59 +00006248// Pseudo-instruction which provides the native debugger with mapping information.
6249// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006250class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006251 public:
6252 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006253 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306254 }
David Srbecky0cf44932015-12-09 14:09:59 +00006255
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006256 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006257 return true;
6258 }
6259
6260 DECLARE_INSTRUCTION(NativeDebugInfo);
6261
Artem Serovcced8ba2017-07-19 18:18:09 +01006262 protected:
6263 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006264};
6265
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006266/**
6267 * Instruction to load a Class object.
6268 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006269class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006270 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006271 // Determines how to load the Class.
6272 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006273 // We cannot load this class. See HSharpening::SharpenLoadClass.
6274 kInvalid = -1,
6275
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006276 // Use the Class* from the method's own ArtMethod*.
6277 kReferrersClass,
6278
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006279 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006280 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006281 kBootImageLinkTimePcRelative,
6282
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006283 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006284 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006285 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006286
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006287 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006288 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006289 kBssEntry,
6290
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006291 // Use a known boot image Class* address, embedded in the code by the codegen.
6292 // Used for boot image classes referenced by apps in JIT-compiled code.
6293 kJitBootImageAddress,
6294
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006295 // Load from the root table associated with the JIT compiled method.
6296 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006297
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006298 // Load using a simple runtime call. This is the fall-back load kind when
6299 // the codegen is unable to use another appropriate kind.
6300 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006301
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006302 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006303 };
6304
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006305 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006306 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006307 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006308 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006309 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006310 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006311 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006312 : HInstruction(kLoadClass,
6313 DataType::Type::kReference,
6314 SideEffectsForArchRuntimeCalls(),
6315 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006316 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006317 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006318 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006319 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006320 // Referrers class should not need access check. We never inline unverified
6321 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006322 DCHECK(!is_referrers_class || !needs_access_check);
6323
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006324 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006325 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006326 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006327 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006328 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006329 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006330 }
6331
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006332 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006333
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006334 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006335
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006336 LoadKind GetLoadKind() const {
6337 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006338 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006339
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006340 bool HasPcRelativeLoadKind() const {
6341 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6342 GetLoadKind() == LoadKind::kBootImageRelRo ||
6343 GetLoadKind() == LoadKind::kBssEntry;
6344 }
6345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006346 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006347
Yi Kong39402542019-03-24 02:47:16 -07006348 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006349
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006350 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006351
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006352 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006354 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006355 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006356 }
6357
Calin Juravle0ba218d2015-05-19 18:46:01 +01006358 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006359 // The entrypoint the code generator is going to call does not do
6360 // clinit of the class.
6361 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006362 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006363 }
6364
6365 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006366 return NeedsAccessCheck() ||
6367 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006368 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006369 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006370 }
6371
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006372 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006373 return NeedsAccessCheck() ||
6374 MustGenerateClinitCheck() ||
6375 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006376 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006377 GetLoadKind() == LoadKind::kBssEntry) &&
6378 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006379 }
6380
Calin Juravleacf735c2015-02-12 15:25:22 +00006381 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006382 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6383 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006384 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006385 } else {
6386 return ReferenceTypeInfo::CreateInvalid();
6387 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006388 }
6389
Vladimir Marko175e7862018-03-27 09:03:13 +00006390 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6391 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6392 DCHECK(klass_ != nullptr);
6393 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006394 }
6395
Andreas Gampea5b09a62016-11-17 15:21:22 -08006396 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006397 const DexFile& GetDexFile() const { return dex_file_; }
6398
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006399 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006400 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006401 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006402
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006403 static SideEffects SideEffectsForArchRuntimeCalls() {
6404 return SideEffects::CanTriggerGC();
6405 }
6406
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006407 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006408 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006409 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006411
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006412 bool MustResolveTypeOnSlowPath() const {
6413 // Check that this instruction has a slow path.
6414 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6415 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6416 return GetLoadKind() == LoadKind::kBssEntry;
6417 }
6418
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006419 void MarkInBootImage() {
6420 SetPackedFlag<kFlagIsInBootImage>(true);
6421 }
6422
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006423 void AddSpecialInput(HInstruction* special_input);
6424
6425 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006426 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006427 return ArrayRef<HUserRecord<HInstruction*>>(
6428 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6429 }
6430
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006431 Handle<mirror::Class> GetClass() const {
6432 return klass_;
6433 }
6434
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006435 DECLARE_INSTRUCTION(LoadClass);
6436
Artem Serovcced8ba2017-07-19 18:18:09 +01006437 protected:
6438 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6439
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006440 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006441 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006442 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006443 // Whether this instruction must generate the initialization check.
6444 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006445 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006446 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6447 static constexpr size_t kFieldLoadKindSize =
6448 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006449 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6450 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006451 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006452 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6453
6454 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006455 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006456 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006457 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006458 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006459 }
6460
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006461 void SetLoadKindInternal(LoadKind load_kind);
6462
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006463 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006464 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006465 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006466 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006467
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006468 // A type index and dex file where the class can be accessed. The dex file can be:
6469 // - The compiling method's dex file if the class is defined there too.
6470 // - The compiling method's dex file if the class is referenced there.
6471 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006472 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006473 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006474 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006475
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006476 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006477};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006478std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6479
6480// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006481inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6482 // The load kind should be determined before inserting the instruction to the graph.
6483 DCHECK(GetBlock() == nullptr);
6484 DCHECK(GetEnvironment() == nullptr);
6485 SetPackedField<LoadKindField>(load_kind);
6486 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6487 special_input_ = HUserRecord<HInstruction*>(nullptr);
6488 }
6489 if (!NeedsEnvironment()) {
6490 SetSideEffects(SideEffects::None());
6491 }
6492}
6493
6494// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006495inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006496 // The special input is used for PC-relative loads on some architectures,
6497 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006498 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006499 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006500 GetLoadKind() == LoadKind::kBssEntry ||
6501 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006502 DCHECK(special_input_.GetInstruction() == nullptr);
6503 special_input_ = HUserRecord<HInstruction*>(special_input);
6504 special_input->AddUseAt(this, 0);
6505}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006506
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006507class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006508 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006509 // Determines how to load the String.
6510 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006511 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006512 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006513 kBootImageLinkTimePcRelative,
6514
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006515 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006516 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006517 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006518
Vladimir Markoaad75c62016-10-03 08:46:48 +00006519 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006520 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006521 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006522
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006523 // Use a known boot image String* address, embedded in the code by the codegen.
6524 // Used for boot image strings referenced by apps in JIT-compiled code.
6525 kJitBootImageAddress,
6526
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006527 // Load from the root table associated with the JIT compiled method.
6528 kJitTableAddress,
6529
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006530 // Load using a simple runtime call. This is the fall-back load kind when
6531 // the codegen is unable to use another appropriate kind.
6532 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006533
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006534 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006535 };
6536
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006537 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006538 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006539 const DexFile& dex_file,
6540 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006541 : HInstruction(kLoadString,
6542 DataType::Type::kReference,
6543 SideEffectsForArchRuntimeCalls(),
6544 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006545 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006546 string_index_(string_index),
6547 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006548 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006549 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006550
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006551 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006552
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006553 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006554
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006555 LoadKind GetLoadKind() const {
6556 return GetPackedField<LoadKindField>();
6557 }
6558
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006559 bool HasPcRelativeLoadKind() const {
6560 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6561 GetLoadKind() == LoadKind::kBootImageRelRo ||
6562 GetLoadKind() == LoadKind::kBssEntry;
6563 }
6564
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006565 const DexFile& GetDexFile() const {
6566 return dex_file_;
6567 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006568
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006569 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006570 return string_index_;
6571 }
6572
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006573 Handle<mirror::String> GetString() const {
6574 return string_;
6575 }
6576
6577 void SetString(Handle<mirror::String> str) {
6578 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006579 }
6580
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006581 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006583 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006584
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006585 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006586
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006587 // Will call the runtime if we need to load the string through
6588 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006589 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006590 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006591 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006592 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006593 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006594 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006595 return false;
6596 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006597 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006598 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006599
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006600 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006601 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006602 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006603
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006604 bool CanBeNull() const override { return false; }
6605 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006606
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006607 static SideEffects SideEffectsForArchRuntimeCalls() {
6608 return SideEffects::CanTriggerGC();
6609 }
6610
Vladimir Marko372f10e2016-05-17 16:30:10 +01006611 void AddSpecialInput(HInstruction* special_input);
6612
6613 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006614 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006615 return ArrayRef<HUserRecord<HInstruction*>>(
6616 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006617 }
6618
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006619 DECLARE_INSTRUCTION(LoadString);
6620
Artem Serovcced8ba2017-07-19 18:18:09 +01006621 protected:
6622 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6623
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006624 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006625 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006626 static constexpr size_t kFieldLoadKindSize =
6627 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6628 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006629 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006630 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006631
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006632 void SetLoadKindInternal(LoadKind load_kind);
6633
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006634 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006635 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006636 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006637 HUserRecord<HInstruction*> special_input_;
6638
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006639 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006640 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006641
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006642 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006643};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006644std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6645
6646// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006647inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6648 // The load kind should be determined before inserting the instruction to the graph.
6649 DCHECK(GetBlock() == nullptr);
6650 DCHECK(GetEnvironment() == nullptr);
6651 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6652 SetPackedField<LoadKindField>(load_kind);
6653 if (load_kind != LoadKind::kRuntimeCall) {
6654 special_input_ = HUserRecord<HInstruction*>(nullptr);
6655 }
6656 if (!NeedsEnvironment()) {
6657 SetSideEffects(SideEffects::None());
6658 }
6659}
6660
6661// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006662inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006663 // The special input is used for PC-relative loads on some architectures,
6664 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006665 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006666 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006667 GetLoadKind() == LoadKind::kBssEntry ||
6668 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006669 // HLoadString::GetInputRecords() returns an empty array at this point,
6670 // so use the GetInputRecords() from the base class to set the input record.
6671 DCHECK(special_input_.GetInstruction() == nullptr);
6672 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006673 special_input->AddUseAt(this, 0);
6674}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006676class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006677 public:
6678 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006679 uint16_t method_handle_idx,
6680 const DexFile& dex_file,
6681 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006682 : HInstruction(kLoadMethodHandle,
6683 DataType::Type::kReference,
6684 SideEffectsForArchRuntimeCalls(),
6685 dex_pc),
6686 special_input_(HUserRecord<HInstruction*>(current_method)),
6687 method_handle_idx_(method_handle_idx),
6688 dex_file_(dex_file) {
6689 }
6690
6691 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006692 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006693 return ArrayRef<HUserRecord<HInstruction*>>(
6694 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6695 }
6696
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006697 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006698
6699 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6700
6701 const DexFile& GetDexFile() const { return dex_file_; }
6702
6703 static SideEffects SideEffectsForArchRuntimeCalls() {
6704 return SideEffects::CanTriggerGC();
6705 }
6706
6707 DECLARE_INSTRUCTION(LoadMethodHandle);
6708
6709 protected:
6710 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6711
6712 private:
6713 // The special input is the HCurrentMethod for kRuntimeCall.
6714 HUserRecord<HInstruction*> special_input_;
6715
6716 const uint16_t method_handle_idx_;
6717 const DexFile& dex_file_;
6718};
6719
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006720class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006721 public:
6722 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006723 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006724 const DexFile& dex_file,
6725 uint32_t dex_pc)
6726 : HInstruction(kLoadMethodType,
6727 DataType::Type::kReference,
6728 SideEffectsForArchRuntimeCalls(),
6729 dex_pc),
6730 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006731 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006732 dex_file_(dex_file) {
6733 }
6734
6735 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006736 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006737 return ArrayRef<HUserRecord<HInstruction*>>(
6738 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6739 }
6740
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006741 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006742
Orion Hodson06d10a72018-05-14 08:53:38 +01006743 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006744
6745 const DexFile& GetDexFile() const { return dex_file_; }
6746
6747 static SideEffects SideEffectsForArchRuntimeCalls() {
6748 return SideEffects::CanTriggerGC();
6749 }
6750
6751 DECLARE_INSTRUCTION(LoadMethodType);
6752
6753 protected:
6754 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6755
6756 private:
6757 // The special input is the HCurrentMethod for kRuntimeCall.
6758 HUserRecord<HInstruction*> special_input_;
6759
Orion Hodson06d10a72018-05-14 08:53:38 +01006760 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006761 const DexFile& dex_file_;
6762};
6763
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006764/**
6765 * Performs an initialization check on its Class object input.
6766 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006767class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006768 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006769 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006770 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306771 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006772 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006773 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006774 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006775 SetRawInputAt(0, constant);
6776 }
Artem Serova6e26142018-06-19 14:55:17 +01006777 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006778 bool CanBeMoved() const override { return true; }
6779 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006780 return true;
6781 }
6782
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006783 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006784 // May call runtime to initialize the class.
6785 return true;
6786 }
6787
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006788 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006789
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006790 HLoadClass* GetLoadClass() const {
6791 DCHECK(InputAt(0)->IsLoadClass());
6792 return InputAt(0)->AsLoadClass();
6793 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006794
6795 DECLARE_INSTRUCTION(ClinitCheck);
6796
Artem Serovcced8ba2017-07-19 18:18:09 +01006797
6798 protected:
6799 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006800};
6801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006802class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006803 public:
6804 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006805 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006806 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006807 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006808 bool is_volatile,
6809 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006810 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006811 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006812 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306813 : HExpression(kStaticFieldGet,
6814 field_type,
6815 SideEffects::FieldReadOfType(field_type, is_volatile),
6816 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006817 field_info_(field,
6818 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006819 field_type,
6820 is_volatile,
6821 field_idx,
6822 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006823 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006824 SetRawInputAt(0, cls);
6825 }
6826
Calin Juravle52c48962014-12-16 17:02:57 +00006827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006828 bool IsClonable() const override { return true; }
6829 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006831 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006832 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006833 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006834 }
6835
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006836 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006837 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6838 }
6839
Calin Juravle52c48962014-12-16 17:02:57 +00006840 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006841 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006842 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006843 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006844
Vladimir Marko61b92282017-10-11 13:23:17 +01006845 void SetType(DataType::Type new_type) {
6846 DCHECK(DataType::IsIntegralType(GetType()));
6847 DCHECK(DataType::IsIntegralType(new_type));
6848 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6849 SetPackedField<TypeField>(new_type);
6850 }
6851
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006852 DECLARE_INSTRUCTION(StaticFieldGet);
6853
Artem Serovcced8ba2017-07-19 18:18:09 +01006854 protected:
6855 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6856
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006857 private:
6858 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006859};
6860
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006861class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006862 public:
6863 HStaticFieldSet(HInstruction* cls,
6864 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006865 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006866 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006867 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006868 bool is_volatile,
6869 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006870 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006871 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006872 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006873 : HExpression(kStaticFieldSet,
6874 SideEffects::FieldWriteOfType(field_type, is_volatile),
6875 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006876 field_info_(field,
6877 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006878 field_type,
6879 is_volatile,
6880 field_idx,
6881 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006882 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006883 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006884 SetRawInputAt(0, cls);
6885 SetRawInputAt(1, value);
6886 }
6887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006888 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006889 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006890 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006891 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006892 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006893
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006894 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006895 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6896 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006897
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006898 DECLARE_INSTRUCTION(StaticFieldSet);
6899
Artem Serovcced8ba2017-07-19 18:18:09 +01006900 protected:
6901 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6902
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006903 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006904 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6905 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6906 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6907 "Too many packed fields.");
6908
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006909 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006910};
6911
Vladimir Marko552a1342017-10-31 10:56:47 +00006912class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6913 public:
6914 HStringBuilderAppend(HIntConstant* format,
6915 uint32_t number_of_arguments,
6916 ArenaAllocator* allocator,
6917 uint32_t dex_pc)
6918 : HVariableInputSizeInstruction(
6919 kStringBuilderAppend,
6920 DataType::Type::kReference,
6921 // The runtime call may read memory from inputs. It never writes outside
6922 // of the newly allocated result object (or newly allocated helper objects).
6923 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6924 dex_pc,
6925 allocator,
6926 number_of_arguments + /* format */ 1u,
6927 kArenaAllocInvokeInputs) {
6928 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6929 SetRawInputAt(FormatIndex(), format);
6930 }
6931
6932 void SetArgumentAt(size_t index, HInstruction* argument) {
6933 DCHECK_LE(index, GetNumberOfArguments());
6934 SetRawInputAt(index, argument);
6935 }
6936
6937 // Return the number of arguments, excluding the format.
6938 size_t GetNumberOfArguments() const {
6939 DCHECK_GE(InputCount(), 1u);
6940 return InputCount() - 1u;
6941 }
6942
6943 size_t FormatIndex() const {
6944 return GetNumberOfArguments();
6945 }
6946
6947 HIntConstant* GetFormat() {
6948 return InputAt(FormatIndex())->AsIntConstant();
6949 }
6950
6951 bool NeedsEnvironment() const override { return true; }
6952
6953 bool CanThrow() const override { return true; }
6954
6955 DECLARE_INSTRUCTION(StringBuilderAppend);
6956
6957 protected:
6958 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
6959};
6960
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006961class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006962 public:
6963 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006964 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006965 uint32_t field_index,
6966 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306967 : HExpression(kUnresolvedInstanceFieldGet,
6968 field_type,
6969 SideEffects::AllExceptGCDependency(),
6970 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006971 field_index_(field_index) {
6972 SetRawInputAt(0, obj);
6973 }
6974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006975 bool IsClonable() const override { return true; }
6976 bool NeedsEnvironment() const override { return true; }
6977 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006978
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006979 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006980 uint32_t GetFieldIndex() const { return field_index_; }
6981
6982 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6983
Artem Serovcced8ba2017-07-19 18:18:09 +01006984 protected:
6985 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6986
Calin Juravlee460d1d2015-09-29 04:52:17 +01006987 private:
6988 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006989};
6990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006991class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006992 public:
6993 HUnresolvedInstanceFieldSet(HInstruction* obj,
6994 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006995 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006996 uint32_t field_index,
6997 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006998 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006999 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007000 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007001 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007002 SetRawInputAt(0, obj);
7003 SetRawInputAt(1, value);
7004 }
7005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007006 bool IsClonable() const override { return true; }
7007 bool NeedsEnvironment() const override { return true; }
7008 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007009
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007010 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007011 uint32_t GetFieldIndex() const { return field_index_; }
7012
7013 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7014
Artem Serovcced8ba2017-07-19 18:18:09 +01007015 protected:
7016 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7017
Calin Juravlee460d1d2015-09-29 04:52:17 +01007018 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007019 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7020 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007021 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007022 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7023 kFieldFieldType + kFieldFieldTypeSize;
7024 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7025 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007026 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007027
Calin Juravlee460d1d2015-09-29 04:52:17 +01007028 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007029};
7030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007031class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007032 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007033 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007034 uint32_t field_index,
7035 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307036 : HExpression(kUnresolvedStaticFieldGet,
7037 field_type,
7038 SideEffects::AllExceptGCDependency(),
7039 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007040 field_index_(field_index) {
7041 }
7042
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007043 bool IsClonable() const override { return true; }
7044 bool NeedsEnvironment() const override { return true; }
7045 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007046
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007047 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007048 uint32_t GetFieldIndex() const { return field_index_; }
7049
7050 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7051
Artem Serovcced8ba2017-07-19 18:18:09 +01007052 protected:
7053 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7054
Calin Juravlee460d1d2015-09-29 04:52:17 +01007055 private:
7056 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007057};
7058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007059class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007060 public:
7061 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007062 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007063 uint32_t field_index,
7064 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007065 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007066 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007067 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007068 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007069 SetRawInputAt(0, value);
7070 }
7071
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007072 bool IsClonable() const override { return true; }
7073 bool NeedsEnvironment() const override { return true; }
7074 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007075
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007076 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007077 uint32_t GetFieldIndex() const { return field_index_; }
7078
7079 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7080
Artem Serovcced8ba2017-07-19 18:18:09 +01007081 protected:
7082 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7083
Calin Juravlee460d1d2015-09-29 04:52:17 +01007084 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007085 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7086 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007087 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007088 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7089 kFieldFieldType + kFieldFieldTypeSize;
7090 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7091 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007092 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007093
Calin Juravlee460d1d2015-09-29 04:52:17 +01007094 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007095};
7096
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007097// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007098class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007099 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007100 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307101 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7102 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007104 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007105
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007106 DECLARE_INSTRUCTION(LoadException);
7107
Artem Serovcced8ba2017-07-19 18:18:09 +01007108 protected:
7109 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007110};
7111
David Brazdilcb1c0552015-08-04 16:22:25 +01007112// Implicit part of move-exception which clears thread-local exception storage.
7113// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007114class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007115 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007116 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007117 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307118 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007119
7120 DECLARE_INSTRUCTION(ClearException);
7121
Artem Serovcced8ba2017-07-19 18:18:09 +01007122 protected:
7123 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007124};
7125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007126class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007127 public:
7128 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007129 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007130 SetRawInputAt(0, exception);
7131 }
7132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007133 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007135 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007136
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007137 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007138
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007139 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007140
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007141 DECLARE_INSTRUCTION(Throw);
7142
Artem Serovcced8ba2017-07-19 18:18:09 +01007143 protected:
7144 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007145};
7146
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007147/**
7148 * Implementation strategies for the code generator of a HInstanceOf
7149 * or `HCheckCast`.
7150 */
7151enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007152 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007153 kExactCheck, // Can do a single class compare.
7154 kClassHierarchyCheck, // Can just walk the super class chain.
7155 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7156 kInterfaceCheck, // No optimization yet when checking against an interface.
7157 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007158 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007159 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007160 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007161};
7162
Roland Levillain86503782016-02-11 19:07:30 +00007163std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7164
Vladimir Marko175e7862018-03-27 09:03:13 +00007165// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7166// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7167class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007168 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007169 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007170 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007171 HInstruction* object,
7172 HInstruction* target_class_or_null,
7173 TypeCheckKind check_kind,
7174 Handle<mirror::Class> klass,
7175 uint32_t dex_pc,
7176 ArenaAllocator* allocator,
7177 HIntConstant* bitstring_path_to_root,
7178 HIntConstant* bitstring_mask,
7179 SideEffects side_effects)
7180 : HVariableInputSizeInstruction(
7181 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007182 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007183 side_effects,
7184 dex_pc,
7185 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007186 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007187 kArenaAllocTypeCheckInputs),
7188 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007189 SetPackedField<TypeCheckKindField>(check_kind);
7190 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007191 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007192 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007193 SetRawInputAt(1, target_class_or_null);
7194 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7195 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7196 if (check_kind == TypeCheckKind::kBitstringCheck) {
7197 DCHECK(target_class_or_null->IsNullConstant());
7198 SetRawInputAt(2, bitstring_path_to_root);
7199 SetRawInputAt(3, bitstring_mask);
7200 } else {
7201 DCHECK(target_class_or_null->IsLoadClass());
7202 }
Vladimir Marko87584542017-12-12 17:47:52 +00007203 }
7204
7205 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007206 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007207 HInstruction* load_class = InputAt(1);
7208 DCHECK(load_class->IsLoadClass());
7209 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007210 }
7211
Vladimir Marko175e7862018-03-27 09:03:13 +00007212 uint32_t GetBitstringPathToRoot() const {
7213 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7214 HInstruction* path_to_root = InputAt(2);
7215 DCHECK(path_to_root->IsIntConstant());
7216 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7217 }
7218
7219 uint32_t GetBitstringMask() const {
7220 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7221 HInstruction* mask = InputAt(3);
7222 DCHECK(mask->IsIntConstant());
7223 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7224 }
7225
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007226 bool IsClonable() const override { return true; }
7227 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007229 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007230 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7231 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007232 }
7233
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007234 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7235 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7236 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7237 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7238
Vladimir Marko175e7862018-03-27 09:03:13 +00007239 ReferenceTypeInfo GetTargetClassRTI() {
7240 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7241 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007242 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007243 } else {
7244 return ReferenceTypeInfo::CreateInvalid();
7245 }
7246 }
7247
7248 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7249 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7250 DCHECK(klass_ != nullptr);
7251 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7252 }
7253
7254 Handle<mirror::Class> GetClass() const {
7255 return klass_;
7256 }
7257
7258 protected:
7259 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7260
7261 private:
7262 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7263 static constexpr size_t kFieldTypeCheckKindSize =
7264 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7265 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7266 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7267 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7268 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7269 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7270
7271 Handle<mirror::Class> klass_;
7272};
7273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007274class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007275 public:
7276 HInstanceOf(HInstruction* object,
7277 HInstruction* target_class_or_null,
7278 TypeCheckKind check_kind,
7279 Handle<mirror::Class> klass,
7280 uint32_t dex_pc,
7281 ArenaAllocator* allocator,
7282 HIntConstant* bitstring_path_to_root,
7283 HIntConstant* bitstring_mask)
7284 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007285 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007286 object,
7287 target_class_or_null,
7288 check_kind,
7289 klass,
7290 dex_pc,
7291 allocator,
7292 bitstring_path_to_root,
7293 bitstring_mask,
7294 SideEffectsForArchRuntimeCalls(check_kind)) {}
7295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007296 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007297
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007298 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007299 return CanCallRuntime(GetTypeCheckKind());
7300 }
7301
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007302 static bool CanCallRuntime(TypeCheckKind check_kind) {
7303 // Mips currently does runtime calls for any other checks.
7304 return check_kind != TypeCheckKind::kExactCheck;
7305 }
7306
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007307 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007308 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007309 }
7310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007311 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007312
Artem Serovcced8ba2017-07-19 18:18:09 +01007313 protected:
7314 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007315};
7316
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007317class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007318 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007319 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307320 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007321 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7322 SetPackedFlag<kFlagUpperCanBeNull>(true);
7323 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007324 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007325 SetRawInputAt(0, input);
7326 }
7327
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007328 bool InstructionDataEquals(const HInstruction* other) const override;
7329 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007330
David Brazdilf5552582015-12-27 13:36:12 +00007331 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007332 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007333 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007334 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007335
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007336 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007337 DCHECK(GetUpperCanBeNull() || !can_be_null);
7338 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007339 }
7340
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007341 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007342
Calin Juravleb1498f62015-02-16 13:13:29 +00007343 DECLARE_INSTRUCTION(BoundType);
7344
Artem Serovcced8ba2017-07-19 18:18:09 +01007345 protected:
7346 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7347
Calin Juravleb1498f62015-02-16 13:13:29 +00007348 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007349 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7350 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007351 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007352 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7353 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7354 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7355
Calin Juravleb1498f62015-02-16 13:13:29 +00007356 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007357 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7358 // It is used to bound the type in cases like:
7359 // if (x instanceof ClassX) {
7360 // // uper_bound_ will be ClassX
7361 // }
David Brazdilf5552582015-12-27 13:36:12 +00007362 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007363};
7364
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007365class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007366 public:
7367 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007368 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007369 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007370 Handle<mirror::Class> klass,
7371 uint32_t dex_pc,
7372 ArenaAllocator* allocator,
7373 HIntConstant* bitstring_path_to_root,
7374 HIntConstant* bitstring_mask)
7375 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007376 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007377 object,
7378 target_class_or_null,
7379 check_kind,
7380 klass,
7381 dex_pc,
7382 allocator,
7383 bitstring_path_to_root,
7384 bitstring_mask,
7385 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007386
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007387 bool IsClonable() const override { return true; }
7388 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007389 // Instruction may throw a CheckCastError.
7390 return true;
7391 }
7392
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007393 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007394
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007395 DECLARE_INSTRUCTION(CheckCast);
7396
Artem Serovcced8ba2017-07-19 18:18:09 +01007397 protected:
7398 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007399};
7400
Andreas Gampe26de38b2016-07-27 17:53:11 -07007401/**
7402 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7403 * @details We define the combined barrier types that are actually required
7404 * by the Java Memory Model, rather than using exactly the terminology from
7405 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7406 * primitives. Note that the JSR-133 cookbook generally does not deal with
7407 * store atomicity issues, and the recipes there are not always entirely sufficient.
7408 * The current recipe is as follows:
7409 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7410 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7411 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7412 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7413 * class has final fields.
7414 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7415 * store-to-memory instructions. Only generated together with non-temporal stores.
7416 */
7417enum MemBarrierKind {
7418 kAnyStore,
7419 kLoadAny,
7420 kStoreStore,
7421 kAnyAny,
7422 kNTStoreStore,
7423 kLastBarrierKind = kNTStoreStore
7424};
7425std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7426
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007427class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007429 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007430 : HExpression(kMemoryBarrier,
7431 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7432 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007433 SetPackedField<BarrierKindField>(barrier_kind);
7434 }
Calin Juravle27df7582015-04-17 19:12:31 +01007435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007436 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007437
Vladimir Markoa1de9182016-02-25 11:37:38 +00007438 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007439
7440 DECLARE_INSTRUCTION(MemoryBarrier);
7441
Artem Serovcced8ba2017-07-19 18:18:09 +01007442 protected:
7443 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7444
Calin Juravle27df7582015-04-17 19:12:31 +01007445 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007446 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7447 static constexpr size_t kFieldBarrierKindSize =
7448 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7449 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7450 kFieldBarrierKind + kFieldBarrierKindSize;
7451 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7452 "Too many packed fields.");
7453 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007454};
7455
Igor Murashkind01745e2017-04-05 16:40:31 -07007456// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7457// the specified object(s), with respect to any subsequent store that might "publish"
7458// (i.e. make visible) the specified object to another thread.
7459//
7460// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7461// for all final fields (that were set) at the end of the invoked constructor.
7462//
7463// The constructor fence models the freeze actions for the final fields of an object
7464// being constructed (semantically at the end of the constructor). Constructor fences
7465// have a per-object affinity; two separate objects being constructed get two separate
7466// constructor fences.
7467//
7468// (Note: that if calling a super-constructor or forwarding to another constructor,
7469// the freezes would happen at the end of *that* constructor being invoked).
7470//
7471// The memory model guarantees that when the object being constructed is "published" after
7472// constructor completion (i.e. escapes the current thread via a store), then any final field
7473// writes must be observable on other threads (once they observe that publication).
7474//
7475// Further, anything written before the freeze, and read by dereferencing through the final field,
7476// must also be visible (so final object field could itself have an object with non-final fields;
7477// yet the freeze must also extend to them).
7478//
7479// Constructor example:
7480//
7481// class HasFinal {
7482// final int field; Optimizing IR for <init>()V:
7483// HasFinal() {
7484// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7485// // freeze(this.field); HConstructorFence(this)
7486// } HReturn
7487// }
7488//
7489// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7490// already-initialized classes; in that case the allocation must act as a "default-initializer"
7491// of the object which effectively writes the class pointer "final field".
7492//
7493// For example, we can model default-initialiation as roughly the equivalent of the following:
7494//
7495// class Object {
7496// private final Class header;
7497// }
7498//
7499// Java code: Optimizing IR:
7500//
7501// T new_instance<T>() {
7502// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7503// obj.header = T.class; // header write is done by above call.
7504// // freeze(obj.header) HConstructorFence(obj)
7505// return (T)obj;
7506// }
7507//
7508// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007509// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007510// * QuasiAtomic::ThreadFenceForConstructor
7511//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007512class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007513 // A fence has variable inputs because the inputs can be removed
7514 // after prepare_for_register_allocation phase.
7515 // (TODO: In the future a fence could freeze multiple objects
7516 // after merging two fences together.)
7517 public:
7518 // `fence_object` is the reference that needs to be protected for correct publication.
7519 //
7520 // It makes sense in the following situations:
7521 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7522 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7523 //
7524 // After construction the `fence_object` becomes the 0th input.
7525 // This is not an input in a real sense, but just a convenient place to stash the information
7526 // about the associated object.
7527 HConstructorFence(HInstruction* fence_object,
7528 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007529 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007530 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7531 // constraints described in the class header. We claim that these SideEffects constraints
7532 // enforce a superset of the real constraints.
7533 //
7534 // The ordering described above is conservatively modeled with SideEffects as follows:
7535 //
7536 // * To prevent reordering of the publication stores:
7537 // ----> "Reads of objects" is the initial SideEffect.
7538 // * For every primitive final field store in the constructor:
7539 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7540 // * If there are any stores to reference final fields in the constructor:
7541 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7542 // that are reachable from `fence_object` also need to be prevented for reordering
7543 // (and we do not want to do alias analysis to figure out what those stores are).
7544 //
7545 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7546 // even more conservative approach than the one described above, and prevents all of the
7547 // above reordering without analyzing any of the instructions in the constructor.
7548 //
7549 // If in a later phase we discover that there are no writes to reference final fields,
7550 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307551 : HVariableInputSizeInstruction(kConstructorFence,
7552 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007553 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007554 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007555 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007556 kArenaAllocConstructorFenceInputs) {
7557 DCHECK(fence_object != nullptr);
7558 SetRawInputAt(0, fence_object);
7559 }
7560
7561 // The object associated with this constructor fence.
7562 //
7563 // (Note: This will be null after the prepare_for_register_allocation phase,
7564 // as all constructor fence inputs are removed there).
7565 HInstruction* GetFenceObject() const {
7566 return InputAt(0);
7567 }
7568
7569 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7570 // - Delete `fence_use` from `this`'s use list.
7571 // - Delete `this` from `fence_use`'s inputs list.
7572 // - If the `fence_use` is dead, remove it from the graph.
7573 //
7574 // A fence is considered dead once it no longer has any uses
7575 // and all of the inputs are dead.
7576 //
7577 // This must *not* be called during/after prepare_for_register_allocation,
7578 // because that removes all the inputs to the fences but the fence is actually
7579 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007580 //
7581 // Returns how many HConstructorFence instructions were removed from graph.
7582 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007583
Igor Murashkindd018df2017-08-09 10:38:31 -07007584 // Combine all inputs of `this` and `other` instruction and remove
7585 // `other` from the graph.
7586 //
7587 // Inputs are unique after the merge.
7588 //
7589 // Requirement: `this` must not be the same as `other.
7590 void Merge(HConstructorFence* other);
7591
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007592 // Check if this constructor fence is protecting
7593 // an HNewInstance or HNewArray that is also the immediate
7594 // predecessor of `this`.
7595 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007596 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7597 // to be one of the inputs of `this`.
7598 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007599 // Returns the associated HNewArray or HNewInstance,
7600 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007601 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007602
Igor Murashkind01745e2017-04-05 16:40:31 -07007603 DECLARE_INSTRUCTION(ConstructorFence);
7604
Artem Serovcced8ba2017-07-19 18:18:09 +01007605 protected:
7606 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007607};
7608
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007609class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007610 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007611 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007612 kEnter,
7613 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007614 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007615 };
7616
7617 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007618 : HExpression(kMonitorOperation,
7619 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7620 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007621 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007622 SetRawInputAt(0, object);
7623 }
7624
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007625 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007626 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007627
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007628 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007629 // Verifier guarantees that monitor-exit cannot throw.
7630 // This is important because it allows the HGraphBuilder to remove
7631 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7632 return IsEnter();
7633 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007634
Vladimir Markoa1de9182016-02-25 11:37:38 +00007635 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7636 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007637
7638 DECLARE_INSTRUCTION(MonitorOperation);
7639
Artem Serovcced8ba2017-07-19 18:18:09 +01007640 protected:
7641 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7642
Calin Juravle52c48962014-12-16 17:02:57 +00007643 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007644 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7645 static constexpr size_t kFieldOperationKindSize =
7646 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7647 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7648 kFieldOperationKind + kFieldOperationKindSize;
7649 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7650 "Too many packed fields.");
7651 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007652};
7653
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007654class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007655 public:
7656 HSelect(HInstruction* condition,
7657 HInstruction* true_value,
7658 HInstruction* false_value,
7659 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307660 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007661 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7662
7663 // First input must be `true_value` or `false_value` to allow codegens to
7664 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7665 // that architectures which implement HSelect as a conditional move also
7666 // will not need to invert the condition.
7667 SetRawInputAt(0, false_value);
7668 SetRawInputAt(1, true_value);
7669 SetRawInputAt(2, condition);
7670 }
7671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007672 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007673 HInstruction* GetFalseValue() const { return InputAt(0); }
7674 HInstruction* GetTrueValue() const { return InputAt(1); }
7675 HInstruction* GetCondition() const { return InputAt(2); }
7676
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007677 bool CanBeMoved() const override { return true; }
7678 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007679 return true;
7680 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007681
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007682 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007683 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7684 }
7685
7686 DECLARE_INSTRUCTION(Select);
7687
Artem Serovcced8ba2017-07-19 18:18:09 +01007688 protected:
7689 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007690};
7691
Vladimir Markof9f64412015-09-02 14:05:49 +01007692class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007693 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007694 MoveOperands(Location source,
7695 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007696 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007697 HInstruction* instruction)
7698 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007699
7700 Location GetSource() const { return source_; }
7701 Location GetDestination() const { return destination_; }
7702
7703 void SetSource(Location value) { source_ = value; }
7704 void SetDestination(Location value) { destination_ = value; }
7705
7706 // The parallel move resolver marks moves as "in-progress" by clearing the
7707 // destination (but not the source).
7708 Location MarkPending() {
7709 DCHECK(!IsPending());
7710 Location dest = destination_;
7711 destination_ = Location::NoLocation();
7712 return dest;
7713 }
7714
7715 void ClearPending(Location dest) {
7716 DCHECK(IsPending());
7717 destination_ = dest;
7718 }
7719
7720 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007721 DCHECK(source_.IsValid() || destination_.IsInvalid());
7722 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007723 }
7724
7725 // True if this blocks a move from the given location.
7726 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007727 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007728 }
7729
7730 // A move is redundant if it's been eliminated, if its source and
7731 // destination are the same, or if its destination is unneeded.
7732 bool IsRedundant() const {
7733 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7734 }
7735
7736 // We clear both operands to indicate move that's been eliminated.
7737 void Eliminate() {
7738 source_ = destination_ = Location::NoLocation();
7739 }
7740
7741 bool IsEliminated() const {
7742 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7743 return source_.IsInvalid();
7744 }
7745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007746 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007747
Nicolas Geoffray90218252015-04-15 11:56:51 +01007748 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007749 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007750 }
7751
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007752 HInstruction* GetInstruction() const { return instruction_; }
7753
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007754 private:
7755 Location source_;
7756 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007757 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007758 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007759 // The instruction this move is assocatied with. Null when this move is
7760 // for moving an input in the expected locations of user (including a phi user).
7761 // This is only used in debug mode, to ensure we do not connect interval siblings
7762 // in the same parallel move.
7763 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007764};
7765
Roland Levillainc9285912015-12-18 10:38:42 +00007766std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7767
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007768static constexpr size_t kDefaultNumberOfMoves = 4;
7769
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007770class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007771 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007772 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007773 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007774 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007775 moves_.reserve(kDefaultNumberOfMoves);
7776 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007777
Nicolas Geoffray90218252015-04-15 11:56:51 +01007778 void AddMove(Location source,
7779 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007780 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007781 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007782 DCHECK(source.IsValid());
7783 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007784 if (kIsDebugBuild) {
7785 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007786 for (const MoveOperands& move : moves_) {
7787 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007788 // Special case the situation where the move is for the spill slot
7789 // of the instruction.
7790 if ((GetPrevious() == instruction)
7791 || ((GetPrevious() == nullptr)
7792 && instruction->IsPhi()
7793 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007794 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007795 << "Doing parallel moves for the same instruction.";
7796 } else {
7797 DCHECK(false) << "Doing parallel moves for the same instruction.";
7798 }
7799 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007800 }
7801 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007802 for (const MoveOperands& move : moves_) {
7803 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007804 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007805 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007806 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007807 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007808 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007809 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007810 }
7811
Vladimir Marko225b6462015-09-28 12:17:40 +01007812 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007813 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007814 }
7815
Vladimir Marko225b6462015-09-28 12:17:40 +01007816 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007817
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007818 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007819
Artem Serovcced8ba2017-07-19 18:18:09 +01007820 protected:
7821 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7822
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007823 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007824 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007825};
7826
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007827// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7828// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7829// never used across anything that can trigger GC.
7830// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7831// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007832class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007833 public:
7834 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307835 : HExpression(kIntermediateAddress,
7836 DataType::Type::kInt32,
7837 SideEffects::DependsOnGC(),
7838 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007839 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7840 DataType::Size(DataType::Type::kReference))
7841 << "kPrimInt and kPrimNot have different sizes.";
7842 SetRawInputAt(0, base_address);
7843 SetRawInputAt(1, offset);
7844 }
7845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007846 bool IsClonable() const override { return true; }
7847 bool CanBeMoved() const override { return true; }
7848 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007849 return true;
7850 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007851 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007852
7853 HInstruction* GetBaseAddress() const { return InputAt(0); }
7854 HInstruction* GetOffset() const { return InputAt(1); }
7855
7856 DECLARE_INSTRUCTION(IntermediateAddress);
7857
Artem Serovcced8ba2017-07-19 18:18:09 +01007858 protected:
7859 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007860};
7861
7862
Mark Mendell0616ae02015-04-17 12:49:27 -04007863} // namespace art
7864
Aart Bikf8f5a162017-02-06 15:35:29 -08007865#include "nodes_vector.h"
7866
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007867#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7868#include "nodes_shared.h"
7869#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007870#ifdef ART_ENABLE_CODEGEN_mips
7871#include "nodes_mips.h"
7872#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307873#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007874#include "nodes_x86.h"
7875#endif
7876
7877namespace art {
7878
Igor Murashkin6ef45672017-08-08 13:59:55 -07007879class OptimizingCompilerStats;
7880
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007881class HGraphVisitor : public ValueObject {
7882 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007883 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7884 : stats_(stats),
7885 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007886 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007887
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007888 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007889 virtual void VisitBasicBlock(HBasicBlock* block);
7890
Roland Levillain633021e2014-10-01 14:12:25 +01007891 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007892 void VisitInsertionOrder();
7893
Roland Levillain633021e2014-10-01 14:12:25 +01007894 // Visit the graph following dominator tree reverse post-order.
7895 void VisitReversePostOrder();
7896
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007897 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007898
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007899 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007900#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007901 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7902
7903 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7904
7905#undef DECLARE_VISIT_INSTRUCTION
7906
Igor Murashkin6ef45672017-08-08 13:59:55 -07007907 protected:
7908 OptimizingCompilerStats* stats_;
7909
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007910 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007911 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007912
7913 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7914};
7915
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007916class HGraphDelegateVisitor : public HGraphVisitor {
7917 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007918 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7919 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007920 virtual ~HGraphDelegateVisitor() {}
7921
7922 // Visit functions that delegate to to super class.
7923#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007924 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007925
7926 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7927
7928#undef DECLARE_VISIT_INSTRUCTION
7929
7930 private:
7931 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7932};
7933
Artem Serovcced8ba2017-07-19 18:18:09 +01007934// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7935// and remove the old instruction.
7936HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7937
7938// Create a clone for each clonable instructions/phis and replace the original with the clone.
7939//
7940// Used for testing individual instruction cloner.
7941class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7942 public:
7943 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007944 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007945
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007946 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007947 if (instruction->IsClonable()) {
7948 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007949 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007950 }
7951 }
7952
Artem Serov7f4aff62017-06-21 17:02:18 +01007953 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007954
7955 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007956 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007957
7958 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7959};
7960
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007961// Iterator over the blocks that art part of the loop. Includes blocks part
7962// of an inner loop. The order in which the blocks are iterated is on their
7963// block id.
7964class HBlocksInLoopIterator : public ValueObject {
7965 public:
7966 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7967 : blocks_in_loop_(info.GetBlocks()),
7968 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7969 index_(0) {
7970 if (!blocks_in_loop_.IsBitSet(index_)) {
7971 Advance();
7972 }
7973 }
7974
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007975 bool Done() const { return index_ == blocks_.size(); }
7976 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007977 void Advance() {
7978 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007979 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007980 if (blocks_in_loop_.IsBitSet(index_)) {
7981 break;
7982 }
7983 }
7984 }
7985
7986 private:
7987 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007988 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007989 size_t index_;
7990
7991 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7992};
7993
Mingyao Yang3584bce2015-05-19 16:01:59 -07007994// Iterator over the blocks that art part of the loop. Includes blocks part
7995// of an inner loop. The order in which the blocks are iterated is reverse
7996// post order.
7997class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7998 public:
7999 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8000 : blocks_in_loop_(info.GetBlocks()),
8001 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8002 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008003 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008004 Advance();
8005 }
8006 }
8007
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008008 bool Done() const { return index_ == blocks_.size(); }
8009 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008010 void Advance() {
8011 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008012 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8013 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008014 break;
8015 }
8016 }
8017 }
8018
8019 private:
8020 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008021 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008022 size_t index_;
8023
8024 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8025};
8026
Aart Bikf3e61ee2017-04-12 17:09:20 -07008027// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008028inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008029 if (constant->IsIntConstant()) {
8030 return constant->AsIntConstant()->GetValue();
8031 } else if (constant->IsLongConstant()) {
8032 return constant->AsLongConstant()->GetValue();
8033 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008034 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008035 return 0;
8036 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008037}
8038
Aart Bikf3e61ee2017-04-12 17:09:20 -07008039// Returns true iff instruction is an integral constant (and sets value on success).
8040inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8041 if (instruction->IsIntConstant()) {
8042 *value = instruction->AsIntConstant()->GetValue();
8043 return true;
8044 } else if (instruction->IsLongConstant()) {
8045 *value = instruction->AsLongConstant()->GetValue();
8046 return true;
8047 } else if (instruction->IsNullConstant()) {
8048 *value = 0;
8049 return true;
8050 }
8051 return false;
8052}
8053
Aart Bik0148de42017-09-05 09:25:01 -07008054// Returns true iff instruction is the given integral constant.
8055inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8056 int64_t val = 0;
8057 return IsInt64AndGet(instruction, &val) && val == value;
8058}
8059
8060// Returns true iff instruction is a zero bit pattern.
8061inline bool IsZeroBitPattern(HInstruction* instruction) {
8062 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8063}
8064
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008065// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008066#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008067 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8068 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8069#undef INSTRUCTION_TYPE_CHECK
8070
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008071// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008072#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8073 std::is_base_of<BaseType, H##type>::value,
8074#define INSTRUCTION_TYPE_CHECK(type, super) \
8075 inline bool HInstruction::Is##type() const { \
8076 DCHECK_LT(GetKind(), kLastInstructionKind); \
8077 using BaseType = H##type; \
8078 static constexpr bool results[] = { \
8079 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8080 }; \
8081 return results[static_cast<size_t>(GetKind())]; \
8082 }
8083
8084 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8085#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008086#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008087
8088#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008089 inline const H##type* HInstruction::As##type() const { \
8090 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8091 } \
8092 inline H##type* HInstruction::As##type() { \
8093 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8094 }
8095
Vladimir Markoa90dd512018-05-04 15:04:45 +01008096 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8097#undef INSTRUCTION_TYPE_CAST
8098
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008099
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008100// Create space in `blocks` for adding `number_of_new_blocks` entries
8101// starting at location `at`. Blocks after `at` are moved accordingly.
8102inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8103 size_t number_of_new_blocks,
8104 size_t after) {
8105 DCHECK_LT(after, blocks->size());
8106 size_t old_size = blocks->size();
8107 size_t new_size = old_size + number_of_new_blocks;
8108 blocks->resize(new_size);
8109 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8110}
8111
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008112/*
8113 * Hunt "under the hood" of array lengths (leading to array references),
8114 * null checks (also leading to array references), and new arrays
8115 * (leading to the actual length). This makes it more likely related
8116 * instructions become actually comparable.
8117 */
8118inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8119 while (instruction->IsArrayLength() ||
8120 instruction->IsNullCheck() ||
8121 instruction->IsNewArray()) {
8122 instruction = instruction->IsNewArray()
8123 ? instruction->AsNewArray()->GetLength()
8124 : instruction->InputAt(0);
8125 }
8126 return instruction;
8127}
8128
Artem Serov21c7e6f2017-07-27 16:04:42 +01008129void RemoveEnvironmentUses(HInstruction* instruction);
8130bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8131void ResetEnvironmentInputRecords(HInstruction* instruction);
8132
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008133} // namespace art
8134
8135#endif // ART_COMPILER_OPTIMIZING_NODES_H_