blob: 3e6e21192053e1226156d69eac5882c9e1c4862e [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) \
1543 M(X86MaskOrResetLeastSetBit, Instruction) \
1544 M(VecAvxSub, VecOperation) \
1545 M(VecAvxAdd, VecOperation)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301546#else
1547#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1548#endif
1549
Alexandre Ramesef20f712015-06-09 10:29:30 +01001550#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1551
1552#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1553 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001554 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001555 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1556 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001557 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001558 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001559 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301560 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1561 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001562
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001563#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1564 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001565 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001566 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001567 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001568 M(Invoke, Instruction) \
1569 M(VecOperation, Instruction) \
1570 M(VecUnaryOperation, VecOperation) \
1571 M(VecBinaryOperation, VecOperation) \
1572 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001573
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001574#define FOR_EACH_INSTRUCTION(M) \
1575 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1576 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1577
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001578#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001579FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1580#undef FORWARD_DECLARATION
1581
Vladimir Marko372f10e2016-05-17 16:30:10 +01001582#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001583 private: \
1584 H##type& operator=(const H##type&) = delete; \
1585 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001586 const char* DebugName() const override { return #type; } \
1587 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001588 DCHECK(IsClonable()); \
1589 return new (arena) H##type(*this->As##type()); \
1590 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001591 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001592
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001593#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001594 private: \
1595 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001596 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001597
Artem Serovcced8ba2017-07-19 18:18:09 +01001598#define DEFAULT_COPY_CONSTRUCTOR(type) \
1599 explicit H##type(const H##type& other) = default;
1600
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001601template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001602class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1603 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001604 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001605 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001606 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001607 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001608 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001609 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001610 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001611
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001612 private:
David Brazdiled596192015-01-23 10:39:45 +00001613 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001614 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001615
1616 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001617 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001618
Vladimir Marko46817b82016-03-29 12:21:58 +01001619 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001620
1621 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1622};
1623
David Brazdiled596192015-01-23 10:39:45 +00001624template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001625using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001626
David Brazdil1abb4192015-02-17 18:33:36 +00001627// This class is used by HEnvironment and HInstruction classes to record the
1628// instructions they use and pointers to the corresponding HUseListNodes kept
1629// by the used instructions.
1630template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001631class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001632 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001633 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1634 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001635
Vladimir Marko46817b82016-03-29 12:21:58 +01001636 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1637 : HUserRecord(old_record.instruction_, before_use_node) {}
1638 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1639 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001640 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001641 }
1642
1643 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001644 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1645 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001646
1647 private:
1648 // Instruction used by the user.
1649 HInstruction* instruction_;
1650
Vladimir Marko46817b82016-03-29 12:21:58 +01001651 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1652 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001653};
1654
Vladimir Markoe9004912016-06-16 16:50:52 +01001655// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1656// This is used for HInstruction::GetInputs() to return a container wrapper providing
1657// HInstruction* values even though the underlying container has HUserRecord<>s.
1658struct HInputExtractor {
1659 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1660 return record.GetInstruction();
1661 }
1662 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1663 return record.GetInstruction();
1664 }
1665};
1666
1667using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1668using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1669
Aart Bik854a02b2015-07-14 16:07:00 -07001670/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001671 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001672 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001673 * For write/read dependences on fields/arrays, the dependence analysis uses
1674 * type disambiguation (e.g. a float field write cannot modify the value of an
1675 * integer field read) and the access type (e.g. a reference array write cannot
1676 * modify the value of a reference field read [although it may modify the
1677 * reference fetch prior to reading the field, which is represented by its own
1678 * write/read dependence]). The analysis makes conservative points-to
1679 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1680 * the same, and any reference read depends on any reference read without
1681 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001682 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001683 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1684 * alive across the point where garbage collection might happen.
1685 *
1686 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1687 *
1688 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1689 * those instructions from the compiler perspective (between this instruction and the next one
1690 * in the IR).
1691 *
1692 * Note: Instructions which can cause GC only on a fatal slow path do not need
1693 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1694 * one. However the execution may return to compiled code if there is a catch block in the
1695 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1696 * set.
1697 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 * The internal representation uses 38-bit and is described in the table below.
1699 * The first line indicates the side effect, and for field/array accesses the
1700 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001701 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001702 * The two numbered lines below indicate the bit position in the bitfield (read
1703 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001704 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001705 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1706 * +-------------+---------+---------+--------------+---------+---------+
1707 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1708 * | 3 |333333322|222222221| 1 |111111110|000000000|
1709 * | 7 |654321098|765432109| 8 |765432109|876543210|
1710 *
1711 * Note that, to ease the implementation, 'changes' bits are least significant
1712 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001713 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001714class SideEffects : public ValueObject {
1715 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001716 SideEffects() : flags_(0) {}
1717
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001718 static SideEffects None() {
1719 return SideEffects(0);
1720 }
1721
1722 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001723 return SideEffects(kAllChangeBits | kAllDependOnBits);
1724 }
1725
1726 static SideEffects AllChanges() {
1727 return SideEffects(kAllChangeBits);
1728 }
1729
1730 static SideEffects AllDependencies() {
1731 return SideEffects(kAllDependOnBits);
1732 }
1733
1734 static SideEffects AllExceptGCDependency() {
1735 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1736 }
1737
1738 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001739 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001740 }
1741
Aart Bik34c3ba92015-07-20 14:08:59 -07001742 static SideEffects AllWrites() {
1743 return SideEffects(kAllWrites);
1744 }
1745
1746 static SideEffects AllReads() {
1747 return SideEffects(kAllReads);
1748 }
1749
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001750 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001751 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001752 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001753 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001754 }
1755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001756 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001757 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001758 }
1759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001761 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001762 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001763 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001764 }
1765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001766 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001767 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001768 }
1769
Artem Serovd1aa7d02018-06-22 11:35:46 +01001770 // Returns whether GC might happen across this instruction from the compiler perspective so
1771 // the next instruction in the IR would see that.
1772 //
1773 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001774 static SideEffects CanTriggerGC() {
1775 return SideEffects(1ULL << kCanTriggerGCBit);
1776 }
1777
Artem Serovd1aa7d02018-06-22 11:35:46 +01001778 // Returns whether the instruction must not be alive across a GC point.
1779 //
1780 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001781 static SideEffects DependsOnGC() {
1782 return SideEffects(1ULL << kDependsOnGCBit);
1783 }
1784
Aart Bik854a02b2015-07-14 16:07:00 -07001785 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001786 SideEffects Union(SideEffects other) const {
1787 return SideEffects(flags_ | other.flags_);
1788 }
1789
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 SideEffects Exclusion(SideEffects other) const {
1791 return SideEffects(flags_ & ~other.flags_);
1792 }
1793
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001794 void Add(SideEffects other) {
1795 flags_ |= other.flags_;
1796 }
1797
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001798 bool Includes(SideEffects other) const {
1799 return (other.flags_ & flags_) == other.flags_;
1800 }
1801
1802 bool HasSideEffects() const {
1803 return (flags_ & kAllChangeBits);
1804 }
1805
1806 bool HasDependencies() const {
1807 return (flags_ & kAllDependOnBits);
1808 }
1809
1810 // Returns true if there are no side effects or dependencies.
1811 bool DoesNothing() const {
1812 return flags_ == 0;
1813 }
1814
Aart Bik854a02b2015-07-14 16:07:00 -07001815 // Returns true if something is written.
1816 bool DoesAnyWrite() const {
1817 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001818 }
1819
Aart Bik854a02b2015-07-14 16:07:00 -07001820 // Returns true if something is read.
1821 bool DoesAnyRead() const {
1822 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001823 }
1824
Aart Bik854a02b2015-07-14 16:07:00 -07001825 // Returns true if potentially everything is written and read
1826 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001827 bool DoesAllReadWrite() const {
1828 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1829 }
1830
Aart Bik854a02b2015-07-14 16:07:00 -07001831 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001832 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001833 }
1834
Roland Levillain0d5a2812015-11-13 10:07:31 +00001835 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001836 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001837 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1838 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001839 }
1840
1841 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001842 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001843 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001844 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001845 for (int s = kLastBit; s >= 0; s--) {
1846 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1847 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1848 // This is a bit for the GC side effect.
1849 if (current_bit_is_set) {
1850 flags += "GC";
1851 }
Aart Bik854a02b2015-07-14 16:07:00 -07001852 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001853 } else {
1854 // This is a bit for the array/field analysis.
1855 // The underscore character stands for the 'can trigger GC' bit.
1856 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1857 if (current_bit_is_set) {
1858 flags += kDebug[s];
1859 }
1860 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1861 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1862 flags += "|";
1863 }
1864 }
Aart Bik854a02b2015-07-14 16:07:00 -07001865 }
1866 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001867 }
1868
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001869 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001870
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001871 private:
1872 static constexpr int kFieldArrayAnalysisBits = 9;
1873
1874 static constexpr int kFieldWriteOffset = 0;
1875 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1876 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1877 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1878
1879 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1880
1881 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1882 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1883 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1884 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1885
1886 static constexpr int kLastBit = kDependsOnGCBit;
1887 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1888
1889 // Aliases.
1890
1891 static_assert(kChangeBits == kDependOnBits,
1892 "the 'change' bits should match the 'depend on' bits.");
1893
1894 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1895 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1896 static constexpr uint64_t kAllWrites =
1897 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1898 static constexpr uint64_t kAllReads =
1899 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001900
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001901 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001902 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001903 int shift;
1904 switch (type) {
1905 case DataType::Type::kReference: shift = 0; break;
1906 case DataType::Type::kBool: shift = 1; break;
1907 case DataType::Type::kInt8: shift = 2; break;
1908 case DataType::Type::kUint16: shift = 3; break;
1909 case DataType::Type::kInt16: shift = 4; break;
1910 case DataType::Type::kInt32: shift = 5; break;
1911 case DataType::Type::kInt64: shift = 6; break;
1912 case DataType::Type::kFloat32: shift = 7; break;
1913 case DataType::Type::kFloat64: shift = 8; break;
1914 default:
1915 LOG(FATAL) << "Unexpected data type " << type;
1916 UNREACHABLE();
1917 }
Aart Bik854a02b2015-07-14 16:07:00 -07001918 DCHECK_LE(kFieldWriteOffset, shift);
1919 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001920 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001921 }
1922
1923 // Private constructor on direct flags value.
1924 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1925
1926 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001927};
1928
David Brazdiled596192015-01-23 10:39:45 +00001929// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001930class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001931 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001932 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001933 size_t number_of_vregs,
1934 ArtMethod* method,
1935 uint32_t dex_pc,
1936 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001937 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1938 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001939 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001940 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001941 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001942 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001943 }
1944
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001945 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1946 const HEnvironment& to_copy,
1947 HInstruction* holder)
1948 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001949 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001950 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001951 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001952 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001953
Vladimir Markoec32f642017-06-02 10:51:55 +01001954 void AllocateLocations() {
1955 DCHECK(locations_.empty());
1956 locations_.resize(vregs_.size());
1957 }
1958
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001959 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001960 if (parent_ != nullptr) {
1961 parent_->SetAndCopyParentChain(allocator, parent);
1962 } else {
1963 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1964 parent_->CopyFrom(parent);
1965 if (parent->GetParent() != nullptr) {
1966 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1967 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001968 }
David Brazdiled596192015-01-23 10:39:45 +00001969 }
1970
Vladimir Marko69d310e2017-10-09 14:12:23 +01001971 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001972 void CopyFrom(HEnvironment* environment);
1973
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001974 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1975 // input to the loop phi instead. This is for inserting instructions that
1976 // require an environment (like HDeoptimization) in the loop pre-header.
1977 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001978
1979 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001980 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001981 }
1982
1983 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001984 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001985 }
1986
David Brazdil1abb4192015-02-17 18:33:36 +00001987 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001988
Artem Serovca210e32017-12-15 13:43:20 +00001989 // Replaces the input at the position 'index' with the replacement; the replacement and old
1990 // input instructions' env_uses_ lists are adjusted. The function works similar to
1991 // HInstruction::ReplaceInput.
1992 void ReplaceInput(HInstruction* replacement, size_t index);
1993
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001994 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001995
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001996 HEnvironment* GetParent() const { return parent_; }
1997
1998 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001999 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002000 }
2001
2002 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002003 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002004 }
2005
2006 uint32_t GetDexPc() const {
2007 return dex_pc_;
2008 }
2009
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002010 ArtMethod* GetMethod() const {
2011 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002012 }
2013
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002014 HInstruction* GetHolder() const {
2015 return holder_;
2016 }
2017
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002018
2019 bool IsFromInlinedInvoke() const {
2020 return GetParent() != nullptr;
2021 }
2022
David Brazdiled596192015-01-23 10:39:45 +00002023 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002024 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2025 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002026 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002027 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002028 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002029
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002030 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002031 HInstruction* const holder_;
2032
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002033 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002034
2035 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2036};
2037
Vladimir Markof9f64412015-09-02 14:05:49 +01002038class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002039 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302040#define DECLARE_KIND(type, super) k##type,
2041 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002042 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302043 kLastInstructionKind
2044 };
2045#undef DECLARE_KIND
2046
2047 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002048 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2049
2050 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002051 : previous_(nullptr),
2052 next_(nullptr),
2053 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002054 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002055 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002056 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002057 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002058 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002059 locations_(nullptr),
2060 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002061 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002062 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002063 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302064 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002065 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002066 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2067 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002068
Dave Allison20dfc792014-06-16 20:44:29 -07002069 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002070
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002071
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002072 HInstruction* GetNext() const { return next_; }
2073 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002074
Calin Juravle77520bc2015-01-12 18:45:46 +00002075 HInstruction* GetNextDisregardingMoves() const;
2076 HInstruction* GetPreviousDisregardingMoves() const;
2077
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002078 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002079 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002080 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002081 bool IsInBlock() const { return block_ != nullptr; }
2082 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002083 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2084 bool IsIrreducibleLoopHeaderPhi() const {
2085 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2086 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002087
Vladimir Marko372f10e2016-05-17 16:30:10 +01002088 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2089
2090 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2091 // One virtual method is enough, just const_cast<> and then re-add the const.
2092 return ArrayRef<const HUserRecord<HInstruction*>>(
2093 const_cast<HInstruction*>(this)->GetInputRecords());
2094 }
2095
Vladimir Markoe9004912016-06-16 16:50:52 +01002096 HInputsRef GetInputs() {
2097 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002098 }
2099
Vladimir Markoe9004912016-06-16 16:50:52 +01002100 HConstInputsRef GetInputs() const {
2101 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002102 }
2103
2104 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002105 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002106
Aart Bik2767f4b2016-10-28 15:03:53 -07002107 bool HasInput(HInstruction* input) const {
2108 for (const HInstruction* i : GetInputs()) {
2109 if (i == input) {
2110 return true;
2111 }
2112 }
2113 return false;
2114 }
2115
Vladimir Marko372f10e2016-05-17 16:30:10 +01002116 void SetRawInputAt(size_t index, HInstruction* input) {
2117 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2118 }
2119
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002120 virtual void Accept(HGraphVisitor* visitor) = 0;
2121 virtual const char* DebugName() const = 0;
2122
Vladimir Markobd785672018-05-03 17:09:09 +01002123 DataType::Type GetType() const {
2124 return TypeField::Decode(GetPackedFields());
2125 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002126
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002127 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002128
2129 uint32_t GetDexPc() const { return dex_pc_; }
2130
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002131 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002132
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002133 // Can the instruction throw?
2134 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2135 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002136 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002137
2138 // Does the instruction always throw an exception unconditionally?
2139 virtual bool AlwaysThrows() const { return false; }
2140
David Brazdilec16f792015-08-19 15:04:01 +01002141 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002142
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002143 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002144 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002145
Calin Juravle10e244f2015-01-26 18:54:32 +00002146 // Does not apply for all instructions, but having this at top level greatly
2147 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002148 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002149 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002151 return true;
2152 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002153
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002154 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002155 return false;
2156 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002157
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002158 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2159 // with it. Otherwise return null.
2160 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002161 // Go over previous non-move instructions that are emitted at use site.
2162 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2163 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2164 if (prev_not_move->IsNullCheck()) {
2165 return prev_not_move->AsNullCheck();
2166 }
2167 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002168 }
2169 return nullptr;
2170 }
2171
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002172 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002173 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002174 }
2175
Calin Juravle2e768302015-07-28 14:41:11 +00002176 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002177
Calin Juravle61d544b2015-02-23 16:46:57 +00002178 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002180 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002181 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002182 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002183
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002184 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002185 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002186 // Note: fixup_end remains valid across push_front().
2187 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2188 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002189 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002190 uses_.push_front(*new_node);
2191 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002192 }
2193
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002194 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002195 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002196 // Note: env_fixup_end remains valid across push_front().
2197 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2198 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002199 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002200 env_uses_.push_front(*new_node);
2201 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002202 }
2203
David Brazdil1abb4192015-02-17 18:33:36 +00002204 void RemoveAsUserOfInput(size_t input) {
2205 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002206 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2207 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2208 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002209 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002210
Vladimir Marko372f10e2016-05-17 16:30:10 +01002211 void RemoveAsUserOfAllInputs() {
2212 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2213 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2214 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2215 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2216 }
2217 }
2218
David Brazdil1abb4192015-02-17 18:33:36 +00002219 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2220 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002221
Vladimir Marko46817b82016-03-29 12:21:58 +01002222 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2223 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2224 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002225 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002226 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002227 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002228
Aart Bikcc42be02016-10-20 16:14:16 -07002229 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002230 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002231 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002232 !CanThrow() &&
2233 !IsSuspendCheck() &&
2234 !IsControlFlow() &&
2235 !IsNativeDebugInfo() &&
2236 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002237 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002238 !IsMemoryBarrier() &&
2239 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002240 }
2241
Aart Bikcc42be02016-10-20 16:14:16 -07002242 bool IsDeadAndRemovable() const {
2243 return IsRemovable() && !HasUses();
2244 }
2245
Roland Levillain6c82d402014-10-13 16:10:27 +01002246 // Does this instruction strictly dominate `other_instruction`?
2247 // Returns false if this instruction and `other_instruction` are the same.
2248 // Aborts if this instruction and `other_instruction` are both phis.
2249 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002250
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002251 int GetId() const { return id_; }
2252 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002253
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002254 int GetSsaIndex() const { return ssa_index_; }
2255 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2256 bool HasSsaIndex() const { return ssa_index_ != -1; }
2257
2258 bool HasEnvironment() const { return environment_ != nullptr; }
2259 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002260 // Set the `environment_` field. Raw because this method does not
2261 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002262 void SetRawEnvironment(HEnvironment* environment) {
2263 DCHECK(environment_ == nullptr);
2264 DCHECK_EQ(environment->GetHolder(), this);
2265 environment_ = environment;
2266 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002267
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002268 void InsertRawEnvironment(HEnvironment* environment) {
2269 DCHECK(environment_ != nullptr);
2270 DCHECK_EQ(environment->GetHolder(), this);
2271 DCHECK(environment->GetParent() == nullptr);
2272 environment->parent_ = environment_;
2273 environment_ = environment;
2274 }
2275
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002276 void RemoveEnvironment();
2277
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002278 // Set the environment of this instruction, copying it from `environment`. While
2279 // copying, the uses lists are being updated.
2280 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002281 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002282 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002283 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002284 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002285 if (environment->GetParent() != nullptr) {
2286 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2287 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002288 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002289
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002290 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2291 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002292 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002293 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002294 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002295 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002296 if (environment->GetParent() != nullptr) {
2297 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2298 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002299 }
2300
Nicolas Geoffray39468442014-09-02 15:17:15 +01002301 // Returns the number of entries in the environment. Typically, that is the
2302 // number of dex registers in a method. It could be more in case of inlining.
2303 size_t EnvironmentSize() const;
2304
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002305 LocationSummary* GetLocations() const { return locations_; }
2306 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002307
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002308 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002309 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002310 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002311 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002312
Alexandre Rames188d4312015-04-09 18:30:21 +01002313 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2314 // uses of this instruction by `other` are *not* updated.
2315 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2316 ReplaceWith(other);
2317 other->ReplaceInput(this, use_index);
2318 }
2319
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002320 // Move `this` instruction before `cursor`
2321 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002322
Vladimir Markofb337ea2015-11-25 15:25:10 +00002323 // Move `this` before its first user and out of any loops. If there is no
2324 // out-of-loop user that dominates all other users, move the instruction
2325 // to the end of the out-of-loop common dominator of the user's blocks.
2326 //
2327 // This can be used only on non-throwing instructions with no side effects that
2328 // have at least one use but no environment uses.
2329 void MoveBeforeFirstUserAndOutOfLoops();
2330
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002331#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002332 bool Is##type() const;
2333
2334 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2335#undef INSTRUCTION_TYPE_CHECK
2336
2337#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002338 const H##type* As##type() const; \
2339 H##type* As##type();
2340
Vladimir Markoa90dd512018-05-04 15:04:45 +01002341 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2342#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002343
Artem Serovcced8ba2017-07-19 18:18:09 +01002344 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2345 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2346 // the instruction then the behaviour of this function is undefined.
2347 //
2348 // Note: It is semantically valid to create a clone of the instruction only until
2349 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2350 // copied.
2351 //
2352 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2353 // 'explicit HInstruction(const HInstruction& other)' for details.
2354 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2355 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2356 DebugName() << " " << GetId();
2357 UNREACHABLE();
2358 }
2359
2360 // Return whether instruction can be cloned (copied).
2361 virtual bool IsClonable() const { return false; }
2362
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002363 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002364 // TODO: this method is used by LICM and GVN with possibly different
2365 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002366 virtual bool CanBeMoved() const { return false; }
2367
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002368 // Returns whether any data encoded in the two instructions is equal.
2369 // This method does not look at the inputs. Both instructions must be
2370 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002371 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002372 return false;
2373 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002374
2375 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002376 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002377 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002378 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002379
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302380 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002381
2382 virtual size_t ComputeHashCode() const {
2383 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002384 for (const HInstruction* input : GetInputs()) {
2385 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002386 }
2387 return result;
2388 }
2389
2390 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002391 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002392 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002393
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002394 size_t GetLifetimePosition() const { return lifetime_position_; }
2395 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2396 LiveInterval* GetLiveInterval() const { return live_interval_; }
2397 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2398 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2399
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002400 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2401
2402 // Returns whether the code generation of the instruction will require to have access
2403 // to the current method. Such instructions are:
2404 // (1): Instructions that require an environment, as calling the runtime requires
2405 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002406 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2407 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002408 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002409 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002410 }
2411
Vladimir Markodc151b22015-10-15 18:02:30 +01002412 // Returns whether the code generation of the instruction will require to have access
2413 // to the dex cache of the current method's declaring class via the current method.
2414 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002415
Mark Mendellc4701932015-04-10 13:18:51 -04002416 // Does this instruction have any use in an environment before
2417 // control flow hits 'other'?
2418 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2419
2420 // Remove all references to environment uses of this instruction.
2421 // The caller must ensure that this is safe to do.
2422 void RemoveEnvironmentUsers();
2423
Vladimir Markoa1de9182016-02-25 11:37:38 +00002424 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2425 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002426
David Brazdil1abb4192015-02-17 18:33:36 +00002427 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002428 // If set, the machine code for this instruction is assumed to be generated by
2429 // its users. Used by liveness analysis to compute use positions accordingly.
2430 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2431 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302432 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2433 static constexpr size_t kFieldInstructionKindSize =
2434 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002435 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302436 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002437 static constexpr size_t kFieldTypeSize =
2438 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2439 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002440 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2441
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302442 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2443 "Too many generic packed fields");
2444
Vladimir Markobd785672018-05-03 17:09:09 +01002445 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2446
Vladimir Marko372f10e2016-05-17 16:30:10 +01002447 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2448 return GetInputRecords()[i];
2449 }
2450
2451 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2452 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2453 input_records[index] = input;
2454 }
David Brazdil1abb4192015-02-17 18:33:36 +00002455
Vladimir Markoa1de9182016-02-25 11:37:38 +00002456 uint32_t GetPackedFields() const {
2457 return packed_fields_;
2458 }
2459
2460 template <size_t flag>
2461 bool GetPackedFlag() const {
2462 return (packed_fields_ & (1u << flag)) != 0u;
2463 }
2464
2465 template <size_t flag>
2466 void SetPackedFlag(bool value = true) {
2467 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2468 }
2469
2470 template <typename BitFieldType>
2471 typename BitFieldType::value_type GetPackedField() const {
2472 return BitFieldType::Decode(packed_fields_);
2473 }
2474
2475 template <typename BitFieldType>
2476 void SetPackedField(typename BitFieldType::value_type value) {
2477 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2478 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2479 }
2480
Artem Serovcced8ba2017-07-19 18:18:09 +01002481 // Copy construction for the instruction (used for Clone function).
2482 //
2483 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2484 // prepare_for_register_allocator are not copied (set to default values).
2485 //
2486 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2487 // fine for most of them. However for some of the instructions a custom copy constructor must be
2488 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2489 // for copying them).
2490 explicit HInstruction(const HInstruction& other)
2491 : previous_(nullptr),
2492 next_(nullptr),
2493 block_(nullptr),
2494 dex_pc_(other.dex_pc_),
2495 id_(-1),
2496 ssa_index_(-1),
2497 packed_fields_(other.packed_fields_),
2498 environment_(nullptr),
2499 locations_(nullptr),
2500 live_interval_(nullptr),
2501 lifetime_position_(kNoLifetime),
2502 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302503 reference_type_handle_(other.reference_type_handle_) {
2504 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002505
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002506 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302507 using InstructionKindField =
2508 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2509
Vladimir Marko46817b82016-03-29 12:21:58 +01002510 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2511 auto before_use_node = uses_.before_begin();
2512 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2513 HInstruction* user = use_node->GetUser();
2514 size_t input_index = use_node->GetIndex();
2515 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2516 before_use_node = use_node;
2517 }
2518 }
2519
2520 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2521 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2522 if (next != uses_.end()) {
2523 HInstruction* next_user = next->GetUser();
2524 size_t next_index = next->GetIndex();
2525 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2526 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2527 }
2528 }
2529
2530 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2531 auto before_env_use_node = env_uses_.before_begin();
2532 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2533 HEnvironment* user = env_use_node->GetUser();
2534 size_t input_index = env_use_node->GetIndex();
2535 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2536 before_env_use_node = env_use_node;
2537 }
2538 }
2539
2540 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2541 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2542 if (next != env_uses_.end()) {
2543 HEnvironment* next_user = next->GetUser();
2544 size_t next_index = next->GetIndex();
2545 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2546 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2547 }
2548 }
David Brazdil1abb4192015-02-17 18:33:36 +00002549
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002550 HInstruction* previous_;
2551 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002552 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002553 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002554
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002555 // An instruction gets an id when it is added to the graph.
2556 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002557 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002558 int id_;
2559
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002560 // When doing liveness analysis, instructions that have uses get an SSA index.
2561 int ssa_index_;
2562
Vladimir Markoa1de9182016-02-25 11:37:38 +00002563 // Packed fields.
2564 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002565
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002566 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002567 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002568
2569 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002570 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002571
Nicolas Geoffray39468442014-09-02 15:17:15 +01002572 // The environment associated with this instruction. Not null if the instruction
2573 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002574 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002575
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002576 // Set by the code generator.
2577 LocationSummary* locations_;
2578
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002579 // Set by the liveness analysis.
2580 LiveInterval* live_interval_;
2581
2582 // Set by the liveness analysis, this is the position in a linear
2583 // order of blocks where this instruction's live interval start.
2584 size_t lifetime_position_;
2585
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002586 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002587
Vladimir Markoa1de9182016-02-25 11:37:38 +00002588 // The reference handle part of the reference type info.
2589 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002590 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002591 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002592
David Brazdil1abb4192015-02-17 18:33:36 +00002593 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002594 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002595 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002596 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002597 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002598};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002599std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002600
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002601// Iterates over the instructions, while preserving the next instruction
2602// in case the current instruction gets removed from the list by the user
2603// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002604class HInstructionIterator : public ValueObject {
2605 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002606 explicit HInstructionIterator(const HInstructionList& instructions)
2607 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002608 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002609 }
2610
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002611 bool Done() const { return instruction_ == nullptr; }
2612 HInstruction* Current() const { return instruction_; }
2613 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002614 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002615 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002616 }
2617
2618 private:
2619 HInstruction* instruction_;
2620 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002621
2622 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002623};
2624
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002625// Iterates over the instructions without saving the next instruction,
2626// therefore handling changes in the graph potentially made by the user
2627// of this iterator.
2628class HInstructionIteratorHandleChanges : public ValueObject {
2629 public:
2630 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2631 : instruction_(instructions.first_instruction_) {
2632 }
2633
2634 bool Done() const { return instruction_ == nullptr; }
2635 HInstruction* Current() const { return instruction_; }
2636 void Advance() {
2637 instruction_ = instruction_->GetNext();
2638 }
2639
2640 private:
2641 HInstruction* instruction_;
2642
2643 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2644};
2645
2646
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002647class HBackwardInstructionIterator : public ValueObject {
2648 public:
2649 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2650 : instruction_(instructions.last_instruction_) {
2651 next_ = Done() ? nullptr : instruction_->GetPrevious();
2652 }
2653
2654 bool Done() const { return instruction_ == nullptr; }
2655 HInstruction* Current() const { return instruction_; }
2656 void Advance() {
2657 instruction_ = next_;
2658 next_ = Done() ? nullptr : instruction_->GetPrevious();
2659 }
2660
2661 private:
2662 HInstruction* instruction_;
2663 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002664
2665 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002666};
2667
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002668class HVariableInputSizeInstruction : public HInstruction {
2669 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002670 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002671 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002672 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2673 }
2674
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002675 void AddInput(HInstruction* input);
2676 void InsertInputAt(size_t index, HInstruction* input);
2677 void RemoveInputAt(size_t index);
2678
Igor Murashkind01745e2017-04-05 16:40:31 -07002679 // Removes all the inputs.
2680 // Also removes this instructions from each input's use list
2681 // (for non-environment uses only).
2682 void RemoveAllInputs();
2683
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002684 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302685 HVariableInputSizeInstruction(InstructionKind inst_kind,
2686 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002687 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002688 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002689 size_t number_of_inputs,
2690 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302691 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002692 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002693 HVariableInputSizeInstruction(InstructionKind inst_kind,
2694 DataType::Type type,
2695 SideEffects side_effects,
2696 uint32_t dex_pc,
2697 ArenaAllocator* allocator,
2698 size_t number_of_inputs,
2699 ArenaAllocKind kind)
2700 : HInstruction(inst_kind, type, side_effects, dex_pc),
2701 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002702
Artem Serovcced8ba2017-07-19 18:18:09 +01002703 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002704
Artem Serovcced8ba2017-07-19 18:18:09 +01002705 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002706};
2707
Vladimir Markof9f64412015-09-02 14:05:49 +01002708template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002709class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002710 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002711 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302712 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002713 HExpression<N>(InstructionKind kind,
2714 DataType::Type type,
2715 SideEffects side_effects,
2716 uint32_t dex_pc)
2717 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2718 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002719
Vladimir Marko372f10e2016-05-17 16:30:10 +01002720 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002721 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002722 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002723 }
2724
Artem Serovcced8ba2017-07-19 18:18:09 +01002725 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002726 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002727
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002728 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002729 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002730
2731 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002732};
2733
Vladimir Markobd785672018-05-03 17:09:09 +01002734// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002735template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002736class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002737 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002738 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002739
Vladimir Markobd785672018-05-03 17:09:09 +01002740 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002741
Vladimir Marko372f10e2016-05-17 16:30:10 +01002742 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002743 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002744 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002745 }
2746
Artem Serovcced8ba2017-07-19 18:18:09 +01002747 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002748 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002749
Vladimir Markof9f64412015-09-02 14:05:49 +01002750 private:
2751 friend class SsaBuilder;
2752};
2753
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002754// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2755// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002756class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002757 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002758 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002759 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302760 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002761
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002762 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002763
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002764 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002765
Artem Serovcced8ba2017-07-19 18:18:09 +01002766 protected:
2767 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002768};
2769
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002770// Represents dex's RETURN opcodes. A HReturn is a control flow
2771// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002772class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002773 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002774 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002775 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002776 SetRawInputAt(0, value);
2777 }
2778
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002779 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002780
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002781 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002782
Artem Serovcced8ba2017-07-19 18:18:09 +01002783 protected:
2784 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002785};
2786
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002787class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002788 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002789 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002790 uint32_t reg_number,
2791 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002793 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002794 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302795 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002796 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002797 SideEffects::None(),
2798 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002799 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002800 number_of_inputs,
2801 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002802 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002804 // Phis are constructed live and marked dead if conflicting or unused.
2805 // Individual steps of SsaBuilder should assume that if a phi has been
2806 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2807 SetPackedFlag<kFlagIsLive>(true);
2808 SetPackedFlag<kFlagCanBeNull>(true);
2809 }
2810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002811 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002812
David Brazdildee58d62016-04-07 09:54:26 +00002813 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 static DataType::Type ToPhiType(DataType::Type type) {
2815 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002816 }
2817
2818 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002820 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002821 // Make sure that only valid type changes occur. The following are allowed:
2822 // (1) int -> float/ref (primitive type propagation),
2823 // (2) long -> double (primitive type propagation).
2824 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002825 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2826 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2827 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002828 SetPackedField<TypeField>(new_type);
2829 }
2830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002831 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002832 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2833
2834 uint32_t GetRegNumber() const { return reg_number_; }
2835
2836 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2837 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2838 bool IsDead() const { return !IsLive(); }
2839 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2840
Vladimir Markoe9004912016-06-16 16:50:52 +01002841 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002842 return other != nullptr
2843 && other->IsPhi()
2844 && other->AsPhi()->GetBlock() == GetBlock()
2845 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2846 }
2847
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002848 bool HasEquivalentPhi() const {
2849 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2850 return true;
2851 }
2852 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2853 return true;
2854 }
2855 return false;
2856 }
2857
David Brazdildee58d62016-04-07 09:54:26 +00002858 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2859 // An equivalent phi is a phi having the same dex register and type.
2860 // It assumes that phis with the same dex register are adjacent.
2861 HPhi* GetNextEquivalentPhiWithSameType() {
2862 HInstruction* next = GetNext();
2863 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2864 if (next->GetType() == GetType()) {
2865 return next->AsPhi();
2866 }
2867 next = next->GetNext();
2868 }
2869 return nullptr;
2870 }
2871
2872 DECLARE_INSTRUCTION(Phi);
2873
Artem Serovcced8ba2017-07-19 18:18:09 +01002874 protected:
2875 DEFAULT_COPY_CONSTRUCTOR(Phi);
2876
David Brazdildee58d62016-04-07 09:54:26 +00002877 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002878 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002879 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2880 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2881 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002882
David Brazdildee58d62016-04-07 09:54:26 +00002883 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002884};
2885
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002886// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002887// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002888// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002889class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002890 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302891 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002892 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302893 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002894
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002895 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002896
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002897 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002898
Artem Serovcced8ba2017-07-19 18:18:09 +01002899 protected:
2900 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002901};
2902
2903// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002904class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002905 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302906 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002907 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302908 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002910 bool IsClonable() const override { return true; }
2911 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002912
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002913 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002914 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002915 }
2916
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002917 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002918
Artem Serovcced8ba2017-07-19 18:18:09 +01002919 protected:
2920 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002921};
2922
Roland Levillain9867bc72015-08-05 10:21:34 +01002923class HConstant : public HExpression<0> {
2924 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302925 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2926 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2927 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002928
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002929 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002930
Roland Levillain1a653882016-03-18 18:05:57 +00002931 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002932 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002933 // Is this constant 0 in the arithmetic sense?
2934 virtual bool IsArithmeticZero() const { return false; }
2935 // Is this constant a 0-bit pattern?
2936 virtual bool IsZeroBitPattern() const { return false; }
2937 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002938 virtual bool IsOne() const { return false; }
2939
David Brazdil77a48ae2015-09-15 12:34:04 +00002940 virtual uint64_t GetValueAsUint64() const = 0;
2941
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002942 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002943
Artem Serovcced8ba2017-07-19 18:18:09 +01002944 protected:
2945 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002946};
2947
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002948class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002949 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002950 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002951 return true;
2952 }
2953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002954 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002955
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002956 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002957
Roland Levillain1a653882016-03-18 18:05:57 +00002958 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002959 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002960
Roland Levillain9867bc72015-08-05 10:21:34 +01002961 DECLARE_INSTRUCTION(NullConstant);
2962
Artem Serovcced8ba2017-07-19 18:18:09 +01002963 protected:
2964 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2965
Roland Levillain9867bc72015-08-05 10:21:34 +01002966 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302968 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2969 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002970
2971 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002972};
2973
2974// Constants of the type int. Those can be from Dex instructions, or
2975// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002976class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002977 public:
2978 int32_t GetValue() const { return value_; }
2979
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002980 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002981 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2982 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002984 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002985 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002986 return other->AsIntConstant()->value_ == value_;
2987 }
2988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002989 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002991 bool IsMinusOne() const override { return GetValue() == -1; }
2992 bool IsArithmeticZero() const override { return GetValue() == 0; }
2993 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2994 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002995
Roland Levillain1a653882016-03-18 18:05:57 +00002996 // Integer constants are used to encode Boolean values as well,
2997 // where 1 means true and 0 means false.
2998 bool IsTrue() const { return GetValue() == 1; }
2999 bool IsFalse() const { return GetValue() == 0; }
3000
Roland Levillain9867bc72015-08-05 10:21:34 +01003001 DECLARE_INSTRUCTION(IntConstant);
3002
Artem Serovcced8ba2017-07-19 18:18:09 +01003003 protected:
3004 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3005
Roland Levillain9867bc72015-08-05 10:21:34 +01003006 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003007 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303008 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3009 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003010 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303011 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3012 value_(value ? 1 : 0) {
3013 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003014
3015 const int32_t value_;
3016
3017 friend class HGraph;
3018 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3019 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003020};
3021
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003022class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003023 public:
3024 int64_t GetValue() const { return value_; }
3025
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003026 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003027
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003028 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003029 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003030 return other->AsLongConstant()->value_ == value_;
3031 }
3032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003033 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003035 bool IsMinusOne() const override { return GetValue() == -1; }
3036 bool IsArithmeticZero() const override { return GetValue() == 0; }
3037 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3038 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003039
3040 DECLARE_INSTRUCTION(LongConstant);
3041
Artem Serovcced8ba2017-07-19 18:18:09 +01003042 protected:
3043 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3044
Roland Levillain9867bc72015-08-05 10:21:34 +01003045 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003046 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303047 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3048 value_(value) {
3049 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003050
3051 const int64_t value_;
3052
3053 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003054};
Dave Allison20dfc792014-06-16 20:44:29 -07003055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003056class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003057 public:
3058 float GetValue() const { return value_; }
3059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003060 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003061 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3062 }
3063
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003064 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065 DCHECK(other->IsFloatConstant()) << other->DebugName();
3066 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3067 }
3068
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003069 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003071 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003072 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3073 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003074 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003075 return std::fpclassify(value_) == FP_ZERO;
3076 }
3077 bool IsArithmeticPositiveZero() const {
3078 return IsArithmeticZero() && !std::signbit(value_);
3079 }
3080 bool IsArithmeticNegativeZero() const {
3081 return IsArithmeticZero() && std::signbit(value_);
3082 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003083 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003084 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003085 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003086 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003087 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3088 }
3089 bool IsNaN() const {
3090 return std::isnan(value_);
3091 }
3092
3093 DECLARE_INSTRUCTION(FloatConstant);
3094
Artem Serovcced8ba2017-07-19 18:18:09 +01003095 protected:
3096 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3097
Roland Levillain31dd3d62016-02-16 12:21:02 +00003098 private:
3099 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303100 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3101 value_(value) {
3102 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003103 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303104 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3105 value_(bit_cast<float, int32_t>(value)) {
3106 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003107
3108 const float value_;
3109
3110 // Only the SsaBuilder and HGraph can create floating-point constants.
3111 friend class SsaBuilder;
3112 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113};
3114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003115class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003116 public:
3117 double GetValue() const { return value_; }
3118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003119 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003121 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003122 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3123 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3124 }
3125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003126 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003128 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003129 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3130 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003131 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003132 return std::fpclassify(value_) == FP_ZERO;
3133 }
3134 bool IsArithmeticPositiveZero() const {
3135 return IsArithmeticZero() && !std::signbit(value_);
3136 }
3137 bool IsArithmeticNegativeZero() const {
3138 return IsArithmeticZero() && std::signbit(value_);
3139 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003140 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003141 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003142 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003143 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003144 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3145 }
3146 bool IsNaN() const {
3147 return std::isnan(value_);
3148 }
3149
3150 DECLARE_INSTRUCTION(DoubleConstant);
3151
Artem Serovcced8ba2017-07-19 18:18:09 +01003152 protected:
3153 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3154
Roland Levillain31dd3d62016-02-16 12:21:02 +00003155 private:
3156 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303157 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3158 value_(value) {
3159 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003160 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303161 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3162 value_(bit_cast<double, int64_t>(value)) {
3163 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164
3165 const double value_;
3166
3167 // Only the SsaBuilder and HGraph can create floating-point constants.
3168 friend class SsaBuilder;
3169 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003170};
3171
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003172// Conditional branch. A block ending with an HIf instruction must have
3173// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003174class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003175 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003176 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003177 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003178 SetRawInputAt(0, input);
3179 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003180
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003181 bool IsClonable() const override { return true; }
3182 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003183
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003184 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003185 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003186 }
3187
3188 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003189 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003190 }
3191
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003192 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003193
Artem Serovcced8ba2017-07-19 18:18:09 +01003194 protected:
3195 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003196};
3197
David Brazdilfc6a86a2015-06-26 10:33:45 +00003198
3199// Abstract instruction which marks the beginning and/or end of a try block and
3200// links it to the respective exception handlers. Behaves the same as a Goto in
3201// non-exceptional control flow.
3202// Normal-flow successor is stored at index zero, exception handlers under
3203// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003204class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003205 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003206 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003207 kEntry,
3208 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003209 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003210 };
3211
Artem Serovd1aa7d02018-06-22 11:35:46 +01003212 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3213 // across the catch block entering edges as GC might happen during throwing an exception.
3214 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3215 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003216 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003217 : HExpression(kTryBoundary,
3218 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3219 : SideEffects::None(),
3220 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003221 SetPackedField<BoundaryKindField>(kind);
3222 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003223
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003224 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003225
3226 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003227 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003228
David Brazdild26a4112015-11-10 11:07:31 +00003229 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3230 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3231 }
3232
David Brazdilfc6a86a2015-06-26 10:33:45 +00003233 // Returns whether `handler` is among its exception handlers (non-zero index
3234 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003235 bool HasExceptionHandler(const HBasicBlock& handler) const {
3236 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003237 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003238 }
3239
3240 // If not present already, adds `handler` to its block's list of exception
3241 // handlers.
3242 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003243 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003244 GetBlock()->AddSuccessor(handler);
3245 }
3246 }
3247
Vladimir Markoa1de9182016-02-25 11:37:38 +00003248 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3249 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003250
David Brazdilffee3d32015-07-06 11:48:53 +01003251 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3252
David Brazdilfc6a86a2015-06-26 10:33:45 +00003253 DECLARE_INSTRUCTION(TryBoundary);
3254
Artem Serovcced8ba2017-07-19 18:18:09 +01003255 protected:
3256 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3257
David Brazdilfc6a86a2015-06-26 10:33:45 +00003258 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003259 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3260 static constexpr size_t kFieldBoundaryKindSize =
3261 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3262 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3263 kFieldBoundaryKind + kFieldBoundaryKindSize;
3264 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3265 "Too many packed fields.");
3266 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003267};
3268
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003269// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003270class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003271 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003272 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3273 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003274 HDeoptimize(ArenaAllocator* allocator,
3275 HInstruction* cond,
3276 DeoptimizationKind kind,
3277 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003278 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303279 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003280 SideEffects::All(),
3281 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003282 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003283 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003284 kArenaAllocMisc) {
3285 SetPackedFlag<kFieldCanBeMoved>(false);
3286 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003287 SetRawInputAt(0, cond);
3288 }
3289
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003290 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003291
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003292 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3293 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3294 // instead of `guard`.
3295 // We set CanTriggerGC to prevent any intermediate address to be live
3296 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003297 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003298 HInstruction* cond,
3299 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003300 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003301 uint32_t dex_pc)
3302 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303303 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003304 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003305 SideEffects::CanTriggerGC(),
3306 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003307 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003308 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003309 kArenaAllocMisc) {
3310 SetPackedFlag<kFieldCanBeMoved>(true);
3311 SetPackedField<DeoptimizeKindField>(kind);
3312 SetRawInputAt(0, cond);
3313 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003314 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003316 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003317
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003318 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003319 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3320 }
3321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003322 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003323
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003324 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003325
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003326 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003327
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003328 bool GuardsAnInput() const {
3329 return InputCount() == 2;
3330 }
3331
3332 HInstruction* GuardedInput() const {
3333 DCHECK(GuardsAnInput());
3334 return InputAt(1);
3335 }
3336
3337 void RemoveGuard() {
3338 RemoveInputAt(1);
3339 }
3340
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003341 DECLARE_INSTRUCTION(Deoptimize);
3342
Artem Serovcced8ba2017-07-19 18:18:09 +01003343 protected:
3344 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3345
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003346 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003347 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3348 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3349 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003350 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003351 static constexpr size_t kNumberOfDeoptimizePackedBits =
3352 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3353 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3354 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003355 using DeoptimizeKindField =
3356 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003357};
3358
Mingyao Yang063fc772016-08-02 11:02:54 -07003359// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3360// The compiled code checks this flag value in a guard before devirtualized call and
3361// if it's true, starts to do deoptimization.
3362// It has a 4-byte slot on stack.
3363// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003364class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003365 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003366 // CHA guards are only optimized in a separate pass and it has no side effects
3367 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003368 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303369 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003370 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303371 SideEffects::None(),
3372 dex_pc,
3373 allocator,
3374 0,
3375 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003376 }
3377
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003378 // We do all CHA guard elimination/motion in a single pass, after which there is no
3379 // further guard elimination/motion since a guard might have been used for justification
3380 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3381 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003382 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003383
3384 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3385
Artem Serovcced8ba2017-07-19 18:18:09 +01003386 protected:
3387 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003388};
3389
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003390// Represents the ArtMethod that was passed as a first argument to
3391// the method. It is used by instructions that depend on it, like
3392// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003393class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003394 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003395 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303396 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3397 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003398
3399 DECLARE_INSTRUCTION(CurrentMethod);
3400
Artem Serovcced8ba2017-07-19 18:18:09 +01003401 protected:
3402 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003403};
3404
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003405// Fetches an ArtMethod from the virtual table or the interface method table
3406// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003407class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003408 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003409 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003410 kVTable,
3411 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003412 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003413 };
3414 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003415 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003416 TableKind kind,
3417 size_t index,
3418 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303419 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003420 index_(index) {
3421 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003422 SetRawInputAt(0, cls);
3423 }
3424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003425 bool IsClonable() const override { return true; }
3426 bool CanBeMoved() const override { return true; }
3427 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003428 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003429 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003430 }
3431
Vladimir Markoa1de9182016-02-25 11:37:38 +00003432 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003433 size_t GetIndex() const { return index_; }
3434
3435 DECLARE_INSTRUCTION(ClassTableGet);
3436
Artem Serovcced8ba2017-07-19 18:18:09 +01003437 protected:
3438 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3439
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003440 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003441 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003442 static constexpr size_t kFieldTableKindSize =
3443 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3444 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3445 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3446 "Too many packed fields.");
3447 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3448
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003449 // The index of the ArtMethod in the table.
3450 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003451};
3452
Mark Mendellfe57faa2015-09-18 09:26:15 -04003453// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3454// have one successor for each entry in the switch table, and the final successor
3455// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003456class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003457 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003458 HPackedSwitch(int32_t start_value,
3459 uint32_t num_entries,
3460 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003461 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003462 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003463 start_value_(start_value),
3464 num_entries_(num_entries) {
3465 SetRawInputAt(0, input);
3466 }
3467
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003468 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003469
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003470 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003471
3472 int32_t GetStartValue() const { return start_value_; }
3473
Vladimir Marko211c2112015-09-24 16:52:33 +01003474 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003475
3476 HBasicBlock* GetDefaultBlock() const {
3477 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003478 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003479 }
3480 DECLARE_INSTRUCTION(PackedSwitch);
3481
Artem Serovcced8ba2017-07-19 18:18:09 +01003482 protected:
3483 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3484
Mark Mendellfe57faa2015-09-18 09:26:15 -04003485 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003486 const int32_t start_value_;
3487 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003488};
3489
Roland Levillain88cb1752014-10-20 16:36:47 +01003490class HUnaryOperation : public HExpression<1> {
3491 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303492 HUnaryOperation(InstructionKind kind,
3493 DataType::Type result_type,
3494 HInstruction* input,
3495 uint32_t dex_pc = kNoDexPc)
3496 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003497 SetRawInputAt(0, input);
3498 }
3499
Artem Serovcced8ba2017-07-19 18:18:09 +01003500 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003501 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003502
Roland Levillain88cb1752014-10-20 16:36:47 +01003503 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003504 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003506 bool CanBeMoved() const override { return true; }
3507 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003508 return true;
3509 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003510
Roland Levillain31dd3d62016-02-16 12:21:02 +00003511 // Try to statically evaluate `this` and return a HConstant
3512 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003513 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003514 HConstant* TryStaticEvaluation() const;
3515
3516 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003517 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3518 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003519 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3520 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003521
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003522 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003523
Artem Serovcced8ba2017-07-19 18:18:09 +01003524 protected:
3525 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003526};
3527
Dave Allison20dfc792014-06-16 20:44:29 -07003528class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003529 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303530 HBinaryOperation(InstructionKind kind,
3531 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003532 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003533 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003534 SideEffects side_effects = SideEffects::None(),
3535 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303536 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003537 SetRawInputAt(0, left);
3538 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003539 }
3540
Artem Serovcced8ba2017-07-19 18:18:09 +01003541 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003542 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003543
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003544 HInstruction* GetLeft() const { return InputAt(0); }
3545 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003546 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003547
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003548 virtual bool IsCommutative() const { return false; }
3549
3550 // Put constant on the right.
3551 // Returns whether order is changed.
3552 bool OrderInputsWithConstantOnTheRight() {
3553 HInstruction* left = InputAt(0);
3554 HInstruction* right = InputAt(1);
3555 if (left->IsConstant() && !right->IsConstant()) {
3556 ReplaceInput(right, 0);
3557 ReplaceInput(left, 1);
3558 return true;
3559 }
3560 return false;
3561 }
3562
3563 // Order inputs by instruction id, but favor constant on the right side.
3564 // This helps GVN for commutative ops.
3565 void OrderInputs() {
3566 DCHECK(IsCommutative());
3567 HInstruction* left = InputAt(0);
3568 HInstruction* right = InputAt(1);
3569 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3570 return;
3571 }
3572 if (OrderInputsWithConstantOnTheRight()) {
3573 return;
3574 }
3575 // Order according to instruction id.
3576 if (left->GetId() > right->GetId()) {
3577 ReplaceInput(right, 0);
3578 ReplaceInput(left, 1);
3579 }
3580 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003582 bool CanBeMoved() const override { return true; }
3583 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003584 return true;
3585 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003586
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 // Try to statically evaluate `this` and return a HConstant
3588 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003589 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003590 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003591
3592 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003593 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3594 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003595 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3596 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003597 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003598 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3599 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003600 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3601 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003602 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3603 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003604 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003605 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3606 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003607
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003608 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003609 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003610 HConstant* GetConstantRight() const;
3611
3612 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003613 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003614 HInstruction* GetLeastConstantLeft() const;
3615
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003616 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003617
Artem Serovcced8ba2017-07-19 18:18:09 +01003618 protected:
3619 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003620};
3621
Mark Mendellc4701932015-04-10 13:18:51 -04003622// The comparison bias applies for floating point operations and indicates how NaN
3623// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003624enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003625 kNoBias, // bias is not applicable (i.e. for long operation)
3626 kGtBias, // return 1 for NaN comparisons
3627 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003628 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003629};
3630
Roland Levillain31dd3d62016-02-16 12:21:02 +00003631std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3632
Dave Allison20dfc792014-06-16 20:44:29 -07003633class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003634 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303635 HCondition(InstructionKind kind,
3636 HInstruction* first,
3637 HInstruction* second,
3638 uint32_t dex_pc = kNoDexPc)
3639 : HBinaryOperation(kind,
3640 DataType::Type::kBool,
3641 first,
3642 second,
3643 SideEffects::None(),
3644 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003645 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3646 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003647
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003648 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003649 // `instruction`, and disregard moves in between.
3650 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003651
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003652 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003653
3654 virtual IfCondition GetCondition() const = 0;
3655
Mark Mendellc4701932015-04-10 13:18:51 -04003656 virtual IfCondition GetOppositeCondition() const = 0;
3657
Vladimir Markoa1de9182016-02-25 11:37:38 +00003658 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003659 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3660
Vladimir Markoa1de9182016-02-25 11:37:38 +00003661 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3662 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003663
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003664 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003665 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003666 }
3667
Roland Levillain4fa13f62015-07-06 18:11:54 +01003668 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003669 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003670 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003671 if (if_cond == kCondNE) {
3672 return true;
3673 } else if (if_cond == kCondEQ) {
3674 return false;
3675 }
3676 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003677 }
3678
3679 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003680 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003681 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003682 if (if_cond == kCondEQ) {
3683 return true;
3684 } else if (if_cond == kCondNE) {
3685 return false;
3686 }
3687 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003688 }
3689
Roland Levillain31dd3d62016-02-16 12:21:02 +00003690 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003691 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003692 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003693 static constexpr size_t kFieldComparisonBiasSize =
3694 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3695 static constexpr size_t kNumberOfConditionPackedBits =
3696 kFieldComparisonBias + kFieldComparisonBiasSize;
3697 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3698 using ComparisonBiasField =
3699 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3700
Roland Levillain31dd3d62016-02-16 12:21:02 +00003701 template <typename T>
3702 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3703
3704 template <typename T>
3705 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003706 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003707 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3708 // Handle the bias.
3709 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3710 }
3711
3712 // Return an integer constant containing the result of a condition evaluated at compile time.
3713 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3714 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3715 }
3716
Artem Serovcced8ba2017-07-19 18:18:09 +01003717 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003718};
3719
3720// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003721class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003722 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003723 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303724 : HCondition(kEqual, first, second, dex_pc) {
3725 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003726
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003727 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003728
Vladimir Marko9e23df52015-11-10 17:14:35 +00003729 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003730 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003731 return MakeConstantCondition(true, GetDexPc());
3732 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003733 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003734 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3735 }
3736 // In the following Evaluate methods, a HCompare instruction has
3737 // been merged into this HEqual instruction; evaluate it as
3738 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003739 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3741 GetDexPc());
3742 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003743 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003744 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3745 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003746 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003747 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003748 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003749
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003750 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003752 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003753 return kCondEQ;
3754 }
3755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003756 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003757 return kCondNE;
3758 }
3759
Artem Serovcced8ba2017-07-19 18:18:09 +01003760 protected:
3761 DEFAULT_COPY_CONSTRUCTOR(Equal);
3762
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003763 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003764 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003765};
3766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003767class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003768 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303769 HNotEqual(HInstruction* first, HInstruction* second,
3770 uint32_t dex_pc = kNoDexPc)
3771 : HCondition(kNotEqual, first, second, dex_pc) {
3772 }
Dave Allison20dfc792014-06-16 20:44:29 -07003773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003774 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003775
Vladimir Marko9e23df52015-11-10 17:14:35 +00003776 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003777 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003778 return MakeConstantCondition(false, GetDexPc());
3779 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003780 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003781 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3782 }
3783 // In the following Evaluate methods, a HCompare instruction has
3784 // been merged into this HNotEqual instruction; evaluate it as
3785 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003786 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003787 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3788 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003789 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003790 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3791 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003792 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003793 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003794 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003795
Dave Allison20dfc792014-06-16 20:44:29 -07003796 DECLARE_INSTRUCTION(NotEqual);
3797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003798 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003799 return kCondNE;
3800 }
3801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003802 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003803 return kCondEQ;
3804 }
3805
Artem Serovcced8ba2017-07-19 18:18:09 +01003806 protected:
3807 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3808
Dave Allison20dfc792014-06-16 20:44:29 -07003809 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003810 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003811};
3812
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003813class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003814 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303815 HLessThan(HInstruction* first, HInstruction* second,
3816 uint32_t dex_pc = kNoDexPc)
3817 : HCondition(kLessThan, first, second, dex_pc) {
3818 }
Dave Allison20dfc792014-06-16 20:44:29 -07003819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003820 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003821 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003822 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003823 // In the following Evaluate methods, a HCompare instruction has
3824 // been merged into this HLessThan instruction; evaluate it as
3825 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003826 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003827 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3828 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003829 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003830 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3831 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003832 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003833 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003834 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003835
Dave Allison20dfc792014-06-16 20:44:29 -07003836 DECLARE_INSTRUCTION(LessThan);
3837
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003838 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003839 return kCondLT;
3840 }
3841
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003842 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003843 return kCondGE;
3844 }
3845
Artem Serovcced8ba2017-07-19 18:18:09 +01003846 protected:
3847 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3848
Dave Allison20dfc792014-06-16 20:44:29 -07003849 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003850 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003851};
3852
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003853class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003854 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303855 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3856 uint32_t dex_pc = kNoDexPc)
3857 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3858 }
Dave Allison20dfc792014-06-16 20:44:29 -07003859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003862 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003863 // In the following Evaluate methods, a HCompare instruction has
3864 // been merged into this HLessThanOrEqual instruction; evaluate it as
3865 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003866 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003867 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3868 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003869 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003870 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3871 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003872 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003873 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003874 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003875
Dave Allison20dfc792014-06-16 20:44:29 -07003876 DECLARE_INSTRUCTION(LessThanOrEqual);
3877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003878 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003879 return kCondLE;
3880 }
3881
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003882 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003883 return kCondGT;
3884 }
3885
Artem Serovcced8ba2017-07-19 18:18:09 +01003886 protected:
3887 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3888
Dave Allison20dfc792014-06-16 20:44:29 -07003889 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003890 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003891};
3892
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003893class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003894 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003895 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303896 : HCondition(kGreaterThan, first, second, dex_pc) {
3897 }
Dave Allison20dfc792014-06-16 20:44:29 -07003898
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003899 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003900 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003901 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003902 // In the following Evaluate methods, a HCompare instruction has
3903 // been merged into this HGreaterThan instruction; evaluate it as
3904 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003905 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003906 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3907 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003908 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003909 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3910 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003911 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003912 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003913 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003914
Dave Allison20dfc792014-06-16 20:44:29 -07003915 DECLARE_INSTRUCTION(GreaterThan);
3916
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003917 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003918 return kCondGT;
3919 }
3920
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003921 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003922 return kCondLE;
3923 }
3924
Artem Serovcced8ba2017-07-19 18:18:09 +01003925 protected:
3926 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3927
Dave Allison20dfc792014-06-16 20:44:29 -07003928 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003929 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003930};
3931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003932class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003933 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003934 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303935 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3936 }
Dave Allison20dfc792014-06-16 20:44:29 -07003937
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003938 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003939 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003940 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003941 // In the following Evaluate methods, a HCompare instruction has
3942 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3943 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003944 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003945 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3946 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003947 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003948 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3949 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003950 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003951 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003952 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003953
Dave Allison20dfc792014-06-16 20:44:29 -07003954 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3955
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003956 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003957 return kCondGE;
3958 }
3959
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003960 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003961 return kCondLT;
3962 }
3963
Artem Serovcced8ba2017-07-19 18:18:09 +01003964 protected:
3965 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3966
Dave Allison20dfc792014-06-16 20:44:29 -07003967 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003968 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003969};
3970
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003971class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003972 public:
3973 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303974 : HCondition(kBelow, first, second, dex_pc) {
3975 }
Aart Bike9f37602015-10-09 11:15:55 -07003976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003977 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003978 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003979 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003980 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003981 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3982 }
3983 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003984 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003985 LOG(FATAL) << DebugName() << " is not defined for float values";
3986 UNREACHABLE();
3987 }
3988 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003989 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003990 LOG(FATAL) << DebugName() << " is not defined for double values";
3991 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003992 }
3993
3994 DECLARE_INSTRUCTION(Below);
3995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003996 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003997 return kCondB;
3998 }
3999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004000 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004001 return kCondAE;
4002 }
4003
Artem Serovcced8ba2017-07-19 18:18:09 +01004004 protected:
4005 DEFAULT_COPY_CONSTRUCTOR(Below);
4006
Aart Bike9f37602015-10-09 11:15:55 -07004007 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004008 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004009 return MakeUnsigned(x) < MakeUnsigned(y);
4010 }
Aart Bike9f37602015-10-09 11:15:55 -07004011};
4012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004013class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004014 public:
4015 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304016 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4017 }
Aart Bike9f37602015-10-09 11:15:55 -07004018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004019 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004020 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004021 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004022 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004023 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4024 }
4025 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004026 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004027 LOG(FATAL) << DebugName() << " is not defined for float values";
4028 UNREACHABLE();
4029 }
4030 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004031 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004032 LOG(FATAL) << DebugName() << " is not defined for double values";
4033 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004034 }
4035
4036 DECLARE_INSTRUCTION(BelowOrEqual);
4037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004038 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004039 return kCondBE;
4040 }
4041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004042 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004043 return kCondA;
4044 }
4045
Artem Serovcced8ba2017-07-19 18:18:09 +01004046 protected:
4047 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4048
Aart Bike9f37602015-10-09 11:15:55 -07004049 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004050 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004051 return MakeUnsigned(x) <= MakeUnsigned(y);
4052 }
Aart Bike9f37602015-10-09 11:15:55 -07004053};
4054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004055class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004056 public:
4057 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304058 : HCondition(kAbove, first, second, dex_pc) {
4059 }
Aart Bike9f37602015-10-09 11:15:55 -07004060
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004061 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004062 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004063 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004064 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004065 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4066 }
4067 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004068 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004069 LOG(FATAL) << DebugName() << " is not defined for float values";
4070 UNREACHABLE();
4071 }
4072 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004073 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004074 LOG(FATAL) << DebugName() << " is not defined for double values";
4075 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004076 }
4077
4078 DECLARE_INSTRUCTION(Above);
4079
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004080 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004081 return kCondA;
4082 }
4083
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004084 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004085 return kCondBE;
4086 }
4087
Artem Serovcced8ba2017-07-19 18:18:09 +01004088 protected:
4089 DEFAULT_COPY_CONSTRUCTOR(Above);
4090
Aart Bike9f37602015-10-09 11:15:55 -07004091 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004092 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004093 return MakeUnsigned(x) > MakeUnsigned(y);
4094 }
Aart Bike9f37602015-10-09 11:15:55 -07004095};
4096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004097class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004098 public:
4099 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304100 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4101 }
Aart Bike9f37602015-10-09 11:15:55 -07004102
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004103 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004104 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004105 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004106 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004107 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4108 }
4109 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004110 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004111 LOG(FATAL) << DebugName() << " is not defined for float values";
4112 UNREACHABLE();
4113 }
4114 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004115 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004116 LOG(FATAL) << DebugName() << " is not defined for double values";
4117 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004118 }
4119
4120 DECLARE_INSTRUCTION(AboveOrEqual);
4121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004122 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004123 return kCondAE;
4124 }
4125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004126 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004127 return kCondB;
4128 }
4129
Artem Serovcced8ba2017-07-19 18:18:09 +01004130 protected:
4131 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4132
Aart Bike9f37602015-10-09 11:15:55 -07004133 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004134 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004135 return MakeUnsigned(x) >= MakeUnsigned(y);
4136 }
Aart Bike9f37602015-10-09 11:15:55 -07004137};
Dave Allison20dfc792014-06-16 20:44:29 -07004138
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004139// Instruction to check how two inputs compare to each other.
4140// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004141class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004142 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004143 // Note that `comparison_type` is the type of comparison performed
4144 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004145 // HCompare instruction (which is always DataType::Type::kInt).
4146 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004147 HInstruction* first,
4148 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004149 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004150 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304151 : HBinaryOperation(kCompare,
4152 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004153 first,
4154 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004155 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004156 dex_pc) {
4157 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004158 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4159 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004160 }
4161
Roland Levillain9867bc72015-08-05 10:21:34 +01004162 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004163 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4164
4165 template <typename T>
4166 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004167 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004168 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4169 // Handle the bias.
4170 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4171 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004173 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004174 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4175 // reach this code path when processing a freshly built HIR
4176 // graph. However HCompare integer instructions can be synthesized
4177 // by the instruction simplifier to implement IntegerCompare and
4178 // IntegerSignum intrinsics, so we have to handle this case.
4179 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004180 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004181 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004182 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4183 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004184 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004185 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4186 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004187 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004188 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004189 }
4190
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004191 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004192 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004193 }
4194
Vladimir Markoa1de9182016-02-25 11:37:38 +00004195 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004196
Roland Levillain31dd3d62016-02-16 12:21:02 +00004197 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004198 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004199 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004200 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004201 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004202 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004204 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004205 // Comparisons do not require a runtime call in any back end.
4206 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004207 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004208
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004209 DECLARE_INSTRUCTION(Compare);
4210
Roland Levillain31dd3d62016-02-16 12:21:02 +00004211 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004212 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004213 static constexpr size_t kFieldComparisonBiasSize =
4214 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4215 static constexpr size_t kNumberOfComparePackedBits =
4216 kFieldComparisonBias + kFieldComparisonBiasSize;
4217 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4218 using ComparisonBiasField =
4219 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4220
Roland Levillain31dd3d62016-02-16 12:21:02 +00004221 // Return an integer constant containing the result of a comparison evaluated at compile time.
4222 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4223 DCHECK(value == -1 || value == 0 || value == 1) << value;
4224 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4225 }
4226
Artem Serovcced8ba2017-07-19 18:18:09 +01004227 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004228};
4229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004230class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004231 public:
4232 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004233 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004234 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004235 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004236 bool finalizable,
4237 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304238 : HExpression(kNewInstance,
4239 DataType::Type::kReference,
4240 SideEffects::CanTriggerGC(),
4241 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004242 type_index_(type_index),
4243 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004244 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004245 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004246 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004247 }
4248
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004249 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004250
Andreas Gampea5b09a62016-11-17 15:21:22 -08004251 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004252 const DexFile& GetDexFile() const { return dex_file_; }
4253
4254 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004255 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004256
Mingyao Yang062157f2016-03-02 10:15:36 -08004257 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004258 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004259
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004260 bool NeedsChecks() const {
4261 return entrypoint_ == kQuickAllocObjectWithChecks;
4262 }
David Brazdil6de19382016-01-08 17:37:10 +00004263
Vladimir Markoa1de9182016-02-25 11:37:38 +00004264 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004265
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004266 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004267
4268 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4269
4270 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4271 entrypoint_ = entrypoint;
4272 }
4273
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004274 HLoadClass* GetLoadClass() const {
4275 HInstruction* input = InputAt(0);
4276 if (input->IsClinitCheck()) {
4277 input = input->InputAt(0);
4278 }
4279 DCHECK(input->IsLoadClass());
4280 return input->AsLoadClass();
4281 }
4282
David Brazdil6de19382016-01-08 17:37:10 +00004283 bool IsStringAlloc() const;
4284
4285 DECLARE_INSTRUCTION(NewInstance);
4286
Artem Serovcced8ba2017-07-19 18:18:09 +01004287 protected:
4288 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4289
David Brazdil6de19382016-01-08 17:37:10 +00004290 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004291 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004292 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4293 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4294 "Too many packed fields.");
4295
Andreas Gampea5b09a62016-11-17 15:21:22 -08004296 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004297 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004298 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004299};
4300
Agi Csaki05f20562015-08-19 14:58:14 -07004301enum IntrinsicNeedsEnvironmentOrCache {
4302 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4303 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004304};
4305
Aart Bik5d75afe2015-12-14 11:57:01 -08004306enum IntrinsicSideEffects {
4307 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4308 kReadSideEffects, // Intrinsic may read heap memory.
4309 kWriteSideEffects, // Intrinsic may write heap memory.
4310 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4311};
4312
4313enum IntrinsicExceptions {
4314 kNoThrow, // Intrinsic does not throw any exceptions.
4315 kCanThrow // Intrinsic may throw exceptions.
4316};
4317
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004318class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004319 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004320 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004321
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004322 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004323 SetRawInputAt(index, argument);
4324 }
4325
Roland Levillain3e3d7332015-04-28 11:00:54 +01004326 // Return the number of arguments. This number can be lower than
4327 // the number of inputs returned by InputCount(), as some invoke
4328 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4329 // inputs at the end of their list of inputs.
4330 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4331
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004332 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4333
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004334 InvokeType GetInvokeType() const {
4335 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004336 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004337
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004338 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004339 return intrinsic_;
4340 }
4341
Aart Bik5d75afe2015-12-14 11:57:01 -08004342 void SetIntrinsic(Intrinsics intrinsic,
4343 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4344 IntrinsicSideEffects side_effects,
4345 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004346
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004347 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004348 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004349 }
4350
Aart Bikff7d89c2016-11-07 08:49:28 -08004351 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4352
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004353 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004354
Aart Bika8b8e9b2018-01-09 11:01:02 -08004355 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4356
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004357 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004358
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004359 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004360
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004361 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004362 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4363 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004364
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004365 uint32_t* GetIntrinsicOptimizations() {
4366 return &intrinsic_optimizations_;
4367 }
4368
4369 const uint32_t* GetIntrinsicOptimizations() const {
4370 return &intrinsic_optimizations_;
4371 }
4372
4373 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4374
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004375 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004376 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004377
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004378 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004379
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004380 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004381 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4382 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004383 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004384 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004385 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4386 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004387 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004388 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004389
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304390 HInvoke(InstructionKind kind,
4391 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004392 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004393 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004394 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004395 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004396 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004397 ArtMethod* resolved_method,
4398 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004399 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304400 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004401 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004402 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4403 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004404 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004405 number_of_arguments + number_of_other_inputs,
4406 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004407 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004408 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004409 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004410 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004411 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004412 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004413 // Check mutator lock, constructors lack annotalysis support.
4414 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4415 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004416 }
4417
Artem Serovcced8ba2017-07-19 18:18:09 +01004418 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4419
Roland Levillain3e3d7332015-04-28 11:00:54 +01004420 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004421 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004422 const uint32_t dex_method_index_;
4423 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004424
4425 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4426 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004427};
4428
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004429class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004430 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004431 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004432 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004433 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004434 uint32_t dex_pc,
4435 uint32_t dex_method_index,
4436 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304437 : HInvoke(kInvokeUnresolved,
4438 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004439 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004440 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004441 return_type,
4442 dex_pc,
4443 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004444 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004445 invoke_type) {
4446 }
4447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004448 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004449
Calin Juravle175dc732015-08-25 15:42:32 +01004450 DECLARE_INSTRUCTION(InvokeUnresolved);
4451
Artem Serovcced8ba2017-07-19 18:18:09 +01004452 protected:
4453 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004454};
4455
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004456class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004457 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004458 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004459 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004460 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004461 uint32_t dex_pc,
4462 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304463 : HInvoke(kInvokePolymorphic,
4464 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004465 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004466 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004467 return_type,
4468 dex_pc,
4469 dex_method_index,
4470 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304471 kVirtual) {
4472 }
Orion Hodsonac141392017-01-13 11:53:47 +00004473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004474 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004475
Orion Hodsonac141392017-01-13 11:53:47 +00004476 DECLARE_INSTRUCTION(InvokePolymorphic);
4477
Artem Serovcced8ba2017-07-19 18:18:09 +01004478 protected:
4479 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004480};
4481
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004482class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004483 public:
4484 HInvokeCustom(ArenaAllocator* allocator,
4485 uint32_t number_of_arguments,
4486 uint32_t call_site_index,
4487 DataType::Type return_type,
4488 uint32_t dex_pc)
4489 : HInvoke(kInvokeCustom,
4490 allocator,
4491 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004492 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004493 return_type,
4494 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004495 /* dex_method_index= */ dex::kDexNoIndex,
4496 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004497 kStatic),
4498 call_site_index_(call_site_index) {
4499 }
4500
4501 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4502
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004503 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004504
4505 DECLARE_INSTRUCTION(InvokeCustom);
4506
4507 protected:
4508 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4509
4510 private:
4511 uint32_t call_site_index_;
4512};
4513
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004514class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004515 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004516 // Requirements of this method call regarding the class
4517 // initialization (clinit) check of its declaring class.
4518 enum class ClinitCheckRequirement {
4519 kNone, // Class already initialized.
4520 kExplicit, // Static call having explicit clinit check as last input.
4521 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004522 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004523 };
4524
Vladimir Marko58155012015-08-19 12:49:41 +00004525 // Determines how to load the target ArtMethod*.
4526 enum class MethodLoadKind {
4527 // Use a String init ArtMethod* loaded from Thread entrypoints.
4528 kStringInit,
4529
4530 // Use the method's own ArtMethod* loaded by the register allocator.
4531 kRecursive,
4532
Vladimir Marko65979462017-05-19 17:25:12 +01004533 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4534 // Used for boot image methods referenced by boot image code.
4535 kBootImageLinkTimePcRelative,
4536
Vladimir Markob066d432018-01-03 13:14:37 +00004537 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4538 // Used for app->boot calls with relocatable image.
4539 kBootImageRelRo,
4540
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004541 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004542 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004543 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004544
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004545 // Use ArtMethod* at a known address, embed the direct address in the code.
4546 // Used for for JIT-compiled calls.
4547 kJitDirectAddress,
4548
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004549 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4550 // used when other kinds are unimplemented on a particular architecture.
4551 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004552 };
4553
4554 // Determines the location of the code pointer.
4555 enum class CodePtrLocation {
4556 // Recursive call, use local PC-relative call instruction.
4557 kCallSelf,
4558
Vladimir Marko58155012015-08-19 12:49:41 +00004559 // Use code pointer from the ArtMethod*.
4560 // Used when we don't know the target code. This is also the last-resort-kind used when
4561 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4562 kCallArtMethod,
4563 };
4564
4565 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004566 MethodLoadKind method_load_kind;
4567 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004568 // The method load data holds
4569 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4570 // Note that there are multiple string init methods, each having its own offset.
4571 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004572 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004573 };
4574
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004575 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004576 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004577 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004578 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004579 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004580 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004581 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004582 InvokeType invoke_type,
4583 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004584 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304585 : HInvoke(kInvokeStaticOrDirect,
4586 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004587 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004588 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004589 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004590 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004591 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004592 return_type,
4593 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004594 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004595 resolved_method,
4596 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004597 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004598 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004599 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4600 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004602 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004603
Vladimir Markodc151b22015-10-15 18:02:30 +01004604 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004605 bool had_current_method_input = HasCurrentMethodInput();
4606 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4607
4608 // Using the current method is the default and once we find a better
4609 // method load kind, we should not go back to using the current method.
4610 DCHECK(had_current_method_input || !needs_current_method_input);
4611
4612 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004613 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4614 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004615 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004616 dispatch_info_ = dispatch_info;
4617 }
4618
Aart Bik6daebeb2017-04-03 14:35:41 -07004619 DispatchInfo GetDispatchInfo() const {
4620 return dispatch_info_;
4621 }
4622
Vladimir Markoc53c0792015-11-19 15:48:33 +00004623 void AddSpecialInput(HInstruction* input) {
4624 // We allow only one special input.
4625 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4626 DCHECK(InputCount() == GetSpecialInputIndex() ||
4627 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4628 InsertInputAt(GetSpecialInputIndex(), input);
4629 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004630
Vladimir Marko372f10e2016-05-17 16:30:10 +01004631 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004632 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004633 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4634 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4635 DCHECK(!input_records.empty());
4636 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4637 HInstruction* last_input = input_records.back().GetInstruction();
4638 // Note: `last_input` may be null during arguments setup.
4639 if (last_input != nullptr) {
4640 // `last_input` is the last input of a static invoke marked as having
4641 // an explicit clinit check. It must either be:
4642 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4643 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4644 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4645 }
4646 }
4647 return input_records;
4648 }
4649
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004650 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004651 // We access the method via the dex cache so we can't do an implicit null check.
4652 // TODO: for intrinsics we can generate implicit null checks.
4653 return false;
4654 }
4655
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004656 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004657 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004658 }
4659
Vladimir Markoc53c0792015-11-19 15:48:33 +00004660 // Get the index of the special input, if any.
4661 //
David Brazdil6de19382016-01-08 17:37:10 +00004662 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4663 // method pointer; otherwise there may be one platform-specific special input,
4664 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004665 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004666 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004667
Vladimir Marko58155012015-08-19 12:49:41 +00004668 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4669 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4670 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004671 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004672 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004673 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004674 bool HasPcRelativeMethodLoadKind() const {
4675 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004676 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004677 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004678 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004679 bool HasCurrentMethodInput() const {
4680 // This function can be called only after the invoke has been fully initialized by the builder.
4681 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004682 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004683 return true;
4684 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004685 DCHECK(InputCount() == GetSpecialInputIndex() ||
4686 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004687 return false;
4688 }
4689 }
Vladimir Marko58155012015-08-19 12:49:41 +00004690
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004691 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004692 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004693 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004694 }
4695
4696 uint64_t GetMethodAddress() const {
4697 DCHECK(HasMethodAddress());
4698 return dispatch_info_.method_load_data;
4699 }
4700
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004701 const DexFile& GetDexFileForPcRelativeDexCache() const;
4702
Vladimir Markoa1de9182016-02-25 11:37:38 +00004703 ClinitCheckRequirement GetClinitCheckRequirement() const {
4704 return GetPackedField<ClinitCheckRequirementField>();
4705 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004706
Roland Levillain4c0eb422015-04-24 16:43:49 +01004707 // Is this instruction a call to a static method?
4708 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004709 return GetInvokeType() == kStatic;
4710 }
4711
4712 MethodReference GetTargetMethod() const {
4713 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004714 }
4715
Vladimir Markofbb184a2015-11-13 14:47:00 +00004716 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4717 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4718 // instruction; only relevant for static calls with explicit clinit check.
4719 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004720 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004721 size_t last_input_index = inputs_.size() - 1u;
4722 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004723 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004724 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004725 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004726 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004727 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004728 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004729 }
4730
4731 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004732 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004733 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004734 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004735 }
4736
4737 // Is this a call to a static method whose declaring class has an
4738 // implicit intialization check requirement?
4739 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004740 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004741 }
4742
Vladimir Markob554b5a2015-11-06 12:57:55 +00004743 // Does this method load kind need the current method as an input?
4744 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004745 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004746 }
4747
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004748 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004749
Artem Serovcced8ba2017-07-19 18:18:09 +01004750 protected:
4751 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4752
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004753 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004754 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004755 static constexpr size_t kFieldClinitCheckRequirementSize =
4756 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4757 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4758 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4759 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4760 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004761 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4762 kFieldClinitCheckRequirement,
4763 kFieldClinitCheckRequirementSize>;
4764
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004765 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004766 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004767 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004768};
Vladimir Markof64242a2015-12-01 14:58:23 +00004769std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004770std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004772class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004773 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004774 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004775 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004776 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004777 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004778 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004779 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004780 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304781 : HInvoke(kInvokeVirtual,
4782 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004783 number_of_arguments,
4784 0u,
4785 return_type,
4786 dex_pc,
4787 dex_method_index,
4788 resolved_method,
4789 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304790 vtable_index_(vtable_index) {
4791 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004792
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004793 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004794
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004795 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004796 switch (GetIntrinsic()) {
4797 case Intrinsics::kThreadCurrentThread:
4798 case Intrinsics::kStringBufferAppend:
4799 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004800 case Intrinsics::kStringBuilderAppendObject:
4801 case Intrinsics::kStringBuilderAppendString:
4802 case Intrinsics::kStringBuilderAppendCharSequence:
4803 case Intrinsics::kStringBuilderAppendCharArray:
4804 case Intrinsics::kStringBuilderAppendBoolean:
4805 case Intrinsics::kStringBuilderAppendChar:
4806 case Intrinsics::kStringBuilderAppendInt:
4807 case Intrinsics::kStringBuilderAppendLong:
4808 case Intrinsics::kStringBuilderAppendFloat:
4809 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004810 case Intrinsics::kStringBuilderToString:
4811 return false;
4812 default:
4813 return HInvoke::CanBeNull();
4814 }
4815 }
4816
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004817 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004818 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004819 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004820 }
4821
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004822 uint32_t GetVTableIndex() const { return vtable_index_; }
4823
4824 DECLARE_INSTRUCTION(InvokeVirtual);
4825
Artem Serovcced8ba2017-07-19 18:18:09 +01004826 protected:
4827 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4828
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004829 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004830 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004831 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004832};
4833
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004834class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004835 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004836 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004837 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004838 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004839 uint32_t dex_pc,
4840 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004841 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004842 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304843 : HInvoke(kInvokeInterface,
4844 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004845 number_of_arguments,
4846 0u,
4847 return_type,
4848 dex_pc,
4849 dex_method_index,
4850 resolved_method,
4851 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304852 imt_index_(imt_index) {
4853 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004854
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004855 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004856
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004857 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004858 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004859 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004860 }
4861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004862 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004863 // The assembly stub currently needs it.
4864 return true;
4865 }
4866
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004867 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004868
4869 DECLARE_INSTRUCTION(InvokeInterface);
4870
Artem Serovcced8ba2017-07-19 18:18:09 +01004871 protected:
4872 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4873
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004874 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004875 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004876 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004877};
4878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004879class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004880 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004881 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304882 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004883 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004884 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004885
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004886 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004888 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004889 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004890 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004891 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004892 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004893 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004894 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004895 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4896 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004897 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004898 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4899 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004900
Roland Levillain88cb1752014-10-20 16:36:47 +01004901 DECLARE_INSTRUCTION(Neg);
4902
Artem Serovcced8ba2017-07-19 18:18:09 +01004903 protected:
4904 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004905};
4906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004907class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004908 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004909 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304910 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004911 SetRawInputAt(0, cls);
4912 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004913 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004914 }
4915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004916 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004917
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004918 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004919 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004920
Mingyao Yang0c365e62015-03-31 15:09:29 -07004921 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004922 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004923
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004924 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004925
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004926 HLoadClass* GetLoadClass() const {
4927 DCHECK(InputAt(0)->IsLoadClass());
4928 return InputAt(0)->AsLoadClass();
4929 }
4930
4931 HInstruction* GetLength() const {
4932 return InputAt(1);
4933 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004934
Vladimir Markob5461632018-10-15 14:24:21 +01004935 size_t GetComponentSizeShift() {
4936 return GetPackedField<ComponentSizeShiftField>();
4937 }
4938
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004939 DECLARE_INSTRUCTION(NewArray);
4940
Artem Serovcced8ba2017-07-19 18:18:09 +01004941 protected:
4942 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004943
4944 private:
4945 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4946 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4947 static constexpr size_t kNumberOfNewArrayPackedBits =
4948 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4949 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4950 using ComponentSizeShiftField =
4951 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004952};
4953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004954class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004955 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004956 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004957 HInstruction* left,
4958 HInstruction* right,
4959 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304960 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4961 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004962
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004963 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004964
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004965 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004966
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004967 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004968 return GetBlock()->GetGraph()->GetIntConstant(
4969 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004970 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004971 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004972 return GetBlock()->GetGraph()->GetLongConstant(
4973 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004974 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004975 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004976 return GetBlock()->GetGraph()->GetFloatConstant(
4977 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4978 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004979 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004980 return GetBlock()->GetGraph()->GetDoubleConstant(
4981 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4982 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004983
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004984 DECLARE_INSTRUCTION(Add);
4985
Artem Serovcced8ba2017-07-19 18:18:09 +01004986 protected:
4987 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004988};
4989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004990class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004991 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004992 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004993 HInstruction* left,
4994 HInstruction* right,
4995 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304996 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4997 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004998
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004999 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005000
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005002 return GetBlock()->GetGraph()->GetIntConstant(
5003 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005004 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005005 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005006 return GetBlock()->GetGraph()->GetLongConstant(
5007 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005008 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005009 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005010 return GetBlock()->GetGraph()->GetFloatConstant(
5011 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5012 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005013 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005014 return GetBlock()->GetGraph()->GetDoubleConstant(
5015 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5016 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005017
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005018 DECLARE_INSTRUCTION(Sub);
5019
Artem Serovcced8ba2017-07-19 18:18:09 +01005020 protected:
5021 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005022};
5023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005024class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005025 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005026 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005027 HInstruction* left,
5028 HInstruction* right,
5029 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305030 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5031 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005033 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005034
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005035 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005037 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005038 return GetBlock()->GetGraph()->GetIntConstant(
5039 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005040 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005041 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005042 return GetBlock()->GetGraph()->GetLongConstant(
5043 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005044 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005045 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005046 return GetBlock()->GetGraph()->GetFloatConstant(
5047 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5048 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005049 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005050 return GetBlock()->GetGraph()->GetDoubleConstant(
5051 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5052 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005053
5054 DECLARE_INSTRUCTION(Mul);
5055
Artem Serovcced8ba2017-07-19 18:18:09 +01005056 protected:
5057 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005058};
5059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005060class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005061 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005062 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 HInstruction* left,
5064 HInstruction* right,
5065 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305066 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5067 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005068
Roland Levillain9867bc72015-08-05 10:21:34 +01005069 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005070 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005071 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005072 // Our graph structure ensures we never have 0 for `y` during
5073 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005074 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005075 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005076 return (y == -1) ? -x : x / y;
5077 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005078
Roland Levillain31dd3d62016-02-16 12:21:02 +00005079 template <typename T>
5080 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005081 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005082 return x / y;
5083 }
5084
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005085 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005086 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005087 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005088 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005089 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005090 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005091 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5092 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005093 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005094 return GetBlock()->GetGraph()->GetFloatConstant(
5095 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5096 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005097 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005098 return GetBlock()->GetGraph()->GetDoubleConstant(
5099 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005100 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005101
5102 DECLARE_INSTRUCTION(Div);
5103
Artem Serovcced8ba2017-07-19 18:18:09 +01005104 protected:
5105 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005106};
5107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005108class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005109 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005110 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005111 HInstruction* left,
5112 HInstruction* right,
5113 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305114 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5115 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005116
Roland Levillain9867bc72015-08-05 10:21:34 +01005117 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005118 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005119 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005120 // Our graph structure ensures we never have 0 for `y` during
5121 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005122 DCHECK_NE(y, 0);
5123 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5124 return (y == -1) ? 0 : x % y;
5125 }
5126
Roland Levillain31dd3d62016-02-16 12:21:02 +00005127 template <typename T>
5128 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005129 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005130 return std::fmod(x, y);
5131 }
5132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005133 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005134 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005135 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005136 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005137 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005138 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005139 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005140 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005141 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005142 return GetBlock()->GetGraph()->GetFloatConstant(
5143 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5144 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005145 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005146 return GetBlock()->GetGraph()->GetDoubleConstant(
5147 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5148 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005149
5150 DECLARE_INSTRUCTION(Rem);
5151
Artem Serovcced8ba2017-07-19 18:18:09 +01005152 protected:
5153 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005154};
5155
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005156class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005157 public:
5158 HMin(DataType::Type result_type,
5159 HInstruction* left,
5160 HInstruction* right,
5161 uint32_t dex_pc)
5162 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5163
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005164 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005165
5166 // Evaluation for integral values.
5167 template <typename T> static T ComputeIntegral(T x, T y) {
5168 return (x <= y) ? x : y;
5169 }
5170
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005171 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005172 return GetBlock()->GetGraph()->GetIntConstant(
5173 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5174 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005175 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005176 return GetBlock()->GetGraph()->GetLongConstant(
5177 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5178 }
5179 // TODO: Evaluation for floating-point values.
5180 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005181 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005182 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005183 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005184
5185 DECLARE_INSTRUCTION(Min);
5186
5187 protected:
5188 DEFAULT_COPY_CONSTRUCTOR(Min);
5189};
5190
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005191class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005192 public:
5193 HMax(DataType::Type result_type,
5194 HInstruction* left,
5195 HInstruction* right,
5196 uint32_t dex_pc)
5197 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5198
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005199 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005200
5201 // Evaluation for integral values.
5202 template <typename T> static T ComputeIntegral(T x, T y) {
5203 return (x >= y) ? x : y;
5204 }
5205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005206 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005207 return GetBlock()->GetGraph()->GetIntConstant(
5208 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5209 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005210 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005211 return GetBlock()->GetGraph()->GetLongConstant(
5212 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5213 }
5214 // TODO: Evaluation for floating-point values.
5215 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005216 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005217 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005218 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005219
5220 DECLARE_INSTRUCTION(Max);
5221
5222 protected:
5223 DEFAULT_COPY_CONSTRUCTOR(Max);
5224};
5225
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005226class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005227 public:
5228 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5229 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5230
5231 // Evaluation for integral values.
5232 template <typename T> static T ComputeIntegral(T x) {
5233 return x < 0 ? -x : x;
5234 }
5235
5236 // Evaluation for floating-point values.
5237 // Note, as a "quality of implementation", rather than pure "spec compliance",
5238 // we require that Math.abs() clears the sign bit (but changes nothing else)
5239 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5240 // http://b/30758343
5241 template <typename T, typename S> static T ComputeFP(T x) {
5242 S bits = bit_cast<S, T>(x);
5243 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5244 }
5245
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005246 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005247 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5248 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005249 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005250 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5251 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005252 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005253 return GetBlock()->GetGraph()->GetFloatConstant(
5254 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5255 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005256 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005257 return GetBlock()->GetGraph()->GetDoubleConstant(
5258 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5259 }
5260
5261 DECLARE_INSTRUCTION(Abs);
5262
5263 protected:
5264 DEFAULT_COPY_CONSTRUCTOR(Abs);
5265};
5266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005267class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005268 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005269 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005270 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5271 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005272 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005273 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005274 SetRawInputAt(0, value);
5275 }
5276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005277 bool IsClonable() const override { return true; }
5278 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005280 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005281 return true;
5282 }
5283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284 bool NeedsEnvironment() const override { return true; }
5285 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005286
Calin Juravled0d48522014-11-04 16:40:20 +00005287 DECLARE_INSTRUCTION(DivZeroCheck);
5288
Artem Serovcced8ba2017-07-19 18:18:09 +01005289 protected:
5290 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005291};
5292
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005293class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005294 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005295 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005296 HInstruction* value,
5297 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005298 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305299 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005300 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5301 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005302 }
5303
Roland Levillain5b5b9312016-03-22 14:57:31 +00005304 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005305 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005306 return value << (distance & max_shift_distance);
5307 }
5308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005309 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005310 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005311 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005312 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005313 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005314 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005315 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005316 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005317 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005318 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005319 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5320 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005321 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005322 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005323 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005324 LOG(FATAL) << DebugName() << " is not defined for float values";
5325 UNREACHABLE();
5326 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005327 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005328 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005329 LOG(FATAL) << DebugName() << " is not defined for double values";
5330 UNREACHABLE();
5331 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005332
5333 DECLARE_INSTRUCTION(Shl);
5334
Artem Serovcced8ba2017-07-19 18:18:09 +01005335 protected:
5336 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005337};
5338
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005339class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005340 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005341 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005342 HInstruction* value,
5343 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005344 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305345 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005346 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5347 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005348 }
5349
Roland Levillain5b5b9312016-03-22 14:57:31 +00005350 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005351 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005352 return value >> (distance & max_shift_distance);
5353 }
5354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005355 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005356 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005357 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005358 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005359 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005360 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005361 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005362 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005363 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005364 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005365 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5366 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005367 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005368 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005369 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005370 LOG(FATAL) << DebugName() << " is not defined for float values";
5371 UNREACHABLE();
5372 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005373 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005374 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005375 LOG(FATAL) << DebugName() << " is not defined for double values";
5376 UNREACHABLE();
5377 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005378
5379 DECLARE_INSTRUCTION(Shr);
5380
Artem Serovcced8ba2017-07-19 18:18:09 +01005381 protected:
5382 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005383};
5384
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005385class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005386 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005387 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005388 HInstruction* value,
5389 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005390 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305391 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5393 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005394 }
5395
Roland Levillain5b5b9312016-03-22 14:57:31 +00005396 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005397 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005398 typedef typename std::make_unsigned<T>::type V;
5399 V ux = static_cast<V>(value);
5400 return static_cast<T>(ux >> (distance & max_shift_distance));
5401 }
5402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005403 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005404 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005405 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005406 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005407 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005408 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005409 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005410 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005411 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005412 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005413 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5414 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005415 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005416 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005417 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005418 LOG(FATAL) << DebugName() << " is not defined for float values";
5419 UNREACHABLE();
5420 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005421 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005422 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005423 LOG(FATAL) << DebugName() << " is not defined for double values";
5424 UNREACHABLE();
5425 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005426
5427 DECLARE_INSTRUCTION(UShr);
5428
Artem Serovcced8ba2017-07-19 18:18:09 +01005429 protected:
5430 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005431};
5432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005433class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005434 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005435 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005436 HInstruction* left,
5437 HInstruction* right,
5438 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305439 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5440 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005441
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005442 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005443
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005444 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005445
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005446 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005447 return GetBlock()->GetGraph()->GetIntConstant(
5448 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005449 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005450 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005451 return GetBlock()->GetGraph()->GetLongConstant(
5452 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005453 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005454 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005455 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005456 LOG(FATAL) << DebugName() << " is not defined for float values";
5457 UNREACHABLE();
5458 }
5459 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005460 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005461 LOG(FATAL) << DebugName() << " is not defined for double values";
5462 UNREACHABLE();
5463 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005464
5465 DECLARE_INSTRUCTION(And);
5466
Artem Serovcced8ba2017-07-19 18:18:09 +01005467 protected:
5468 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005469};
5470
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005471class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005472 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005473 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005474 HInstruction* left,
5475 HInstruction* right,
5476 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305477 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5478 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005479
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005480 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005481
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005482 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005483
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005484 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005485 return GetBlock()->GetGraph()->GetIntConstant(
5486 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005487 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005488 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005489 return GetBlock()->GetGraph()->GetLongConstant(
5490 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005491 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005492 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005493 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005494 LOG(FATAL) << DebugName() << " is not defined for float values";
5495 UNREACHABLE();
5496 }
5497 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005498 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005499 LOG(FATAL) << DebugName() << " is not defined for double values";
5500 UNREACHABLE();
5501 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005502
5503 DECLARE_INSTRUCTION(Or);
5504
Artem Serovcced8ba2017-07-19 18:18:09 +01005505 protected:
5506 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005507};
5508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005509class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005510 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005512 HInstruction* left,
5513 HInstruction* right,
5514 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305515 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5516 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005517
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005518 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005519
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005520 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005521
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005522 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005523 return GetBlock()->GetGraph()->GetIntConstant(
5524 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005525 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005526 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005527 return GetBlock()->GetGraph()->GetLongConstant(
5528 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005529 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005530 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005531 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005532 LOG(FATAL) << DebugName() << " is not defined for float values";
5533 UNREACHABLE();
5534 }
5535 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005536 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005537 LOG(FATAL) << DebugName() << " is not defined for double values";
5538 UNREACHABLE();
5539 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005540
5541 DECLARE_INSTRUCTION(Xor);
5542
Artem Serovcced8ba2017-07-19 18:18:09 +01005543 protected:
5544 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005545};
5546
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005547class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005548 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005549 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305550 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005551 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5552 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005553 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005554
Roland Levillain5b5b9312016-03-22 14:57:31 +00005555 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005556 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005557 typedef typename std::make_unsigned<T>::type V;
5558 V ux = static_cast<V>(value);
5559 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005560 return static_cast<T>(ux);
5561 } else {
5562 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005563 return static_cast<T>(ux >> (distance & max_shift_value)) |
5564 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005565 }
5566 }
5567
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005568 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005569 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005570 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005571 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005572 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005573 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005574 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005575 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005576 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005577 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005578 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5579 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005580 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005581 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005582 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005583 LOG(FATAL) << DebugName() << " is not defined for float values";
5584 UNREACHABLE();
5585 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005586 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005587 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005588 LOG(FATAL) << DebugName() << " is not defined for double values";
5589 UNREACHABLE();
5590 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005591
5592 DECLARE_INSTRUCTION(Ror);
5593
Artem Serovcced8ba2017-07-19 18:18:09 +01005594 protected:
5595 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005596};
5597
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005598// The value of a parameter in this method. Its location depends on
5599// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005600class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005601 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005602 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005603 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005604 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005605 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005606 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305607 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005608 dex_file_(dex_file),
5609 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005610 index_(index) {
5611 SetPackedFlag<kFlagIsThis>(is_this);
5612 SetPackedFlag<kFlagCanBeNull>(!is_this);
5613 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005614
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005615 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005616 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005617 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005618 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005619
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005620 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005621 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005622
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005623 DECLARE_INSTRUCTION(ParameterValue);
5624
Artem Serovcced8ba2017-07-19 18:18:09 +01005625 protected:
5626 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5627
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005628 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005629 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005630 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005631 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5632 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5633 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5634 "Too many packed fields.");
5635
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005636 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005637 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005638 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005639 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005640 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005641};
5642
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005643class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005644 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005645 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305646 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5647 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005648
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005649 bool CanBeMoved() const override { return true; }
5650 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005651 return true;
5652 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005653
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005654 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005655
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005656 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005657 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005658 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005659 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005660 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005661 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005662 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005663 LOG(FATAL) << DebugName() << " is not defined for float values";
5664 UNREACHABLE();
5665 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005666 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005667 LOG(FATAL) << DebugName() << " is not defined for double values";
5668 UNREACHABLE();
5669 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005670
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005671 DECLARE_INSTRUCTION(Not);
5672
Artem Serovcced8ba2017-07-19 18:18:09 +01005673 protected:
5674 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005675};
5676
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005677class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005678 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005679 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305680 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5681 }
David Brazdil66d126e2015-04-03 16:02:44 +01005682
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005683 bool CanBeMoved() const override { return true; }
5684 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005685 return true;
5686 }
5687
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005688 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005689 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005690 return !x;
5691 }
5692
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005693 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005694 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005695 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005696 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005697 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005698 UNREACHABLE();
5699 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005700 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005701 LOG(FATAL) << DebugName() << " is not defined for float values";
5702 UNREACHABLE();
5703 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005704 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005705 LOG(FATAL) << DebugName() << " is not defined for double values";
5706 UNREACHABLE();
5707 }
David Brazdil66d126e2015-04-03 16:02:44 +01005708
5709 DECLARE_INSTRUCTION(BooleanNot);
5710
Artem Serovcced8ba2017-07-19 18:18:09 +01005711 protected:
5712 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005713};
5714
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005715class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005716 public:
5717 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005718 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305719 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005720 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005721 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005722 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005723 }
5724
5725 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005726 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5727 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005729 bool IsClonable() const override { return true; }
5730 bool CanBeMoved() const override { return true; }
5731 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005732 return true;
5733 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005734 // Return whether the conversion is implicit. This includes conversion to the same type.
5735 bool IsImplicitConversion() const {
5736 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5737 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005738
Mark Mendelle82549b2015-05-06 10:55:34 -04005739 // Try to statically evaluate the conversion and return a HConstant
5740 // containing the result. If the input cannot be converted, return nullptr.
5741 HConstant* TryStaticEvaluation() const;
5742
Roland Levillaindff1f282014-11-05 14:15:05 +00005743 DECLARE_INSTRUCTION(TypeConversion);
5744
Artem Serovcced8ba2017-07-19 18:18:09 +01005745 protected:
5746 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005747};
5748
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005749static constexpr uint32_t kNoRegNumber = -1;
5750
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005751class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005752 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005753 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005754 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5755 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005756 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005757 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005758 SetRawInputAt(0, value);
5759 }
5760
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005761 bool IsClonable() const override { return true; }
5762 bool CanBeMoved() const override { return true; }
5763 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005764 return true;
5765 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005767 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005769 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005770
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005771 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005772
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005773 DECLARE_INSTRUCTION(NullCheck);
5774
Artem Serovcced8ba2017-07-19 18:18:09 +01005775 protected:
5776 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005777};
5778
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005779// Embeds an ArtField and all the information required by the compiler. We cache
5780// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005781class FieldInfo : public ValueObject {
5782 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005783 FieldInfo(ArtField* field,
5784 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005785 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005786 bool is_volatile,
5787 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005788 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005789 const DexFile& dex_file)
5790 : field_(field),
5791 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005792 field_type_(field_type),
5793 is_volatile_(is_volatile),
5794 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005795 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005796 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005797
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005798 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005799 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005801 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005802 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005803 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005804 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005805
5806 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005807 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005808 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005809 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005810 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005811 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005812 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005813 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005814};
5815
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005816class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005817 public:
5818 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005819 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005820 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005821 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005822 bool is_volatile,
5823 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005824 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005825 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005826 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305827 : HExpression(kInstanceFieldGet,
5828 field_type,
5829 SideEffects::FieldReadOfType(field_type, is_volatile),
5830 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005831 field_info_(field,
5832 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005833 field_type,
5834 is_volatile,
5835 field_idx,
5836 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005837 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005838 SetRawInputAt(0, value);
5839 }
5840
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005841 bool IsClonable() const override { return true; }
5842 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005844 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005845 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005846 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005847 }
5848
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005849 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005850 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005851 }
5852
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005853 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005854 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5855 }
5856
Calin Juravle52c48962014-12-16 17:02:57 +00005857 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005858 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005859 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005860 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005861
Vladimir Marko61b92282017-10-11 13:23:17 +01005862 void SetType(DataType::Type new_type) {
5863 DCHECK(DataType::IsIntegralType(GetType()));
5864 DCHECK(DataType::IsIntegralType(new_type));
5865 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5866 SetPackedField<TypeField>(new_type);
5867 }
5868
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005869 DECLARE_INSTRUCTION(InstanceFieldGet);
5870
Artem Serovcced8ba2017-07-19 18:18:09 +01005871 protected:
5872 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5873
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005874 private:
5875 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005876};
5877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005878class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005879 public:
5880 HInstanceFieldSet(HInstruction* object,
5881 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005882 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005883 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005884 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005885 bool is_volatile,
5886 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005887 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005888 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005889 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005890 : HExpression(kInstanceFieldSet,
5891 SideEffects::FieldWriteOfType(field_type, is_volatile),
5892 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005893 field_info_(field,
5894 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005895 field_type,
5896 is_volatile,
5897 field_idx,
5898 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005899 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005900 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005901 SetRawInputAt(0, object);
5902 SetRawInputAt(1, value);
5903 }
5904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005905 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005907 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005908 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005909 }
5910
Calin Juravle52c48962014-12-16 17:02:57 +00005911 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005912 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005913 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005914 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005915 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005916 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5917 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005918
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005919 DECLARE_INSTRUCTION(InstanceFieldSet);
5920
Artem Serovcced8ba2017-07-19 18:18:09 +01005921 protected:
5922 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5923
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005924 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005925 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5926 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5927 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5928 "Too many packed fields.");
5929
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005930 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005931};
5932
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005933class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005934 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005935 HArrayGet(HInstruction* array,
5936 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005938 uint32_t dex_pc)
5939 : HArrayGet(array,
5940 index,
5941 type,
5942 SideEffects::ArrayReadOfType(type),
5943 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005944 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305945 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005946
5947 HArrayGet(HInstruction* array,
5948 HInstruction* index,
5949 DataType::Type type,
5950 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005951 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005952 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305953 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005954 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005955 SetRawInputAt(0, array);
5956 SetRawInputAt(1, index);
5957 }
5958
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005959 bool IsClonable() const override { return true; }
5960 bool CanBeMoved() const override { return true; }
5961 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005962 return true;
5963 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005964 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005965 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005966 // Currently, unless the array is the result of NewArray, the array access is always
5967 // preceded by some form of null NullCheck necessary for the bounds check, usually
5968 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5969 // dynamic BCE. There are cases when these could be removed to produce better code.
5970 // If we ever add optimizations to do so we should allow an implicit check here
5971 // (as long as the address falls in the first page).
5972 //
5973 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5974 // a = cond ? new int[1] : null;
5975 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005976 return false;
5977 }
5978
David Brazdil4833f5a2015-12-16 10:37:39 +00005979 bool IsEquivalentOf(HArrayGet* other) const {
5980 bool result = (GetDexPc() == other->GetDexPc());
5981 if (kIsDebugBuild && result) {
5982 DCHECK_EQ(GetBlock(), other->GetBlock());
5983 DCHECK_EQ(GetArray(), other->GetArray());
5984 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 if (DataType::IsIntOrLongType(GetType())) {
5986 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005987 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005988 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5989 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005990 }
5991 }
5992 return result;
5993 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005994
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005995 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5996
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005997 HInstruction* GetArray() const { return InputAt(0); }
5998 HInstruction* GetIndex() const { return InputAt(1); }
5999
Vladimir Marko61b92282017-10-11 13:23:17 +01006000 void SetType(DataType::Type new_type) {
6001 DCHECK(DataType::IsIntegralType(GetType()));
6002 DCHECK(DataType::IsIntegralType(new_type));
6003 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6004 SetPackedField<TypeField>(new_type);
6005 }
6006
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006007 DECLARE_INSTRUCTION(ArrayGet);
6008
Artem Serovcced8ba2017-07-19 18:18:09 +01006009 protected:
6010 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6011
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006012 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006013 // We treat a String as an array, creating the HArrayGet from String.charAt()
6014 // intrinsic in the instruction simplifier. We can always determine whether
6015 // a particular HArrayGet is actually a String.charAt() by looking at the type
6016 // of the input but that requires holding the mutator lock, so we prefer to use
6017 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006018 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006019 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6020 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6021 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006022};
6023
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006024class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006025 public:
6026 HArraySet(HInstruction* array,
6027 HInstruction* index,
6028 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006029 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006030 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006031 : HArraySet(array,
6032 index,
6033 value,
6034 expected_component_type,
6035 // Make a best guess for side effects now, may be refined during SSA building.
6036 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306037 dex_pc) {
6038 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006039
6040 HArraySet(HInstruction* array,
6041 HInstruction* index,
6042 HInstruction* value,
6043 DataType::Type expected_component_type,
6044 SideEffects side_effects,
6045 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006046 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006047 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006048 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 SetPackedFlag<kFlagValueCanBeNull>(true);
6050 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051 SetRawInputAt(0, array);
6052 SetRawInputAt(1, index);
6053 SetRawInputAt(2, value);
6054 }
6055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006056 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006058 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006059 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006060 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006061 }
6062
Mingyao Yang81014cb2015-06-02 03:16:27 -07006063 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006064 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006065
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006066 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006067 // TODO: Same as for ArrayGet.
6068 return false;
6069 }
6070
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006071 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006072 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006073 }
6074
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006075 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006076 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006077 }
6078
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006079 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006080 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006081 }
6082
Vladimir Markoa1de9182016-02-25 11:37:38 +00006083 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6084 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6085 bool StaticTypeOfArrayIsObjectArray() const {
6086 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6087 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006088
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006089 HInstruction* GetArray() const { return InputAt(0); }
6090 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006091 HInstruction* GetValue() const { return InputAt(2); }
6092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006093 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006094 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6095 }
6096
6097 static DataType::Type GetComponentType(DataType::Type value_type,
6098 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006099 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006100 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006101 // be correct, we also check what is the value type. If it is a floating
6102 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006103 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006104 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006105 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006106 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006108 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006109 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006110 }
6111
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006112 static SideEffects ComputeSideEffects(DataType::Type type) {
6113 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006114 }
6115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006116 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6117 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6118 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006119 }
6120
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006121 DECLARE_INSTRUCTION(ArraySet);
6122
Artem Serovcced8ba2017-07-19 18:18:09 +01006123 protected:
6124 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6125
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006126 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006127 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6128 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006129 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006130 static constexpr size_t kFlagNeedsTypeCheck =
6131 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6132 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006133 // Cached information for the reference_type_info_ so that codegen
6134 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006135 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6136 static constexpr size_t kNumberOfArraySetPackedBits =
6137 kFlagStaticTypeOfArrayIsObjectArray + 1;
6138 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6139 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006140 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006141};
6142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006143class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006144 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006145 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306146 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006147 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006148 // Note that arrays do not change length, so the instruction does not
6149 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006150 SetRawInputAt(0, array);
6151 }
6152
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006153 bool IsClonable() const override { return true; }
6154 bool CanBeMoved() const override { return true; }
6155 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006156 return true;
6157 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006158 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006159 return obj == InputAt(0);
6160 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006161
Vladimir Markodce016e2016-04-28 13:10:02 +01006162 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6163
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006164 DECLARE_INSTRUCTION(ArrayLength);
6165
Artem Serovcced8ba2017-07-19 18:18:09 +01006166 protected:
6167 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6168
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006169 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006170 // We treat a String as an array, creating the HArrayLength from String.length()
6171 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6172 // determine whether a particular HArrayLength is actually a String.length() by
6173 // looking at the type of the input but that requires holding the mutator lock, so
6174 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006175 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006176 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6177 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6178 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006179};
6180
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006181class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006182 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006183 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006184 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6185 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006186 HBoundsCheck(HInstruction* index,
6187 HInstruction* length,
6188 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006189 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006190 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006191 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006192 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006193 SetRawInputAt(0, index);
6194 SetRawInputAt(1, length);
6195 }
6196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006197 bool IsClonable() const override { return true; }
6198 bool CanBeMoved() const override { return true; }
6199 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006200 return true;
6201 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006202
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006203 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006204
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006205 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006206
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006207 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006208
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006209 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006210
6211 DECLARE_INSTRUCTION(BoundsCheck);
6212
Artem Serovcced8ba2017-07-19 18:18:09 +01006213 protected:
6214 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6215
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006216 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006217 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006218 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6219 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6220 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006221};
6222
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006223class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006224 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006225 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006226 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306227 slow_path_(nullptr) {
6228 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006230 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006231
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006232 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006233 return true;
6234 }
6235
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006236 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6237 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006238
6239 DECLARE_INSTRUCTION(SuspendCheck);
6240
Artem Serovcced8ba2017-07-19 18:18:09 +01006241 protected:
6242 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6243
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006244 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006245 // Only used for code generation, in order to share the same slow path between back edges
6246 // of a same loop.
6247 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006248};
6249
David Srbecky0cf44932015-12-09 14:09:59 +00006250// Pseudo-instruction which provides the native debugger with mapping information.
6251// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006252class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006253 public:
6254 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006255 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306256 }
David Srbecky0cf44932015-12-09 14:09:59 +00006257
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006258 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006259 return true;
6260 }
6261
6262 DECLARE_INSTRUCTION(NativeDebugInfo);
6263
Artem Serovcced8ba2017-07-19 18:18:09 +01006264 protected:
6265 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006266};
6267
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006268/**
6269 * Instruction to load a Class object.
6270 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006271class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006272 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006273 // Determines how to load the Class.
6274 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006275 // We cannot load this class. See HSharpening::SharpenLoadClass.
6276 kInvalid = -1,
6277
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006278 // Use the Class* from the method's own ArtMethod*.
6279 kReferrersClass,
6280
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006281 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006282 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006283 kBootImageLinkTimePcRelative,
6284
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006285 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006286 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006287 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006288
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006289 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006290 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006291 kBssEntry,
6292
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006293 // Use a known boot image Class* address, embedded in the code by the codegen.
6294 // Used for boot image classes referenced by apps in JIT-compiled code.
6295 kJitBootImageAddress,
6296
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006297 // Load from the root table associated with the JIT compiled method.
6298 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006299
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006300 // Load using a simple runtime call. This is the fall-back load kind when
6301 // the codegen is unable to use another appropriate kind.
6302 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006303
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006304 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006305 };
6306
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006307 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006308 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006309 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006310 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006311 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006312 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006313 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006314 : HInstruction(kLoadClass,
6315 DataType::Type::kReference,
6316 SideEffectsForArchRuntimeCalls(),
6317 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006318 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006319 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006320 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006321 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006322 // Referrers class should not need access check. We never inline unverified
6323 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006324 DCHECK(!is_referrers_class || !needs_access_check);
6325
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006326 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006327 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006328 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006329 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006330 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006331 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006332 }
6333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006334 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006335
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006336 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006337
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006338 LoadKind GetLoadKind() const {
6339 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006340 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006341
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006342 bool HasPcRelativeLoadKind() const {
6343 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6344 GetLoadKind() == LoadKind::kBootImageRelRo ||
6345 GetLoadKind() == LoadKind::kBssEntry;
6346 }
6347
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006348 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006349
Yi Kong39402542019-03-24 02:47:16 -07006350 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006351
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006352 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006354 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006355
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006356 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006357 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006358 }
6359
Calin Juravle0ba218d2015-05-19 18:46:01 +01006360 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006361 // The entrypoint the code generator is going to call does not do
6362 // clinit of the class.
6363 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006364 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006365 }
6366
6367 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006368 return NeedsAccessCheck() ||
6369 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006370 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006371 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006372 }
6373
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006374 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006375 return NeedsAccessCheck() ||
6376 MustGenerateClinitCheck() ||
6377 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006378 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006379 GetLoadKind() == LoadKind::kBssEntry) &&
6380 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006381 }
6382
Calin Juravleacf735c2015-02-12 15:25:22 +00006383 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006384 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6385 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006386 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006387 } else {
6388 return ReferenceTypeInfo::CreateInvalid();
6389 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006390 }
6391
Vladimir Marko175e7862018-03-27 09:03:13 +00006392 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6393 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6394 DCHECK(klass_ != nullptr);
6395 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006396 }
6397
Andreas Gampea5b09a62016-11-17 15:21:22 -08006398 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006399 const DexFile& GetDexFile() const { return dex_file_; }
6400
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006401 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006402 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006403 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006404
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006405 static SideEffects SideEffectsForArchRuntimeCalls() {
6406 return SideEffects::CanTriggerGC();
6407 }
6408
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006409 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006411 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006412 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006413
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006414 bool MustResolveTypeOnSlowPath() const {
6415 // Check that this instruction has a slow path.
6416 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6417 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6418 return GetLoadKind() == LoadKind::kBssEntry;
6419 }
6420
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006421 void MarkInBootImage() {
6422 SetPackedFlag<kFlagIsInBootImage>(true);
6423 }
6424
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006425 void AddSpecialInput(HInstruction* special_input);
6426
6427 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006428 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006429 return ArrayRef<HUserRecord<HInstruction*>>(
6430 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6431 }
6432
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006433 Handle<mirror::Class> GetClass() const {
6434 return klass_;
6435 }
6436
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006437 DECLARE_INSTRUCTION(LoadClass);
6438
Artem Serovcced8ba2017-07-19 18:18:09 +01006439 protected:
6440 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6441
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006442 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006443 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006444 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006445 // Whether this instruction must generate the initialization check.
6446 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006447 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006448 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6449 static constexpr size_t kFieldLoadKindSize =
6450 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006451 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6452 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006453 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006454 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6455
6456 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006457 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006458 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006459 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006460 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006461 }
6462
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006463 void SetLoadKindInternal(LoadKind load_kind);
6464
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006465 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006466 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006467 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006468 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006469
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006470 // A type index and dex file where the class can be accessed. The dex file can be:
6471 // - The compiling method's dex file if the class is defined there too.
6472 // - The compiling method's dex file if the class is referenced there.
6473 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006474 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006475 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006476 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006477
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006478 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006479};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006480std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6481
6482// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006483inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6484 // The load kind should be determined before inserting the instruction to the graph.
6485 DCHECK(GetBlock() == nullptr);
6486 DCHECK(GetEnvironment() == nullptr);
6487 SetPackedField<LoadKindField>(load_kind);
6488 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6489 special_input_ = HUserRecord<HInstruction*>(nullptr);
6490 }
6491 if (!NeedsEnvironment()) {
6492 SetSideEffects(SideEffects::None());
6493 }
6494}
6495
6496// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006497inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006498 // The special input is used for PC-relative loads on some architectures,
6499 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006500 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006501 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006502 GetLoadKind() == LoadKind::kBssEntry ||
6503 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006504 DCHECK(special_input_.GetInstruction() == nullptr);
6505 special_input_ = HUserRecord<HInstruction*>(special_input);
6506 special_input->AddUseAt(this, 0);
6507}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006509class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006510 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006511 // Determines how to load the String.
6512 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006513 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006514 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006515 kBootImageLinkTimePcRelative,
6516
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006517 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006518 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006519 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006520
Vladimir Markoaad75c62016-10-03 08:46:48 +00006521 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006522 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006523 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006524
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006525 // Use a known boot image String* address, embedded in the code by the codegen.
6526 // Used for boot image strings referenced by apps in JIT-compiled code.
6527 kJitBootImageAddress,
6528
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006529 // Load from the root table associated with the JIT compiled method.
6530 kJitTableAddress,
6531
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006532 // Load using a simple runtime call. This is the fall-back load kind when
6533 // the codegen is unable to use another appropriate kind.
6534 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006535
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006536 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006537 };
6538
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006539 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006540 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006541 const DexFile& dex_file,
6542 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006543 : HInstruction(kLoadString,
6544 DataType::Type::kReference,
6545 SideEffectsForArchRuntimeCalls(),
6546 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006547 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006548 string_index_(string_index),
6549 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006550 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006551 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006552
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006553 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006554
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006555 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006556
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006557 LoadKind GetLoadKind() const {
6558 return GetPackedField<LoadKindField>();
6559 }
6560
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006561 bool HasPcRelativeLoadKind() const {
6562 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6563 GetLoadKind() == LoadKind::kBootImageRelRo ||
6564 GetLoadKind() == LoadKind::kBssEntry;
6565 }
6566
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006567 const DexFile& GetDexFile() const {
6568 return dex_file_;
6569 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006570
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006571 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006572 return string_index_;
6573 }
6574
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006575 Handle<mirror::String> GetString() const {
6576 return string_;
6577 }
6578
6579 void SetString(Handle<mirror::String> str) {
6580 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006581 }
6582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006583 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006584
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006585 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006586
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006587 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006588
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006589 // Will call the runtime if we need to load the string through
6590 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006591 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006592 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006593 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006594 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006595 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006596 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006597 return false;
6598 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006599 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006600 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006602 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006603 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006604 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006605
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006606 bool CanBeNull() const override { return false; }
6607 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006608
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006609 static SideEffects SideEffectsForArchRuntimeCalls() {
6610 return SideEffects::CanTriggerGC();
6611 }
6612
Vladimir Marko372f10e2016-05-17 16:30:10 +01006613 void AddSpecialInput(HInstruction* special_input);
6614
6615 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006616 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006617 return ArrayRef<HUserRecord<HInstruction*>>(
6618 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006619 }
6620
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006621 DECLARE_INSTRUCTION(LoadString);
6622
Artem Serovcced8ba2017-07-19 18:18:09 +01006623 protected:
6624 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6625
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006626 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006627 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006628 static constexpr size_t kFieldLoadKindSize =
6629 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6630 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006631 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006632 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006633
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006634 void SetLoadKindInternal(LoadKind load_kind);
6635
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006636 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006637 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006638 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006639 HUserRecord<HInstruction*> special_input_;
6640
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006641 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006642 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006643
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006644 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006645};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006646std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6647
6648// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006649inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6650 // The load kind should be determined before inserting the instruction to the graph.
6651 DCHECK(GetBlock() == nullptr);
6652 DCHECK(GetEnvironment() == nullptr);
6653 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6654 SetPackedField<LoadKindField>(load_kind);
6655 if (load_kind != LoadKind::kRuntimeCall) {
6656 special_input_ = HUserRecord<HInstruction*>(nullptr);
6657 }
6658 if (!NeedsEnvironment()) {
6659 SetSideEffects(SideEffects::None());
6660 }
6661}
6662
6663// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006664inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006665 // The special input is used for PC-relative loads on some architectures,
6666 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006667 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006668 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006669 GetLoadKind() == LoadKind::kBssEntry ||
6670 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006671 // HLoadString::GetInputRecords() returns an empty array at this point,
6672 // so use the GetInputRecords() from the base class to set the input record.
6673 DCHECK(special_input_.GetInstruction() == nullptr);
6674 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006675 special_input->AddUseAt(this, 0);
6676}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006677
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006678class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006679 public:
6680 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006681 uint16_t method_handle_idx,
6682 const DexFile& dex_file,
6683 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006684 : HInstruction(kLoadMethodHandle,
6685 DataType::Type::kReference,
6686 SideEffectsForArchRuntimeCalls(),
6687 dex_pc),
6688 special_input_(HUserRecord<HInstruction*>(current_method)),
6689 method_handle_idx_(method_handle_idx),
6690 dex_file_(dex_file) {
6691 }
6692
6693 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006694 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006695 return ArrayRef<HUserRecord<HInstruction*>>(
6696 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6697 }
6698
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006699 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006700
6701 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6702
6703 const DexFile& GetDexFile() const { return dex_file_; }
6704
6705 static SideEffects SideEffectsForArchRuntimeCalls() {
6706 return SideEffects::CanTriggerGC();
6707 }
6708
6709 DECLARE_INSTRUCTION(LoadMethodHandle);
6710
6711 protected:
6712 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6713
6714 private:
6715 // The special input is the HCurrentMethod for kRuntimeCall.
6716 HUserRecord<HInstruction*> special_input_;
6717
6718 const uint16_t method_handle_idx_;
6719 const DexFile& dex_file_;
6720};
6721
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006722class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006723 public:
6724 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006725 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006726 const DexFile& dex_file,
6727 uint32_t dex_pc)
6728 : HInstruction(kLoadMethodType,
6729 DataType::Type::kReference,
6730 SideEffectsForArchRuntimeCalls(),
6731 dex_pc),
6732 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006733 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006734 dex_file_(dex_file) {
6735 }
6736
6737 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006738 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006739 return ArrayRef<HUserRecord<HInstruction*>>(
6740 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6741 }
6742
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006743 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006744
Orion Hodson06d10a72018-05-14 08:53:38 +01006745 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006746
6747 const DexFile& GetDexFile() const { return dex_file_; }
6748
6749 static SideEffects SideEffectsForArchRuntimeCalls() {
6750 return SideEffects::CanTriggerGC();
6751 }
6752
6753 DECLARE_INSTRUCTION(LoadMethodType);
6754
6755 protected:
6756 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6757
6758 private:
6759 // The special input is the HCurrentMethod for kRuntimeCall.
6760 HUserRecord<HInstruction*> special_input_;
6761
Orion Hodson06d10a72018-05-14 08:53:38 +01006762 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006763 const DexFile& dex_file_;
6764};
6765
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006766/**
6767 * Performs an initialization check on its Class object input.
6768 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006769class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006770 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006771 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006772 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306773 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006774 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006775 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006776 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006777 SetRawInputAt(0, constant);
6778 }
Artem Serova6e26142018-06-19 14:55:17 +01006779 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006780 bool CanBeMoved() const override { return true; }
6781 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006782 return true;
6783 }
6784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006785 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006786 // May call runtime to initialize the class.
6787 return true;
6788 }
6789
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006790 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006791
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006792 HLoadClass* GetLoadClass() const {
6793 DCHECK(InputAt(0)->IsLoadClass());
6794 return InputAt(0)->AsLoadClass();
6795 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006796
6797 DECLARE_INSTRUCTION(ClinitCheck);
6798
Artem Serovcced8ba2017-07-19 18:18:09 +01006799
6800 protected:
6801 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006802};
6803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006804class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006805 public:
6806 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006807 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006808 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006809 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006810 bool is_volatile,
6811 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006812 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006813 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006814 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306815 : HExpression(kStaticFieldGet,
6816 field_type,
6817 SideEffects::FieldReadOfType(field_type, is_volatile),
6818 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006819 field_info_(field,
6820 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006821 field_type,
6822 is_volatile,
6823 field_idx,
6824 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006825 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006826 SetRawInputAt(0, cls);
6827 }
6828
Calin Juravle52c48962014-12-16 17:02:57 +00006829
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006830 bool IsClonable() const override { return true; }
6831 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006833 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006834 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006835 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006836 }
6837
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006838 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006839 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6840 }
6841
Calin Juravle52c48962014-12-16 17:02:57 +00006842 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006843 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006844 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006845 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006846
Vladimir Marko61b92282017-10-11 13:23:17 +01006847 void SetType(DataType::Type new_type) {
6848 DCHECK(DataType::IsIntegralType(GetType()));
6849 DCHECK(DataType::IsIntegralType(new_type));
6850 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6851 SetPackedField<TypeField>(new_type);
6852 }
6853
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006854 DECLARE_INSTRUCTION(StaticFieldGet);
6855
Artem Serovcced8ba2017-07-19 18:18:09 +01006856 protected:
6857 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6858
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006859 private:
6860 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006861};
6862
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006863class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006864 public:
6865 HStaticFieldSet(HInstruction* cls,
6866 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006867 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006868 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006869 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006870 bool is_volatile,
6871 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006872 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006873 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006874 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006875 : HExpression(kStaticFieldSet,
6876 SideEffects::FieldWriteOfType(field_type, is_volatile),
6877 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006878 field_info_(field,
6879 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006880 field_type,
6881 is_volatile,
6882 field_idx,
6883 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006884 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006885 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006886 SetRawInputAt(0, cls);
6887 SetRawInputAt(1, value);
6888 }
6889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006890 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006891 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006892 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006893 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006894 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006895
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006896 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006897 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6898 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006899
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006900 DECLARE_INSTRUCTION(StaticFieldSet);
6901
Artem Serovcced8ba2017-07-19 18:18:09 +01006902 protected:
6903 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6904
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006905 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006906 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6907 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6908 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6909 "Too many packed fields.");
6910
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006911 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006912};
6913
Vladimir Marko552a1342017-10-31 10:56:47 +00006914class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6915 public:
6916 HStringBuilderAppend(HIntConstant* format,
6917 uint32_t number_of_arguments,
6918 ArenaAllocator* allocator,
6919 uint32_t dex_pc)
6920 : HVariableInputSizeInstruction(
6921 kStringBuilderAppend,
6922 DataType::Type::kReference,
6923 // The runtime call may read memory from inputs. It never writes outside
6924 // of the newly allocated result object (or newly allocated helper objects).
6925 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6926 dex_pc,
6927 allocator,
6928 number_of_arguments + /* format */ 1u,
6929 kArenaAllocInvokeInputs) {
6930 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6931 SetRawInputAt(FormatIndex(), format);
6932 }
6933
6934 void SetArgumentAt(size_t index, HInstruction* argument) {
6935 DCHECK_LE(index, GetNumberOfArguments());
6936 SetRawInputAt(index, argument);
6937 }
6938
6939 // Return the number of arguments, excluding the format.
6940 size_t GetNumberOfArguments() const {
6941 DCHECK_GE(InputCount(), 1u);
6942 return InputCount() - 1u;
6943 }
6944
6945 size_t FormatIndex() const {
6946 return GetNumberOfArguments();
6947 }
6948
6949 HIntConstant* GetFormat() {
6950 return InputAt(FormatIndex())->AsIntConstant();
6951 }
6952
6953 bool NeedsEnvironment() const override { return true; }
6954
6955 bool CanThrow() const override { return true; }
6956
6957 DECLARE_INSTRUCTION(StringBuilderAppend);
6958
6959 protected:
6960 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
6961};
6962
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006963class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006964 public:
6965 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006966 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006967 uint32_t field_index,
6968 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306969 : HExpression(kUnresolvedInstanceFieldGet,
6970 field_type,
6971 SideEffects::AllExceptGCDependency(),
6972 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006973 field_index_(field_index) {
6974 SetRawInputAt(0, obj);
6975 }
6976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006977 bool IsClonable() const override { return true; }
6978 bool NeedsEnvironment() const override { return true; }
6979 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006980
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006981 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006982 uint32_t GetFieldIndex() const { return field_index_; }
6983
6984 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6985
Artem Serovcced8ba2017-07-19 18:18:09 +01006986 protected:
6987 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6988
Calin Juravlee460d1d2015-09-29 04:52:17 +01006989 private:
6990 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006991};
6992
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006993class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006994 public:
6995 HUnresolvedInstanceFieldSet(HInstruction* obj,
6996 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006997 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006998 uint32_t field_index,
6999 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007000 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007001 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007002 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007003 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007004 SetRawInputAt(0, obj);
7005 SetRawInputAt(1, value);
7006 }
7007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007008 bool IsClonable() const override { return true; }
7009 bool NeedsEnvironment() const override { return true; }
7010 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007012 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007013 uint32_t GetFieldIndex() const { return field_index_; }
7014
7015 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7016
Artem Serovcced8ba2017-07-19 18:18:09 +01007017 protected:
7018 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7019
Calin Juravlee460d1d2015-09-29 04:52:17 +01007020 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007021 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7022 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007023 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007024 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7025 kFieldFieldType + kFieldFieldTypeSize;
7026 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7027 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007028 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007029
Calin Juravlee460d1d2015-09-29 04:52:17 +01007030 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007031};
7032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007033class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007034 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007035 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007036 uint32_t field_index,
7037 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307038 : HExpression(kUnresolvedStaticFieldGet,
7039 field_type,
7040 SideEffects::AllExceptGCDependency(),
7041 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007042 field_index_(field_index) {
7043 }
7044
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007045 bool IsClonable() const override { return true; }
7046 bool NeedsEnvironment() const override { return true; }
7047 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007048
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007049 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007050 uint32_t GetFieldIndex() const { return field_index_; }
7051
7052 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7053
Artem Serovcced8ba2017-07-19 18:18:09 +01007054 protected:
7055 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7056
Calin Juravlee460d1d2015-09-29 04:52:17 +01007057 private:
7058 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007059};
7060
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007061class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007062 public:
7063 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007064 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007065 uint32_t field_index,
7066 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007067 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007068 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007069 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007070 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007071 SetRawInputAt(0, value);
7072 }
7073
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007074 bool IsClonable() const override { return true; }
7075 bool NeedsEnvironment() const override { return true; }
7076 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007077
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007078 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007079 uint32_t GetFieldIndex() const { return field_index_; }
7080
7081 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7082
Artem Serovcced8ba2017-07-19 18:18:09 +01007083 protected:
7084 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7085
Calin Juravlee460d1d2015-09-29 04:52:17 +01007086 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007087 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7088 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007089 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007090 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7091 kFieldFieldType + kFieldFieldTypeSize;
7092 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7093 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007094 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007095
Calin Juravlee460d1d2015-09-29 04:52:17 +01007096 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007097};
7098
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007099// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007100class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007101 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007102 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307103 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7104 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007106 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007107
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007108 DECLARE_INSTRUCTION(LoadException);
7109
Artem Serovcced8ba2017-07-19 18:18:09 +01007110 protected:
7111 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007112};
7113
David Brazdilcb1c0552015-08-04 16:22:25 +01007114// Implicit part of move-exception which clears thread-local exception storage.
7115// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007116class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007117 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007118 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007119 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307120 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007121
7122 DECLARE_INSTRUCTION(ClearException);
7123
Artem Serovcced8ba2017-07-19 18:18:09 +01007124 protected:
7125 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007126};
7127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007128class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007129 public:
7130 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007131 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007132 SetRawInputAt(0, exception);
7133 }
7134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007135 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007136
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007137 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007138
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007139 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007140
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007141 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007142
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007143 DECLARE_INSTRUCTION(Throw);
7144
Artem Serovcced8ba2017-07-19 18:18:09 +01007145 protected:
7146 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007147};
7148
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007149/**
7150 * Implementation strategies for the code generator of a HInstanceOf
7151 * or `HCheckCast`.
7152 */
7153enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007154 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007155 kExactCheck, // Can do a single class compare.
7156 kClassHierarchyCheck, // Can just walk the super class chain.
7157 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7158 kInterfaceCheck, // No optimization yet when checking against an interface.
7159 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007160 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007161 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007162 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007163};
7164
Roland Levillain86503782016-02-11 19:07:30 +00007165std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7166
Vladimir Marko175e7862018-03-27 09:03:13 +00007167// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7168// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7169class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007170 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007171 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007172 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007173 HInstruction* object,
7174 HInstruction* target_class_or_null,
7175 TypeCheckKind check_kind,
7176 Handle<mirror::Class> klass,
7177 uint32_t dex_pc,
7178 ArenaAllocator* allocator,
7179 HIntConstant* bitstring_path_to_root,
7180 HIntConstant* bitstring_mask,
7181 SideEffects side_effects)
7182 : HVariableInputSizeInstruction(
7183 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007184 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007185 side_effects,
7186 dex_pc,
7187 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007188 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007189 kArenaAllocTypeCheckInputs),
7190 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007191 SetPackedField<TypeCheckKindField>(check_kind);
7192 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007193 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007194 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007195 SetRawInputAt(1, target_class_or_null);
7196 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7197 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7198 if (check_kind == TypeCheckKind::kBitstringCheck) {
7199 DCHECK(target_class_or_null->IsNullConstant());
7200 SetRawInputAt(2, bitstring_path_to_root);
7201 SetRawInputAt(3, bitstring_mask);
7202 } else {
7203 DCHECK(target_class_or_null->IsLoadClass());
7204 }
Vladimir Marko87584542017-12-12 17:47:52 +00007205 }
7206
7207 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007208 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007209 HInstruction* load_class = InputAt(1);
7210 DCHECK(load_class->IsLoadClass());
7211 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007212 }
7213
Vladimir Marko175e7862018-03-27 09:03:13 +00007214 uint32_t GetBitstringPathToRoot() const {
7215 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7216 HInstruction* path_to_root = InputAt(2);
7217 DCHECK(path_to_root->IsIntConstant());
7218 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7219 }
7220
7221 uint32_t GetBitstringMask() const {
7222 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7223 HInstruction* mask = InputAt(3);
7224 DCHECK(mask->IsIntConstant());
7225 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7226 }
7227
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007228 bool IsClonable() const override { return true; }
7229 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007230
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007231 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007232 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7233 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007234 }
7235
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007236 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7237 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7238 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7239 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7240
Vladimir Marko175e7862018-03-27 09:03:13 +00007241 ReferenceTypeInfo GetTargetClassRTI() {
7242 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7243 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007244 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007245 } else {
7246 return ReferenceTypeInfo::CreateInvalid();
7247 }
7248 }
7249
7250 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7251 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7252 DCHECK(klass_ != nullptr);
7253 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7254 }
7255
7256 Handle<mirror::Class> GetClass() const {
7257 return klass_;
7258 }
7259
7260 protected:
7261 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7262
7263 private:
7264 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7265 static constexpr size_t kFieldTypeCheckKindSize =
7266 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7267 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7268 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7269 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7270 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7271 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7272
7273 Handle<mirror::Class> klass_;
7274};
7275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007276class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007277 public:
7278 HInstanceOf(HInstruction* object,
7279 HInstruction* target_class_or_null,
7280 TypeCheckKind check_kind,
7281 Handle<mirror::Class> klass,
7282 uint32_t dex_pc,
7283 ArenaAllocator* allocator,
7284 HIntConstant* bitstring_path_to_root,
7285 HIntConstant* bitstring_mask)
7286 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007287 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007288 object,
7289 target_class_or_null,
7290 check_kind,
7291 klass,
7292 dex_pc,
7293 allocator,
7294 bitstring_path_to_root,
7295 bitstring_mask,
7296 SideEffectsForArchRuntimeCalls(check_kind)) {}
7297
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007298 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007300 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007301 return CanCallRuntime(GetTypeCheckKind());
7302 }
7303
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007304 static bool CanCallRuntime(TypeCheckKind check_kind) {
7305 // Mips currently does runtime calls for any other checks.
7306 return check_kind != TypeCheckKind::kExactCheck;
7307 }
7308
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007309 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007310 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007311 }
7312
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007313 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007314
Artem Serovcced8ba2017-07-19 18:18:09 +01007315 protected:
7316 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007317};
7318
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007319class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007320 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007321 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307322 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007323 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7324 SetPackedFlag<kFlagUpperCanBeNull>(true);
7325 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007326 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007327 SetRawInputAt(0, input);
7328 }
7329
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007330 bool InstructionDataEquals(const HInstruction* other) const override;
7331 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007332
David Brazdilf5552582015-12-27 13:36:12 +00007333 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007334 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007335 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007336 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007337
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007338 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007339 DCHECK(GetUpperCanBeNull() || !can_be_null);
7340 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007341 }
7342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007343 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007344
Calin Juravleb1498f62015-02-16 13:13:29 +00007345 DECLARE_INSTRUCTION(BoundType);
7346
Artem Serovcced8ba2017-07-19 18:18:09 +01007347 protected:
7348 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7349
Calin Juravleb1498f62015-02-16 13:13:29 +00007350 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007351 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7352 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007353 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007354 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7355 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7356 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7357
Calin Juravleb1498f62015-02-16 13:13:29 +00007358 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007359 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7360 // It is used to bound the type in cases like:
7361 // if (x instanceof ClassX) {
7362 // // uper_bound_ will be ClassX
7363 // }
David Brazdilf5552582015-12-27 13:36:12 +00007364 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007365};
7366
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007367class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007368 public:
7369 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007370 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007371 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007372 Handle<mirror::Class> klass,
7373 uint32_t dex_pc,
7374 ArenaAllocator* allocator,
7375 HIntConstant* bitstring_path_to_root,
7376 HIntConstant* bitstring_mask)
7377 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007378 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007379 object,
7380 target_class_or_null,
7381 check_kind,
7382 klass,
7383 dex_pc,
7384 allocator,
7385 bitstring_path_to_root,
7386 bitstring_mask,
7387 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007388
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007389 bool IsClonable() const override { return true; }
7390 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007391 // Instruction may throw a CheckCastError.
7392 return true;
7393 }
7394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007395 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007396
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007397 DECLARE_INSTRUCTION(CheckCast);
7398
Artem Serovcced8ba2017-07-19 18:18:09 +01007399 protected:
7400 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007401};
7402
Andreas Gampe26de38b2016-07-27 17:53:11 -07007403/**
7404 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7405 * @details We define the combined barrier types that are actually required
7406 * by the Java Memory Model, rather than using exactly the terminology from
7407 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7408 * primitives. Note that the JSR-133 cookbook generally does not deal with
7409 * store atomicity issues, and the recipes there are not always entirely sufficient.
7410 * The current recipe is as follows:
7411 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7412 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7413 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7414 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7415 * class has final fields.
7416 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7417 * store-to-memory instructions. Only generated together with non-temporal stores.
7418 */
7419enum MemBarrierKind {
7420 kAnyStore,
7421 kLoadAny,
7422 kStoreStore,
7423 kAnyAny,
7424 kNTStoreStore,
7425 kLastBarrierKind = kNTStoreStore
7426};
7427std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7428
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007429class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007430 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007431 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007432 : HExpression(kMemoryBarrier,
7433 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7434 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007435 SetPackedField<BarrierKindField>(barrier_kind);
7436 }
Calin Juravle27df7582015-04-17 19:12:31 +01007437
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007438 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007439
Vladimir Markoa1de9182016-02-25 11:37:38 +00007440 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007441
7442 DECLARE_INSTRUCTION(MemoryBarrier);
7443
Artem Serovcced8ba2017-07-19 18:18:09 +01007444 protected:
7445 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7446
Calin Juravle27df7582015-04-17 19:12:31 +01007447 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007448 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7449 static constexpr size_t kFieldBarrierKindSize =
7450 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7451 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7452 kFieldBarrierKind + kFieldBarrierKindSize;
7453 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7454 "Too many packed fields.");
7455 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007456};
7457
Igor Murashkind01745e2017-04-05 16:40:31 -07007458// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7459// the specified object(s), with respect to any subsequent store that might "publish"
7460// (i.e. make visible) the specified object to another thread.
7461//
7462// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7463// for all final fields (that were set) at the end of the invoked constructor.
7464//
7465// The constructor fence models the freeze actions for the final fields of an object
7466// being constructed (semantically at the end of the constructor). Constructor fences
7467// have a per-object affinity; two separate objects being constructed get two separate
7468// constructor fences.
7469//
7470// (Note: that if calling a super-constructor or forwarding to another constructor,
7471// the freezes would happen at the end of *that* constructor being invoked).
7472//
7473// The memory model guarantees that when the object being constructed is "published" after
7474// constructor completion (i.e. escapes the current thread via a store), then any final field
7475// writes must be observable on other threads (once they observe that publication).
7476//
7477// Further, anything written before the freeze, and read by dereferencing through the final field,
7478// must also be visible (so final object field could itself have an object with non-final fields;
7479// yet the freeze must also extend to them).
7480//
7481// Constructor example:
7482//
7483// class HasFinal {
7484// final int field; Optimizing IR for <init>()V:
7485// HasFinal() {
7486// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7487// // freeze(this.field); HConstructorFence(this)
7488// } HReturn
7489// }
7490//
7491// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7492// already-initialized classes; in that case the allocation must act as a "default-initializer"
7493// of the object which effectively writes the class pointer "final field".
7494//
7495// For example, we can model default-initialiation as roughly the equivalent of the following:
7496//
7497// class Object {
7498// private final Class header;
7499// }
7500//
7501// Java code: Optimizing IR:
7502//
7503// T new_instance<T>() {
7504// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7505// obj.header = T.class; // header write is done by above call.
7506// // freeze(obj.header) HConstructorFence(obj)
7507// return (T)obj;
7508// }
7509//
7510// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007511// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007512// * QuasiAtomic::ThreadFenceForConstructor
7513//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007514class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007515 // A fence has variable inputs because the inputs can be removed
7516 // after prepare_for_register_allocation phase.
7517 // (TODO: In the future a fence could freeze multiple objects
7518 // after merging two fences together.)
7519 public:
7520 // `fence_object` is the reference that needs to be protected for correct publication.
7521 //
7522 // It makes sense in the following situations:
7523 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7524 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7525 //
7526 // After construction the `fence_object` becomes the 0th input.
7527 // This is not an input in a real sense, but just a convenient place to stash the information
7528 // about the associated object.
7529 HConstructorFence(HInstruction* fence_object,
7530 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007531 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007532 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7533 // constraints described in the class header. We claim that these SideEffects constraints
7534 // enforce a superset of the real constraints.
7535 //
7536 // The ordering described above is conservatively modeled with SideEffects as follows:
7537 //
7538 // * To prevent reordering of the publication stores:
7539 // ----> "Reads of objects" is the initial SideEffect.
7540 // * For every primitive final field store in the constructor:
7541 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7542 // * If there are any stores to reference final fields in the constructor:
7543 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7544 // that are reachable from `fence_object` also need to be prevented for reordering
7545 // (and we do not want to do alias analysis to figure out what those stores are).
7546 //
7547 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7548 // even more conservative approach than the one described above, and prevents all of the
7549 // above reordering without analyzing any of the instructions in the constructor.
7550 //
7551 // If in a later phase we discover that there are no writes to reference final fields,
7552 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307553 : HVariableInputSizeInstruction(kConstructorFence,
7554 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007555 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007556 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007557 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007558 kArenaAllocConstructorFenceInputs) {
7559 DCHECK(fence_object != nullptr);
7560 SetRawInputAt(0, fence_object);
7561 }
7562
7563 // The object associated with this constructor fence.
7564 //
7565 // (Note: This will be null after the prepare_for_register_allocation phase,
7566 // as all constructor fence inputs are removed there).
7567 HInstruction* GetFenceObject() const {
7568 return InputAt(0);
7569 }
7570
7571 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7572 // - Delete `fence_use` from `this`'s use list.
7573 // - Delete `this` from `fence_use`'s inputs list.
7574 // - If the `fence_use` is dead, remove it from the graph.
7575 //
7576 // A fence is considered dead once it no longer has any uses
7577 // and all of the inputs are dead.
7578 //
7579 // This must *not* be called during/after prepare_for_register_allocation,
7580 // because that removes all the inputs to the fences but the fence is actually
7581 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007582 //
7583 // Returns how many HConstructorFence instructions were removed from graph.
7584 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007585
Igor Murashkindd018df2017-08-09 10:38:31 -07007586 // Combine all inputs of `this` and `other` instruction and remove
7587 // `other` from the graph.
7588 //
7589 // Inputs are unique after the merge.
7590 //
7591 // Requirement: `this` must not be the same as `other.
7592 void Merge(HConstructorFence* other);
7593
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007594 // Check if this constructor fence is protecting
7595 // an HNewInstance or HNewArray that is also the immediate
7596 // predecessor of `this`.
7597 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007598 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7599 // to be one of the inputs of `this`.
7600 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007601 // Returns the associated HNewArray or HNewInstance,
7602 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007603 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007604
Igor Murashkind01745e2017-04-05 16:40:31 -07007605 DECLARE_INSTRUCTION(ConstructorFence);
7606
Artem Serovcced8ba2017-07-19 18:18:09 +01007607 protected:
7608 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007609};
7610
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007611class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007612 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007613 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007614 kEnter,
7615 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007616 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007617 };
7618
7619 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007620 : HExpression(kMonitorOperation,
7621 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7622 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007623 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007624 SetRawInputAt(0, object);
7625 }
7626
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007627 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007628 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007629
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007630 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007631 // Verifier guarantees that monitor-exit cannot throw.
7632 // This is important because it allows the HGraphBuilder to remove
7633 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7634 return IsEnter();
7635 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007636
Vladimir Markoa1de9182016-02-25 11:37:38 +00007637 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7638 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007639
7640 DECLARE_INSTRUCTION(MonitorOperation);
7641
Artem Serovcced8ba2017-07-19 18:18:09 +01007642 protected:
7643 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7644
Calin Juravle52c48962014-12-16 17:02:57 +00007645 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007646 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7647 static constexpr size_t kFieldOperationKindSize =
7648 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7649 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7650 kFieldOperationKind + kFieldOperationKindSize;
7651 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7652 "Too many packed fields.");
7653 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007654};
7655
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007656class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007657 public:
7658 HSelect(HInstruction* condition,
7659 HInstruction* true_value,
7660 HInstruction* false_value,
7661 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307662 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007663 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7664
7665 // First input must be `true_value` or `false_value` to allow codegens to
7666 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7667 // that architectures which implement HSelect as a conditional move also
7668 // will not need to invert the condition.
7669 SetRawInputAt(0, false_value);
7670 SetRawInputAt(1, true_value);
7671 SetRawInputAt(2, condition);
7672 }
7673
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007674 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007675 HInstruction* GetFalseValue() const { return InputAt(0); }
7676 HInstruction* GetTrueValue() const { return InputAt(1); }
7677 HInstruction* GetCondition() const { return InputAt(2); }
7678
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007679 bool CanBeMoved() const override { return true; }
7680 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007681 return true;
7682 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007683
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007684 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007685 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7686 }
7687
7688 DECLARE_INSTRUCTION(Select);
7689
Artem Serovcced8ba2017-07-19 18:18:09 +01007690 protected:
7691 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007692};
7693
Vladimir Markof9f64412015-09-02 14:05:49 +01007694class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007695 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007696 MoveOperands(Location source,
7697 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007698 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007699 HInstruction* instruction)
7700 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007701
7702 Location GetSource() const { return source_; }
7703 Location GetDestination() const { return destination_; }
7704
7705 void SetSource(Location value) { source_ = value; }
7706 void SetDestination(Location value) { destination_ = value; }
7707
7708 // The parallel move resolver marks moves as "in-progress" by clearing the
7709 // destination (but not the source).
7710 Location MarkPending() {
7711 DCHECK(!IsPending());
7712 Location dest = destination_;
7713 destination_ = Location::NoLocation();
7714 return dest;
7715 }
7716
7717 void ClearPending(Location dest) {
7718 DCHECK(IsPending());
7719 destination_ = dest;
7720 }
7721
7722 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007723 DCHECK(source_.IsValid() || destination_.IsInvalid());
7724 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007725 }
7726
7727 // True if this blocks a move from the given location.
7728 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007729 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007730 }
7731
7732 // A move is redundant if it's been eliminated, if its source and
7733 // destination are the same, or if its destination is unneeded.
7734 bool IsRedundant() const {
7735 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7736 }
7737
7738 // We clear both operands to indicate move that's been eliminated.
7739 void Eliminate() {
7740 source_ = destination_ = Location::NoLocation();
7741 }
7742
7743 bool IsEliminated() const {
7744 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7745 return source_.IsInvalid();
7746 }
7747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007748 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007749
Nicolas Geoffray90218252015-04-15 11:56:51 +01007750 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007751 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007752 }
7753
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007754 HInstruction* GetInstruction() const { return instruction_; }
7755
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007756 private:
7757 Location source_;
7758 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007759 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007760 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007761 // The instruction this move is assocatied with. Null when this move is
7762 // for moving an input in the expected locations of user (including a phi user).
7763 // This is only used in debug mode, to ensure we do not connect interval siblings
7764 // in the same parallel move.
7765 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007766};
7767
Roland Levillainc9285912015-12-18 10:38:42 +00007768std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7769
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007770static constexpr size_t kDefaultNumberOfMoves = 4;
7771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007772class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007773 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007774 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007775 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007776 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007777 moves_.reserve(kDefaultNumberOfMoves);
7778 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007779
Nicolas Geoffray90218252015-04-15 11:56:51 +01007780 void AddMove(Location source,
7781 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007782 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007783 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007784 DCHECK(source.IsValid());
7785 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007786 if (kIsDebugBuild) {
7787 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007788 for (const MoveOperands& move : moves_) {
7789 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007790 // Special case the situation where the move is for the spill slot
7791 // of the instruction.
7792 if ((GetPrevious() == instruction)
7793 || ((GetPrevious() == nullptr)
7794 && instruction->IsPhi()
7795 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007796 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007797 << "Doing parallel moves for the same instruction.";
7798 } else {
7799 DCHECK(false) << "Doing parallel moves for the same instruction.";
7800 }
7801 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007802 }
7803 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007804 for (const MoveOperands& move : moves_) {
7805 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007806 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007807 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007808 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007809 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007810 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007811 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007812 }
7813
Vladimir Marko225b6462015-09-28 12:17:40 +01007814 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007815 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007816 }
7817
Vladimir Marko225b6462015-09-28 12:17:40 +01007818 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007819
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007820 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007821
Artem Serovcced8ba2017-07-19 18:18:09 +01007822 protected:
7823 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7824
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007825 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007826 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007827};
7828
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007829// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7830// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7831// never used across anything that can trigger GC.
7832// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7833// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007834class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007835 public:
7836 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307837 : HExpression(kIntermediateAddress,
7838 DataType::Type::kInt32,
7839 SideEffects::DependsOnGC(),
7840 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007841 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7842 DataType::Size(DataType::Type::kReference))
7843 << "kPrimInt and kPrimNot have different sizes.";
7844 SetRawInputAt(0, base_address);
7845 SetRawInputAt(1, offset);
7846 }
7847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007848 bool IsClonable() const override { return true; }
7849 bool CanBeMoved() const override { return true; }
7850 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007851 return true;
7852 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007853 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007854
7855 HInstruction* GetBaseAddress() const { return InputAt(0); }
7856 HInstruction* GetOffset() const { return InputAt(1); }
7857
7858 DECLARE_INSTRUCTION(IntermediateAddress);
7859
Artem Serovcced8ba2017-07-19 18:18:09 +01007860 protected:
7861 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007862};
7863
7864
Mark Mendell0616ae02015-04-17 12:49:27 -04007865} // namespace art
7866
Aart Bikf8f5a162017-02-06 15:35:29 -08007867#include "nodes_vector.h"
7868
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007869#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7870#include "nodes_shared.h"
7871#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007872#ifdef ART_ENABLE_CODEGEN_mips
7873#include "nodes_mips.h"
7874#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307875#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007876#include "nodes_x86.h"
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05307877#include "nodes_vector_x86.h"
Mark Mendell0616ae02015-04-17 12:49:27 -04007878#endif
7879
7880namespace art {
7881
Igor Murashkin6ef45672017-08-08 13:59:55 -07007882class OptimizingCompilerStats;
7883
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007884class HGraphVisitor : public ValueObject {
7885 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007886 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7887 : stats_(stats),
7888 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007889 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007890
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007891 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007892 virtual void VisitBasicBlock(HBasicBlock* block);
7893
Roland Levillain633021e2014-10-01 14:12:25 +01007894 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007895 void VisitInsertionOrder();
7896
Roland Levillain633021e2014-10-01 14:12:25 +01007897 // Visit the graph following dominator tree reverse post-order.
7898 void VisitReversePostOrder();
7899
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007900 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007901
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007902 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007903#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007904 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7905
7906 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7907
7908#undef DECLARE_VISIT_INSTRUCTION
7909
Igor Murashkin6ef45672017-08-08 13:59:55 -07007910 protected:
7911 OptimizingCompilerStats* stats_;
7912
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007913 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007914 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007915
7916 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7917};
7918
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007919class HGraphDelegateVisitor : public HGraphVisitor {
7920 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007921 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7922 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007923 virtual ~HGraphDelegateVisitor() {}
7924
7925 // Visit functions that delegate to to super class.
7926#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007927 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007928
7929 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7930
7931#undef DECLARE_VISIT_INSTRUCTION
7932
7933 private:
7934 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7935};
7936
Artem Serovcced8ba2017-07-19 18:18:09 +01007937// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7938// and remove the old instruction.
7939HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7940
7941// Create a clone for each clonable instructions/phis and replace the original with the clone.
7942//
7943// Used for testing individual instruction cloner.
7944class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7945 public:
7946 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007947 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007949 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007950 if (instruction->IsClonable()) {
7951 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007952 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007953 }
7954 }
7955
Artem Serov7f4aff62017-06-21 17:02:18 +01007956 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007957
7958 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007959 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007960
7961 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7962};
7963
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007964// Iterator over the blocks that art part of the loop. Includes blocks part
7965// of an inner loop. The order in which the blocks are iterated is on their
7966// block id.
7967class HBlocksInLoopIterator : public ValueObject {
7968 public:
7969 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7970 : blocks_in_loop_(info.GetBlocks()),
7971 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7972 index_(0) {
7973 if (!blocks_in_loop_.IsBitSet(index_)) {
7974 Advance();
7975 }
7976 }
7977
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007978 bool Done() const { return index_ == blocks_.size(); }
7979 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007980 void Advance() {
7981 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007982 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007983 if (blocks_in_loop_.IsBitSet(index_)) {
7984 break;
7985 }
7986 }
7987 }
7988
7989 private:
7990 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007991 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007992 size_t index_;
7993
7994 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7995};
7996
Mingyao Yang3584bce2015-05-19 16:01:59 -07007997// Iterator over the blocks that art part of the loop. Includes blocks part
7998// of an inner loop. The order in which the blocks are iterated is reverse
7999// post order.
8000class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8001 public:
8002 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8003 : blocks_in_loop_(info.GetBlocks()),
8004 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8005 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008006 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008007 Advance();
8008 }
8009 }
8010
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008011 bool Done() const { return index_ == blocks_.size(); }
8012 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008013 void Advance() {
8014 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008015 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8016 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008017 break;
8018 }
8019 }
8020 }
8021
8022 private:
8023 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008024 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008025 size_t index_;
8026
8027 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8028};
8029
Aart Bikf3e61ee2017-04-12 17:09:20 -07008030// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008031inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008032 if (constant->IsIntConstant()) {
8033 return constant->AsIntConstant()->GetValue();
8034 } else if (constant->IsLongConstant()) {
8035 return constant->AsLongConstant()->GetValue();
8036 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008037 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008038 return 0;
8039 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008040}
8041
Aart Bikf3e61ee2017-04-12 17:09:20 -07008042// Returns true iff instruction is an integral constant (and sets value on success).
8043inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8044 if (instruction->IsIntConstant()) {
8045 *value = instruction->AsIntConstant()->GetValue();
8046 return true;
8047 } else if (instruction->IsLongConstant()) {
8048 *value = instruction->AsLongConstant()->GetValue();
8049 return true;
8050 } else if (instruction->IsNullConstant()) {
8051 *value = 0;
8052 return true;
8053 }
8054 return false;
8055}
8056
Aart Bik0148de42017-09-05 09:25:01 -07008057// Returns true iff instruction is the given integral constant.
8058inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8059 int64_t val = 0;
8060 return IsInt64AndGet(instruction, &val) && val == value;
8061}
8062
8063// Returns true iff instruction is a zero bit pattern.
8064inline bool IsZeroBitPattern(HInstruction* instruction) {
8065 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8066}
8067
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008068// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008069#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008070 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8071 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8072#undef INSTRUCTION_TYPE_CHECK
8073
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008074// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008075#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8076 std::is_base_of<BaseType, H##type>::value,
8077#define INSTRUCTION_TYPE_CHECK(type, super) \
8078 inline bool HInstruction::Is##type() const { \
8079 DCHECK_LT(GetKind(), kLastInstructionKind); \
8080 using BaseType = H##type; \
8081 static constexpr bool results[] = { \
8082 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8083 }; \
8084 return results[static_cast<size_t>(GetKind())]; \
8085 }
8086
8087 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8088#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008089#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008090
8091#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008092 inline const H##type* HInstruction::As##type() const { \
8093 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8094 } \
8095 inline H##type* HInstruction::As##type() { \
8096 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8097 }
8098
Vladimir Markoa90dd512018-05-04 15:04:45 +01008099 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8100#undef INSTRUCTION_TYPE_CAST
8101
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008102
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008103// Create space in `blocks` for adding `number_of_new_blocks` entries
8104// starting at location `at`. Blocks after `at` are moved accordingly.
8105inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8106 size_t number_of_new_blocks,
8107 size_t after) {
8108 DCHECK_LT(after, blocks->size());
8109 size_t old_size = blocks->size();
8110 size_t new_size = old_size + number_of_new_blocks;
8111 blocks->resize(new_size);
8112 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8113}
8114
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008115/*
8116 * Hunt "under the hood" of array lengths (leading to array references),
8117 * null checks (also leading to array references), and new arrays
8118 * (leading to the actual length). This makes it more likely related
8119 * instructions become actually comparable.
8120 */
8121inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8122 while (instruction->IsArrayLength() ||
8123 instruction->IsNullCheck() ||
8124 instruction->IsNewArray()) {
8125 instruction = instruction->IsNewArray()
8126 ? instruction->AsNewArray()->GetLength()
8127 : instruction->InputAt(0);
8128 }
8129 return instruction;
8130}
8131
Artem Serov21c7e6f2017-07-27 16:04:42 +01008132void RemoveEnvironmentUses(HInstruction* instruction);
8133bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8134void ResetEnvironmentInputRecords(HInstruction* instruction);
8135
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008136} // namespace art
8137
8138#endif // ART_COMPILER_OPTIMIZING_NODES_H_