blob: 342789348c1183cc1c5f95667886dd48eec4d072 [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
Vladimir Marko0a516052019-10-14 13:00:44 +000050namespace art {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051
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 Geoffray7cfc8f52019-08-07 10:41:09 +0100134 kAnalysisFailPhiEquivalentInOsr,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000135 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000136};
137
Andreas Gampe9186ced2016-12-12 14:28:21 -0800138template <typename T>
139static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
140 return static_cast<typename std::make_unsigned<T>::type>(x);
141}
142
Vladimir Markof9f64412015-09-02 14:05:49 +0100143class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100144 public:
145 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
146
147 void AddInstruction(HInstruction* instruction);
148 void RemoveInstruction(HInstruction* instruction);
149
David Brazdilc3d743f2015-04-22 13:40:50 +0100150 // Insert `instruction` before/after an existing instruction `cursor`.
151 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
152 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
153
Roland Levillain6b469232014-09-25 10:10:38 +0100154 // Return true if this list contains `instruction`.
155 bool Contains(HInstruction* instruction) const;
156
Roland Levillainccc07a92014-09-16 14:48:16 +0100157 // Return true if `instruction1` is found before `instruction2` in
158 // this instruction list and false otherwise. Abort if none
159 // of these instructions is found.
160 bool FoundBefore(const HInstruction* instruction1,
161 const HInstruction* instruction2) const;
162
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000163 bool IsEmpty() const { return first_instruction_ == nullptr; }
164 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
165
166 // Update the block of all instructions to be `block`.
167 void SetBlockOfInstructions(HBasicBlock* block) const;
168
169 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000170 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000171 void Add(const HInstructionList& instruction_list);
172
David Brazdil2d7352b2015-04-20 14:52:42 +0100173 // Return the number of instructions in the list. This is an expensive operation.
174 size_t CountSize() const;
175
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176 private:
177 HInstruction* first_instruction_;
178 HInstruction* last_instruction_;
179
180 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 friend class HGraph;
182 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100183 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000184 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100185 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100186
187 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
188};
189
David Brazdil4833f5a2015-12-16 10:37:39 +0000190class ReferenceTypeInfo : ValueObject {
191 public:
192 typedef Handle<mirror::Class> TypeHandle;
193
Vladimir Markoa1de9182016-02-25 11:37:38 +0000194 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
195
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700196 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100197 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
198 }
199
Vladimir Markoa1de9182016-02-25 11:37:38 +0000200 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return ReferenceTypeInfo(type_handle, is_exact);
202 }
203
204 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
205
Vladimir Markof39745e2016-01-26 12:16:55 +0000206 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 return handle.GetReference() != nullptr;
208 }
209
Vladimir Marko456307a2016-04-19 14:12:13 +0000210 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 return IsValidHandle(type_handle_);
212 }
213
214 bool IsExact() const { return is_exact_; }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return GetTypeHandle()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsStringClass();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsInterface();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 return GetTypeHandle()->IsPrimitiveArray();
244 }
245
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000247 DCHECK(IsValid());
248 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
249 }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 if (!IsExact()) return false;
254 if (!IsArrayClass()) return false;
255 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
256 }
257
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700258 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000259 DCHECK(IsValid());
260 if (!IsExact()) return false;
261 if (!IsArrayClass()) return false;
262 if (!rti.IsArrayClass()) return false;
263 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
264 rti.GetTypeHandle()->GetComponentType());
265 }
266
267 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 DCHECK(IsValid());
271 DCHECK(rti.IsValid());
272 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
273 }
274
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 DCHECK(IsValid());
277 DCHECK(rti.IsValid());
278 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
279 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
280 }
281
282 // Returns true if the type information provide the same amount of details.
283 // Note that it does not mean that the instructions have the same actual type
284 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700285 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000286 if (!IsValid() && !rti.IsValid()) {
287 // Invalid types are equal.
288 return true;
289 }
290 if (!IsValid() || !rti.IsValid()) {
291 // One is valid, the other not.
292 return false;
293 }
294 return IsExact() == rti.IsExact()
295 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
296 }
297
298 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000299 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
300 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
301 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000302
303 // The class of the object.
304 TypeHandle type_handle_;
305 // Whether or not the type is exact or a superclass of the actual type.
306 // Whether or not we have any information about this type.
307 bool is_exact_;
308};
309
310std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
311
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100313class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100315 HGraph(ArenaAllocator* allocator,
316 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 const DexFile& dex_file,
318 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100320 InvokeType invoke_type = kInvalidInvokeType,
Hans Boehm206348c2018-12-05 11:11:33 -0800321 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100322 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000323 bool osr = false,
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000324 bool is_shared_jit_code = false,
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000325 bool baseline = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100326 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100327 : allocator_(allocator),
328 arena_stack_(arena_stack),
329 blocks_(allocator->Adapter(kArenaAllocBlockList)),
330 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
331 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700332 entry_block_(nullptr),
333 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100334 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100335 number_of_vregs_(0),
336 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000337 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400338 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000339 has_try_catch_(false),
Artem Serov2808be82018-12-20 19:15:11 +0000340 has_monitor_operations_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700341 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800342 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000343 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800344 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000345 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000346 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100347 dex_file_(dex_file),
348 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100349 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100350 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800351 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000353 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100354 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
355 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
356 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
357 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000358 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100359 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000360 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700361 osr_(osr),
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000362 baseline_(baseline),
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000363 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)),
364 is_shared_jit_code_(is_shared_jit_code) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100365 blocks_.reserve(kDefaultNumberOfBlocks);
366 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000367
David Brazdilbadd8262016-02-02 16:28:56 +0000368 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700369 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000370
Vladimir Markoca6fff82017-10-03 14:49:14 +0100371 ArenaAllocator* GetAllocator() const { return allocator_; }
372 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100373 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
374
David Brazdil69ba7b72015-06-23 18:27:30 +0100375 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000376 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100377
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000378 HBasicBlock* GetEntryBlock() const { return entry_block_; }
379 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100380 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000381
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000382 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
383 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000384
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000385 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100386
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100387 void ComputeDominanceInformation();
388 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000389 void ClearLoopInformation();
390 void FindBackEdges(ArenaBitVector* visited);
391 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100392 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100393 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000394
David Brazdil4833f5a2015-12-16 10:37:39 +0000395 // Analyze all natural loops in this graph. Returns a code specifying that it
396 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000397 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000398 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100399
David Brazdilffee3d32015-07-06 11:48:53 +0100400 // Iterate over blocks to compute try block membership. Needs reverse post
401 // order and loop information.
402 void ComputeTryBlockInformation();
403
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000404 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000405 // Returns the instruction to replace the invoke expression or null if the
406 // invoke is for a void method. Note that the caller is responsible for replacing
407 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000408 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000409
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000410 // Update the loop and try membership of `block`, which was spawned from `reference`.
411 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
412 // should be the new back edge.
413 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
414 HBasicBlock* reference,
415 bool replace_if_back_edge);
416
Mingyao Yang3584bce2015-05-19 16:01:59 -0700417 // Need to add a couple of blocks to test if the loop body is entered and
418 // put deoptimization instructions, etc.
419 void TransformLoopHeaderForBCE(HBasicBlock* header);
420
Aart Bikf8f5a162017-02-06 15:35:29 -0800421 // Adds a new loop directly after the loop with the given header and exit.
422 // Returns the new preheader.
423 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
424 HBasicBlock* body,
425 HBasicBlock* exit);
426
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000427 // Removes `block` from the graph. Assumes `block` has been disconnected from
428 // other blocks and has no instructions or phis.
429 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000430
David Brazdilfc6a86a2015-06-26 10:33:45 +0000431 // Splits the edge between `block` and `successor` while preserving the
432 // indices in the predecessor/successor lists. If there are multiple edges
433 // between the blocks, the lowest indices are used.
434 // Returns the new block which is empty and has the same dex pc as `successor`.
435 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
436
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100437 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100438 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000439
440 // Transform a loop into a format with a single preheader.
441 //
442 // Each phi in the header should be split: original one in the header should only hold
443 // inputs reachable from the back edges and a single input from the preheader. The newly created
444 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
445 //
446 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
447 // that no longer have this property.
448 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
449
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100450 void SimplifyLoop(HBasicBlock* header);
451
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000452 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100453 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000454 return current_instruction_id_++;
455 }
456
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000457 int32_t GetCurrentInstructionId() const {
458 return current_instruction_id_;
459 }
460
461 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100462 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000463 current_instruction_id_ = id;
464 }
465
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100466 uint16_t GetMaximumNumberOfOutVRegs() const {
467 return maximum_number_of_out_vregs_;
468 }
469
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000470 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
471 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100472 }
473
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100474 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
475 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
476 }
477
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000478 void UpdateTemporariesVRegSlots(size_t slots) {
479 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100480 }
481
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000482 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100483 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000484 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100485 }
486
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100487 void SetNumberOfVRegs(uint16_t number_of_vregs) {
488 number_of_vregs_ = number_of_vregs;
489 }
490
491 uint16_t GetNumberOfVRegs() const {
492 return number_of_vregs_;
493 }
494
495 void SetNumberOfInVRegs(uint16_t value) {
496 number_of_in_vregs_ = value;
497 }
498
David Brazdildee58d62016-04-07 09:54:26 +0000499 uint16_t GetNumberOfInVRegs() const {
500 return number_of_in_vregs_;
501 }
502
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100503 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100504 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100505 return number_of_vregs_ - number_of_in_vregs_;
506 }
507
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100508 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100510 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100511
Eric Holk1868de92020-02-12 09:10:21 -0800512 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() const {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100513 DCHECK(GetReversePostOrder()[0] == entry_block_);
514 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
515 }
516
517 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
518 return ReverseRange(GetReversePostOrder());
519 }
520
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100521 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100522 return linear_order_;
523 }
524
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100525 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
526 return ReverseRange(GetLinearOrder());
527 }
528
Mark Mendell1152c922015-04-24 17:06:35 -0400529 bool HasBoundsChecks() const {
530 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800531 }
532
Mark Mendell1152c922015-04-24 17:06:35 -0400533 void SetHasBoundsChecks(bool value) {
534 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800535 }
536
Hans Boehm206348c2018-12-05 11:11:33 -0800537 // Is the code known to be robust against eliminating dead references
538 // and the effects of early finalization?
539 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
540
541 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
542
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000543 bool IsDebuggable() const { return debuggable_; }
544
David Brazdil8d5b8b22015-03-24 10:51:52 +0000545 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000546 // already, it is created and inserted into the graph. This method is only for
547 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100548 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000549
550 // TODO: This is problematic for the consistency of reference type propagation
551 // because it can be created anytime after the pass and thus it will be left
552 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600553 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000554
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600555 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
556 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000557 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600558 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
559 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000560 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600561 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
562 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000563 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600564 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
565 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000566 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000567
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100568 HCurrentMethod* GetCurrentMethod();
569
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100570 const DexFile& GetDexFile() const {
571 return dex_file_;
572 }
573
574 uint32_t GetMethodIdx() const {
575 return method_idx_;
576 }
577
Igor Murashkind01745e2017-04-05 16:40:31 -0700578 // Get the method name (without the signature), e.g. "<init>"
579 const char* GetMethodName() const;
580
581 // Get the pretty method name (class + name + optionally signature).
582 std::string PrettyMethod(bool with_signature = true) const;
583
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100584 InvokeType GetInvokeType() const {
585 return invoke_type_;
586 }
587
Mark Mendellc4701932015-04-10 13:18:51 -0400588 InstructionSet GetInstructionSet() const {
589 return instruction_set_;
590 }
591
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000592 bool IsCompilingOsr() const { return osr_; }
593
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000594 bool IsCompilingBaseline() const { return baseline_; }
595
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000596 bool IsCompilingForSharedJitCode() const {
597 return is_shared_jit_code_;
598 }
599
Mingyao Yang063fc772016-08-02 11:02:54 -0700600 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
601 return cha_single_implementation_list_;
602 }
603
604 void AddCHASingleImplementationDependency(ArtMethod* method) {
605 cha_single_implementation_list_.insert(method);
606 }
607
608 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800609 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700610 }
611
David Brazdil77a48ae2015-09-15 12:34:04 +0000612 bool HasTryCatch() const { return has_try_catch_; }
613 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100614
Artem Serov2808be82018-12-20 19:15:11 +0000615 bool HasMonitorOperations() const { return has_monitor_operations_; }
616 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
617
Aart Bikb13c65b2017-03-21 20:14:07 -0700618 bool HasSIMD() const { return has_simd_; }
619 void SetHasSIMD(bool value) { has_simd_ = value; }
620
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800621 bool HasLoops() const { return has_loops_; }
622 void SetHasLoops(bool value) { has_loops_ = value; }
623
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000624 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
625 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
626
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100627 ArtMethod* GetArtMethod() const { return art_method_; }
628 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
629
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100630 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500631 // The instruction has been inserted into the graph, either as a constant, or
632 // before cursor.
633 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
634
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000635 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
636
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800637 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
638 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
639 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
640
David Brazdil2d7352b2015-04-20 14:52:42 +0100641 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000642 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100643 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000645 template <class InstructionType, typename ValueType>
646 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600647 ArenaSafeMap<ValueType, InstructionType*>* cache,
648 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000649 // Try to find an existing constant of the given value.
650 InstructionType* constant = nullptr;
651 auto cached_constant = cache->find(value);
652 if (cached_constant != cache->end()) {
653 constant = cached_constant->second;
654 }
655
656 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100657 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000658 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100659 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000660 cache->Overwrite(value, constant);
661 InsertConstant(constant);
662 }
663 return constant;
664 }
665
David Brazdil8d5b8b22015-03-24 10:51:52 +0000666 void InsertConstant(HConstant* instruction);
667
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000668 // Cache a float constant into the graph. This method should only be
669 // called by the SsaBuilder when creating "equivalent" instructions.
670 void CacheFloatConstant(HFloatConstant* constant);
671
672 // See CacheFloatConstant comment.
673 void CacheDoubleConstant(HDoubleConstant* constant);
674
Vladimir Markoca6fff82017-10-03 14:49:14 +0100675 ArenaAllocator* const allocator_;
676 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677
678 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000680
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100682 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000683
Aart Bik281c6812016-08-26 11:31:48 -0700684 // List of blocks to perform a linear order tree traversal. Unlike the reverse
685 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100686 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100687
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000688 HBasicBlock* entry_block_;
689 HBasicBlock* exit_block_;
690
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100691 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100692 uint16_t maximum_number_of_out_vregs_;
693
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100694 // The number of virtual registers in this method. Contains the parameters.
695 uint16_t number_of_vregs_;
696
697 // The number of virtual registers used by parameters of this method.
698 uint16_t number_of_in_vregs_;
699
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000700 // Number of vreg size slots that the temporaries use (used in baseline compiler).
701 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100702
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800703 // Flag whether there are bounds checks in the graph. We can skip
704 // BCE if it's false. It's only best effort to keep it up to date in
705 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400706 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800707
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800708 // Flag whether there are try/catch blocks in the graph. We will skip
709 // try/catch-related passes if it's false. It's only best effort to keep
710 // it up to date in the presence of code elimination so there might be
711 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000712 bool has_try_catch_;
713
Artem Serov2808be82018-12-20 19:15:11 +0000714 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
715 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
716 bool has_monitor_operations_;
717
Aart Bikb13c65b2017-03-21 20:14:07 -0700718 // Flag whether SIMD instructions appear in the graph. If true, the
719 // code generators may have to be more careful spilling the wider
720 // contents of SIMD registers.
721 bool has_simd_;
722
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800723 // Flag whether there are any loops in the graph. We can skip loop
724 // optimization if it's false. It's only best effort to keep it up
725 // to date in the presence of code elimination so there might be false
726 // positives.
727 bool has_loops_;
728
729 // Flag whether there are any irreducible loops in the graph. It's only
730 // best effort to keep it up to date in the presence of code elimination
731 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000732 bool has_irreducible_loops_;
733
Hans Boehm206348c2018-12-05 11:11:33 -0800734 // Is the code known to be robust against eliminating dead references
735 // and the effects of early finalization? If false, dead reference variables
736 // are kept if they might be visible to the garbage collector.
737 // Currently this means that the class was declared to be dead-reference-safe,
738 // the method accesses no reachability-sensitive fields or data, and the same
739 // is true for any methods that were inlined into the current one.
740 bool dead_reference_safe_;
741
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000742 // Indicates whether the graph should be compiled in a way that
743 // ensures full debuggability. If false, we can apply more
744 // aggressive optimizations that may limit the level of debugging.
745 const bool debuggable_;
746
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000747 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000748 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000749
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100750 // The dex file from which the method is from.
751 const DexFile& dex_file_;
752
753 // The method index in the dex file.
754 const uint32_t method_idx_;
755
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100756 // If inlined, this encodes how the callee is being invoked.
757 const InvokeType invoke_type_;
758
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100759 // Whether the graph has been transformed to SSA form. Only used
760 // in debug mode to ensure we are not using properties only valid
761 // for non-SSA form (like the number of temporaries).
762 bool in_ssa_form_;
763
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800764 // Number of CHA guards in the graph. Used to short-circuit the
765 // CHA guard optimization pass when there is no CHA guard left.
766 uint32_t number_of_cha_guards_;
767
Mathieu Chartiere401d142015-04-22 13:56:20 -0700768 const InstructionSet instruction_set_;
769
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000770 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000771 HNullConstant* cached_null_constant_;
772 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000773 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000774 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000775 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000776
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100777 HCurrentMethod* cached_current_method_;
778
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100779 // The ArtMethod this graph is for. Note that for AOT, it may be null,
780 // for example for methods whose declaring class could not be resolved
781 // (such as when the superclass could not be found).
782 ArtMethod* art_method_;
783
David Brazdil4833f5a2015-12-16 10:37:39 +0000784 // Keep the RTI of inexact Object to avoid having to pass stack handle
785 // collection pointer to passes which may create NullConstant.
786 ReferenceTypeInfo inexact_object_rti_;
787
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000788 // Whether we are compiling this graph for on stack replacement: this will
789 // make all loops seen as irreducible and emit special stack maps to mark
790 // compiled code entries which the interpreter can directly jump to.
791 const bool osr_;
792
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000793 // Whether we are compiling baseline (not running optimizations). This affects
794 // the code being generated.
795 const bool baseline_;
796
Mingyao Yang063fc772016-08-02 11:02:54 -0700797 // List of methods that are assumed to have single implementation.
798 ArenaSet<ArtMethod*> cha_single_implementation_list_;
799
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000800 // Whether we are JIT compiling in the shared region area, putting
801 // restrictions on, for example, how literals are being generated.
802 bool is_shared_jit_code_;
803
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000804 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100805 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000806 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000807 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000808 DISALLOW_COPY_AND_ASSIGN(HGraph);
809};
810
Vladimir Markof9f64412015-09-02 14:05:49 +0100811class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000812 public:
813 HLoopInformation(HBasicBlock* header, HGraph* graph)
814 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100815 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000816 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100817 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100818 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100819 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100820 blocks_(graph->GetAllocator(),
821 graph->GetBlocks().size(),
822 true,
823 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100824 back_edges_.reserve(kDefaultNumberOfBackEdges);
825 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100826
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000827 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100828 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000829
830 void Dump(std::ostream& os);
831
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100832 HBasicBlock* GetHeader() const {
833 return header_;
834 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000835
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000836 void SetHeader(HBasicBlock* block) {
837 header_ = block;
838 }
839
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100840 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
841 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
842 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
843
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000844 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100845 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000846 }
847
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100848 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100849 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100850 }
851
David Brazdil46e2a392015-03-16 17:31:52 +0000852 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100853 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100854 }
855
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000856 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100857 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000858 }
859
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100860 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100861
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100862 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100863 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100864 }
865
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100866 // Returns the lifetime position of the back edge that has the
867 // greatest lifetime position.
868 size_t GetLifetimeEnd() const;
869
870 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100871 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100872 }
873
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000874 // Finds blocks that are part of this loop.
875 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100876
Artem Serov7f4aff62017-06-21 17:02:18 +0100877 // Updates blocks population of the loop and all of its outer' ones recursively after the
878 // population of the inner loop is updated.
879 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
880
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100881 // Returns whether this loop information contains `block`.
882 // Note that this loop information *must* be populated before entering this function.
883 bool Contains(const HBasicBlock& block) const;
884
885 // Returns whether this loop information is an inner loop of `other`.
886 // Note that `other` *must* be populated before entering this function.
887 bool IsIn(const HLoopInformation& other) const;
888
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800889 // Returns true if instruction is not defined within this loop.
890 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700891
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 const ArenaBitVector& GetBlocks() const { return blocks_; }
893
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000894 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000895 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000896
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000897 void ClearAllBlocks() {
898 blocks_.ClearAllBits();
899 }
900
David Brazdil3f4a5222016-05-06 12:46:21 +0100901 bool HasBackEdgeNotDominatedByHeader() const;
902
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100903 bool IsPopulated() const {
904 return blocks_.GetHighestBitSet() != -1;
905 }
906
Anton Shaminf89381f2016-05-16 16:44:13 +0600907 bool DominatesAllBackEdges(HBasicBlock* block);
908
David Sehrc757dec2016-11-04 15:48:34 -0700909 bool HasExitEdge() const;
910
Artem Serov7f4aff62017-06-21 17:02:18 +0100911 // Resets back edge and blocks-in-loop data.
912 void ResetBasicBlockData() {
913 back_edges_.clear();
914 ClearAllBlocks();
915 }
916
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000917 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100918 // Internal recursive implementation of `Populate`.
919 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100920 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100921
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000922 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100923 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000924 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100925 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100926 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100927 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000928
929 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
930};
931
David Brazdilec16f792015-08-19 15:04:01 +0100932// Stores try/catch information for basic blocks.
933// Note that HGraph is constructed so that catch blocks cannot simultaneously
934// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100935class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100936 public:
937 // Try block information constructor.
938 explicit TryCatchInformation(const HTryBoundary& try_entry)
939 : try_entry_(&try_entry),
940 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000941 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100942 DCHECK(try_entry_ != nullptr);
943 }
944
945 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800946 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100947 : try_entry_(nullptr),
948 catch_dex_file_(&dex_file),
949 catch_type_index_(catch_type_index) {}
950
951 bool IsTryBlock() const { return try_entry_ != nullptr; }
952
953 const HTryBoundary& GetTryEntry() const {
954 DCHECK(IsTryBlock());
955 return *try_entry_;
956 }
957
958 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
959
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000960 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100961 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000962 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100963 }
964
Andreas Gampea5b09a62016-11-17 15:21:22 -0800965 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100966 DCHECK(IsCatchBlock());
967 return catch_type_index_;
968 }
969
970 const DexFile& GetCatchDexFile() const {
971 DCHECK(IsCatchBlock());
972 return *catch_dex_file_;
973 }
974
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000975 void SetInvalidTypeIndex() {
976 catch_type_index_ = dex::TypeIndex::Invalid();
977 }
978
David Brazdilec16f792015-08-19 15:04:01 +0100979 private:
980 // One of possibly several TryBoundary instructions entering the block's try.
981 // Only set for try blocks.
982 const HTryBoundary* try_entry_;
983
984 // Exception type information. Only set for catch blocks.
985 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000986 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100987};
988
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100989static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100990static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100991
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992// A block in a method. Contains the list of instructions represented
993// as a double linked list. Each block knows its predecessors and
994// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100995
Vladimir Markof9f64412015-09-02 14:05:49 +0100996class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700998 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000999 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001000 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
1001 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001002 loop_information_(nullptr),
1003 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001004 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001005 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001006 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001007 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001008 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001009 try_catch_information_(nullptr) {
1010 predecessors_.reserve(kDefaultNumberOfPredecessors);
1011 successors_.reserve(kDefaultNumberOfSuccessors);
1012 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1013 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001014
Vladimir Marko60584552015-09-03 13:35:12 +00001015 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001016 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 }
1018
Vladimir Marko60584552015-09-03 13:35:12 +00001019 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001020 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001021 }
1022
David Brazdild26a4112015-11-10 11:07:31 +00001023 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1024 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1025
Vladimir Marko60584552015-09-03 13:35:12 +00001026 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1027 return ContainsElement(successors_, block, start_from);
1028 }
1029
1030 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001031 return dominated_blocks_;
1032 }
1033
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001034 bool IsEntryBlock() const {
1035 return graph_->GetEntryBlock() == this;
1036 }
1037
1038 bool IsExitBlock() const {
1039 return graph_->GetExitBlock() == this;
1040 }
1041
David Brazdil46e2a392015-03-16 17:31:52 +00001042 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001043 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001044 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001045 bool IsSingleTryBoundary() const;
1046
1047 // Returns true if this block emits nothing but a jump.
1048 bool IsSingleJump() const {
1049 HLoopInformation* loop_info = GetLoopInformation();
1050 return (IsSingleGoto() || IsSingleTryBoundary())
1051 // Back edges generate a suspend check.
1052 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1053 }
David Brazdil46e2a392015-03-16 17:31:52 +00001054
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001055 void AddBackEdge(HBasicBlock* back_edge) {
1056 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001057 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001058 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001059 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001060 loop_information_->AddBackEdge(back_edge);
1061 }
1062
Artem Serov7f4aff62017-06-21 17:02:18 +01001063 // Registers a back edge; if the block was not a loop header before the call associates a newly
1064 // created loop info with it.
1065 //
1066 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1067 // info for all blocks during back edges recalculation.
1068 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1069 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1070 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1071 }
1072 loop_information_->AddBackEdge(back_edge);
1073 }
1074
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001075 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001076 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001077
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001078 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001079 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001080 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001081
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001082 HBasicBlock* GetDominator() const { return dominator_; }
1083 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001084 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1085
1086 void RemoveDominatedBlock(HBasicBlock* block) {
1087 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001088 }
Vladimir Marko60584552015-09-03 13:35:12 +00001089
1090 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1091 ReplaceElement(dominated_blocks_, existing, new_block);
1092 }
1093
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001094 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001095
1096 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001097 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001098 }
1099
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001100 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1101 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001102 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001103 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001104 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1105 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001106
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001107 HInstruction* GetFirstInstructionDisregardMoves() const;
1108
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001109 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001110 successors_.push_back(block);
1111 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001112 }
1113
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001114 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1115 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001116 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001117 new_block->predecessors_.push_back(this);
1118 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001119 }
1120
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001121 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1122 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001123 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001124 new_block->successors_.push_back(this);
1125 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001126 }
1127
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001128 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001129 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001130 // `predecessor` and `successor`.
1131 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1132 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001133 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001134 successor->predecessors_[predecessor_index] = this;
1135 predecessor->successors_[successor_index] = this;
1136 successors_.push_back(successor);
1137 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001138 }
1139
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001140 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001141 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001142 }
1143
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001144 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001145 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001146 }
1147
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001148 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001149 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001150 }
1151
1152 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001153 predecessors_.push_back(block);
1154 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001155 }
1156
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001157 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001158 DCHECK_EQ(predecessors_.size(), 2u);
1159 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001160 }
1161
David Brazdil769c9e52015-04-27 13:54:09 +01001162 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001163 DCHECK_EQ(successors_.size(), 2u);
1164 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001165 }
1166
David Brazdilfc6a86a2015-06-26 10:33:45 +00001167 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001168 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001169 }
1170
David Brazdilfc6a86a2015-06-26 10:33:45 +00001171 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001172 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001173 }
1174
David Brazdilfc6a86a2015-06-26 10:33:45 +00001175 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001176 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001177 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001178 }
1179
1180 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001181 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001182 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001183 }
1184
1185 // Returns whether the first occurrence of `predecessor` in the list of
1186 // predecessors is at index `idx`.
1187 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001188 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001189 return GetPredecessorIndexOf(predecessor) == idx;
1190 }
1191
David Brazdild7558da2015-09-22 13:04:14 +01001192 // Create a new block between this block and its predecessors. The new block
1193 // is added to the graph, all predecessor edges are relinked to it and an edge
1194 // is created to `this`. Returns the new empty block. Reverse post order or
1195 // loop and try/catch information are not updated.
1196 HBasicBlock* CreateImmediateDominator();
1197
David Brazdilfc6a86a2015-06-26 10:33:45 +00001198 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001199 // created, latter block. Note that this method will add the block to the
1200 // graph, create a Goto at the end of the former block and will create an edge
1201 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001202 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001203 HBasicBlock* SplitBefore(HInstruction* cursor);
1204
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001205 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001206 // created block. Note that this method just updates raw block information,
1207 // like predecessors, successors, dominators, and instruction list. It does not
1208 // update the graph, reverse post order, loop information, nor make sure the
1209 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001210 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1211
1212 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1213 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001214
1215 // Merge `other` at the end of `this`. Successors and dominated blocks of
1216 // `other` are changed to be successors and dominated blocks of `this`. Note
1217 // that this method does not update the graph, reverse post order, loop
1218 // information, nor make sure the blocks are consistent (for example ending
1219 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001220 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001221
1222 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1223 // of `this` are moved to `other`.
1224 // Note that this method does not update the graph, reverse post order, loop
1225 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001226 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001227 void ReplaceWith(HBasicBlock* other);
1228
Aart Bik6b69e0a2017-01-11 10:20:43 -08001229 // Merges the instructions of `other` at the end of `this`.
1230 void MergeInstructionsWith(HBasicBlock* other);
1231
David Brazdil2d7352b2015-04-20 14:52:42 +01001232 // Merge `other` at the end of `this`. This method updates loops, reverse post
1233 // order, links to predecessors, successors, dominators and deletes the block
1234 // from the graph. The two blocks must be successive, i.e. `this` the only
1235 // predecessor of `other` and vice versa.
1236 void MergeWith(HBasicBlock* other);
1237
1238 // Disconnects `this` from all its predecessors, successors and dominator,
1239 // removes it from all loops it is included in and eventually from the graph.
1240 // The block must not dominate any other block. Predecessors and successors
1241 // are safely updated.
1242 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001243
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001244 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001245 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001246 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001247 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001248 // Replace phi `initial` with `replacement` within this block.
1249 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001250 // Replace instruction `initial` with `replacement` within this block.
1251 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1252 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001253 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001254 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001255 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1256 // instruction list. With 'ensure_safety' set to true, it verifies that the
1257 // instruction is not in use and removes it from the use lists of its inputs.
1258 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1259 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001260 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001261
1262 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001263 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001264 }
1265
Roland Levillain6b879dd2014-09-22 17:13:44 +01001266 bool IsLoopPreHeaderFirstPredecessor() const {
1267 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001268 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001269 }
1270
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001271 bool IsFirstPredecessorBackEdge() const {
1272 DCHECK(IsLoopHeader());
1273 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1274 }
1275
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001276 HLoopInformation* GetLoopInformation() const {
1277 return loop_information_;
1278 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001279
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001280 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001281 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001282 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001283 void SetInLoop(HLoopInformation* info) {
1284 if (IsLoopHeader()) {
1285 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001286 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001287 loop_information_ = info;
1288 } else if (loop_information_->Contains(*info->GetHeader())) {
1289 // Block is currently part of an outer loop. Make it part of this inner loop.
1290 // Note that a non loop header having a loop information means this loop information
1291 // has already been populated
1292 loop_information_ = info;
1293 } else {
1294 // Block is part of an inner loop. Do not update the loop information.
1295 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1296 // at this point, because this method is being called while populating `info`.
1297 }
1298 }
1299
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001300 // Raw update of the loop information.
1301 void SetLoopInformation(HLoopInformation* info) {
1302 loop_information_ = info;
1303 }
1304
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001305 bool IsInLoop() const { return loop_information_ != nullptr; }
1306
David Brazdilec16f792015-08-19 15:04:01 +01001307 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1308
1309 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1310 try_catch_information_ = try_catch_information;
1311 }
1312
1313 bool IsTryBlock() const {
1314 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1315 }
1316
1317 bool IsCatchBlock() const {
1318 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1319 }
David Brazdilffee3d32015-07-06 11:48:53 +01001320
1321 // Returns the try entry that this block's successors should have. They will
1322 // be in the same try, unless the block ends in a try boundary. In that case,
1323 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001324 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001325
Aart Bik75ff2c92018-04-21 01:28:11 +00001326 bool HasThrowingInstructions() const;
1327
David Brazdila4b8c212015-05-07 09:59:30 +01001328 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001329 bool Dominates(HBasicBlock* block) const;
1330
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001331 size_t GetLifetimeStart() const { return lifetime_start_; }
1332 size_t GetLifetimeEnd() const { return lifetime_end_; }
1333
1334 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1335 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1336
David Brazdil8d5b8b22015-03-24 10:51:52 +00001337 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001338 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001339 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001340 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001341 bool HasSinglePhi() const;
1342
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001343 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001344 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001345 ArenaVector<HBasicBlock*> predecessors_;
1346 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347 HInstructionList instructions_;
1348 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001349 HLoopInformation* loop_information_;
1350 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001351 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001352 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001353 // The dex program counter of the first instruction of this block.
1354 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001355 size_t lifetime_start_;
1356 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001357 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001358
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001359 friend class HGraph;
1360 friend class HInstruction;
1361
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001362 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1363};
1364
David Brazdilb2bd1c52015-03-25 11:17:37 +00001365// Iterates over the LoopInformation of all loops which contain 'block'
1366// from the innermost to the outermost.
1367class HLoopInformationOutwardIterator : public ValueObject {
1368 public:
1369 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1370 : current_(block.GetLoopInformation()) {}
1371
1372 bool Done() const { return current_ == nullptr; }
1373
1374 void Advance() {
1375 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001376 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001377 }
1378
1379 HLoopInformation* Current() const {
1380 DCHECK(!Done());
1381 return current_;
1382 }
1383
1384 private:
1385 HLoopInformation* current_;
1386
1387 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1388};
1389
Alexandre Ramesef20f712015-06-09 10:29:30 +01001390#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001391 M(Above, Condition) \
1392 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001393 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001394 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001395 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(ArrayGet, Instruction) \
1397 M(ArrayLength, Instruction) \
1398 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001399 M(Below, Condition) \
1400 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001401 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001402 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001403 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001404 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001405 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001406 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001407 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001408 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001409 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001410 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001411 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001412 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001413 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001414 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001415 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001416 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001417 M(Exit, Instruction) \
1418 M(FloatConstant, Constant) \
1419 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001420 M(GreaterThan, Condition) \
1421 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001422 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001423 M(InstanceFieldGet, Instruction) \
1424 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001425 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001426 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001427 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001428 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001429 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001430 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001431 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001432 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001433 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001434 M(LessThan, Condition) \
1435 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001436 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001437 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001438 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001439 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001440 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001441 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001442 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001443 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001444 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001445 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001446 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001447 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001448 M(Neg, UnaryOperation) \
1449 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001450 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001451 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001452 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001453 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001454 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001455 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001456 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001457 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001458 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001459 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001460 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001461 M(Return, Instruction) \
1462 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001463 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001464 M(Shl, BinaryOperation) \
1465 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001466 M(StaticFieldGet, Instruction) \
1467 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001468 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001469 M(UnresolvedInstanceFieldGet, Instruction) \
1470 M(UnresolvedInstanceFieldSet, Instruction) \
1471 M(UnresolvedStaticFieldGet, Instruction) \
1472 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001473 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001474 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001475 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001476 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001477 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001478 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001479 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001480 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001481 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001482 M(VecExtractScalar, VecUnaryOperation) \
1483 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001484 M(VecCnv, VecUnaryOperation) \
1485 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001486 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001487 M(VecNot, VecUnaryOperation) \
1488 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001489 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001490 M(VecSub, VecBinaryOperation) \
1491 M(VecMul, VecBinaryOperation) \
1492 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001493 M(VecMin, VecBinaryOperation) \
1494 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001495 M(VecAnd, VecBinaryOperation) \
1496 M(VecAndNot, VecBinaryOperation) \
1497 M(VecOr, VecBinaryOperation) \
1498 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001499 M(VecSaturationAdd, VecBinaryOperation) \
1500 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001501 M(VecShl, VecBinaryOperation) \
1502 M(VecShr, VecBinaryOperation) \
1503 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001504 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001505 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001506 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001507 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001508 M(VecLoad, VecMemoryOperation) \
1509 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001510
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001511/*
1512 * Instructions, shared across several (not all) architectures.
1513 */
1514#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1515#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1516#else
1517#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001518 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001519 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001520 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001521 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001522#endif
1523
Alexandre Ramesef20f712015-06-09 10:29:30 +01001524#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1525
1526#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1527
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001528#ifndef ART_ENABLE_CODEGEN_x86
1529#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1530#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001531#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1532 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001533 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001534 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001535 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001536#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001537
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301538#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1539#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301540 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301541 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301542#else
1543#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1544#endif
1545
Alexandre Ramesef20f712015-06-09 10:29:30 +01001546#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1547
1548#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1549 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001550 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001551 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1552 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1553 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301554 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1555 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001556
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001557#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1558 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001559 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001560 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001561 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001562 M(Invoke, Instruction) \
1563 M(VecOperation, Instruction) \
1564 M(VecUnaryOperation, VecOperation) \
1565 M(VecBinaryOperation, VecOperation) \
1566 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001567
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001568#define FOR_EACH_INSTRUCTION(M) \
1569 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1570 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1571
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001572#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001573FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1574#undef FORWARD_DECLARATION
1575
Vladimir Marko372f10e2016-05-17 16:30:10 +01001576#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001577 private: \
1578 H##type& operator=(const H##type&) = delete; \
1579 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001580 const char* DebugName() const override { return #type; } \
1581 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001582 DCHECK(IsClonable()); \
1583 return new (arena) H##type(*this->As##type()); \
1584 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001585 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001586
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001587#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001588 private: \
1589 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001590 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001591
Artem Serovcced8ba2017-07-19 18:18:09 +01001592#define DEFAULT_COPY_CONSTRUCTOR(type) \
1593 explicit H##type(const H##type& other) = default;
1594
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001595template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001596class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1597 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001598 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001599 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001600 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001601 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001602 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001603 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001604 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001605
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001606 private:
David Brazdiled596192015-01-23 10:39:45 +00001607 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001608 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001609
1610 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001611 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001612
Vladimir Marko46817b82016-03-29 12:21:58 +01001613 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001614
1615 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1616};
1617
David Brazdiled596192015-01-23 10:39:45 +00001618template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001619using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001620
David Brazdil1abb4192015-02-17 18:33:36 +00001621// This class is used by HEnvironment and HInstruction classes to record the
1622// instructions they use and pointers to the corresponding HUseListNodes kept
1623// by the used instructions.
1624template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001625class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001626 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001627 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1628 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001629
Vladimir Marko46817b82016-03-29 12:21:58 +01001630 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1631 : HUserRecord(old_record.instruction_, before_use_node) {}
1632 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1633 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001634 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001635 }
1636
1637 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001638 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1639 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001640
1641 private:
1642 // Instruction used by the user.
1643 HInstruction* instruction_;
1644
Vladimir Marko46817b82016-03-29 12:21:58 +01001645 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1646 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001647};
1648
Vladimir Markoe9004912016-06-16 16:50:52 +01001649// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1650// This is used for HInstruction::GetInputs() to return a container wrapper providing
1651// HInstruction* values even though the underlying container has HUserRecord<>s.
1652struct HInputExtractor {
1653 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1654 return record.GetInstruction();
1655 }
1656 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1657 return record.GetInstruction();
1658 }
1659};
1660
1661using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1662using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1663
Aart Bik854a02b2015-07-14 16:07:00 -07001664/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001665 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001666 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001667 * For write/read dependences on fields/arrays, the dependence analysis uses
1668 * type disambiguation (e.g. a float field write cannot modify the value of an
1669 * integer field read) and the access type (e.g. a reference array write cannot
1670 * modify the value of a reference field read [although it may modify the
1671 * reference fetch prior to reading the field, which is represented by its own
1672 * write/read dependence]). The analysis makes conservative points-to
1673 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1674 * the same, and any reference read depends on any reference read without
1675 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001676 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001677 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1678 * alive across the point where garbage collection might happen.
1679 *
1680 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1681 *
1682 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1683 * those instructions from the compiler perspective (between this instruction and the next one
1684 * in the IR).
1685 *
1686 * Note: Instructions which can cause GC only on a fatal slow path do not need
1687 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1688 * one. However the execution may return to compiled code if there is a catch block in the
1689 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1690 * set.
1691 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001692 * The internal representation uses 38-bit and is described in the table below.
1693 * The first line indicates the side effect, and for field/array accesses the
1694 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001695 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001696 * The two numbered lines below indicate the bit position in the bitfield (read
1697 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001698 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001699 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1700 * +-------------+---------+---------+--------------+---------+---------+
1701 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1702 * | 3 |333333322|222222221| 1 |111111110|000000000|
1703 * | 7 |654321098|765432109| 8 |765432109|876543210|
1704 *
1705 * Note that, to ease the implementation, 'changes' bits are least significant
1706 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001707 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001708class SideEffects : public ValueObject {
1709 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001710 SideEffects() : flags_(0) {}
1711
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001712 static SideEffects None() {
1713 return SideEffects(0);
1714 }
1715
1716 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001717 return SideEffects(kAllChangeBits | kAllDependOnBits);
1718 }
1719
1720 static SideEffects AllChanges() {
1721 return SideEffects(kAllChangeBits);
1722 }
1723
1724 static SideEffects AllDependencies() {
1725 return SideEffects(kAllDependOnBits);
1726 }
1727
1728 static SideEffects AllExceptGCDependency() {
1729 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1730 }
1731
1732 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001733 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001734 }
1735
Aart Bik34c3ba92015-07-20 14:08:59 -07001736 static SideEffects AllWrites() {
1737 return SideEffects(kAllWrites);
1738 }
1739
1740 static SideEffects AllReads() {
1741 return SideEffects(kAllReads);
1742 }
1743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001744 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001745 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001746 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001747 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001748 }
1749
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001750 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001751 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001752 }
1753
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001755 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001757 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001758 }
1759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001761 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001762 }
1763
Artem Serovd1aa7d02018-06-22 11:35:46 +01001764 // Returns whether GC might happen across this instruction from the compiler perspective so
1765 // the next instruction in the IR would see that.
1766 //
1767 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001768 static SideEffects CanTriggerGC() {
1769 return SideEffects(1ULL << kCanTriggerGCBit);
1770 }
1771
Artem Serovd1aa7d02018-06-22 11:35:46 +01001772 // Returns whether the instruction must not be alive across a GC point.
1773 //
1774 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001775 static SideEffects DependsOnGC() {
1776 return SideEffects(1ULL << kDependsOnGCBit);
1777 }
1778
Aart Bik854a02b2015-07-14 16:07:00 -07001779 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001780 SideEffects Union(SideEffects other) const {
1781 return SideEffects(flags_ | other.flags_);
1782 }
1783
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001784 SideEffects Exclusion(SideEffects other) const {
1785 return SideEffects(flags_ & ~other.flags_);
1786 }
1787
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001788 void Add(SideEffects other) {
1789 flags_ |= other.flags_;
1790 }
1791
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001792 bool Includes(SideEffects other) const {
1793 return (other.flags_ & flags_) == other.flags_;
1794 }
1795
1796 bool HasSideEffects() const {
1797 return (flags_ & kAllChangeBits);
1798 }
1799
1800 bool HasDependencies() const {
1801 return (flags_ & kAllDependOnBits);
1802 }
1803
1804 // Returns true if there are no side effects or dependencies.
1805 bool DoesNothing() const {
1806 return flags_ == 0;
1807 }
1808
Aart Bik854a02b2015-07-14 16:07:00 -07001809 // Returns true if something is written.
1810 bool DoesAnyWrite() const {
1811 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001812 }
1813
Aart Bik854a02b2015-07-14 16:07:00 -07001814 // Returns true if something is read.
1815 bool DoesAnyRead() const {
1816 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001817 }
1818
Aart Bik854a02b2015-07-14 16:07:00 -07001819 // Returns true if potentially everything is written and read
1820 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001821 bool DoesAllReadWrite() const {
1822 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1823 }
1824
Aart Bik854a02b2015-07-14 16:07:00 -07001825 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001826 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001827 }
1828
Roland Levillain0d5a2812015-11-13 10:07:31 +00001829 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001830 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001831 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1832 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001833 }
1834
1835 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001836 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001837 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001838 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001839 for (int s = kLastBit; s >= 0; s--) {
1840 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1841 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1842 // This is a bit for the GC side effect.
1843 if (current_bit_is_set) {
1844 flags += "GC";
1845 }
Aart Bik854a02b2015-07-14 16:07:00 -07001846 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001847 } else {
1848 // This is a bit for the array/field analysis.
1849 // The underscore character stands for the 'can trigger GC' bit.
1850 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1851 if (current_bit_is_set) {
1852 flags += kDebug[s];
1853 }
1854 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1855 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1856 flags += "|";
1857 }
1858 }
Aart Bik854a02b2015-07-14 16:07:00 -07001859 }
1860 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001861 }
1862
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001863 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001864
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001865 private:
1866 static constexpr int kFieldArrayAnalysisBits = 9;
1867
1868 static constexpr int kFieldWriteOffset = 0;
1869 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1870 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1871 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1872
1873 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1874
1875 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1876 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1877 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1878 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1879
1880 static constexpr int kLastBit = kDependsOnGCBit;
1881 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1882
1883 // Aliases.
1884
1885 static_assert(kChangeBits == kDependOnBits,
1886 "the 'change' bits should match the 'depend on' bits.");
1887
1888 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1889 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1890 static constexpr uint64_t kAllWrites =
1891 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1892 static constexpr uint64_t kAllReads =
1893 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001894
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001895 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001896 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001897 int shift;
1898 switch (type) {
1899 case DataType::Type::kReference: shift = 0; break;
1900 case DataType::Type::kBool: shift = 1; break;
1901 case DataType::Type::kInt8: shift = 2; break;
1902 case DataType::Type::kUint16: shift = 3; break;
1903 case DataType::Type::kInt16: shift = 4; break;
1904 case DataType::Type::kInt32: shift = 5; break;
1905 case DataType::Type::kInt64: shift = 6; break;
1906 case DataType::Type::kFloat32: shift = 7; break;
1907 case DataType::Type::kFloat64: shift = 8; break;
1908 default:
1909 LOG(FATAL) << "Unexpected data type " << type;
1910 UNREACHABLE();
1911 }
Aart Bik854a02b2015-07-14 16:07:00 -07001912 DCHECK_LE(kFieldWriteOffset, shift);
1913 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001914 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001915 }
1916
1917 // Private constructor on direct flags value.
1918 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1919
1920 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001921};
1922
David Brazdiled596192015-01-23 10:39:45 +00001923// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001924class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001925 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001926 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001927 size_t number_of_vregs,
1928 ArtMethod* method,
1929 uint32_t dex_pc,
1930 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001931 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1932 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001933 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001934 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001935 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001936 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001937 }
1938
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001939 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1940 const HEnvironment& to_copy,
1941 HInstruction* holder)
1942 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001943 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001944 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001945 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001946 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001947
Vladimir Markoec32f642017-06-02 10:51:55 +01001948 void AllocateLocations() {
1949 DCHECK(locations_.empty());
1950 locations_.resize(vregs_.size());
1951 }
1952
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001953 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001954 if (parent_ != nullptr) {
1955 parent_->SetAndCopyParentChain(allocator, parent);
1956 } else {
1957 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1958 parent_->CopyFrom(parent);
1959 if (parent->GetParent() != nullptr) {
1960 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1961 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001962 }
David Brazdiled596192015-01-23 10:39:45 +00001963 }
1964
Vladimir Marko69d310e2017-10-09 14:12:23 +01001965 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001966 void CopyFrom(HEnvironment* environment);
1967
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001968 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1969 // input to the loop phi instead. This is for inserting instructions that
1970 // require an environment (like HDeoptimization) in the loop pre-header.
1971 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001972
1973 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001974 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001975 }
1976
1977 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001978 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001979 }
1980
David Brazdil1abb4192015-02-17 18:33:36 +00001981 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001982
Artem Serovca210e32017-12-15 13:43:20 +00001983 // Replaces the input at the position 'index' with the replacement; the replacement and old
1984 // input instructions' env_uses_ lists are adjusted. The function works similar to
1985 // HInstruction::ReplaceInput.
1986 void ReplaceInput(HInstruction* replacement, size_t index);
1987
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001988 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001989
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001990 HEnvironment* GetParent() const { return parent_; }
1991
1992 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001993 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001994 }
1995
1996 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001997 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001998 }
1999
2000 uint32_t GetDexPc() const {
2001 return dex_pc_;
2002 }
2003
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002004 ArtMethod* GetMethod() const {
2005 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002006 }
2007
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002008 HInstruction* GetHolder() const {
2009 return holder_;
2010 }
2011
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002012
2013 bool IsFromInlinedInvoke() const {
2014 return GetParent() != nullptr;
2015 }
2016
David Brazdiled596192015-01-23 10:39:45 +00002017 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002018 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2019 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002020 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002021 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002022 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002023
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002024 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002025 HInstruction* const holder_;
2026
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002027 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002028
2029 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2030};
2031
Vladimir Markof9f64412015-09-02 14:05:49 +01002032class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002033 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302034#define DECLARE_KIND(type, super) k##type,
2035 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002036 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302037 kLastInstructionKind
2038 };
2039#undef DECLARE_KIND
2040
2041 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002042 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2043
2044 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002045 : previous_(nullptr),
2046 next_(nullptr),
2047 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002048 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002049 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002050 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002051 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002052 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002053 locations_(nullptr),
2054 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002055 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002056 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002057 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302058 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002059 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002060 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2061 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002062
Dave Allison20dfc792014-06-16 20:44:29 -07002063 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002065
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002066 HInstruction* GetNext() const { return next_; }
2067 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002068
Calin Juravle77520bc2015-01-12 18:45:46 +00002069 HInstruction* GetNextDisregardingMoves() const;
2070 HInstruction* GetPreviousDisregardingMoves() const;
2071
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002072 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002073 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002074 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002075 bool IsInBlock() const { return block_ != nullptr; }
2076 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002077 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2078 bool IsIrreducibleLoopHeaderPhi() const {
2079 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2080 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002081
Vladimir Marko372f10e2016-05-17 16:30:10 +01002082 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2083
2084 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2085 // One virtual method is enough, just const_cast<> and then re-add the const.
2086 return ArrayRef<const HUserRecord<HInstruction*>>(
2087 const_cast<HInstruction*>(this)->GetInputRecords());
2088 }
2089
Vladimir Markoe9004912016-06-16 16:50:52 +01002090 HInputsRef GetInputs() {
2091 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002092 }
2093
Vladimir Markoe9004912016-06-16 16:50:52 +01002094 HConstInputsRef GetInputs() const {
2095 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002096 }
2097
2098 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002099 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002100
Aart Bik2767f4b2016-10-28 15:03:53 -07002101 bool HasInput(HInstruction* input) const {
2102 for (const HInstruction* i : GetInputs()) {
2103 if (i == input) {
2104 return true;
2105 }
2106 }
2107 return false;
2108 }
2109
Vladimir Marko372f10e2016-05-17 16:30:10 +01002110 void SetRawInputAt(size_t index, HInstruction* input) {
2111 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2112 }
2113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114 virtual void Accept(HGraphVisitor* visitor) = 0;
2115 virtual const char* DebugName() const = 0;
2116
Vladimir Markobd785672018-05-03 17:09:09 +01002117 DataType::Type GetType() const {
2118 return TypeField::Decode(GetPackedFields());
2119 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002121 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002122
2123 uint32_t GetDexPc() const { return dex_pc_; }
2124
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002125 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002126
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002127 // Can the instruction throw?
2128 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2129 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002130 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002131
2132 // Does the instruction always throw an exception unconditionally?
2133 virtual bool AlwaysThrows() const { return false; }
2134
David Brazdilec16f792015-08-19 15:04:01 +01002135 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002136
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002137 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002138 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002139
Calin Juravle10e244f2015-01-26 18:54:32 +00002140 // Does not apply for all instructions, but having this at top level greatly
2141 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002142 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002143 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002145 return true;
2146 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002147
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002148 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002149 return false;
2150 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002151
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002152 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2153 // with it. Otherwise return null.
2154 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002155 // Go over previous non-move instructions that are emitted at use site.
2156 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2157 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2158 if (prev_not_move->IsNullCheck()) {
2159 return prev_not_move->AsNullCheck();
2160 }
2161 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002162 }
2163 return nullptr;
2164 }
2165
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002166 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002168 }
2169
Calin Juravle2e768302015-07-28 14:41:11 +00002170 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002171
Calin Juravle61d544b2015-02-23 16:46:57 +00002172 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002173 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002174 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002175 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002176 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002177
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002178 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002179 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002180 // Note: fixup_end remains valid across push_front().
2181 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2182 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002183 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002184 uses_.push_front(*new_node);
2185 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002186 }
2187
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002188 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002189 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002190 // Note: env_fixup_end remains valid across push_front().
2191 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2192 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002193 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002194 env_uses_.push_front(*new_node);
2195 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002196 }
2197
David Brazdil1abb4192015-02-17 18:33:36 +00002198 void RemoveAsUserOfInput(size_t input) {
2199 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002200 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2201 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2202 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002203 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002204
Vladimir Marko372f10e2016-05-17 16:30:10 +01002205 void RemoveAsUserOfAllInputs() {
2206 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2207 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2208 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2209 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2210 }
2211 }
2212
David Brazdil1abb4192015-02-17 18:33:36 +00002213 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2214 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002215
Vladimir Marko46817b82016-03-29 12:21:58 +01002216 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2217 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2218 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002219 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002220 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002221 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002222
Aart Bikcc42be02016-10-20 16:14:16 -07002223 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002224 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002225 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002226 !CanThrow() &&
2227 !IsSuspendCheck() &&
2228 !IsControlFlow() &&
2229 !IsNativeDebugInfo() &&
2230 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002231 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002232 !IsMemoryBarrier() &&
2233 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002234 }
2235
Aart Bikcc42be02016-10-20 16:14:16 -07002236 bool IsDeadAndRemovable() const {
2237 return IsRemovable() && !HasUses();
2238 }
2239
Roland Levillain6c82d402014-10-13 16:10:27 +01002240 // Does this instruction strictly dominate `other_instruction`?
2241 // Returns false if this instruction and `other_instruction` are the same.
2242 // Aborts if this instruction and `other_instruction` are both phis.
2243 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002244
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002245 int GetId() const { return id_; }
2246 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002247
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002248 int GetSsaIndex() const { return ssa_index_; }
2249 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2250 bool HasSsaIndex() const { return ssa_index_ != -1; }
2251
2252 bool HasEnvironment() const { return environment_ != nullptr; }
2253 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002254 // Set the `environment_` field. Raw because this method does not
2255 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002256 void SetRawEnvironment(HEnvironment* environment) {
2257 DCHECK(environment_ == nullptr);
2258 DCHECK_EQ(environment->GetHolder(), this);
2259 environment_ = environment;
2260 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002261
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002262 void InsertRawEnvironment(HEnvironment* environment) {
2263 DCHECK(environment_ != nullptr);
2264 DCHECK_EQ(environment->GetHolder(), this);
2265 DCHECK(environment->GetParent() == nullptr);
2266 environment->parent_ = environment_;
2267 environment_ = environment;
2268 }
2269
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002270 void RemoveEnvironment();
2271
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002272 // Set the environment of this instruction, copying it from `environment`. While
2273 // copying, the uses lists are being updated.
2274 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002275 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002276 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002277 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002278 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002279 if (environment->GetParent() != nullptr) {
2280 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2281 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002282 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002283
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002284 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2285 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002286 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002287 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002288 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002289 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002290 if (environment->GetParent() != nullptr) {
2291 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2292 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002293 }
2294
Nicolas Geoffray39468442014-09-02 15:17:15 +01002295 // Returns the number of entries in the environment. Typically, that is the
2296 // number of dex registers in a method. It could be more in case of inlining.
2297 size_t EnvironmentSize() const;
2298
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002299 LocationSummary* GetLocations() const { return locations_; }
2300 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002301
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002302 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002303 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002304 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002305 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002306
Alexandre Rames188d4312015-04-09 18:30:21 +01002307 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2308 // uses of this instruction by `other` are *not* updated.
2309 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2310 ReplaceWith(other);
2311 other->ReplaceInput(this, use_index);
2312 }
2313
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002314 // Move `this` instruction before `cursor`
2315 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002316
Vladimir Markofb337ea2015-11-25 15:25:10 +00002317 // Move `this` before its first user and out of any loops. If there is no
2318 // out-of-loop user that dominates all other users, move the instruction
2319 // to the end of the out-of-loop common dominator of the user's blocks.
2320 //
2321 // This can be used only on non-throwing instructions with no side effects that
2322 // have at least one use but no environment uses.
2323 void MoveBeforeFirstUserAndOutOfLoops();
2324
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002325#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002326 bool Is##type() const;
2327
2328 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2329#undef INSTRUCTION_TYPE_CHECK
2330
2331#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002332 const H##type* As##type() const; \
2333 H##type* As##type();
2334
Vladimir Markoa90dd512018-05-04 15:04:45 +01002335 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2336#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002337
Artem Serovcced8ba2017-07-19 18:18:09 +01002338 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2339 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2340 // the instruction then the behaviour of this function is undefined.
2341 //
2342 // Note: It is semantically valid to create a clone of the instruction only until
2343 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2344 // copied.
2345 //
2346 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2347 // 'explicit HInstruction(const HInstruction& other)' for details.
2348 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2349 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2350 DebugName() << " " << GetId();
2351 UNREACHABLE();
2352 }
2353
2354 // Return whether instruction can be cloned (copied).
2355 virtual bool IsClonable() const { return false; }
2356
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002357 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002358 // TODO: this method is used by LICM and GVN with possibly different
2359 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002360 virtual bool CanBeMoved() const { return false; }
2361
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002362 // Returns whether any data encoded in the two instructions is equal.
2363 // This method does not look at the inputs. Both instructions must be
2364 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002365 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002366 return false;
2367 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002368
2369 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002370 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002371 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002372 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002373
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302374 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002375
2376 virtual size_t ComputeHashCode() const {
2377 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002378 for (const HInstruction* input : GetInputs()) {
2379 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002380 }
2381 return result;
2382 }
2383
2384 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002385 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002386 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002387
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002388 size_t GetLifetimePosition() const { return lifetime_position_; }
2389 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2390 LiveInterval* GetLiveInterval() const { return live_interval_; }
2391 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2392 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2393
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002394 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2395
2396 // Returns whether the code generation of the instruction will require to have access
2397 // to the current method. Such instructions are:
2398 // (1): Instructions that require an environment, as calling the runtime requires
2399 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002400 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2401 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002402 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002403 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002404 }
2405
Vladimir Markodc151b22015-10-15 18:02:30 +01002406 // Returns whether the code generation of the instruction will require to have access
2407 // to the dex cache of the current method's declaring class via the current method.
2408 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002409
Mark Mendellc4701932015-04-10 13:18:51 -04002410 // Does this instruction have any use in an environment before
2411 // control flow hits 'other'?
2412 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2413
2414 // Remove all references to environment uses of this instruction.
2415 // The caller must ensure that this is safe to do.
2416 void RemoveEnvironmentUsers();
2417
Vladimir Markoa1de9182016-02-25 11:37:38 +00002418 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2419 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002420
David Brazdil1abb4192015-02-17 18:33:36 +00002421 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002422 // If set, the machine code for this instruction is assumed to be generated by
2423 // its users. Used by liveness analysis to compute use positions accordingly.
2424 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2425 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302426 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2427 static constexpr size_t kFieldInstructionKindSize =
2428 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002429 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302430 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002431 static constexpr size_t kFieldTypeSize =
2432 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2433 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002434 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2435
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302436 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2437 "Too many generic packed fields");
2438
Vladimir Markobd785672018-05-03 17:09:09 +01002439 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2440
Vladimir Marko372f10e2016-05-17 16:30:10 +01002441 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2442 return GetInputRecords()[i];
2443 }
2444
2445 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2446 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2447 input_records[index] = input;
2448 }
David Brazdil1abb4192015-02-17 18:33:36 +00002449
Vladimir Markoa1de9182016-02-25 11:37:38 +00002450 uint32_t GetPackedFields() const {
2451 return packed_fields_;
2452 }
2453
2454 template <size_t flag>
2455 bool GetPackedFlag() const {
2456 return (packed_fields_ & (1u << flag)) != 0u;
2457 }
2458
2459 template <size_t flag>
2460 void SetPackedFlag(bool value = true) {
2461 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2462 }
2463
2464 template <typename BitFieldType>
2465 typename BitFieldType::value_type GetPackedField() const {
2466 return BitFieldType::Decode(packed_fields_);
2467 }
2468
2469 template <typename BitFieldType>
2470 void SetPackedField(typename BitFieldType::value_type value) {
2471 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2472 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2473 }
2474
Artem Serovcced8ba2017-07-19 18:18:09 +01002475 // Copy construction for the instruction (used for Clone function).
2476 //
2477 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2478 // prepare_for_register_allocator are not copied (set to default values).
2479 //
2480 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2481 // fine for most of them. However for some of the instructions a custom copy constructor must be
2482 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2483 // for copying them).
2484 explicit HInstruction(const HInstruction& other)
2485 : previous_(nullptr),
2486 next_(nullptr),
2487 block_(nullptr),
2488 dex_pc_(other.dex_pc_),
2489 id_(-1),
2490 ssa_index_(-1),
2491 packed_fields_(other.packed_fields_),
2492 environment_(nullptr),
2493 locations_(nullptr),
2494 live_interval_(nullptr),
2495 lifetime_position_(kNoLifetime),
2496 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302497 reference_type_handle_(other.reference_type_handle_) {
2498 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002499
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002500 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302501 using InstructionKindField =
2502 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2503
Vladimir Marko46817b82016-03-29 12:21:58 +01002504 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2505 auto before_use_node = uses_.before_begin();
2506 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2507 HInstruction* user = use_node->GetUser();
2508 size_t input_index = use_node->GetIndex();
2509 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2510 before_use_node = use_node;
2511 }
2512 }
2513
2514 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2515 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2516 if (next != uses_.end()) {
2517 HInstruction* next_user = next->GetUser();
2518 size_t next_index = next->GetIndex();
2519 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2520 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2521 }
2522 }
2523
2524 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2525 auto before_env_use_node = env_uses_.before_begin();
2526 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2527 HEnvironment* user = env_use_node->GetUser();
2528 size_t input_index = env_use_node->GetIndex();
2529 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2530 before_env_use_node = env_use_node;
2531 }
2532 }
2533
2534 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2535 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2536 if (next != env_uses_.end()) {
2537 HEnvironment* next_user = next->GetUser();
2538 size_t next_index = next->GetIndex();
2539 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2540 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2541 }
2542 }
David Brazdil1abb4192015-02-17 18:33:36 +00002543
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002544 HInstruction* previous_;
2545 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002546 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002547 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002548
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002549 // An instruction gets an id when it is added to the graph.
2550 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002551 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002552 int id_;
2553
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002554 // When doing liveness analysis, instructions that have uses get an SSA index.
2555 int ssa_index_;
2556
Vladimir Markoa1de9182016-02-25 11:37:38 +00002557 // Packed fields.
2558 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002559
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002560 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002561 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002562
2563 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002564 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002565
Nicolas Geoffray39468442014-09-02 15:17:15 +01002566 // The environment associated with this instruction. Not null if the instruction
2567 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002568 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002569
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002570 // Set by the code generator.
2571 LocationSummary* locations_;
2572
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002573 // Set by the liveness analysis.
2574 LiveInterval* live_interval_;
2575
2576 // Set by the liveness analysis, this is the position in a linear
2577 // order of blocks where this instruction's live interval start.
2578 size_t lifetime_position_;
2579
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002580 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002581
Vladimir Markoa1de9182016-02-25 11:37:38 +00002582 // The reference handle part of the reference type info.
2583 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002584 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002585 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002586
David Brazdil1abb4192015-02-17 18:33:36 +00002587 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002588 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002589 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002590 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002591 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002592};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002593std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002594
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002595// Iterates over the instructions, while preserving the next instruction
2596// in case the current instruction gets removed from the list by the user
2597// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002598class HInstructionIterator : public ValueObject {
2599 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002600 explicit HInstructionIterator(const HInstructionList& instructions)
2601 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002602 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002603 }
2604
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002605 bool Done() const { return instruction_ == nullptr; }
2606 HInstruction* Current() const { return instruction_; }
2607 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002608 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002609 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002610 }
2611
2612 private:
2613 HInstruction* instruction_;
2614 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002615
2616 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002617};
2618
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002619// Iterates over the instructions without saving the next instruction,
2620// therefore handling changes in the graph potentially made by the user
2621// of this iterator.
2622class HInstructionIteratorHandleChanges : public ValueObject {
2623 public:
2624 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2625 : instruction_(instructions.first_instruction_) {
2626 }
2627
2628 bool Done() const { return instruction_ == nullptr; }
2629 HInstruction* Current() const { return instruction_; }
2630 void Advance() {
2631 instruction_ = instruction_->GetNext();
2632 }
2633
2634 private:
2635 HInstruction* instruction_;
2636
2637 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2638};
2639
2640
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002641class HBackwardInstructionIterator : public ValueObject {
2642 public:
2643 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2644 : instruction_(instructions.last_instruction_) {
2645 next_ = Done() ? nullptr : instruction_->GetPrevious();
2646 }
2647
2648 bool Done() const { return instruction_ == nullptr; }
2649 HInstruction* Current() const { return instruction_; }
2650 void Advance() {
2651 instruction_ = next_;
2652 next_ = Done() ? nullptr : instruction_->GetPrevious();
2653 }
2654
2655 private:
2656 HInstruction* instruction_;
2657 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002658
2659 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002660};
2661
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002662class HVariableInputSizeInstruction : public HInstruction {
2663 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002664 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002665 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002666 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2667 }
2668
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002669 void AddInput(HInstruction* input);
2670 void InsertInputAt(size_t index, HInstruction* input);
2671 void RemoveInputAt(size_t index);
2672
Igor Murashkind01745e2017-04-05 16:40:31 -07002673 // Removes all the inputs.
2674 // Also removes this instructions from each input's use list
2675 // (for non-environment uses only).
2676 void RemoveAllInputs();
2677
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002678 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302679 HVariableInputSizeInstruction(InstructionKind inst_kind,
2680 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002681 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002682 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002683 size_t number_of_inputs,
2684 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302685 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002686 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002687 HVariableInputSizeInstruction(InstructionKind inst_kind,
2688 DataType::Type type,
2689 SideEffects side_effects,
2690 uint32_t dex_pc,
2691 ArenaAllocator* allocator,
2692 size_t number_of_inputs,
2693 ArenaAllocKind kind)
2694 : HInstruction(inst_kind, type, side_effects, dex_pc),
2695 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002696
Artem Serovcced8ba2017-07-19 18:18:09 +01002697 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002698
Artem Serovcced8ba2017-07-19 18:18:09 +01002699 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002700};
2701
Vladimir Markof9f64412015-09-02 14:05:49 +01002702template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002703class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002704 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002705 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302706 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002707 HExpression<N>(InstructionKind kind,
2708 DataType::Type type,
2709 SideEffects side_effects,
2710 uint32_t dex_pc)
2711 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2712 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002713
Vladimir Marko372f10e2016-05-17 16:30:10 +01002714 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002715 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002716 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002717 }
2718
Artem Serovcced8ba2017-07-19 18:18:09 +01002719 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002720 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002721
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002722 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002723 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002724
2725 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002726};
2727
Vladimir Markobd785672018-05-03 17:09:09 +01002728// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002729template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002730class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002731 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002732 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002733
Vladimir Markobd785672018-05-03 17:09:09 +01002734 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002735
Vladimir Marko372f10e2016-05-17 16:30:10 +01002736 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002737 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002738 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002739 }
2740
Artem Serovcced8ba2017-07-19 18:18:09 +01002741 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002742 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002743
Vladimir Markof9f64412015-09-02 14:05:49 +01002744 private:
2745 friend class SsaBuilder;
2746};
2747
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002748// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2749// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002750class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002751 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002752 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002753 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302754 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002756 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002757
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002758 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002759
Artem Serovcced8ba2017-07-19 18:18:09 +01002760 protected:
2761 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002762};
2763
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002764// Represents dex's RETURN opcodes. A HReturn is a control flow
2765// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002766class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002768 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002769 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002770 SetRawInputAt(0, value);
2771 }
2772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002773 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002774
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002775 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002776
Artem Serovcced8ba2017-07-19 18:18:09 +01002777 protected:
2778 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002779};
2780
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002781class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002782 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002783 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002784 uint32_t reg_number,
2785 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002787 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002788 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302789 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002790 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002791 SideEffects::None(),
2792 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002793 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002794 number_of_inputs,
2795 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002796 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002797 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002798 // Phis are constructed live and marked dead if conflicting or unused.
2799 // Individual steps of SsaBuilder should assume that if a phi has been
2800 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2801 SetPackedFlag<kFlagIsLive>(true);
2802 SetPackedFlag<kFlagCanBeNull>(true);
2803 }
2804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002805 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002806
David Brazdildee58d62016-04-07 09:54:26 +00002807 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002808 static DataType::Type ToPhiType(DataType::Type type) {
2809 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002810 }
2811
2812 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002815 // Make sure that only valid type changes occur. The following are allowed:
2816 // (1) int -> float/ref (primitive type propagation),
2817 // (2) long -> double (primitive type propagation).
2818 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2820 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2821 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002822 SetPackedField<TypeField>(new_type);
2823 }
2824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002825 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002826 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2827
2828 uint32_t GetRegNumber() const { return reg_number_; }
2829
2830 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2831 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2832 bool IsDead() const { return !IsLive(); }
2833 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2834
Vladimir Markoe9004912016-06-16 16:50:52 +01002835 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002836 return other != nullptr
2837 && other->IsPhi()
2838 && other->AsPhi()->GetBlock() == GetBlock()
2839 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2840 }
2841
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002842 bool HasEquivalentPhi() const {
2843 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2844 return true;
2845 }
2846 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2847 return true;
2848 }
2849 return false;
2850 }
2851
David Brazdildee58d62016-04-07 09:54:26 +00002852 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2853 // An equivalent phi is a phi having the same dex register and type.
2854 // It assumes that phis with the same dex register are adjacent.
2855 HPhi* GetNextEquivalentPhiWithSameType() {
2856 HInstruction* next = GetNext();
2857 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2858 if (next->GetType() == GetType()) {
2859 return next->AsPhi();
2860 }
2861 next = next->GetNext();
2862 }
2863 return nullptr;
2864 }
2865
2866 DECLARE_INSTRUCTION(Phi);
2867
Artem Serovcced8ba2017-07-19 18:18:09 +01002868 protected:
2869 DEFAULT_COPY_CONSTRUCTOR(Phi);
2870
David Brazdildee58d62016-04-07 09:54:26 +00002871 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002872 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002873 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2874 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2875 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002876
David Brazdildee58d62016-04-07 09:54:26 +00002877 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002878};
2879
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002880// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002881// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002882// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002883class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002884 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302885 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002886 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302887 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002889 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002890
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002891 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002892
Artem Serovcced8ba2017-07-19 18:18:09 +01002893 protected:
2894 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002895};
2896
2897// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002898class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002899 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302900 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002901 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302902 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002903
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002904 bool IsClonable() const override { return true; }
2905 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002906
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002907 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002908 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002909 }
2910
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002911 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002912
Artem Serovcced8ba2017-07-19 18:18:09 +01002913 protected:
2914 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002915};
2916
Roland Levillain9867bc72015-08-05 10:21:34 +01002917class HConstant : public HExpression<0> {
2918 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302919 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2920 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2921 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002922
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002923 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002924
Roland Levillain1a653882016-03-18 18:05:57 +00002925 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002926 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002927 // Is this constant 0 in the arithmetic sense?
2928 virtual bool IsArithmeticZero() const { return false; }
2929 // Is this constant a 0-bit pattern?
2930 virtual bool IsZeroBitPattern() const { return false; }
2931 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002932 virtual bool IsOne() const { return false; }
2933
David Brazdil77a48ae2015-09-15 12:34:04 +00002934 virtual uint64_t GetValueAsUint64() const = 0;
2935
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002936 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002937
Artem Serovcced8ba2017-07-19 18:18:09 +01002938 protected:
2939 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002940};
2941
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002942class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002943 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002944 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002945 return true;
2946 }
2947
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002948 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002950 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002951
Roland Levillain1a653882016-03-18 18:05:57 +00002952 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002953 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002954
Roland Levillain9867bc72015-08-05 10:21:34 +01002955 DECLARE_INSTRUCTION(NullConstant);
2956
Artem Serovcced8ba2017-07-19 18:18:09 +01002957 protected:
2958 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2959
Roland Levillain9867bc72015-08-05 10:21:34 +01002960 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002961 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302962 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2963 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002964
2965 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002966};
2967
2968// Constants of the type int. Those can be from Dex instructions, or
2969// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002970class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002971 public:
2972 int32_t GetValue() const { return value_; }
2973
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002974 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002975 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2976 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002977
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002978 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002979 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002980 return other->AsIntConstant()->value_ == value_;
2981 }
2982
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002983 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002985 bool IsMinusOne() const override { return GetValue() == -1; }
2986 bool IsArithmeticZero() const override { return GetValue() == 0; }
2987 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2988 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002989
Roland Levillain1a653882016-03-18 18:05:57 +00002990 // Integer constants are used to encode Boolean values as well,
2991 // where 1 means true and 0 means false.
2992 bool IsTrue() const { return GetValue() == 1; }
2993 bool IsFalse() const { return GetValue() == 0; }
2994
Roland Levillain9867bc72015-08-05 10:21:34 +01002995 DECLARE_INSTRUCTION(IntConstant);
2996
Artem Serovcced8ba2017-07-19 18:18:09 +01002997 protected:
2998 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2999
Roland Levillain9867bc72015-08-05 10:21:34 +01003000 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003001 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303002 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3003 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003004 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303005 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3006 value_(value ? 1 : 0) {
3007 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003008
3009 const int32_t value_;
3010
3011 friend class HGraph;
3012 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3013 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003014};
3015
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003016class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003017 public:
3018 int64_t GetValue() const { return value_; }
3019
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003020 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003021
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003022 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003024 return other->AsLongConstant()->value_ == value_;
3025 }
3026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003027 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003029 bool IsMinusOne() const override { return GetValue() == -1; }
3030 bool IsArithmeticZero() const override { return GetValue() == 0; }
3031 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3032 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003033
3034 DECLARE_INSTRUCTION(LongConstant);
3035
Artem Serovcced8ba2017-07-19 18:18:09 +01003036 protected:
3037 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3038
Roland Levillain9867bc72015-08-05 10:21:34 +01003039 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003040 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303041 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3042 value_(value) {
3043 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003044
3045 const int64_t value_;
3046
3047 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003048};
Dave Allison20dfc792014-06-16 20:44:29 -07003049
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003050class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003051 public:
3052 float GetValue() const { return value_; }
3053
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003054 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003055 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3056 }
3057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003058 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003059 DCHECK(other->IsFloatConstant()) << other->DebugName();
3060 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3061 }
3062
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003063 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003065 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3067 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003068 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003069 return std::fpclassify(value_) == FP_ZERO;
3070 }
3071 bool IsArithmeticPositiveZero() const {
3072 return IsArithmeticZero() && !std::signbit(value_);
3073 }
3074 bool IsArithmeticNegativeZero() const {
3075 return IsArithmeticZero() && std::signbit(value_);
3076 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003077 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003078 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003080 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003081 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3082 }
3083 bool IsNaN() const {
3084 return std::isnan(value_);
3085 }
3086
3087 DECLARE_INSTRUCTION(FloatConstant);
3088
Artem Serovcced8ba2017-07-19 18:18:09 +01003089 protected:
3090 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3091
Roland Levillain31dd3d62016-02-16 12:21:02 +00003092 private:
3093 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303094 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3095 value_(value) {
3096 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003097 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303098 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3099 value_(bit_cast<float, int32_t>(value)) {
3100 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003101
3102 const float value_;
3103
3104 // Only the SsaBuilder and HGraph can create floating-point constants.
3105 friend class SsaBuilder;
3106 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003107};
3108
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003109class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 public:
3111 double GetValue() const { return value_; }
3112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003113 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003115 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003116 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3117 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3118 }
3119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003120 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003122 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003123 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3124 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003125 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003126 return std::fpclassify(value_) == FP_ZERO;
3127 }
3128 bool IsArithmeticPositiveZero() const {
3129 return IsArithmeticZero() && !std::signbit(value_);
3130 }
3131 bool IsArithmeticNegativeZero() const {
3132 return IsArithmeticZero() && std::signbit(value_);
3133 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003134 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003135 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003136 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003137 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003138 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3139 }
3140 bool IsNaN() const {
3141 return std::isnan(value_);
3142 }
3143
3144 DECLARE_INSTRUCTION(DoubleConstant);
3145
Artem Serovcced8ba2017-07-19 18:18:09 +01003146 protected:
3147 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3148
Roland Levillain31dd3d62016-02-16 12:21:02 +00003149 private:
3150 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303151 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3152 value_(value) {
3153 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003154 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303155 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3156 value_(bit_cast<double, int64_t>(value)) {
3157 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003158
3159 const double value_;
3160
3161 // Only the SsaBuilder and HGraph can create floating-point constants.
3162 friend class SsaBuilder;
3163 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164};
3165
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003166// Conditional branch. A block ending with an HIf instruction must have
3167// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003168class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003169 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003170 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003171 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003172 SetRawInputAt(0, input);
3173 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003174
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003175 bool IsClonable() const override { return true; }
3176 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003177
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003178 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003179 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003180 }
3181
3182 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003183 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003184 }
3185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003186 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003187
Artem Serovcced8ba2017-07-19 18:18:09 +01003188 protected:
3189 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003190};
3191
David Brazdilfc6a86a2015-06-26 10:33:45 +00003192
3193// Abstract instruction which marks the beginning and/or end of a try block and
3194// links it to the respective exception handlers. Behaves the same as a Goto in
3195// non-exceptional control flow.
3196// Normal-flow successor is stored at index zero, exception handlers under
3197// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003198class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003199 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003200 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003201 kEntry,
3202 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003203 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003204 };
3205
Artem Serovd1aa7d02018-06-22 11:35:46 +01003206 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3207 // across the catch block entering edges as GC might happen during throwing an exception.
3208 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3209 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003210 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003211 : HExpression(kTryBoundary,
3212 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3213 : SideEffects::None(),
3214 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003215 SetPackedField<BoundaryKindField>(kind);
3216 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003217
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003218 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003219
3220 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003221 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003222
David Brazdild26a4112015-11-10 11:07:31 +00003223 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3224 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3225 }
3226
David Brazdilfc6a86a2015-06-26 10:33:45 +00003227 // Returns whether `handler` is among its exception handlers (non-zero index
3228 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003229 bool HasExceptionHandler(const HBasicBlock& handler) const {
3230 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003231 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003232 }
3233
3234 // If not present already, adds `handler` to its block's list of exception
3235 // handlers.
3236 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003237 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003238 GetBlock()->AddSuccessor(handler);
3239 }
3240 }
3241
Vladimir Markoa1de9182016-02-25 11:37:38 +00003242 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3243 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003244
David Brazdilffee3d32015-07-06 11:48:53 +01003245 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3246
David Brazdilfc6a86a2015-06-26 10:33:45 +00003247 DECLARE_INSTRUCTION(TryBoundary);
3248
Artem Serovcced8ba2017-07-19 18:18:09 +01003249 protected:
3250 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3251
David Brazdilfc6a86a2015-06-26 10:33:45 +00003252 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003253 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3254 static constexpr size_t kFieldBoundaryKindSize =
3255 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3256 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3257 kFieldBoundaryKind + kFieldBoundaryKindSize;
3258 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3259 "Too many packed fields.");
3260 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003261};
3262
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003263// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003264class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003265 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003266 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3267 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003268 HDeoptimize(ArenaAllocator* allocator,
3269 HInstruction* cond,
3270 DeoptimizationKind kind,
3271 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003272 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303273 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003274 SideEffects::All(),
3275 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003276 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003277 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003278 kArenaAllocMisc) {
3279 SetPackedFlag<kFieldCanBeMoved>(false);
3280 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003281 SetRawInputAt(0, cond);
3282 }
3283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003284 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003285
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003286 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3287 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3288 // instead of `guard`.
3289 // We set CanTriggerGC to prevent any intermediate address to be live
3290 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003291 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003292 HInstruction* cond,
3293 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003294 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003295 uint32_t dex_pc)
3296 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303297 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003298 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003299 SideEffects::CanTriggerGC(),
3300 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003301 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003302 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003303 kArenaAllocMisc) {
3304 SetPackedFlag<kFieldCanBeMoved>(true);
3305 SetPackedField<DeoptimizeKindField>(kind);
3306 SetRawInputAt(0, cond);
3307 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003308 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003309
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003310 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003311
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003312 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003313 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3314 }
3315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003316 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003317
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003318 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003319
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003320 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003321
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003322 bool GuardsAnInput() const {
3323 return InputCount() == 2;
3324 }
3325
3326 HInstruction* GuardedInput() const {
3327 DCHECK(GuardsAnInput());
3328 return InputAt(1);
3329 }
3330
3331 void RemoveGuard() {
3332 RemoveInputAt(1);
3333 }
3334
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003335 DECLARE_INSTRUCTION(Deoptimize);
3336
Artem Serovcced8ba2017-07-19 18:18:09 +01003337 protected:
3338 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3339
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003340 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003341 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3342 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3343 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003344 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003345 static constexpr size_t kNumberOfDeoptimizePackedBits =
3346 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3347 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3348 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003349 using DeoptimizeKindField =
3350 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003351};
3352
Mingyao Yang063fc772016-08-02 11:02:54 -07003353// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3354// The compiled code checks this flag value in a guard before devirtualized call and
3355// if it's true, starts to do deoptimization.
3356// It has a 4-byte slot on stack.
3357// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003358class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003359 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003360 // CHA guards are only optimized in a separate pass and it has no side effects
3361 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003362 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303363 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003364 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303365 SideEffects::None(),
3366 dex_pc,
3367 allocator,
3368 0,
3369 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003370 }
3371
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003372 // We do all CHA guard elimination/motion in a single pass, after which there is no
3373 // further guard elimination/motion since a guard might have been used for justification
3374 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3375 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003376 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003377
3378 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3379
Artem Serovcced8ba2017-07-19 18:18:09 +01003380 protected:
3381 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003382};
3383
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003384// Represents the ArtMethod that was passed as a first argument to
3385// the method. It is used by instructions that depend on it, like
3386// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003387class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003388 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003389 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303390 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3391 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003392
3393 DECLARE_INSTRUCTION(CurrentMethod);
3394
Artem Serovcced8ba2017-07-19 18:18:09 +01003395 protected:
3396 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003397};
3398
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003399// Fetches an ArtMethod from the virtual table or the interface method table
3400// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003401class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003402 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003403 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003404 kVTable,
3405 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003406 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003407 };
3408 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003409 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003410 TableKind kind,
3411 size_t index,
3412 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303413 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003414 index_(index) {
3415 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003416 SetRawInputAt(0, cls);
3417 }
3418
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003419 bool IsClonable() const override { return true; }
3420 bool CanBeMoved() const override { return true; }
3421 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003422 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003423 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003424 }
3425
Vladimir Markoa1de9182016-02-25 11:37:38 +00003426 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003427 size_t GetIndex() const { return index_; }
3428
3429 DECLARE_INSTRUCTION(ClassTableGet);
3430
Artem Serovcced8ba2017-07-19 18:18:09 +01003431 protected:
3432 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3433
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003434 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003435 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003436 static constexpr size_t kFieldTableKindSize =
3437 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3438 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3439 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3440 "Too many packed fields.");
3441 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3442
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003443 // The index of the ArtMethod in the table.
3444 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003445};
3446
Mark Mendellfe57faa2015-09-18 09:26:15 -04003447// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3448// have one successor for each entry in the switch table, and the final successor
3449// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003450class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003451 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003452 HPackedSwitch(int32_t start_value,
3453 uint32_t num_entries,
3454 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003455 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003456 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003457 start_value_(start_value),
3458 num_entries_(num_entries) {
3459 SetRawInputAt(0, input);
3460 }
3461
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003462 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003463
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003464 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003465
3466 int32_t GetStartValue() const { return start_value_; }
3467
Vladimir Marko211c2112015-09-24 16:52:33 +01003468 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003469
3470 HBasicBlock* GetDefaultBlock() const {
3471 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003472 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003473 }
3474 DECLARE_INSTRUCTION(PackedSwitch);
3475
Artem Serovcced8ba2017-07-19 18:18:09 +01003476 protected:
3477 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3478
Mark Mendellfe57faa2015-09-18 09:26:15 -04003479 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003480 const int32_t start_value_;
3481 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003482};
3483
Roland Levillain88cb1752014-10-20 16:36:47 +01003484class HUnaryOperation : public HExpression<1> {
3485 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303486 HUnaryOperation(InstructionKind kind,
3487 DataType::Type result_type,
3488 HInstruction* input,
3489 uint32_t dex_pc = kNoDexPc)
3490 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003491 SetRawInputAt(0, input);
3492 }
3493
Artem Serovcced8ba2017-07-19 18:18:09 +01003494 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003495 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003496
Roland Levillain88cb1752014-10-20 16:36:47 +01003497 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003498 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003500 bool CanBeMoved() const override { return true; }
3501 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003502 return true;
3503 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003504
Roland Levillain31dd3d62016-02-16 12:21:02 +00003505 // Try to statically evaluate `this` and return a HConstant
3506 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003507 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003508 HConstant* TryStaticEvaluation() const;
3509
3510 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003511 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3512 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003513 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3514 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003515
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003516 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003517
Artem Serovcced8ba2017-07-19 18:18:09 +01003518 protected:
3519 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003520};
3521
Dave Allison20dfc792014-06-16 20:44:29 -07003522class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003523 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303524 HBinaryOperation(InstructionKind kind,
3525 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003526 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003527 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003528 SideEffects side_effects = SideEffects::None(),
3529 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303530 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003531 SetRawInputAt(0, left);
3532 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003533 }
3534
Artem Serovcced8ba2017-07-19 18:18:09 +01003535 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003536 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003537
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003538 HInstruction* GetLeft() const { return InputAt(0); }
3539 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003540 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003541
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003542 virtual bool IsCommutative() const { return false; }
3543
3544 // Put constant on the right.
3545 // Returns whether order is changed.
3546 bool OrderInputsWithConstantOnTheRight() {
3547 HInstruction* left = InputAt(0);
3548 HInstruction* right = InputAt(1);
3549 if (left->IsConstant() && !right->IsConstant()) {
3550 ReplaceInput(right, 0);
3551 ReplaceInput(left, 1);
3552 return true;
3553 }
3554 return false;
3555 }
3556
3557 // Order inputs by instruction id, but favor constant on the right side.
3558 // This helps GVN for commutative ops.
3559 void OrderInputs() {
3560 DCHECK(IsCommutative());
3561 HInstruction* left = InputAt(0);
3562 HInstruction* right = InputAt(1);
3563 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3564 return;
3565 }
3566 if (OrderInputsWithConstantOnTheRight()) {
3567 return;
3568 }
3569 // Order according to instruction id.
3570 if (left->GetId() > right->GetId()) {
3571 ReplaceInput(right, 0);
3572 ReplaceInput(left, 1);
3573 }
3574 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003575
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003576 bool CanBeMoved() const override { return true; }
3577 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003578 return true;
3579 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003580
Roland Levillain31dd3d62016-02-16 12:21:02 +00003581 // Try to statically evaluate `this` and return a HConstant
3582 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003583 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003584 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003585
3586 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3588 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003589 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3590 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003592 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3593 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003594 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3595 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003596 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3597 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003598 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003599 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3600 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003601
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003602 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003603 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003604 HConstant* GetConstantRight() const;
3605
3606 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003607 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003608 HInstruction* GetLeastConstantLeft() const;
3609
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003610 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003611
Artem Serovcced8ba2017-07-19 18:18:09 +01003612 protected:
3613 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003614};
3615
Mark Mendellc4701932015-04-10 13:18:51 -04003616// The comparison bias applies for floating point operations and indicates how NaN
3617// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003618enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003619 kNoBias, // bias is not applicable (i.e. for long operation)
3620 kGtBias, // return 1 for NaN comparisons
3621 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003622 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003623};
3624
Roland Levillain31dd3d62016-02-16 12:21:02 +00003625std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3626
Dave Allison20dfc792014-06-16 20:44:29 -07003627class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003628 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303629 HCondition(InstructionKind kind,
3630 HInstruction* first,
3631 HInstruction* second,
3632 uint32_t dex_pc = kNoDexPc)
3633 : HBinaryOperation(kind,
3634 DataType::Type::kBool,
3635 first,
3636 second,
3637 SideEffects::None(),
3638 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003639 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3640 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003641
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003642 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003643 // `instruction`, and disregard moves in between.
3644 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003645
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003646 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003647
3648 virtual IfCondition GetCondition() const = 0;
3649
Mark Mendellc4701932015-04-10 13:18:51 -04003650 virtual IfCondition GetOppositeCondition() const = 0;
3651
Vladimir Markoa1de9182016-02-25 11:37:38 +00003652 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003653 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3654
Vladimir Markoa1de9182016-02-25 11:37:38 +00003655 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3656 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003657
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003658 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003659 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003660 }
3661
Roland Levillain4fa13f62015-07-06 18:11:54 +01003662 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003664 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003665 if (if_cond == kCondNE) {
3666 return true;
3667 } else if (if_cond == kCondEQ) {
3668 return false;
3669 }
3670 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003671 }
3672
3673 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003674 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003675 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003676 if (if_cond == kCondEQ) {
3677 return true;
3678 } else if (if_cond == kCondNE) {
3679 return false;
3680 }
3681 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003682 }
3683
Roland Levillain31dd3d62016-02-16 12:21:02 +00003684 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003685 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003686 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003687 static constexpr size_t kFieldComparisonBiasSize =
3688 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3689 static constexpr size_t kNumberOfConditionPackedBits =
3690 kFieldComparisonBias + kFieldComparisonBiasSize;
3691 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3692 using ComparisonBiasField =
3693 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3694
Roland Levillain31dd3d62016-02-16 12:21:02 +00003695 template <typename T>
3696 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3697
3698 template <typename T>
3699 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003700 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003701 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3702 // Handle the bias.
3703 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3704 }
3705
3706 // Return an integer constant containing the result of a condition evaluated at compile time.
3707 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3708 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3709 }
3710
Artem Serovcced8ba2017-07-19 18:18:09 +01003711 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003712};
3713
3714// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003715class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003716 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003717 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303718 : HCondition(kEqual, first, second, dex_pc) {
3719 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003720
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003721 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003722
Vladimir Marko9e23df52015-11-10 17:14:35 +00003723 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003724 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003725 return MakeConstantCondition(true, GetDexPc());
3726 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003727 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3729 }
3730 // In the following Evaluate methods, a HCompare instruction has
3731 // been merged into this HEqual instruction; evaluate it as
3732 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003733 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003734 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3735 GetDexPc());
3736 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003737 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003738 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3739 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003740 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003741 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003742 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003743
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003744 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003745
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003746 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003747 return kCondEQ;
3748 }
3749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003750 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003751 return kCondNE;
3752 }
3753
Artem Serovcced8ba2017-07-19 18:18:09 +01003754 protected:
3755 DEFAULT_COPY_CONSTRUCTOR(Equal);
3756
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003757 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003758 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003759};
3760
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003761class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003762 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303763 HNotEqual(HInstruction* first, HInstruction* second,
3764 uint32_t dex_pc = kNoDexPc)
3765 : HCondition(kNotEqual, first, second, dex_pc) {
3766 }
Dave Allison20dfc792014-06-16 20:44:29 -07003767
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003768 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003769
Vladimir Marko9e23df52015-11-10 17:14:35 +00003770 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003771 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003772 return MakeConstantCondition(false, GetDexPc());
3773 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003774 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003775 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3776 }
3777 // In the following Evaluate methods, a HCompare instruction has
3778 // been merged into this HNotEqual instruction; evaluate it as
3779 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003780 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003781 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3782 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003783 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003784 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3785 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003786 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003787 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003788 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003789
Dave Allison20dfc792014-06-16 20:44:29 -07003790 DECLARE_INSTRUCTION(NotEqual);
3791
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003792 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003793 return kCondNE;
3794 }
3795
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003796 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003797 return kCondEQ;
3798 }
3799
Artem Serovcced8ba2017-07-19 18:18:09 +01003800 protected:
3801 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3802
Dave Allison20dfc792014-06-16 20:44:29 -07003803 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003804 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003805};
3806
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003807class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003808 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303809 HLessThan(HInstruction* first, HInstruction* second,
3810 uint32_t dex_pc = kNoDexPc)
3811 : HCondition(kLessThan, first, second, dex_pc) {
3812 }
Dave Allison20dfc792014-06-16 20:44:29 -07003813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003814 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003815 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003816 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003817 // In the following Evaluate methods, a HCompare instruction has
3818 // been merged into this HLessThan instruction; evaluate it as
3819 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003820 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003821 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3822 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003823 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003824 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3825 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003826 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003827 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003828 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003829
Dave Allison20dfc792014-06-16 20:44:29 -07003830 DECLARE_INSTRUCTION(LessThan);
3831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003832 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003833 return kCondLT;
3834 }
3835
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003836 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003837 return kCondGE;
3838 }
3839
Artem Serovcced8ba2017-07-19 18:18:09 +01003840 protected:
3841 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3842
Dave Allison20dfc792014-06-16 20:44:29 -07003843 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003844 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003845};
3846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003847class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003848 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303849 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3850 uint32_t dex_pc = kNoDexPc)
3851 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3852 }
Dave Allison20dfc792014-06-16 20:44:29 -07003853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003854 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003855 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003856 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003857 // In the following Evaluate methods, a HCompare instruction has
3858 // been merged into this HLessThanOrEqual instruction; evaluate it as
3859 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3862 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003863 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003864 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3865 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003866 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003867 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003868 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003869
Dave Allison20dfc792014-06-16 20:44:29 -07003870 DECLARE_INSTRUCTION(LessThanOrEqual);
3871
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003872 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003873 return kCondLE;
3874 }
3875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003876 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003877 return kCondGT;
3878 }
3879
Artem Serovcced8ba2017-07-19 18:18:09 +01003880 protected:
3881 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3882
Dave Allison20dfc792014-06-16 20:44:29 -07003883 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003884 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003885};
3886
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003887class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003888 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003889 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303890 : HCondition(kGreaterThan, first, second, dex_pc) {
3891 }
Dave Allison20dfc792014-06-16 20:44:29 -07003892
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003893 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003894 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003895 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003896 // In the following Evaluate methods, a HCompare instruction has
3897 // been merged into this HGreaterThan instruction; evaluate it as
3898 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003899 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003900 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3901 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003902 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003903 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3904 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003905 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003906 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003907 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003908
Dave Allison20dfc792014-06-16 20:44:29 -07003909 DECLARE_INSTRUCTION(GreaterThan);
3910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003911 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003912 return kCondGT;
3913 }
3914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003915 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003916 return kCondLE;
3917 }
3918
Artem Serovcced8ba2017-07-19 18:18:09 +01003919 protected:
3920 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3921
Dave Allison20dfc792014-06-16 20:44:29 -07003922 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003923 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003924};
3925
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003926class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003927 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003928 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303929 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3930 }
Dave Allison20dfc792014-06-16 20:44:29 -07003931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003932 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003933 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003934 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003935 // In the following Evaluate methods, a HCompare instruction has
3936 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3937 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003938 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003939 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3940 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003941 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003942 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3943 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003944 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003945 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003946 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003947
Dave Allison20dfc792014-06-16 20:44:29 -07003948 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003950 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003951 return kCondGE;
3952 }
3953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003954 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003955 return kCondLT;
3956 }
3957
Artem Serovcced8ba2017-07-19 18:18:09 +01003958 protected:
3959 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3960
Dave Allison20dfc792014-06-16 20:44:29 -07003961 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003962 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003963};
3964
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003965class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003966 public:
3967 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303968 : HCondition(kBelow, first, second, dex_pc) {
3969 }
Aart Bike9f37602015-10-09 11:15:55 -07003970
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003971 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003972 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003973 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003974 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003975 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3976 }
3977 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003978 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003979 LOG(FATAL) << DebugName() << " is not defined for float values";
3980 UNREACHABLE();
3981 }
3982 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003983 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003984 LOG(FATAL) << DebugName() << " is not defined for double values";
3985 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003986 }
3987
3988 DECLARE_INSTRUCTION(Below);
3989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003990 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003991 return kCondB;
3992 }
3993
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003994 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003995 return kCondAE;
3996 }
3997
Artem Serovcced8ba2017-07-19 18:18:09 +01003998 protected:
3999 DEFAULT_COPY_CONSTRUCTOR(Below);
4000
Aart Bike9f37602015-10-09 11:15:55 -07004001 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004002 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004003 return MakeUnsigned(x) < MakeUnsigned(y);
4004 }
Aart Bike9f37602015-10-09 11:15:55 -07004005};
4006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004007class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004008 public:
4009 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304010 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4011 }
Aart Bike9f37602015-10-09 11:15:55 -07004012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004013 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004014 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004015 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004016 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004017 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4018 }
4019 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004020 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004021 LOG(FATAL) << DebugName() << " is not defined for float values";
4022 UNREACHABLE();
4023 }
4024 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004025 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004026 LOG(FATAL) << DebugName() << " is not defined for double values";
4027 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004028 }
4029
4030 DECLARE_INSTRUCTION(BelowOrEqual);
4031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004032 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004033 return kCondBE;
4034 }
4035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004036 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004037 return kCondA;
4038 }
4039
Artem Serovcced8ba2017-07-19 18:18:09 +01004040 protected:
4041 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4042
Aart Bike9f37602015-10-09 11:15:55 -07004043 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004044 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004045 return MakeUnsigned(x) <= MakeUnsigned(y);
4046 }
Aart Bike9f37602015-10-09 11:15:55 -07004047};
4048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004049class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004050 public:
4051 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304052 : HCondition(kAbove, first, second, dex_pc) {
4053 }
Aart Bike9f37602015-10-09 11:15:55 -07004054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004055 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004056 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004057 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004058 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004059 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4060 }
4061 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004062 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004063 LOG(FATAL) << DebugName() << " is not defined for float values";
4064 UNREACHABLE();
4065 }
4066 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004067 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004068 LOG(FATAL) << DebugName() << " is not defined for double values";
4069 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004070 }
4071
4072 DECLARE_INSTRUCTION(Above);
4073
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004074 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004075 return kCondA;
4076 }
4077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004078 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004079 return kCondBE;
4080 }
4081
Artem Serovcced8ba2017-07-19 18:18:09 +01004082 protected:
4083 DEFAULT_COPY_CONSTRUCTOR(Above);
4084
Aart Bike9f37602015-10-09 11:15:55 -07004085 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004086 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004087 return MakeUnsigned(x) > MakeUnsigned(y);
4088 }
Aart Bike9f37602015-10-09 11:15:55 -07004089};
4090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004091class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004092 public:
4093 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304094 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4095 }
Aart Bike9f37602015-10-09 11:15:55 -07004096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004097 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004098 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004099 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004100 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004101 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4102 }
4103 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004104 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004105 LOG(FATAL) << DebugName() << " is not defined for float values";
4106 UNREACHABLE();
4107 }
4108 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004109 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004110 LOG(FATAL) << DebugName() << " is not defined for double values";
4111 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004112 }
4113
4114 DECLARE_INSTRUCTION(AboveOrEqual);
4115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004116 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004117 return kCondAE;
4118 }
4119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004120 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004121 return kCondB;
4122 }
4123
Artem Serovcced8ba2017-07-19 18:18:09 +01004124 protected:
4125 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4126
Aart Bike9f37602015-10-09 11:15:55 -07004127 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004128 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004129 return MakeUnsigned(x) >= MakeUnsigned(y);
4130 }
Aart Bike9f37602015-10-09 11:15:55 -07004131};
Dave Allison20dfc792014-06-16 20:44:29 -07004132
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004133// Instruction to check how two inputs compare to each other.
4134// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004135class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004136 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004137 // Note that `comparison_type` is the type of comparison performed
4138 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004139 // HCompare instruction (which is always DataType::Type::kInt).
4140 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004141 HInstruction* first,
4142 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004143 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004144 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304145 : HBinaryOperation(kCompare,
4146 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 first,
4148 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004149 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004150 dex_pc) {
4151 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004152 }
4153
Roland Levillain9867bc72015-08-05 10:21:34 +01004154 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004155 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4156
4157 template <typename T>
4158 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004159 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004160 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4161 // Handle the bias.
4162 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4163 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004164
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004165 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004166 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4167 // reach this code path when processing a freshly built HIR
4168 // graph. However HCompare integer instructions can be synthesized
4169 // by the instruction simplifier to implement IntegerCompare and
4170 // IntegerSignum intrinsics, so we have to handle this case.
4171 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004172 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004173 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004174 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4175 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004176 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004177 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4178 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004179 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004180 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004181 }
4182
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004183 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004184 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004185 }
4186
Vladimir Markoa1de9182016-02-25 11:37:38 +00004187 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004188
Roland Levillain31dd3d62016-02-16 12:21:02 +00004189 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004190 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004191 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004192 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004193 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004194 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004195
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004196 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004197 // Comparisons do not require a runtime call in any back end.
4198 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004199 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004200
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004201 DECLARE_INSTRUCTION(Compare);
4202
Roland Levillain31dd3d62016-02-16 12:21:02 +00004203 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004204 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004205 static constexpr size_t kFieldComparisonBiasSize =
4206 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4207 static constexpr size_t kNumberOfComparePackedBits =
4208 kFieldComparisonBias + kFieldComparisonBiasSize;
4209 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4210 using ComparisonBiasField =
4211 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4212
Roland Levillain31dd3d62016-02-16 12:21:02 +00004213 // Return an integer constant containing the result of a comparison evaluated at compile time.
4214 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4215 DCHECK(value == -1 || value == 0 || value == 1) << value;
4216 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4217 }
4218
Artem Serovcced8ba2017-07-19 18:18:09 +01004219 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004220};
4221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004222class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004223 public:
4224 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004225 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004226 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004227 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004228 bool finalizable,
4229 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304230 : HExpression(kNewInstance,
4231 DataType::Type::kReference,
4232 SideEffects::CanTriggerGC(),
4233 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004234 type_index_(type_index),
4235 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004236 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004237 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004238 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004239 }
4240
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004241 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004242
Andreas Gampea5b09a62016-11-17 15:21:22 -08004243 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004244 const DexFile& GetDexFile() const { return dex_file_; }
4245
4246 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004247 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004248
Mingyao Yang062157f2016-03-02 10:15:36 -08004249 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004250 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004251
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004252 bool NeedsChecks() const {
4253 return entrypoint_ == kQuickAllocObjectWithChecks;
4254 }
David Brazdil6de19382016-01-08 17:37:10 +00004255
Vladimir Markoa1de9182016-02-25 11:37:38 +00004256 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004257
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004258 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004259
4260 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4261
4262 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4263 entrypoint_ = entrypoint;
4264 }
4265
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004266 HLoadClass* GetLoadClass() const {
4267 HInstruction* input = InputAt(0);
4268 if (input->IsClinitCheck()) {
4269 input = input->InputAt(0);
4270 }
4271 DCHECK(input->IsLoadClass());
4272 return input->AsLoadClass();
4273 }
4274
David Brazdil6de19382016-01-08 17:37:10 +00004275 bool IsStringAlloc() const;
4276
4277 DECLARE_INSTRUCTION(NewInstance);
4278
Artem Serovcced8ba2017-07-19 18:18:09 +01004279 protected:
4280 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4281
David Brazdil6de19382016-01-08 17:37:10 +00004282 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004283 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004284 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4285 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4286 "Too many packed fields.");
4287
Andreas Gampea5b09a62016-11-17 15:21:22 -08004288 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004289 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004290 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004291};
4292
Agi Csaki05f20562015-08-19 14:58:14 -07004293enum IntrinsicNeedsEnvironmentOrCache {
4294 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4295 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004296};
4297
Aart Bik5d75afe2015-12-14 11:57:01 -08004298enum IntrinsicSideEffects {
4299 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4300 kReadSideEffects, // Intrinsic may read heap memory.
4301 kWriteSideEffects, // Intrinsic may write heap memory.
4302 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4303};
4304
4305enum IntrinsicExceptions {
4306 kNoThrow, // Intrinsic does not throw any exceptions.
4307 kCanThrow // Intrinsic may throw exceptions.
4308};
4309
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004310class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004311 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004312 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004313
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004314 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004315 SetRawInputAt(index, argument);
4316 }
4317
Roland Levillain3e3d7332015-04-28 11:00:54 +01004318 // Return the number of arguments. This number can be lower than
4319 // the number of inputs returned by InputCount(), as some invoke
4320 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4321 // inputs at the end of their list of inputs.
4322 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4323
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004324 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4325
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004326 InvokeType GetInvokeType() const {
4327 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004328 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004329
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004330 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004331 return intrinsic_;
4332 }
4333
Aart Bik5d75afe2015-12-14 11:57:01 -08004334 void SetIntrinsic(Intrinsics intrinsic,
4335 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4336 IntrinsicSideEffects side_effects,
4337 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004338
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004339 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004340 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004341 }
4342
Aart Bikff7d89c2016-11-07 08:49:28 -08004343 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4344
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004345 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004346
Aart Bika8b8e9b2018-01-09 11:01:02 -08004347 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4348
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004349 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004350
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004351 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004352
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004353 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004354 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4355 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004356
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004357 uint32_t* GetIntrinsicOptimizations() {
4358 return &intrinsic_optimizations_;
4359 }
4360
4361 const uint32_t* GetIntrinsicOptimizations() const {
4362 return &intrinsic_optimizations_;
4363 }
4364
4365 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4366
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004367 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004368 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004369
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004370 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004371
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004372 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004373 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4374 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004375 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004376 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004377 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4378 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004379 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004380 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004381
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304382 HInvoke(InstructionKind kind,
4383 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004384 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004385 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004386 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004387 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004388 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004389 ArtMethod* resolved_method,
4390 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004391 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304392 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004393 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004394 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4395 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004396 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004397 number_of_arguments + number_of_other_inputs,
4398 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004399 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004400 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004401 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004402 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004403 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004404 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004405 // Check mutator lock, constructors lack annotalysis support.
4406 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4407 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004408 }
4409
Artem Serovcced8ba2017-07-19 18:18:09 +01004410 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4411
Roland Levillain3e3d7332015-04-28 11:00:54 +01004412 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004413 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004414 const uint32_t dex_method_index_;
4415 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004416
4417 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4418 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004419};
4420
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004421class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004422 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004423 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004424 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004425 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004426 uint32_t dex_pc,
4427 uint32_t dex_method_index,
4428 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304429 : HInvoke(kInvokeUnresolved,
4430 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004431 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004432 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004433 return_type,
4434 dex_pc,
4435 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004436 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004437 invoke_type) {
4438 }
4439
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004440 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004441
Calin Juravle175dc732015-08-25 15:42:32 +01004442 DECLARE_INSTRUCTION(InvokeUnresolved);
4443
Artem Serovcced8ba2017-07-19 18:18:09 +01004444 protected:
4445 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004446};
4447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004448class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004449 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004450 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004451 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004452 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004453 uint32_t dex_pc,
4454 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304455 : HInvoke(kInvokePolymorphic,
4456 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004457 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004458 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004459 return_type,
4460 dex_pc,
4461 dex_method_index,
4462 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304463 kVirtual) {
4464 }
Orion Hodsonac141392017-01-13 11:53:47 +00004465
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004466 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004467
Orion Hodsonac141392017-01-13 11:53:47 +00004468 DECLARE_INSTRUCTION(InvokePolymorphic);
4469
Artem Serovcced8ba2017-07-19 18:18:09 +01004470 protected:
4471 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004472};
4473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004474class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004475 public:
4476 HInvokeCustom(ArenaAllocator* allocator,
4477 uint32_t number_of_arguments,
4478 uint32_t call_site_index,
4479 DataType::Type return_type,
4480 uint32_t dex_pc)
4481 : HInvoke(kInvokeCustom,
4482 allocator,
4483 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004484 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004485 return_type,
4486 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004487 /* dex_method_index= */ dex::kDexNoIndex,
4488 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004489 kStatic),
4490 call_site_index_(call_site_index) {
4491 }
4492
4493 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4494
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004495 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004496
4497 DECLARE_INSTRUCTION(InvokeCustom);
4498
4499 protected:
4500 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4501
4502 private:
4503 uint32_t call_site_index_;
4504};
4505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004506class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004507 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004508 // Requirements of this method call regarding the class
4509 // initialization (clinit) check of its declaring class.
4510 enum class ClinitCheckRequirement {
4511 kNone, // Class already initialized.
4512 kExplicit, // Static call having explicit clinit check as last input.
4513 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004514 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004515 };
4516
Vladimir Marko58155012015-08-19 12:49:41 +00004517 // Determines how to load the target ArtMethod*.
4518 enum class MethodLoadKind {
4519 // Use a String init ArtMethod* loaded from Thread entrypoints.
4520 kStringInit,
4521
4522 // Use the method's own ArtMethod* loaded by the register allocator.
4523 kRecursive,
4524
Vladimir Marko65979462017-05-19 17:25:12 +01004525 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4526 // Used for boot image methods referenced by boot image code.
4527 kBootImageLinkTimePcRelative,
4528
Vladimir Markob066d432018-01-03 13:14:37 +00004529 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4530 // Used for app->boot calls with relocatable image.
4531 kBootImageRelRo,
4532
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004533 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004534 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004535 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004536
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004537 // Use ArtMethod* at a known address, embed the direct address in the code.
4538 // Used for for JIT-compiled calls.
4539 kJitDirectAddress,
4540
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004541 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4542 // used when other kinds are unimplemented on a particular architecture.
4543 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004544 };
4545
4546 // Determines the location of the code pointer.
4547 enum class CodePtrLocation {
4548 // Recursive call, use local PC-relative call instruction.
4549 kCallSelf,
4550
Vladimir Marko58155012015-08-19 12:49:41 +00004551 // Use code pointer from the ArtMethod*.
4552 // Used when we don't know the target code. This is also the last-resort-kind used when
4553 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4554 kCallArtMethod,
4555 };
4556
4557 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004558 MethodLoadKind method_load_kind;
4559 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004560 // The method load data holds
4561 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4562 // Note that there are multiple string init methods, each having its own offset.
4563 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004564 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004565 };
4566
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004567 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004568 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004569 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004570 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004571 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004572 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004573 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004574 InvokeType invoke_type,
4575 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004576 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304577 : HInvoke(kInvokeStaticOrDirect,
4578 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004579 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004580 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004581 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004582 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004583 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004584 return_type,
4585 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004586 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004587 resolved_method,
4588 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004589 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004590 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004591 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4592 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004593
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004594 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004595
Vladimir Markodc151b22015-10-15 18:02:30 +01004596 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004597 bool had_current_method_input = HasCurrentMethodInput();
4598 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4599
4600 // Using the current method is the default and once we find a better
4601 // method load kind, we should not go back to using the current method.
4602 DCHECK(had_current_method_input || !needs_current_method_input);
4603
4604 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004605 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4606 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004607 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004608 dispatch_info_ = dispatch_info;
4609 }
4610
Aart Bik6daebeb2017-04-03 14:35:41 -07004611 DispatchInfo GetDispatchInfo() const {
4612 return dispatch_info_;
4613 }
4614
Vladimir Markoc53c0792015-11-19 15:48:33 +00004615 void AddSpecialInput(HInstruction* input) {
4616 // We allow only one special input.
4617 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4618 DCHECK(InputCount() == GetSpecialInputIndex() ||
4619 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4620 InsertInputAt(GetSpecialInputIndex(), input);
4621 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004622
Vladimir Marko372f10e2016-05-17 16:30:10 +01004623 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004624 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004625 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4626 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4627 DCHECK(!input_records.empty());
4628 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4629 HInstruction* last_input = input_records.back().GetInstruction();
4630 // Note: `last_input` may be null during arguments setup.
4631 if (last_input != nullptr) {
4632 // `last_input` is the last input of a static invoke marked as having
4633 // an explicit clinit check. It must either be:
4634 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4635 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4636 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4637 }
4638 }
4639 return input_records;
4640 }
4641
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004642 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004643 // We access the method via the dex cache so we can't do an implicit null check.
4644 // TODO: for intrinsics we can generate implicit null checks.
4645 return false;
4646 }
4647
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004648 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004649 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004650 }
4651
Vladimir Markoc53c0792015-11-19 15:48:33 +00004652 // Get the index of the special input, if any.
4653 //
David Brazdil6de19382016-01-08 17:37:10 +00004654 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4655 // method pointer; otherwise there may be one platform-specific special input,
4656 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004657 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004658 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004659
Vladimir Marko58155012015-08-19 12:49:41 +00004660 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4661 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4662 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004663 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004664 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004665 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004666 bool HasPcRelativeMethodLoadKind() const {
4667 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004668 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004669 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004670 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004671 bool HasCurrentMethodInput() const {
4672 // This function can be called only after the invoke has been fully initialized by the builder.
4673 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004674 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004675 return true;
4676 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004677 DCHECK(InputCount() == GetSpecialInputIndex() ||
4678 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004679 return false;
4680 }
4681 }
Vladimir Marko58155012015-08-19 12:49:41 +00004682
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004683 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004684 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004685 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004686 }
4687
4688 uint64_t GetMethodAddress() const {
4689 DCHECK(HasMethodAddress());
4690 return dispatch_info_.method_load_data;
4691 }
4692
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004693 const DexFile& GetDexFileForPcRelativeDexCache() const;
4694
Vladimir Markoa1de9182016-02-25 11:37:38 +00004695 ClinitCheckRequirement GetClinitCheckRequirement() const {
4696 return GetPackedField<ClinitCheckRequirementField>();
4697 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004698
Roland Levillain4c0eb422015-04-24 16:43:49 +01004699 // Is this instruction a call to a static method?
4700 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004701 return GetInvokeType() == kStatic;
4702 }
4703
4704 MethodReference GetTargetMethod() const {
4705 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004706 }
4707
Vladimir Markofbb184a2015-11-13 14:47:00 +00004708 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4709 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4710 // instruction; only relevant for static calls with explicit clinit check.
4711 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004712 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004713 size_t last_input_index = inputs_.size() - 1u;
4714 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004715 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004716 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004717 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004718 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004719 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004720 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004721 }
4722
4723 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004724 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004725 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004726 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004727 }
4728
4729 // Is this a call to a static method whose declaring class has an
4730 // implicit intialization check requirement?
4731 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004732 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004733 }
4734
Vladimir Markob554b5a2015-11-06 12:57:55 +00004735 // Does this method load kind need the current method as an input?
4736 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004737 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004738 }
4739
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004740 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004741
Artem Serovcced8ba2017-07-19 18:18:09 +01004742 protected:
4743 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4744
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004745 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004746 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004747 static constexpr size_t kFieldClinitCheckRequirementSize =
4748 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4749 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4750 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4751 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4752 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004753 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4754 kFieldClinitCheckRequirement,
4755 kFieldClinitCheckRequirementSize>;
4756
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004757 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004758 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004759 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004760};
Vladimir Markof64242a2015-12-01 14:58:23 +00004761std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004762std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004763
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004764class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004765 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004766 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004767 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004768 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004769 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004770 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004771 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004772 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304773 : HInvoke(kInvokeVirtual,
4774 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004775 number_of_arguments,
4776 0u,
4777 return_type,
4778 dex_pc,
4779 dex_method_index,
4780 resolved_method,
4781 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304782 vtable_index_(vtable_index) {
4783 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004785 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004786
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004787 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004788 switch (GetIntrinsic()) {
4789 case Intrinsics::kThreadCurrentThread:
4790 case Intrinsics::kStringBufferAppend:
4791 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004792 case Intrinsics::kStringBuilderAppendObject:
4793 case Intrinsics::kStringBuilderAppendString:
4794 case Intrinsics::kStringBuilderAppendCharSequence:
4795 case Intrinsics::kStringBuilderAppendCharArray:
4796 case Intrinsics::kStringBuilderAppendBoolean:
4797 case Intrinsics::kStringBuilderAppendChar:
4798 case Intrinsics::kStringBuilderAppendInt:
4799 case Intrinsics::kStringBuilderAppendLong:
4800 case Intrinsics::kStringBuilderAppendFloat:
4801 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004802 case Intrinsics::kStringBuilderToString:
4803 return false;
4804 default:
4805 return HInvoke::CanBeNull();
4806 }
4807 }
4808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004809 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004810 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004811 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004812 }
4813
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004814 uint32_t GetVTableIndex() const { return vtable_index_; }
4815
4816 DECLARE_INSTRUCTION(InvokeVirtual);
4817
Artem Serovcced8ba2017-07-19 18:18:09 +01004818 protected:
4819 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4820
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004821 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004822 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004823 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004824};
4825
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004826class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004827 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004828 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004829 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004830 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004831 uint32_t dex_pc,
4832 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004833 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004834 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304835 : HInvoke(kInvokeInterface,
4836 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004837 number_of_arguments,
4838 0u,
4839 return_type,
4840 dex_pc,
4841 dex_method_index,
4842 resolved_method,
4843 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304844 imt_index_(imt_index) {
4845 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004847 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004848
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004849 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004850 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004851 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004852 }
4853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004854 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004855 // The assembly stub currently needs it.
4856 return true;
4857 }
4858
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004859 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004860
4861 DECLARE_INSTRUCTION(InvokeInterface);
4862
Artem Serovcced8ba2017-07-19 18:18:09 +01004863 protected:
4864 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4865
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004866 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004867 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004868 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004869};
4870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004871class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004872 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004873 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304874 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004875 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004876 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004877
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004878 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004879
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004880 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004881 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004882 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004883 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004884 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004885 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004886 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004887 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4888 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004889 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004890 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4891 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004892
Roland Levillain88cb1752014-10-20 16:36:47 +01004893 DECLARE_INSTRUCTION(Neg);
4894
Artem Serovcced8ba2017-07-19 18:18:09 +01004895 protected:
4896 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004897};
4898
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004899class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004900 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004901 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304902 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004903 SetRawInputAt(0, cls);
4904 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004905 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004906 }
4907
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004908 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004909
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004910 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004911 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004912
Mingyao Yang0c365e62015-03-31 15:09:29 -07004913 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004914 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004916 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004917
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004918 HLoadClass* GetLoadClass() const {
4919 DCHECK(InputAt(0)->IsLoadClass());
4920 return InputAt(0)->AsLoadClass();
4921 }
4922
4923 HInstruction* GetLength() const {
4924 return InputAt(1);
4925 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004926
Vladimir Markob5461632018-10-15 14:24:21 +01004927 size_t GetComponentSizeShift() {
4928 return GetPackedField<ComponentSizeShiftField>();
4929 }
4930
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004931 DECLARE_INSTRUCTION(NewArray);
4932
Artem Serovcced8ba2017-07-19 18:18:09 +01004933 protected:
4934 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004935
4936 private:
4937 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4938 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4939 static constexpr size_t kNumberOfNewArrayPackedBits =
4940 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4941 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4942 using ComponentSizeShiftField =
4943 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004944};
4945
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004946class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004947 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004948 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004949 HInstruction* left,
4950 HInstruction* right,
4951 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304952 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4953 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004955 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004956
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004957 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004958
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004959 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004960 return GetBlock()->GetGraph()->GetIntConstant(
4961 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004962 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004963 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004964 return GetBlock()->GetGraph()->GetLongConstant(
4965 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004966 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004967 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004968 return GetBlock()->GetGraph()->GetFloatConstant(
4969 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4970 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004971 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004972 return GetBlock()->GetGraph()->GetDoubleConstant(
4973 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4974 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004975
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004976 DECLARE_INSTRUCTION(Add);
4977
Artem Serovcced8ba2017-07-19 18:18:09 +01004978 protected:
4979 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004980};
4981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004982class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004983 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004984 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004985 HInstruction* left,
4986 HInstruction* right,
4987 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304988 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4989 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004990
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004991 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004992
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004993 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 return GetBlock()->GetGraph()->GetIntConstant(
4995 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004996 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004997 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004998 return GetBlock()->GetGraph()->GetLongConstant(
4999 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005000 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005001 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005002 return GetBlock()->GetGraph()->GetFloatConstant(
5003 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5004 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005005 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005006 return GetBlock()->GetGraph()->GetDoubleConstant(
5007 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5008 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005009
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005010 DECLARE_INSTRUCTION(Sub);
5011
Artem Serovcced8ba2017-07-19 18:18:09 +01005012 protected:
5013 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005014};
5015
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005016class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005017 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005018 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005019 HInstruction* left,
5020 HInstruction* right,
5021 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305022 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5023 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005024
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005025 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005026
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005027 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005029 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005030 return GetBlock()->GetGraph()->GetIntConstant(
5031 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005032 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005033 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005034 return GetBlock()->GetGraph()->GetLongConstant(
5035 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005036 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005037 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005038 return GetBlock()->GetGraph()->GetFloatConstant(
5039 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5040 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005041 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005042 return GetBlock()->GetGraph()->GetDoubleConstant(
5043 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5044 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005045
5046 DECLARE_INSTRUCTION(Mul);
5047
Artem Serovcced8ba2017-07-19 18:18:09 +01005048 protected:
5049 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005050};
5051
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005052class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005053 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005054 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005055 HInstruction* left,
5056 HInstruction* right,
5057 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305058 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5059 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005060
Roland Levillain9867bc72015-08-05 10:21:34 +01005061 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005062 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005063 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005064 // Our graph structure ensures we never have 0 for `y` during
5065 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005066 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005067 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005068 return (y == -1) ? -x : x / y;
5069 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005070
Roland Levillain31dd3d62016-02-16 12:21:02 +00005071 template <typename T>
5072 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005073 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005074 return x / y;
5075 }
5076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005077 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005078 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005079 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005080 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005081 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005082 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005083 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5084 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005085 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005086 return GetBlock()->GetGraph()->GetFloatConstant(
5087 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5088 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005089 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005090 return GetBlock()->GetGraph()->GetDoubleConstant(
5091 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005092 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005093
5094 DECLARE_INSTRUCTION(Div);
5095
Artem Serovcced8ba2017-07-19 18:18:09 +01005096 protected:
5097 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005098};
5099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005100class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005101 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005102 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005103 HInstruction* left,
5104 HInstruction* right,
5105 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305106 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5107 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005108
Roland Levillain9867bc72015-08-05 10:21:34 +01005109 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005110 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005111 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005112 // Our graph structure ensures we never have 0 for `y` during
5113 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005114 DCHECK_NE(y, 0);
5115 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5116 return (y == -1) ? 0 : x % y;
5117 }
5118
Roland Levillain31dd3d62016-02-16 12:21:02 +00005119 template <typename T>
5120 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005121 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005122 return std::fmod(x, y);
5123 }
5124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005125 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005126 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005127 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005128 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005129 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005130 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005131 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005132 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005133 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005134 return GetBlock()->GetGraph()->GetFloatConstant(
5135 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5136 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005137 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005138 return GetBlock()->GetGraph()->GetDoubleConstant(
5139 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5140 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005141
5142 DECLARE_INSTRUCTION(Rem);
5143
Artem Serovcced8ba2017-07-19 18:18:09 +01005144 protected:
5145 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005146};
5147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005148class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005149 public:
5150 HMin(DataType::Type result_type,
5151 HInstruction* left,
5152 HInstruction* right,
5153 uint32_t dex_pc)
5154 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5155
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005156 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005157
5158 // Evaluation for integral values.
5159 template <typename T> static T ComputeIntegral(T x, T y) {
5160 return (x <= y) ? x : y;
5161 }
5162
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005163 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005164 return GetBlock()->GetGraph()->GetIntConstant(
5165 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5166 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005167 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005168 return GetBlock()->GetGraph()->GetLongConstant(
5169 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5170 }
5171 // TODO: Evaluation for floating-point values.
5172 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005173 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005174 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005175 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005176
5177 DECLARE_INSTRUCTION(Min);
5178
5179 protected:
5180 DEFAULT_COPY_CONSTRUCTOR(Min);
5181};
5182
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005183class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005184 public:
5185 HMax(DataType::Type result_type,
5186 HInstruction* left,
5187 HInstruction* right,
5188 uint32_t dex_pc)
5189 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5190
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005191 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005192
5193 // Evaluation for integral values.
5194 template <typename T> static T ComputeIntegral(T x, T y) {
5195 return (x >= y) ? x : y;
5196 }
5197
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005198 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005199 return GetBlock()->GetGraph()->GetIntConstant(
5200 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5201 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005202 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005203 return GetBlock()->GetGraph()->GetLongConstant(
5204 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5205 }
5206 // TODO: Evaluation for floating-point values.
5207 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005208 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005209 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005210 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005211
5212 DECLARE_INSTRUCTION(Max);
5213
5214 protected:
5215 DEFAULT_COPY_CONSTRUCTOR(Max);
5216};
5217
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005218class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005219 public:
5220 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5221 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5222
5223 // Evaluation for integral values.
5224 template <typename T> static T ComputeIntegral(T x) {
5225 return x < 0 ? -x : x;
5226 }
5227
5228 // Evaluation for floating-point values.
5229 // Note, as a "quality of implementation", rather than pure "spec compliance",
5230 // we require that Math.abs() clears the sign bit (but changes nothing else)
5231 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5232 // http://b/30758343
5233 template <typename T, typename S> static T ComputeFP(T x) {
5234 S bits = bit_cast<S, T>(x);
5235 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5236 }
5237
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005238 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005239 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5240 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005241 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005242 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5243 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005244 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005245 return GetBlock()->GetGraph()->GetFloatConstant(
5246 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5247 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005248 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005249 return GetBlock()->GetGraph()->GetDoubleConstant(
5250 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5251 }
5252
5253 DECLARE_INSTRUCTION(Abs);
5254
5255 protected:
5256 DEFAULT_COPY_CONSTRUCTOR(Abs);
5257};
5258
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005259class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005260 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005261 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005262 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5263 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005264 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005265 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005266 SetRawInputAt(0, value);
5267 }
5268
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005269 bool IsClonable() const override { return true; }
5270 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005271
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005272 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005273 return true;
5274 }
5275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005276 bool NeedsEnvironment() const override { return true; }
5277 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005278
Calin Juravled0d48522014-11-04 16:40:20 +00005279 DECLARE_INSTRUCTION(DivZeroCheck);
5280
Artem Serovcced8ba2017-07-19 18:18:09 +01005281 protected:
5282 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005283};
5284
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005285class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005286 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005287 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005288 HInstruction* value,
5289 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005290 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305291 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005292 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5293 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005294 }
5295
Roland Levillain5b5b9312016-03-22 14:57:31 +00005296 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005297 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005298 return value << (distance & max_shift_distance);
5299 }
5300
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005301 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005302 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005303 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005304 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005305 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005306 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005307 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005308 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005309 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005310 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005311 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5312 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005313 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005314 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005315 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005316 LOG(FATAL) << DebugName() << " is not defined for float values";
5317 UNREACHABLE();
5318 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005319 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005320 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005321 LOG(FATAL) << DebugName() << " is not defined for double values";
5322 UNREACHABLE();
5323 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005324
5325 DECLARE_INSTRUCTION(Shl);
5326
Artem Serovcced8ba2017-07-19 18:18:09 +01005327 protected:
5328 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005329};
5330
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005331class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005332 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005333 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005334 HInstruction* value,
5335 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005336 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305337 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005338 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5339 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005340 }
5341
Roland Levillain5b5b9312016-03-22 14:57:31 +00005342 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005343 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005344 return value >> (distance & max_shift_distance);
5345 }
5346
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005347 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005348 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005349 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005350 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005351 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005352 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005353 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005354 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005355 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005356 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005357 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5358 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005359 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005360 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005361 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005362 LOG(FATAL) << DebugName() << " is not defined for float values";
5363 UNREACHABLE();
5364 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005365 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005366 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005367 LOG(FATAL) << DebugName() << " is not defined for double values";
5368 UNREACHABLE();
5369 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005370
5371 DECLARE_INSTRUCTION(Shr);
5372
Artem Serovcced8ba2017-07-19 18:18:09 +01005373 protected:
5374 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005375};
5376
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005377class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005378 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005379 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005380 HInstruction* value,
5381 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005382 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305383 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005384 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5385 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005386 }
5387
Roland Levillain5b5b9312016-03-22 14:57:31 +00005388 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005389 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005390 typedef typename std::make_unsigned<T>::type V;
5391 V ux = static_cast<V>(value);
5392 return static_cast<T>(ux >> (distance & max_shift_distance));
5393 }
5394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005395 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005396 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005397 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005398 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005399 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005400 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005401 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005402 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005403 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005404 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005405 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5406 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005407 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005408 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005409 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005410 LOG(FATAL) << DebugName() << " is not defined for float values";
5411 UNREACHABLE();
5412 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005413 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005414 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005415 LOG(FATAL) << DebugName() << " is not defined for double values";
5416 UNREACHABLE();
5417 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005418
5419 DECLARE_INSTRUCTION(UShr);
5420
Artem Serovcced8ba2017-07-19 18:18:09 +01005421 protected:
5422 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005423};
5424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005425class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005426 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005427 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005428 HInstruction* left,
5429 HInstruction* right,
5430 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305431 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5432 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005434 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005435
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005436 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005437
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005438 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005439 return GetBlock()->GetGraph()->GetIntConstant(
5440 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005441 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005442 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005443 return GetBlock()->GetGraph()->GetLongConstant(
5444 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005445 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005446 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005447 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005448 LOG(FATAL) << DebugName() << " is not defined for float values";
5449 UNREACHABLE();
5450 }
5451 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005452 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005453 LOG(FATAL) << DebugName() << " is not defined for double values";
5454 UNREACHABLE();
5455 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005456
5457 DECLARE_INSTRUCTION(And);
5458
Artem Serovcced8ba2017-07-19 18:18:09 +01005459 protected:
5460 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005461};
5462
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005463class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005464 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005465 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005466 HInstruction* left,
5467 HInstruction* right,
5468 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305469 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5470 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005471
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005472 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005473
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005474 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005475
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005476 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005477 return GetBlock()->GetGraph()->GetIntConstant(
5478 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005479 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005480 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005481 return GetBlock()->GetGraph()->GetLongConstant(
5482 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005483 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005484 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005485 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005486 LOG(FATAL) << DebugName() << " is not defined for float values";
5487 UNREACHABLE();
5488 }
5489 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005490 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005491 LOG(FATAL) << DebugName() << " is not defined for double values";
5492 UNREACHABLE();
5493 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005494
5495 DECLARE_INSTRUCTION(Or);
5496
Artem Serovcced8ba2017-07-19 18:18:09 +01005497 protected:
5498 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005499};
5500
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005501class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005502 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005503 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005504 HInstruction* left,
5505 HInstruction* right,
5506 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305507 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5508 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005509
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005510 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005511
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005512 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005513
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005514 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005515 return GetBlock()->GetGraph()->GetIntConstant(
5516 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005517 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005518 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005519 return GetBlock()->GetGraph()->GetLongConstant(
5520 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005521 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005522 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005523 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005524 LOG(FATAL) << DebugName() << " is not defined for float values";
5525 UNREACHABLE();
5526 }
5527 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005528 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005529 LOG(FATAL) << DebugName() << " is not defined for double values";
5530 UNREACHABLE();
5531 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005532
5533 DECLARE_INSTRUCTION(Xor);
5534
Artem Serovcced8ba2017-07-19 18:18:09 +01005535 protected:
5536 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005537};
5538
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005539class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005540 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005541 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305542 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005543 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005544
Roland Levillain5b5b9312016-03-22 14:57:31 +00005545 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005546 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005547 typedef typename std::make_unsigned<T>::type V;
5548 V ux = static_cast<V>(value);
5549 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005550 return static_cast<T>(ux);
5551 } else {
5552 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005553 return static_cast<T>(ux >> (distance & max_shift_value)) |
5554 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005555 }
5556 }
5557
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005558 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005559 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005560 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005561 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005562 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005563 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005564 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005565 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005566 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005567 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005568 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5569 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005570 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005571 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005572 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005573 LOG(FATAL) << DebugName() << " is not defined for float values";
5574 UNREACHABLE();
5575 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005576 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005577 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005578 LOG(FATAL) << DebugName() << " is not defined for double values";
5579 UNREACHABLE();
5580 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005581
5582 DECLARE_INSTRUCTION(Ror);
5583
Artem Serovcced8ba2017-07-19 18:18:09 +01005584 protected:
5585 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005586};
5587
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005588// The value of a parameter in this method. Its location depends on
5589// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005590class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005591 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005592 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005593 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005594 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005595 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005596 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305597 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005598 dex_file_(dex_file),
5599 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005600 index_(index) {
5601 SetPackedFlag<kFlagIsThis>(is_this);
5602 SetPackedFlag<kFlagCanBeNull>(!is_this);
5603 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005604
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005605 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005606 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005607 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005608 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005609
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005610 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005611 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005612
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005613 DECLARE_INSTRUCTION(ParameterValue);
5614
Artem Serovcced8ba2017-07-19 18:18:09 +01005615 protected:
5616 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5617
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005618 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005619 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005620 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005621 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5622 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5623 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5624 "Too many packed fields.");
5625
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005626 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005627 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005628 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005629 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005630 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005631};
5632
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005633class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005634 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005635 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305636 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5637 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005638
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005639 bool CanBeMoved() const override { return true; }
5640 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005641 return true;
5642 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005643
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005644 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005645
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005646 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005647 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005648 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005649 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005650 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005651 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005652 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005653 LOG(FATAL) << DebugName() << " is not defined for float values";
5654 UNREACHABLE();
5655 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005656 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005657 LOG(FATAL) << DebugName() << " is not defined for double values";
5658 UNREACHABLE();
5659 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005660
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005661 DECLARE_INSTRUCTION(Not);
5662
Artem Serovcced8ba2017-07-19 18:18:09 +01005663 protected:
5664 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005665};
5666
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005667class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005669 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305670 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5671 }
David Brazdil66d126e2015-04-03 16:02:44 +01005672
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005673 bool CanBeMoved() const override { return true; }
5674 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005675 return true;
5676 }
5677
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005678 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005679 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005680 return !x;
5681 }
5682
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005683 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005684 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005685 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005686 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005687 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005688 UNREACHABLE();
5689 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005690 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005691 LOG(FATAL) << DebugName() << " is not defined for float values";
5692 UNREACHABLE();
5693 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005694 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005695 LOG(FATAL) << DebugName() << " is not defined for double values";
5696 UNREACHABLE();
5697 }
David Brazdil66d126e2015-04-03 16:02:44 +01005698
5699 DECLARE_INSTRUCTION(BooleanNot);
5700
Artem Serovcced8ba2017-07-19 18:18:09 +01005701 protected:
5702 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005703};
5704
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005705class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005706 public:
5707 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005708 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305709 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005710 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005711 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005712 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005713 }
5714
5715 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005716 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5717 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005718
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005719 bool IsClonable() const override { return true; }
5720 bool CanBeMoved() const override { return true; }
5721 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005722 return true;
5723 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005724 // Return whether the conversion is implicit. This includes conversion to the same type.
5725 bool IsImplicitConversion() const {
5726 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5727 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005728
Mark Mendelle82549b2015-05-06 10:55:34 -04005729 // Try to statically evaluate the conversion and return a HConstant
5730 // containing the result. If the input cannot be converted, return nullptr.
5731 HConstant* TryStaticEvaluation() const;
5732
Roland Levillaindff1f282014-11-05 14:15:05 +00005733 DECLARE_INSTRUCTION(TypeConversion);
5734
Artem Serovcced8ba2017-07-19 18:18:09 +01005735 protected:
5736 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005737};
5738
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005739static constexpr uint32_t kNoRegNumber = -1;
5740
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005741class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005742 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005743 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005744 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5745 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005746 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005747 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005748 SetRawInputAt(0, value);
5749 }
5750
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005751 bool IsClonable() const override { return true; }
5752 bool CanBeMoved() const override { return true; }
5753 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005754 return true;
5755 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005757 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005759 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005760
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005761 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005762
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005763 DECLARE_INSTRUCTION(NullCheck);
5764
Artem Serovcced8ba2017-07-19 18:18:09 +01005765 protected:
5766 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005767};
5768
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005769// Embeds an ArtField and all the information required by the compiler. We cache
5770// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005771class FieldInfo : public ValueObject {
5772 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005773 FieldInfo(ArtField* field,
5774 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005776 bool is_volatile,
5777 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005778 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005779 const DexFile& dex_file)
5780 : field_(field),
5781 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005782 field_type_(field_type),
5783 is_volatile_(is_volatile),
5784 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005785 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005786 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005787
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005788 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005789 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005790 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005791 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005792 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005793 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005794 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005795
5796 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005797 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005798 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005799 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005800 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005801 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005802 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005803 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005804};
5805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005806class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005807 public:
5808 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005809 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005810 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005811 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005812 bool is_volatile,
5813 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005814 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005815 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005816 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305817 : HExpression(kInstanceFieldGet,
5818 field_type,
5819 SideEffects::FieldReadOfType(field_type, is_volatile),
5820 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005821 field_info_(field,
5822 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005823 field_type,
5824 is_volatile,
5825 field_idx,
5826 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005827 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005828 SetRawInputAt(0, value);
5829 }
5830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005831 bool IsClonable() const override { return true; }
5832 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005833
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005834 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005835 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005836 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005837 }
5838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005839 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005840 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005841 }
5842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005843 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005844 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5845 }
5846
Calin Juravle52c48962014-12-16 17:02:57 +00005847 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005848 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005849 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005850 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005851
Vladimir Marko61b92282017-10-11 13:23:17 +01005852 void SetType(DataType::Type new_type) {
5853 DCHECK(DataType::IsIntegralType(GetType()));
5854 DCHECK(DataType::IsIntegralType(new_type));
5855 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5856 SetPackedField<TypeField>(new_type);
5857 }
5858
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005859 DECLARE_INSTRUCTION(InstanceFieldGet);
5860
Artem Serovcced8ba2017-07-19 18:18:09 +01005861 protected:
5862 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5863
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005864 private:
5865 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005866};
5867
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005868class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005869 public:
5870 HInstanceFieldSet(HInstruction* object,
5871 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005872 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005873 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005874 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005875 bool is_volatile,
5876 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005877 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005878 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005879 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005880 : HExpression(kInstanceFieldSet,
5881 SideEffects::FieldWriteOfType(field_type, is_volatile),
5882 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005883 field_info_(field,
5884 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005885 field_type,
5886 is_volatile,
5887 field_idx,
5888 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005889 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005890 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005891 SetRawInputAt(0, object);
5892 SetRawInputAt(1, value);
5893 }
5894
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005895 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005897 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005898 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005899 }
5900
Calin Juravle52c48962014-12-16 17:02:57 +00005901 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005902 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005903 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005904 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005905 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005906 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5907 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005908
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005909 DECLARE_INSTRUCTION(InstanceFieldSet);
5910
Artem Serovcced8ba2017-07-19 18:18:09 +01005911 protected:
5912 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5913
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005914 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005915 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5916 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5917 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5918 "Too many packed fields.");
5919
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005920 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005921};
5922
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005923class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005924 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005925 HArrayGet(HInstruction* array,
5926 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005927 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005928 uint32_t dex_pc)
5929 : HArrayGet(array,
5930 index,
5931 type,
5932 SideEffects::ArrayReadOfType(type),
5933 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005934 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305935 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005936
5937 HArrayGet(HInstruction* array,
5938 HInstruction* index,
5939 DataType::Type type,
5940 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005941 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005942 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305943 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005944 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005945 SetRawInputAt(0, array);
5946 SetRawInputAt(1, index);
5947 }
5948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005949 bool IsClonable() const override { return true; }
5950 bool CanBeMoved() const override { return true; }
5951 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005952 return true;
5953 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005954 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005955 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005956 // Currently, unless the array is the result of NewArray, the array access is always
5957 // preceded by some form of null NullCheck necessary for the bounds check, usually
5958 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5959 // dynamic BCE. There are cases when these could be removed to produce better code.
5960 // If we ever add optimizations to do so we should allow an implicit check here
5961 // (as long as the address falls in the first page).
5962 //
5963 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5964 // a = cond ? new int[1] : null;
5965 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005966 return false;
5967 }
5968
David Brazdil4833f5a2015-12-16 10:37:39 +00005969 bool IsEquivalentOf(HArrayGet* other) const {
5970 bool result = (GetDexPc() == other->GetDexPc());
5971 if (kIsDebugBuild && result) {
5972 DCHECK_EQ(GetBlock(), other->GetBlock());
5973 DCHECK_EQ(GetArray(), other->GetArray());
5974 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005975 if (DataType::IsIntOrLongType(GetType())) {
5976 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005977 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005978 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5979 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005980 }
5981 }
5982 return result;
5983 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005984
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005985 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5986
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005987 HInstruction* GetArray() const { return InputAt(0); }
5988 HInstruction* GetIndex() const { return InputAt(1); }
5989
Vladimir Marko61b92282017-10-11 13:23:17 +01005990 void SetType(DataType::Type new_type) {
5991 DCHECK(DataType::IsIntegralType(GetType()));
5992 DCHECK(DataType::IsIntegralType(new_type));
5993 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5994 SetPackedField<TypeField>(new_type);
5995 }
5996
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005997 DECLARE_INSTRUCTION(ArrayGet);
5998
Artem Serovcced8ba2017-07-19 18:18:09 +01005999 protected:
6000 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6001
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006002 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006003 // We treat a String as an array, creating the HArrayGet from String.charAt()
6004 // intrinsic in the instruction simplifier. We can always determine whether
6005 // a particular HArrayGet is actually a String.charAt() by looking at the type
6006 // of the input but that requires holding the mutator lock, so we prefer to use
6007 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006008 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006009 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6010 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6011 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006012};
6013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006014class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006015 public:
6016 HArraySet(HInstruction* array,
6017 HInstruction* index,
6018 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006019 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006020 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006021 : HArraySet(array,
6022 index,
6023 value,
6024 expected_component_type,
6025 // Make a best guess for side effects now, may be refined during SSA building.
6026 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306027 dex_pc) {
6028 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006029
6030 HArraySet(HInstruction* array,
6031 HInstruction* index,
6032 HInstruction* value,
6033 DataType::Type expected_component_type,
6034 SideEffects side_effects,
6035 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006036 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006037 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006038 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006039 SetPackedFlag<kFlagValueCanBeNull>(true);
6040 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006041 SetRawInputAt(0, array);
6042 SetRawInputAt(1, index);
6043 SetRawInputAt(2, value);
6044 }
6045
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006046 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006047
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006048 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006049 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006050 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051 }
6052
Mingyao Yang81014cb2015-06-02 03:16:27 -07006053 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006054 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006056 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006057 // TODO: Same as for ArrayGet.
6058 return false;
6059 }
6060
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006061 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006062 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006063 }
6064
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006065 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006066 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006067 }
6068
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006069 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006071 }
6072
Vladimir Markoa1de9182016-02-25 11:37:38 +00006073 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6074 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6075 bool StaticTypeOfArrayIsObjectArray() const {
6076 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6077 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006078
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006079 HInstruction* GetArray() const { return InputAt(0); }
6080 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006081 HInstruction* GetValue() const { return InputAt(2); }
6082
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006083 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006084 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6085 }
6086
6087 static DataType::Type GetComponentType(DataType::Type value_type,
6088 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006089 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006090 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006091 // be correct, we also check what is the value type. If it is a floating
6092 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006093 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006094 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006095 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006096 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006098 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006099 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006100 }
6101
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006102 static SideEffects ComputeSideEffects(DataType::Type type) {
6103 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006104 }
6105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006106 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6107 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6108 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006109 }
6110
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006111 DECLARE_INSTRUCTION(ArraySet);
6112
Artem Serovcced8ba2017-07-19 18:18:09 +01006113 protected:
6114 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6115
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006116 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006117 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6118 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006119 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006120 static constexpr size_t kFlagNeedsTypeCheck =
6121 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6122 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006123 // Cached information for the reference_type_info_ so that codegen
6124 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006125 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6126 static constexpr size_t kNumberOfArraySetPackedBits =
6127 kFlagStaticTypeOfArrayIsObjectArray + 1;
6128 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6129 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006130 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006131};
6132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006133class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006134 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006135 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306136 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006137 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006138 // Note that arrays do not change length, so the instruction does not
6139 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006140 SetRawInputAt(0, array);
6141 }
6142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006143 bool IsClonable() const override { return true; }
6144 bool CanBeMoved() const override { return true; }
6145 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006146 return true;
6147 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006148 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006149 return obj == InputAt(0);
6150 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006151
Vladimir Markodce016e2016-04-28 13:10:02 +01006152 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6153
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006154 DECLARE_INSTRUCTION(ArrayLength);
6155
Artem Serovcced8ba2017-07-19 18:18:09 +01006156 protected:
6157 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6158
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006159 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006160 // We treat a String as an array, creating the HArrayLength from String.length()
6161 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6162 // determine whether a particular HArrayLength is actually a String.length() by
6163 // looking at the type of the input but that requires holding the mutator lock, so
6164 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006165 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006166 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6167 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6168 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006169};
6170
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006171class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006172 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006173 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006174 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6175 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006176 HBoundsCheck(HInstruction* index,
6177 HInstruction* length,
6178 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006179 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006180 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006181 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006182 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006183 SetRawInputAt(0, index);
6184 SetRawInputAt(1, length);
6185 }
6186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006187 bool IsClonable() const override { return true; }
6188 bool CanBeMoved() const override { return true; }
6189 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006190 return true;
6191 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006192
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006193 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006195 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006196
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006197 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006198
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006199 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006200
6201 DECLARE_INSTRUCTION(BoundsCheck);
6202
Artem Serovcced8ba2017-07-19 18:18:09 +01006203 protected:
6204 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6205
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006206 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006207 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006208 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6209 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6210 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006211};
6212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006213class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006214 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006215 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006216 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306217 slow_path_(nullptr) {
6218 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006219
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006220 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006222 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006223 return true;
6224 }
6225
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006226 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6227 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006228
6229 DECLARE_INSTRUCTION(SuspendCheck);
6230
Artem Serovcced8ba2017-07-19 18:18:09 +01006231 protected:
6232 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6233
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006234 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006235 // Only used for code generation, in order to share the same slow path between back edges
6236 // of a same loop.
6237 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006238};
6239
David Srbecky0cf44932015-12-09 14:09:59 +00006240// Pseudo-instruction which provides the native debugger with mapping information.
6241// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006242class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006243 public:
6244 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006245 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306246 }
David Srbecky0cf44932015-12-09 14:09:59 +00006247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006248 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006249 return true;
6250 }
6251
6252 DECLARE_INSTRUCTION(NativeDebugInfo);
6253
Artem Serovcced8ba2017-07-19 18:18:09 +01006254 protected:
6255 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006256};
6257
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006258/**
6259 * Instruction to load a Class object.
6260 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006261class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006262 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006263 // Determines how to load the Class.
6264 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006265 // We cannot load this class. See HSharpening::SharpenLoadClass.
6266 kInvalid = -1,
6267
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006268 // Use the Class* from the method's own ArtMethod*.
6269 kReferrersClass,
6270
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006271 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006272 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006273 kBootImageLinkTimePcRelative,
6274
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006275 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006276 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006277 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006278
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006279 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006280 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006281 kBssEntry,
6282
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006283 // Use a known boot image Class* address, embedded in the code by the codegen.
6284 // Used for boot image classes referenced by apps in JIT-compiled code.
6285 kJitBootImageAddress,
6286
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006287 // Load from the root table associated with the JIT compiled method.
6288 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006289
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006290 // Load using a simple runtime call. This is the fall-back load kind when
6291 // the codegen is unable to use another appropriate kind.
6292 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006293
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006294 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006295 };
6296
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006297 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006298 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006299 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006300 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006301 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006302 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006303 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006304 : HInstruction(kLoadClass,
6305 DataType::Type::kReference,
6306 SideEffectsForArchRuntimeCalls(),
6307 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006308 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006309 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006310 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006311 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006312 // Referrers class should not need access check. We never inline unverified
6313 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006314 DCHECK(!is_referrers_class || !needs_access_check);
6315
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006316 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006317 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006318 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006319 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006320 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006321 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006322 }
6323
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006324 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006325
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006326 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006327
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006328 LoadKind GetLoadKind() const {
6329 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006330 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006331
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006332 bool HasPcRelativeLoadKind() const {
6333 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6334 GetLoadKind() == LoadKind::kBootImageRelRo ||
6335 GetLoadKind() == LoadKind::kBssEntry;
6336 }
6337
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006338 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006339
Yi Kong39402542019-03-24 02:47:16 -07006340 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006341
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006342 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006343
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006344 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006346 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006347 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006348 }
6349
Calin Juravle0ba218d2015-05-19 18:46:01 +01006350 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006351 // The entrypoint the code generator is going to call does not do
6352 // clinit of the class.
6353 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006354 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006355 }
6356
6357 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006358 return NeedsAccessCheck() ||
6359 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006360 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006361 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006362 }
6363
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006364 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006365 return NeedsAccessCheck() ||
6366 MustGenerateClinitCheck() ||
6367 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006368 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006369 GetLoadKind() == LoadKind::kBssEntry) &&
6370 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006371 }
6372
Calin Juravleacf735c2015-02-12 15:25:22 +00006373 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006374 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6375 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006376 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006377 } else {
6378 return ReferenceTypeInfo::CreateInvalid();
6379 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006380 }
6381
Vladimir Marko175e7862018-03-27 09:03:13 +00006382 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6383 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6384 DCHECK(klass_ != nullptr);
6385 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006386 }
6387
Andreas Gampea5b09a62016-11-17 15:21:22 -08006388 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006389 const DexFile& GetDexFile() const { return dex_file_; }
6390
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006391 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006392 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006393 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006394
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006395 static SideEffects SideEffectsForArchRuntimeCalls() {
6396 return SideEffects::CanTriggerGC();
6397 }
6398
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006399 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006400 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006401 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006402 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006403
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006404 bool MustResolveTypeOnSlowPath() const {
6405 // Check that this instruction has a slow path.
6406 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6407 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6408 return GetLoadKind() == LoadKind::kBssEntry;
6409 }
6410
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006411 void MarkInBootImage() {
6412 SetPackedFlag<kFlagIsInBootImage>(true);
6413 }
6414
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006415 void AddSpecialInput(HInstruction* special_input);
6416
6417 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006418 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006419 return ArrayRef<HUserRecord<HInstruction*>>(
6420 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6421 }
6422
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006423 Handle<mirror::Class> GetClass() const {
6424 return klass_;
6425 }
6426
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006427 DECLARE_INSTRUCTION(LoadClass);
6428
Artem Serovcced8ba2017-07-19 18:18:09 +01006429 protected:
6430 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6431
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006432 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006433 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006434 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006435 // Whether this instruction must generate the initialization check.
6436 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006437 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006438 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6439 static constexpr size_t kFieldLoadKindSize =
6440 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006441 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6442 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006443 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006444 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6445
6446 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006447 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006448 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006449 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006450 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006451 }
6452
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006453 void SetLoadKindInternal(LoadKind load_kind);
6454
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006455 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006456 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006457 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006458 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006459
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006460 // A type index and dex file where the class can be accessed. The dex file can be:
6461 // - The compiling method's dex file if the class is defined there too.
6462 // - The compiling method's dex file if the class is referenced there.
6463 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006464 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006465 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006466 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006467
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006468 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006469};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006470std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6471
6472// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006473inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6474 // The load kind should be determined before inserting the instruction to the graph.
6475 DCHECK(GetBlock() == nullptr);
6476 DCHECK(GetEnvironment() == nullptr);
6477 SetPackedField<LoadKindField>(load_kind);
6478 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6479 special_input_ = HUserRecord<HInstruction*>(nullptr);
6480 }
6481 if (!NeedsEnvironment()) {
6482 SetSideEffects(SideEffects::None());
6483 }
6484}
6485
6486// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006487inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006488 // The special input is used for PC-relative loads on some architectures,
6489 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006490 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006491 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006492 GetLoadKind() == LoadKind::kBssEntry ||
6493 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006494 DCHECK(special_input_.GetInstruction() == nullptr);
6495 special_input_ = HUserRecord<HInstruction*>(special_input);
6496 special_input->AddUseAt(this, 0);
6497}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006498
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006499class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006500 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006501 // Determines how to load the String.
6502 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006503 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006504 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006505 kBootImageLinkTimePcRelative,
6506
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006507 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006508 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006509 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006510
Vladimir Markoaad75c62016-10-03 08:46:48 +00006511 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006512 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006513 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006514
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006515 // Use a known boot image String* address, embedded in the code by the codegen.
6516 // Used for boot image strings referenced by apps in JIT-compiled code.
6517 kJitBootImageAddress,
6518
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006519 // Load from the root table associated with the JIT compiled method.
6520 kJitTableAddress,
6521
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006522 // Load using a simple runtime call. This is the fall-back load kind when
6523 // the codegen is unable to use another appropriate kind.
6524 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006525
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006526 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006527 };
6528
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006529 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006530 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006531 const DexFile& dex_file,
6532 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006533 : HInstruction(kLoadString,
6534 DataType::Type::kReference,
6535 SideEffectsForArchRuntimeCalls(),
6536 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006537 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006538 string_index_(string_index),
6539 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006540 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006541 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006542
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006543 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006544
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006545 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006546
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006547 LoadKind GetLoadKind() const {
6548 return GetPackedField<LoadKindField>();
6549 }
6550
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006551 bool HasPcRelativeLoadKind() const {
6552 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6553 GetLoadKind() == LoadKind::kBootImageRelRo ||
6554 GetLoadKind() == LoadKind::kBssEntry;
6555 }
6556
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006557 const DexFile& GetDexFile() const {
6558 return dex_file_;
6559 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006560
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006561 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006562 return string_index_;
6563 }
6564
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006565 Handle<mirror::String> GetString() const {
6566 return string_;
6567 }
6568
6569 void SetString(Handle<mirror::String> str) {
6570 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006571 }
6572
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006573 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006574
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006575 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006576
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006577 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006578
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006579 // Will call the runtime if we need to load the string through
6580 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006581 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006582 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006583 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006584 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006585 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006586 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006587 return false;
6588 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006589 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006590 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006591
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006592 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006593 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006594 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006595
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006596 bool CanBeNull() const override { return false; }
6597 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006598
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006599 static SideEffects SideEffectsForArchRuntimeCalls() {
6600 return SideEffects::CanTriggerGC();
6601 }
6602
Vladimir Marko372f10e2016-05-17 16:30:10 +01006603 void AddSpecialInput(HInstruction* special_input);
6604
6605 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006606 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006607 return ArrayRef<HUserRecord<HInstruction*>>(
6608 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006609 }
6610
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006611 DECLARE_INSTRUCTION(LoadString);
6612
Artem Serovcced8ba2017-07-19 18:18:09 +01006613 protected:
6614 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6615
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006616 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006617 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006618 static constexpr size_t kFieldLoadKindSize =
6619 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6620 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006621 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006622 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006623
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006624 void SetLoadKindInternal(LoadKind load_kind);
6625
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006626 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006627 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006628 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006629 HUserRecord<HInstruction*> special_input_;
6630
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006631 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006632 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006633
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006634 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006635};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006636std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6637
6638// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006639inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6640 // The load kind should be determined before inserting the instruction to the graph.
6641 DCHECK(GetBlock() == nullptr);
6642 DCHECK(GetEnvironment() == nullptr);
6643 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6644 SetPackedField<LoadKindField>(load_kind);
6645 if (load_kind != LoadKind::kRuntimeCall) {
6646 special_input_ = HUserRecord<HInstruction*>(nullptr);
6647 }
6648 if (!NeedsEnvironment()) {
6649 SetSideEffects(SideEffects::None());
6650 }
6651}
6652
6653// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006654inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006655 // The special input is used for PC-relative loads on some architectures,
6656 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006657 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006658 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006659 GetLoadKind() == LoadKind::kBssEntry ||
6660 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006661 // HLoadString::GetInputRecords() returns an empty array at this point,
6662 // so use the GetInputRecords() from the base class to set the input record.
6663 DCHECK(special_input_.GetInstruction() == nullptr);
6664 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006665 special_input->AddUseAt(this, 0);
6666}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006667
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006668class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006669 public:
6670 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006671 uint16_t method_handle_idx,
6672 const DexFile& dex_file,
6673 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006674 : HInstruction(kLoadMethodHandle,
6675 DataType::Type::kReference,
6676 SideEffectsForArchRuntimeCalls(),
6677 dex_pc),
6678 special_input_(HUserRecord<HInstruction*>(current_method)),
6679 method_handle_idx_(method_handle_idx),
6680 dex_file_(dex_file) {
6681 }
6682
6683 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006684 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006685 return ArrayRef<HUserRecord<HInstruction*>>(
6686 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6687 }
6688
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006689 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006690
6691 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6692
6693 const DexFile& GetDexFile() const { return dex_file_; }
6694
6695 static SideEffects SideEffectsForArchRuntimeCalls() {
6696 return SideEffects::CanTriggerGC();
6697 }
6698
6699 DECLARE_INSTRUCTION(LoadMethodHandle);
6700
6701 protected:
6702 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6703
6704 private:
6705 // The special input is the HCurrentMethod for kRuntimeCall.
6706 HUserRecord<HInstruction*> special_input_;
6707
6708 const uint16_t method_handle_idx_;
6709 const DexFile& dex_file_;
6710};
6711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006712class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006713 public:
6714 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006715 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006716 const DexFile& dex_file,
6717 uint32_t dex_pc)
6718 : HInstruction(kLoadMethodType,
6719 DataType::Type::kReference,
6720 SideEffectsForArchRuntimeCalls(),
6721 dex_pc),
6722 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006723 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006724 dex_file_(dex_file) {
6725 }
6726
6727 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006728 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006729 return ArrayRef<HUserRecord<HInstruction*>>(
6730 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6731 }
6732
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006733 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006734
Orion Hodson06d10a72018-05-14 08:53:38 +01006735 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006736
6737 const DexFile& GetDexFile() const { return dex_file_; }
6738
6739 static SideEffects SideEffectsForArchRuntimeCalls() {
6740 return SideEffects::CanTriggerGC();
6741 }
6742
6743 DECLARE_INSTRUCTION(LoadMethodType);
6744
6745 protected:
6746 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6747
6748 private:
6749 // The special input is the HCurrentMethod for kRuntimeCall.
6750 HUserRecord<HInstruction*> special_input_;
6751
Orion Hodson06d10a72018-05-14 08:53:38 +01006752 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006753 const DexFile& dex_file_;
6754};
6755
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006756/**
6757 * Performs an initialization check on its Class object input.
6758 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006759class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006760 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006761 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006762 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306763 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006764 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006765 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006766 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006767 SetRawInputAt(0, constant);
6768 }
Artem Serova6e26142018-06-19 14:55:17 +01006769 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006770 bool CanBeMoved() const override { return true; }
6771 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006772 return true;
6773 }
6774
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006775 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006776 // May call runtime to initialize the class.
6777 return true;
6778 }
6779
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006780 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006781
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006782 HLoadClass* GetLoadClass() const {
6783 DCHECK(InputAt(0)->IsLoadClass());
6784 return InputAt(0)->AsLoadClass();
6785 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006786
6787 DECLARE_INSTRUCTION(ClinitCheck);
6788
Artem Serovcced8ba2017-07-19 18:18:09 +01006789
6790 protected:
6791 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006792};
6793
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006794class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006795 public:
6796 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006797 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006798 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006799 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006800 bool is_volatile,
6801 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006802 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006803 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006804 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306805 : HExpression(kStaticFieldGet,
6806 field_type,
6807 SideEffects::FieldReadOfType(field_type, is_volatile),
6808 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006809 field_info_(field,
6810 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006811 field_type,
6812 is_volatile,
6813 field_idx,
6814 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006815 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006816 SetRawInputAt(0, cls);
6817 }
6818
Calin Juravle52c48962014-12-16 17:02:57 +00006819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006820 bool IsClonable() const override { return true; }
6821 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006822
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006823 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006824 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006825 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006826 }
6827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006828 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006829 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6830 }
6831
Calin Juravle52c48962014-12-16 17:02:57 +00006832 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006833 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006834 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006835 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006836
Vladimir Marko61b92282017-10-11 13:23:17 +01006837 void SetType(DataType::Type new_type) {
6838 DCHECK(DataType::IsIntegralType(GetType()));
6839 DCHECK(DataType::IsIntegralType(new_type));
6840 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6841 SetPackedField<TypeField>(new_type);
6842 }
6843
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006844 DECLARE_INSTRUCTION(StaticFieldGet);
6845
Artem Serovcced8ba2017-07-19 18:18:09 +01006846 protected:
6847 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6848
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006849 private:
6850 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006851};
6852
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006853class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006854 public:
6855 HStaticFieldSet(HInstruction* cls,
6856 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006857 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006858 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006859 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006860 bool is_volatile,
6861 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006862 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006863 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006864 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006865 : HExpression(kStaticFieldSet,
6866 SideEffects::FieldWriteOfType(field_type, is_volatile),
6867 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006868 field_info_(field,
6869 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006870 field_type,
6871 is_volatile,
6872 field_idx,
6873 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006874 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006875 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006876 SetRawInputAt(0, cls);
6877 SetRawInputAt(1, value);
6878 }
6879
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006880 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006881 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006882 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006883 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006884 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006885
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006886 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006887 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6888 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006889
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006890 DECLARE_INSTRUCTION(StaticFieldSet);
6891
Artem Serovcced8ba2017-07-19 18:18:09 +01006892 protected:
6893 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6894
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006895 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006896 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6897 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6898 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6899 "Too many packed fields.");
6900
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006901 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006902};
6903
Vladimir Marko552a1342017-10-31 10:56:47 +00006904class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6905 public:
6906 HStringBuilderAppend(HIntConstant* format,
6907 uint32_t number_of_arguments,
6908 ArenaAllocator* allocator,
6909 uint32_t dex_pc)
6910 : HVariableInputSizeInstruction(
6911 kStringBuilderAppend,
6912 DataType::Type::kReference,
6913 // The runtime call may read memory from inputs. It never writes outside
6914 // of the newly allocated result object (or newly allocated helper objects).
6915 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6916 dex_pc,
6917 allocator,
6918 number_of_arguments + /* format */ 1u,
6919 kArenaAllocInvokeInputs) {
6920 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6921 SetRawInputAt(FormatIndex(), format);
6922 }
6923
6924 void SetArgumentAt(size_t index, HInstruction* argument) {
6925 DCHECK_LE(index, GetNumberOfArguments());
6926 SetRawInputAt(index, argument);
6927 }
6928
6929 // Return the number of arguments, excluding the format.
6930 size_t GetNumberOfArguments() const {
6931 DCHECK_GE(InputCount(), 1u);
6932 return InputCount() - 1u;
6933 }
6934
6935 size_t FormatIndex() const {
6936 return GetNumberOfArguments();
6937 }
6938
6939 HIntConstant* GetFormat() {
6940 return InputAt(FormatIndex())->AsIntConstant();
6941 }
6942
6943 bool NeedsEnvironment() const override { return true; }
6944
6945 bool CanThrow() const override { return true; }
6946
Vladimir Markob1fe5e12020-03-10 14:30:49 +00006947 bool CanBeNull() const override { return false; }
6948
Vladimir Marko552a1342017-10-31 10:56:47 +00006949 DECLARE_INSTRUCTION(StringBuilderAppend);
6950
6951 protected:
6952 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
6953};
6954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006955class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006956 public:
6957 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006958 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006959 uint32_t field_index,
6960 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306961 : HExpression(kUnresolvedInstanceFieldGet,
6962 field_type,
6963 SideEffects::AllExceptGCDependency(),
6964 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006965 field_index_(field_index) {
6966 SetRawInputAt(0, obj);
6967 }
6968
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006969 bool IsClonable() const override { return true; }
6970 bool NeedsEnvironment() const override { return true; }
6971 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006972
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006973 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006974 uint32_t GetFieldIndex() const { return field_index_; }
6975
6976 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6977
Artem Serovcced8ba2017-07-19 18:18:09 +01006978 protected:
6979 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6980
Calin Juravlee460d1d2015-09-29 04:52:17 +01006981 private:
6982 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006983};
6984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006985class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006986 public:
6987 HUnresolvedInstanceFieldSet(HInstruction* obj,
6988 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006989 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006990 uint32_t field_index,
6991 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006992 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006993 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006994 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006995 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006996 SetRawInputAt(0, obj);
6997 SetRawInputAt(1, value);
6998 }
6999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007000 bool IsClonable() const override { return true; }
7001 bool NeedsEnvironment() const override { return true; }
7002 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007003
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007004 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007005 uint32_t GetFieldIndex() const { return field_index_; }
7006
7007 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7008
Artem Serovcced8ba2017-07-19 18:18:09 +01007009 protected:
7010 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7011
Calin Juravlee460d1d2015-09-29 04:52:17 +01007012 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007013 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7014 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007015 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007016 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7017 kFieldFieldType + kFieldFieldTypeSize;
7018 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7019 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007020 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007021
Calin Juravlee460d1d2015-09-29 04:52:17 +01007022 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007023};
7024
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007025class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007026 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007027 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007028 uint32_t field_index,
7029 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307030 : HExpression(kUnresolvedStaticFieldGet,
7031 field_type,
7032 SideEffects::AllExceptGCDependency(),
7033 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007034 field_index_(field_index) {
7035 }
7036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007037 bool IsClonable() const override { return true; }
7038 bool NeedsEnvironment() const override { return true; }
7039 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007040
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007041 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007042 uint32_t GetFieldIndex() const { return field_index_; }
7043
7044 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7045
Artem Serovcced8ba2017-07-19 18:18:09 +01007046 protected:
7047 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7048
Calin Juravlee460d1d2015-09-29 04:52:17 +01007049 private:
7050 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007051};
7052
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007053class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007054 public:
7055 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007056 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007057 uint32_t field_index,
7058 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007059 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007060 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007061 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007062 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007063 SetRawInputAt(0, value);
7064 }
7065
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007066 bool IsClonable() const override { return true; }
7067 bool NeedsEnvironment() const override { return true; }
7068 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007070 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007071 uint32_t GetFieldIndex() const { return field_index_; }
7072
7073 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7074
Artem Serovcced8ba2017-07-19 18:18:09 +01007075 protected:
7076 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7077
Calin Juravlee460d1d2015-09-29 04:52:17 +01007078 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007079 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7080 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007081 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007082 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7083 kFieldFieldType + kFieldFieldTypeSize;
7084 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7085 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007086 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007087
Calin Juravlee460d1d2015-09-29 04:52:17 +01007088 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007089};
7090
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007091// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007092class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007093 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007094 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307095 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7096 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007097
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007098 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007099
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007100 DECLARE_INSTRUCTION(LoadException);
7101
Artem Serovcced8ba2017-07-19 18:18:09 +01007102 protected:
7103 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007104};
7105
David Brazdilcb1c0552015-08-04 16:22:25 +01007106// Implicit part of move-exception which clears thread-local exception storage.
7107// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007108class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007109 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007110 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007111 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307112 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007113
7114 DECLARE_INSTRUCTION(ClearException);
7115
Artem Serovcced8ba2017-07-19 18:18:09 +01007116 protected:
7117 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007118};
7119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007120class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007121 public:
7122 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007123 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007124 SetRawInputAt(0, exception);
7125 }
7126
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007127 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007128
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007129 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007130
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007131 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007133 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007134
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007135 DECLARE_INSTRUCTION(Throw);
7136
Artem Serovcced8ba2017-07-19 18:18:09 +01007137 protected:
7138 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007139};
7140
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007141/**
7142 * Implementation strategies for the code generator of a HInstanceOf
7143 * or `HCheckCast`.
7144 */
7145enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007146 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007147 kExactCheck, // Can do a single class compare.
7148 kClassHierarchyCheck, // Can just walk the super class chain.
7149 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7150 kInterfaceCheck, // No optimization yet when checking against an interface.
7151 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007152 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007153 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007154 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007155};
7156
Roland Levillain86503782016-02-11 19:07:30 +00007157std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7158
Vladimir Marko175e7862018-03-27 09:03:13 +00007159// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7160// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7161class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007162 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007163 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007164 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007165 HInstruction* object,
7166 HInstruction* target_class_or_null,
7167 TypeCheckKind check_kind,
7168 Handle<mirror::Class> klass,
7169 uint32_t dex_pc,
7170 ArenaAllocator* allocator,
7171 HIntConstant* bitstring_path_to_root,
7172 HIntConstant* bitstring_mask,
7173 SideEffects side_effects)
7174 : HVariableInputSizeInstruction(
7175 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007176 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007177 side_effects,
7178 dex_pc,
7179 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007180 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007181 kArenaAllocTypeCheckInputs),
7182 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007183 SetPackedField<TypeCheckKindField>(check_kind);
7184 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007185 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007186 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007187 SetRawInputAt(1, target_class_or_null);
7188 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7189 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7190 if (check_kind == TypeCheckKind::kBitstringCheck) {
7191 DCHECK(target_class_or_null->IsNullConstant());
7192 SetRawInputAt(2, bitstring_path_to_root);
7193 SetRawInputAt(3, bitstring_mask);
7194 } else {
7195 DCHECK(target_class_or_null->IsLoadClass());
7196 }
Vladimir Marko87584542017-12-12 17:47:52 +00007197 }
7198
7199 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007200 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007201 HInstruction* load_class = InputAt(1);
7202 DCHECK(load_class->IsLoadClass());
7203 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007204 }
7205
Vladimir Marko175e7862018-03-27 09:03:13 +00007206 uint32_t GetBitstringPathToRoot() const {
7207 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7208 HInstruction* path_to_root = InputAt(2);
7209 DCHECK(path_to_root->IsIntConstant());
7210 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7211 }
7212
7213 uint32_t GetBitstringMask() const {
7214 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7215 HInstruction* mask = InputAt(3);
7216 DCHECK(mask->IsIntConstant());
7217 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7218 }
7219
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007220 bool IsClonable() const override { return true; }
7221 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007222
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007223 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007224 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7225 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007226 }
7227
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007228 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7229 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7230 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7231 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7232
Vladimir Marko175e7862018-03-27 09:03:13 +00007233 ReferenceTypeInfo GetTargetClassRTI() {
7234 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7235 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007236 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007237 } else {
7238 return ReferenceTypeInfo::CreateInvalid();
7239 }
7240 }
7241
7242 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7243 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7244 DCHECK(klass_ != nullptr);
7245 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7246 }
7247
7248 Handle<mirror::Class> GetClass() const {
7249 return klass_;
7250 }
7251
7252 protected:
7253 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7254
7255 private:
7256 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7257 static constexpr size_t kFieldTypeCheckKindSize =
7258 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7259 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7260 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7261 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7262 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7263 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7264
7265 Handle<mirror::Class> klass_;
7266};
7267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007268class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007269 public:
7270 HInstanceOf(HInstruction* object,
7271 HInstruction* target_class_or_null,
7272 TypeCheckKind check_kind,
7273 Handle<mirror::Class> klass,
7274 uint32_t dex_pc,
7275 ArenaAllocator* allocator,
7276 HIntConstant* bitstring_path_to_root,
7277 HIntConstant* bitstring_mask)
7278 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007279 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007280 object,
7281 target_class_or_null,
7282 check_kind,
7283 klass,
7284 dex_pc,
7285 allocator,
7286 bitstring_path_to_root,
7287 bitstring_mask,
7288 SideEffectsForArchRuntimeCalls(check_kind)) {}
7289
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007290 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007292 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007293 return CanCallRuntime(GetTypeCheckKind());
7294 }
7295
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007296 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007297 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007298 return check_kind != TypeCheckKind::kExactCheck;
7299 }
7300
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007301 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007302 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007303 }
7304
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007305 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007306
Artem Serovcced8ba2017-07-19 18:18:09 +01007307 protected:
7308 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007309};
7310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007311class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007312 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007313 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307314 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007315 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7316 SetPackedFlag<kFlagUpperCanBeNull>(true);
7317 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007318 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007319 SetRawInputAt(0, input);
7320 }
7321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007322 bool InstructionDataEquals(const HInstruction* other) const override;
7323 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007324
David Brazdilf5552582015-12-27 13:36:12 +00007325 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007326 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007327 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007328 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007329
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007330 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007331 DCHECK(GetUpperCanBeNull() || !can_be_null);
7332 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007333 }
7334
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007335 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007336
Calin Juravleb1498f62015-02-16 13:13:29 +00007337 DECLARE_INSTRUCTION(BoundType);
7338
Artem Serovcced8ba2017-07-19 18:18:09 +01007339 protected:
7340 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7341
Calin Juravleb1498f62015-02-16 13:13:29 +00007342 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007343 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7344 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007345 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007346 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7347 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7348 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7349
Calin Juravleb1498f62015-02-16 13:13:29 +00007350 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007351 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7352 // It is used to bound the type in cases like:
7353 // if (x instanceof ClassX) {
7354 // // uper_bound_ will be ClassX
7355 // }
David Brazdilf5552582015-12-27 13:36:12 +00007356 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007357};
7358
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007359class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007360 public:
7361 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007362 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007363 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007364 Handle<mirror::Class> klass,
7365 uint32_t dex_pc,
7366 ArenaAllocator* allocator,
7367 HIntConstant* bitstring_path_to_root,
7368 HIntConstant* bitstring_mask)
7369 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007370 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007371 object,
7372 target_class_or_null,
7373 check_kind,
7374 klass,
7375 dex_pc,
7376 allocator,
7377 bitstring_path_to_root,
7378 bitstring_mask,
7379 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007381 bool IsClonable() const override { return true; }
7382 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007383 // Instruction may throw a CheckCastError.
7384 return true;
7385 }
7386
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007387 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007388
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007389 DECLARE_INSTRUCTION(CheckCast);
7390
Artem Serovcced8ba2017-07-19 18:18:09 +01007391 protected:
7392 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007393};
7394
Andreas Gampe26de38b2016-07-27 17:53:11 -07007395/**
7396 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7397 * @details We define the combined barrier types that are actually required
7398 * by the Java Memory Model, rather than using exactly the terminology from
7399 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7400 * primitives. Note that the JSR-133 cookbook generally does not deal with
7401 * store atomicity issues, and the recipes there are not always entirely sufficient.
7402 * The current recipe is as follows:
7403 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7404 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7405 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7406 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7407 * class has final fields.
7408 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7409 * store-to-memory instructions. Only generated together with non-temporal stores.
7410 */
7411enum MemBarrierKind {
7412 kAnyStore,
7413 kLoadAny,
7414 kStoreStore,
7415 kAnyAny,
7416 kNTStoreStore,
7417 kLastBarrierKind = kNTStoreStore
7418};
7419std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7420
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007421class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007423 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007424 : HExpression(kMemoryBarrier,
7425 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7426 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007427 SetPackedField<BarrierKindField>(barrier_kind);
7428 }
Calin Juravle27df7582015-04-17 19:12:31 +01007429
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007430 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007431
Vladimir Markoa1de9182016-02-25 11:37:38 +00007432 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007433
7434 DECLARE_INSTRUCTION(MemoryBarrier);
7435
Artem Serovcced8ba2017-07-19 18:18:09 +01007436 protected:
7437 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7438
Calin Juravle27df7582015-04-17 19:12:31 +01007439 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007440 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7441 static constexpr size_t kFieldBarrierKindSize =
7442 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7443 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7444 kFieldBarrierKind + kFieldBarrierKindSize;
7445 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7446 "Too many packed fields.");
7447 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007448};
7449
Igor Murashkind01745e2017-04-05 16:40:31 -07007450// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7451// the specified object(s), with respect to any subsequent store that might "publish"
7452// (i.e. make visible) the specified object to another thread.
7453//
7454// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7455// for all final fields (that were set) at the end of the invoked constructor.
7456//
7457// The constructor fence models the freeze actions for the final fields of an object
7458// being constructed (semantically at the end of the constructor). Constructor fences
7459// have a per-object affinity; two separate objects being constructed get two separate
7460// constructor fences.
7461//
7462// (Note: that if calling a super-constructor or forwarding to another constructor,
7463// the freezes would happen at the end of *that* constructor being invoked).
7464//
7465// The memory model guarantees that when the object being constructed is "published" after
7466// constructor completion (i.e. escapes the current thread via a store), then any final field
7467// writes must be observable on other threads (once they observe that publication).
7468//
7469// Further, anything written before the freeze, and read by dereferencing through the final field,
7470// must also be visible (so final object field could itself have an object with non-final fields;
7471// yet the freeze must also extend to them).
7472//
7473// Constructor example:
7474//
7475// class HasFinal {
7476// final int field; Optimizing IR for <init>()V:
7477// HasFinal() {
7478// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7479// // freeze(this.field); HConstructorFence(this)
7480// } HReturn
7481// }
7482//
7483// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7484// already-initialized classes; in that case the allocation must act as a "default-initializer"
7485// of the object which effectively writes the class pointer "final field".
7486//
7487// For example, we can model default-initialiation as roughly the equivalent of the following:
7488//
7489// class Object {
7490// private final Class header;
7491// }
7492//
7493// Java code: Optimizing IR:
7494//
7495// T new_instance<T>() {
7496// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7497// obj.header = T.class; // header write is done by above call.
7498// // freeze(obj.header) HConstructorFence(obj)
7499// return (T)obj;
7500// }
7501//
7502// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007503// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007504// * QuasiAtomic::ThreadFenceForConstructor
7505//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007506class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007507 // A fence has variable inputs because the inputs can be removed
7508 // after prepare_for_register_allocation phase.
7509 // (TODO: In the future a fence could freeze multiple objects
7510 // after merging two fences together.)
7511 public:
7512 // `fence_object` is the reference that needs to be protected for correct publication.
7513 //
7514 // It makes sense in the following situations:
7515 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7516 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7517 //
7518 // After construction the `fence_object` becomes the 0th input.
7519 // This is not an input in a real sense, but just a convenient place to stash the information
7520 // about the associated object.
7521 HConstructorFence(HInstruction* fence_object,
7522 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007523 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007524 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7525 // constraints described in the class header. We claim that these SideEffects constraints
7526 // enforce a superset of the real constraints.
7527 //
7528 // The ordering described above is conservatively modeled with SideEffects as follows:
7529 //
7530 // * To prevent reordering of the publication stores:
7531 // ----> "Reads of objects" is the initial SideEffect.
7532 // * For every primitive final field store in the constructor:
7533 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7534 // * If there are any stores to reference final fields in the constructor:
7535 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7536 // that are reachable from `fence_object` also need to be prevented for reordering
7537 // (and we do not want to do alias analysis to figure out what those stores are).
7538 //
7539 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7540 // even more conservative approach than the one described above, and prevents all of the
7541 // above reordering without analyzing any of the instructions in the constructor.
7542 //
7543 // If in a later phase we discover that there are no writes to reference final fields,
7544 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307545 : HVariableInputSizeInstruction(kConstructorFence,
7546 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007547 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007548 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007549 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007550 kArenaAllocConstructorFenceInputs) {
7551 DCHECK(fence_object != nullptr);
7552 SetRawInputAt(0, fence_object);
7553 }
7554
7555 // The object associated with this constructor fence.
7556 //
7557 // (Note: This will be null after the prepare_for_register_allocation phase,
7558 // as all constructor fence inputs are removed there).
7559 HInstruction* GetFenceObject() const {
7560 return InputAt(0);
7561 }
7562
7563 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7564 // - Delete `fence_use` from `this`'s use list.
7565 // - Delete `this` from `fence_use`'s inputs list.
7566 // - If the `fence_use` is dead, remove it from the graph.
7567 //
7568 // A fence is considered dead once it no longer has any uses
7569 // and all of the inputs are dead.
7570 //
7571 // This must *not* be called during/after prepare_for_register_allocation,
7572 // because that removes all the inputs to the fences but the fence is actually
7573 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007574 //
7575 // Returns how many HConstructorFence instructions were removed from graph.
7576 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007577
Igor Murashkindd018df2017-08-09 10:38:31 -07007578 // Combine all inputs of `this` and `other` instruction and remove
7579 // `other` from the graph.
7580 //
7581 // Inputs are unique after the merge.
7582 //
7583 // Requirement: `this` must not be the same as `other.
7584 void Merge(HConstructorFence* other);
7585
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007586 // Check if this constructor fence is protecting
7587 // an HNewInstance or HNewArray that is also the immediate
7588 // predecessor of `this`.
7589 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007590 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7591 // to be one of the inputs of `this`.
7592 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007593 // Returns the associated HNewArray or HNewInstance,
7594 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007595 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007596
Igor Murashkind01745e2017-04-05 16:40:31 -07007597 DECLARE_INSTRUCTION(ConstructorFence);
7598
Artem Serovcced8ba2017-07-19 18:18:09 +01007599 protected:
7600 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007601};
7602
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007603class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007604 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007605 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007606 kEnter,
7607 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007608 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007609 };
7610
7611 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007612 : HExpression(kMonitorOperation,
7613 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7614 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007615 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007616 SetRawInputAt(0, object);
7617 }
7618
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007619 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007620 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007621
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007622 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007623 // Verifier guarantees that monitor-exit cannot throw.
7624 // This is important because it allows the HGraphBuilder to remove
7625 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7626 return IsEnter();
7627 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007628
Vladimir Markoa1de9182016-02-25 11:37:38 +00007629 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7630 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007631
7632 DECLARE_INSTRUCTION(MonitorOperation);
7633
Artem Serovcced8ba2017-07-19 18:18:09 +01007634 protected:
7635 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7636
Calin Juravle52c48962014-12-16 17:02:57 +00007637 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007638 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7639 static constexpr size_t kFieldOperationKindSize =
7640 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7641 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7642 kFieldOperationKind + kFieldOperationKindSize;
7643 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7644 "Too many packed fields.");
7645 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007646};
7647
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007648class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007649 public:
7650 HSelect(HInstruction* condition,
7651 HInstruction* true_value,
7652 HInstruction* false_value,
7653 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307654 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007655 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7656
7657 // First input must be `true_value` or `false_value` to allow codegens to
7658 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7659 // that architectures which implement HSelect as a conditional move also
7660 // will not need to invert the condition.
7661 SetRawInputAt(0, false_value);
7662 SetRawInputAt(1, true_value);
7663 SetRawInputAt(2, condition);
7664 }
7665
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007666 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007667 HInstruction* GetFalseValue() const { return InputAt(0); }
7668 HInstruction* GetTrueValue() const { return InputAt(1); }
7669 HInstruction* GetCondition() const { return InputAt(2); }
7670
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007671 bool CanBeMoved() const override { return true; }
7672 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007673 return true;
7674 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007676 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007677 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7678 }
7679
7680 DECLARE_INSTRUCTION(Select);
7681
Artem Serovcced8ba2017-07-19 18:18:09 +01007682 protected:
7683 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007684};
7685
Vladimir Markof9f64412015-09-02 14:05:49 +01007686class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007687 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007688 MoveOperands(Location source,
7689 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007690 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007691 HInstruction* instruction)
7692 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007693
7694 Location GetSource() const { return source_; }
7695 Location GetDestination() const { return destination_; }
7696
7697 void SetSource(Location value) { source_ = value; }
7698 void SetDestination(Location value) { destination_ = value; }
7699
7700 // The parallel move resolver marks moves as "in-progress" by clearing the
7701 // destination (but not the source).
7702 Location MarkPending() {
7703 DCHECK(!IsPending());
7704 Location dest = destination_;
7705 destination_ = Location::NoLocation();
7706 return dest;
7707 }
7708
7709 void ClearPending(Location dest) {
7710 DCHECK(IsPending());
7711 destination_ = dest;
7712 }
7713
7714 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007715 DCHECK(source_.IsValid() || destination_.IsInvalid());
7716 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007717 }
7718
7719 // True if this blocks a move from the given location.
7720 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007721 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007722 }
7723
7724 // A move is redundant if it's been eliminated, if its source and
7725 // destination are the same, or if its destination is unneeded.
7726 bool IsRedundant() const {
7727 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7728 }
7729
7730 // We clear both operands to indicate move that's been eliminated.
7731 void Eliminate() {
7732 source_ = destination_ = Location::NoLocation();
7733 }
7734
7735 bool IsEliminated() const {
7736 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7737 return source_.IsInvalid();
7738 }
7739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007740 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007741
Nicolas Geoffray90218252015-04-15 11:56:51 +01007742 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007743 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007744 }
7745
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007746 HInstruction* GetInstruction() const { return instruction_; }
7747
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007748 private:
7749 Location source_;
7750 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007751 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007752 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007753 // The instruction this move is assocatied with. Null when this move is
7754 // for moving an input in the expected locations of user (including a phi user).
7755 // This is only used in debug mode, to ensure we do not connect interval siblings
7756 // in the same parallel move.
7757 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007758};
7759
Roland Levillainc9285912015-12-18 10:38:42 +00007760std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7761
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007762static constexpr size_t kDefaultNumberOfMoves = 4;
7763
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007764class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007765 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007766 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007767 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007768 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007769 moves_.reserve(kDefaultNumberOfMoves);
7770 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007771
Nicolas Geoffray90218252015-04-15 11:56:51 +01007772 void AddMove(Location source,
7773 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007774 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007775 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007776 DCHECK(source.IsValid());
7777 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007778 if (kIsDebugBuild) {
7779 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007780 for (const MoveOperands& move : moves_) {
7781 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007782 // Special case the situation where the move is for the spill slot
7783 // of the instruction.
7784 if ((GetPrevious() == instruction)
7785 || ((GetPrevious() == nullptr)
7786 && instruction->IsPhi()
7787 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007788 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007789 << "Doing parallel moves for the same instruction.";
7790 } else {
7791 DCHECK(false) << "Doing parallel moves for the same instruction.";
7792 }
7793 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007794 }
7795 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007796 for (const MoveOperands& move : moves_) {
7797 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007798 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007799 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007800 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007801 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007802 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007803 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007804 }
7805
Vladimir Marko225b6462015-09-28 12:17:40 +01007806 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007807 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007808 }
7809
Vladimir Marko225b6462015-09-28 12:17:40 +01007810 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007811
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007812 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007813
Artem Serovcced8ba2017-07-19 18:18:09 +01007814 protected:
7815 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7816
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007817 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007818 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007819};
7820
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007821// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7822// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7823// never used across anything that can trigger GC.
7824// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7825// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007826class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007827 public:
7828 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307829 : HExpression(kIntermediateAddress,
7830 DataType::Type::kInt32,
7831 SideEffects::DependsOnGC(),
7832 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007833 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7834 DataType::Size(DataType::Type::kReference))
7835 << "kPrimInt and kPrimNot have different sizes.";
7836 SetRawInputAt(0, base_address);
7837 SetRawInputAt(1, offset);
7838 }
7839
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007840 bool IsClonable() const override { return true; }
7841 bool CanBeMoved() const override { return true; }
7842 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007843 return true;
7844 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007845 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007846
7847 HInstruction* GetBaseAddress() const { return InputAt(0); }
7848 HInstruction* GetOffset() const { return InputAt(1); }
7849
7850 DECLARE_INSTRUCTION(IntermediateAddress);
7851
Artem Serovcced8ba2017-07-19 18:18:09 +01007852 protected:
7853 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007854};
7855
7856
Mark Mendell0616ae02015-04-17 12:49:27 -04007857} // namespace art
7858
Aart Bikf8f5a162017-02-06 15:35:29 -08007859#include "nodes_vector.h"
7860
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007861#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7862#include "nodes_shared.h"
7863#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307864#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007865#include "nodes_x86.h"
7866#endif
7867
Vladimir Marko0a516052019-10-14 13:00:44 +00007868namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007869
Igor Murashkin6ef45672017-08-08 13:59:55 -07007870class OptimizingCompilerStats;
7871
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007872class HGraphVisitor : public ValueObject {
7873 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007874 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7875 : stats_(stats),
7876 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007877 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007878
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007879 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007880 virtual void VisitBasicBlock(HBasicBlock* block);
7881
Roland Levillain633021e2014-10-01 14:12:25 +01007882 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007883 void VisitInsertionOrder();
7884
Roland Levillain633021e2014-10-01 14:12:25 +01007885 // Visit the graph following dominator tree reverse post-order.
7886 void VisitReversePostOrder();
7887
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007888 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007889
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007890 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007891#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007892 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7893
7894 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7895
7896#undef DECLARE_VISIT_INSTRUCTION
7897
Igor Murashkin6ef45672017-08-08 13:59:55 -07007898 protected:
7899 OptimizingCompilerStats* stats_;
7900
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007901 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007902 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007903
7904 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7905};
7906
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007907class HGraphDelegateVisitor : public HGraphVisitor {
7908 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007909 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7910 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007911 virtual ~HGraphDelegateVisitor() {}
7912
7913 // Visit functions that delegate to to super class.
7914#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007915 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007916
7917 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7918
7919#undef DECLARE_VISIT_INSTRUCTION
7920
7921 private:
7922 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7923};
7924
Artem Serovcced8ba2017-07-19 18:18:09 +01007925// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7926// and remove the old instruction.
7927HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7928
7929// Create a clone for each clonable instructions/phis and replace the original with the clone.
7930//
7931// Used for testing individual instruction cloner.
7932class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7933 public:
7934 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007935 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007937 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007938 if (instruction->IsClonable()) {
7939 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007940 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007941 }
7942 }
7943
Artem Serov7f4aff62017-06-21 17:02:18 +01007944 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007945
7946 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007947 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007948
7949 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7950};
7951
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007952// Iterator over the blocks that art part of the loop. Includes blocks part
7953// of an inner loop. The order in which the blocks are iterated is on their
7954// block id.
7955class HBlocksInLoopIterator : public ValueObject {
7956 public:
7957 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7958 : blocks_in_loop_(info.GetBlocks()),
7959 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7960 index_(0) {
7961 if (!blocks_in_loop_.IsBitSet(index_)) {
7962 Advance();
7963 }
7964 }
7965
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007966 bool Done() const { return index_ == blocks_.size(); }
7967 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007968 void Advance() {
7969 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007970 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007971 if (blocks_in_loop_.IsBitSet(index_)) {
7972 break;
7973 }
7974 }
7975 }
7976
7977 private:
7978 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007979 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007980 size_t index_;
7981
7982 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7983};
7984
Mingyao Yang3584bce2015-05-19 16:01:59 -07007985// Iterator over the blocks that art part of the loop. Includes blocks part
7986// of an inner loop. The order in which the blocks are iterated is reverse
7987// post order.
7988class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7989 public:
7990 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7991 : blocks_in_loop_(info.GetBlocks()),
7992 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7993 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007994 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007995 Advance();
7996 }
7997 }
7998
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007999 bool Done() const { return index_ == blocks_.size(); }
8000 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008001 void Advance() {
8002 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008003 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8004 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008005 break;
8006 }
8007 }
8008 }
8009
8010 private:
8011 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008012 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008013 size_t index_;
8014
8015 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8016};
8017
Aart Bikf3e61ee2017-04-12 17:09:20 -07008018// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008019inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008020 if (constant->IsIntConstant()) {
8021 return constant->AsIntConstant()->GetValue();
8022 } else if (constant->IsLongConstant()) {
8023 return constant->AsLongConstant()->GetValue();
8024 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008025 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008026 return 0;
8027 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008028}
8029
Aart Bikf3e61ee2017-04-12 17:09:20 -07008030// Returns true iff instruction is an integral constant (and sets value on success).
8031inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8032 if (instruction->IsIntConstant()) {
8033 *value = instruction->AsIntConstant()->GetValue();
8034 return true;
8035 } else if (instruction->IsLongConstant()) {
8036 *value = instruction->AsLongConstant()->GetValue();
8037 return true;
8038 } else if (instruction->IsNullConstant()) {
8039 *value = 0;
8040 return true;
8041 }
8042 return false;
8043}
8044
Aart Bik0148de42017-09-05 09:25:01 -07008045// Returns true iff instruction is the given integral constant.
8046inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8047 int64_t val = 0;
8048 return IsInt64AndGet(instruction, &val) && val == value;
8049}
8050
8051// Returns true iff instruction is a zero bit pattern.
8052inline bool IsZeroBitPattern(HInstruction* instruction) {
8053 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8054}
8055
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008056// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008057#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008058 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8059 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8060#undef INSTRUCTION_TYPE_CHECK
8061
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008062// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008063#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8064 std::is_base_of<BaseType, H##type>::value,
8065#define INSTRUCTION_TYPE_CHECK(type, super) \
8066 inline bool HInstruction::Is##type() const { \
8067 DCHECK_LT(GetKind(), kLastInstructionKind); \
8068 using BaseType = H##type; \
8069 static constexpr bool results[] = { \
8070 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8071 }; \
8072 return results[static_cast<size_t>(GetKind())]; \
8073 }
8074
8075 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8076#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008077#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008078
8079#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008080 inline const H##type* HInstruction::As##type() const { \
8081 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8082 } \
8083 inline H##type* HInstruction::As##type() { \
8084 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8085 }
8086
Vladimir Markoa90dd512018-05-04 15:04:45 +01008087 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8088#undef INSTRUCTION_TYPE_CAST
8089
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008090
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008091// Create space in `blocks` for adding `number_of_new_blocks` entries
8092// starting at location `at`. Blocks after `at` are moved accordingly.
8093inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8094 size_t number_of_new_blocks,
8095 size_t after) {
8096 DCHECK_LT(after, blocks->size());
8097 size_t old_size = blocks->size();
8098 size_t new_size = old_size + number_of_new_blocks;
8099 blocks->resize(new_size);
8100 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8101}
8102
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008103/*
8104 * Hunt "under the hood" of array lengths (leading to array references),
8105 * null checks (also leading to array references), and new arrays
8106 * (leading to the actual length). This makes it more likely related
8107 * instructions become actually comparable.
8108 */
8109inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8110 while (instruction->IsArrayLength() ||
8111 instruction->IsNullCheck() ||
8112 instruction->IsNewArray()) {
8113 instruction = instruction->IsNewArray()
8114 ? instruction->AsNewArray()->GetLength()
8115 : instruction->InputAt(0);
8116 }
8117 return instruction;
8118}
8119
Artem Serovb47b9782019-12-04 21:02:09 +00008120inline bool IsAddOrSub(const HInstruction* instruction) {
8121 return instruction->IsAdd() || instruction->IsSub();
8122}
8123
Artem Serov21c7e6f2017-07-27 16:04:42 +01008124void RemoveEnvironmentUses(HInstruction* instruction);
8125bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8126void ResetEnvironmentInputRecords(HInstruction* instruction);
8127
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008128} // namespace art
8129
8130#endif // ART_COMPILER_OPTIMIZING_NODES_H_