blob: 09ae6fab843c1e72e4ed006b914fca5f8a1e25af [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 Geoffray0a23d742015-05-07 11:57:35 +0100325 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100326 : allocator_(allocator),
327 arena_stack_(arena_stack),
328 blocks_(allocator->Adapter(kArenaAllocBlockList)),
329 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
330 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700331 entry_block_(nullptr),
332 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100333 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100334 number_of_vregs_(0),
335 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000336 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400337 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000338 has_try_catch_(false),
Artem Serov2808be82018-12-20 19:15:11 +0000339 has_monitor_operations_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700340 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800341 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000342 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800343 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000344 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100346 dex_file_(dex_file),
347 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100348 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100349 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800350 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700351 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000352 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100353 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
354 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
355 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
356 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000357 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100358 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000359 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700360 osr_(osr),
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000361 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)),
362 is_shared_jit_code_(is_shared_jit_code) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100363 blocks_.reserve(kDefaultNumberOfBlocks);
364 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000365
David Brazdilbadd8262016-02-02 16:28:56 +0000366 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700367 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000368
Vladimir Markoca6fff82017-10-03 14:49:14 +0100369 ArenaAllocator* GetAllocator() const { return allocator_; }
370 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100371 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
372
David Brazdil69ba7b72015-06-23 18:27:30 +0100373 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000374 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100375
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000376 HBasicBlock* GetEntryBlock() const { return entry_block_; }
377 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100378 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000379
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000380 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
381 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000382
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100384
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100385 void ComputeDominanceInformation();
386 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000387 void ClearLoopInformation();
388 void FindBackEdges(ArenaBitVector* visited);
389 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100390 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100391 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000392
David Brazdil4833f5a2015-12-16 10:37:39 +0000393 // Analyze all natural loops in this graph. Returns a code specifying that it
394 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000395 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000396 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100397
David Brazdilffee3d32015-07-06 11:48:53 +0100398 // Iterate over blocks to compute try block membership. Needs reverse post
399 // order and loop information.
400 void ComputeTryBlockInformation();
401
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000402 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000403 // Returns the instruction to replace the invoke expression or null if the
404 // invoke is for a void method. Note that the caller is responsible for replacing
405 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000406 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000407
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000408 // Update the loop and try membership of `block`, which was spawned from `reference`.
409 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
410 // should be the new back edge.
411 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
412 HBasicBlock* reference,
413 bool replace_if_back_edge);
414
Mingyao Yang3584bce2015-05-19 16:01:59 -0700415 // Need to add a couple of blocks to test if the loop body is entered and
416 // put deoptimization instructions, etc.
417 void TransformLoopHeaderForBCE(HBasicBlock* header);
418
Aart Bikf8f5a162017-02-06 15:35:29 -0800419 // Adds a new loop directly after the loop with the given header and exit.
420 // Returns the new preheader.
421 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
422 HBasicBlock* body,
423 HBasicBlock* exit);
424
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000425 // Removes `block` from the graph. Assumes `block` has been disconnected from
426 // other blocks and has no instructions or phis.
427 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000428
David Brazdilfc6a86a2015-06-26 10:33:45 +0000429 // Splits the edge between `block` and `successor` while preserving the
430 // indices in the predecessor/successor lists. If there are multiple edges
431 // between the blocks, the lowest indices are used.
432 // Returns the new block which is empty and has the same dex pc as `successor`.
433 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
434
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100435 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100436 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000437
438 // Transform a loop into a format with a single preheader.
439 //
440 // Each phi in the header should be split: original one in the header should only hold
441 // inputs reachable from the back edges and a single input from the preheader. The newly created
442 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
443 //
444 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
445 // that no longer have this property.
446 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
447
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100448 void SimplifyLoop(HBasicBlock* header);
449
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000450 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100451 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000452 return current_instruction_id_++;
453 }
454
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000455 int32_t GetCurrentInstructionId() const {
456 return current_instruction_id_;
457 }
458
459 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100460 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000461 current_instruction_id_ = id;
462 }
463
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100464 uint16_t GetMaximumNumberOfOutVRegs() const {
465 return maximum_number_of_out_vregs_;
466 }
467
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000468 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
469 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100470 }
471
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100472 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
473 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
474 }
475
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000476 void UpdateTemporariesVRegSlots(size_t slots) {
477 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100478 }
479
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000480 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100481 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000482 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100483 }
484
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100485 void SetNumberOfVRegs(uint16_t number_of_vregs) {
486 number_of_vregs_ = number_of_vregs;
487 }
488
489 uint16_t GetNumberOfVRegs() const {
490 return number_of_vregs_;
491 }
492
493 void SetNumberOfInVRegs(uint16_t value) {
494 number_of_in_vregs_ = value;
495 }
496
David Brazdildee58d62016-04-07 09:54:26 +0000497 uint16_t GetNumberOfInVRegs() const {
498 return number_of_in_vregs_;
499 }
500
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100501 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100502 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100503 return number_of_vregs_ - number_of_in_vregs_;
504 }
505
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100506 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100507 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100508 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100509
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100510 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
511 DCHECK(GetReversePostOrder()[0] == entry_block_);
512 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
513 }
514
515 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
516 return ReverseRange(GetReversePostOrder());
517 }
518
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100519 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100520 return linear_order_;
521 }
522
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100523 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
524 return ReverseRange(GetLinearOrder());
525 }
526
Mark Mendell1152c922015-04-24 17:06:35 -0400527 bool HasBoundsChecks() const {
528 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800529 }
530
Mark Mendell1152c922015-04-24 17:06:35 -0400531 void SetHasBoundsChecks(bool value) {
532 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800533 }
534
Hans Boehm206348c2018-12-05 11:11:33 -0800535 // Is the code known to be robust against eliminating dead references
536 // and the effects of early finalization?
537 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
538
539 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
540
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000541 bool IsDebuggable() const { return debuggable_; }
542
David Brazdil8d5b8b22015-03-24 10:51:52 +0000543 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000544 // already, it is created and inserted into the graph. This method is only for
545 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100546 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000547
548 // TODO: This is problematic for the consistency of reference type propagation
549 // because it can be created anytime after the pass and thus it will be left
550 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600551 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000552
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600553 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
554 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000555 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600556 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
557 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000558 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600559 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
560 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000561 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600562 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
563 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000564 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000565
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100566 HCurrentMethod* GetCurrentMethod();
567
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100568 const DexFile& GetDexFile() const {
569 return dex_file_;
570 }
571
572 uint32_t GetMethodIdx() const {
573 return method_idx_;
574 }
575
Igor Murashkind01745e2017-04-05 16:40:31 -0700576 // Get the method name (without the signature), e.g. "<init>"
577 const char* GetMethodName() const;
578
579 // Get the pretty method name (class + name + optionally signature).
580 std::string PrettyMethod(bool with_signature = true) const;
581
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100582 InvokeType GetInvokeType() const {
583 return invoke_type_;
584 }
585
Mark Mendellc4701932015-04-10 13:18:51 -0400586 InstructionSet GetInstructionSet() const {
587 return instruction_set_;
588 }
589
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000590 bool IsCompilingOsr() const { return osr_; }
591
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000592 bool IsCompilingForSharedJitCode() const {
593 return is_shared_jit_code_;
594 }
595
Mingyao Yang063fc772016-08-02 11:02:54 -0700596 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
597 return cha_single_implementation_list_;
598 }
599
600 void AddCHASingleImplementationDependency(ArtMethod* method) {
601 cha_single_implementation_list_.insert(method);
602 }
603
604 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800605 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700606 }
607
David Brazdil77a48ae2015-09-15 12:34:04 +0000608 bool HasTryCatch() const { return has_try_catch_; }
609 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100610
Artem Serov2808be82018-12-20 19:15:11 +0000611 bool HasMonitorOperations() const { return has_monitor_operations_; }
612 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
613
Aart Bikb13c65b2017-03-21 20:14:07 -0700614 bool HasSIMD() const { return has_simd_; }
615 void SetHasSIMD(bool value) { has_simd_ = value; }
616
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800617 bool HasLoops() const { return has_loops_; }
618 void SetHasLoops(bool value) { has_loops_ = value; }
619
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000620 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
621 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
622
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100623 ArtMethod* GetArtMethod() const { return art_method_; }
624 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
625
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100626 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500627 // The instruction has been inserted into the graph, either as a constant, or
628 // before cursor.
629 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
630
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000631 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
632
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800633 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
634 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
635 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
636
David Brazdil2d7352b2015-04-20 14:52:42 +0100637 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000638 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100639 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000640
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000641 template <class InstructionType, typename ValueType>
642 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600643 ArenaSafeMap<ValueType, InstructionType*>* cache,
644 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000645 // Try to find an existing constant of the given value.
646 InstructionType* constant = nullptr;
647 auto cached_constant = cache->find(value);
648 if (cached_constant != cache->end()) {
649 constant = cached_constant->second;
650 }
651
652 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100653 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000654 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100655 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000656 cache->Overwrite(value, constant);
657 InsertConstant(constant);
658 }
659 return constant;
660 }
661
David Brazdil8d5b8b22015-03-24 10:51:52 +0000662 void InsertConstant(HConstant* instruction);
663
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000664 // Cache a float constant into the graph. This method should only be
665 // called by the SsaBuilder when creating "equivalent" instructions.
666 void CacheFloatConstant(HFloatConstant* constant);
667
668 // See CacheFloatConstant comment.
669 void CacheDoubleConstant(HDoubleConstant* constant);
670
Vladimir Markoca6fff82017-10-03 14:49:14 +0100671 ArenaAllocator* const allocator_;
672 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000673
674 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000676
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100677 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100678 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000679
Aart Bik281c6812016-08-26 11:31:48 -0700680 // List of blocks to perform a linear order tree traversal. Unlike the reverse
681 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100682 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100683
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000684 HBasicBlock* entry_block_;
685 HBasicBlock* exit_block_;
686
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100687 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100688 uint16_t maximum_number_of_out_vregs_;
689
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100690 // The number of virtual registers in this method. Contains the parameters.
691 uint16_t number_of_vregs_;
692
693 // The number of virtual registers used by parameters of this method.
694 uint16_t number_of_in_vregs_;
695
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000696 // Number of vreg size slots that the temporaries use (used in baseline compiler).
697 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100698
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800699 // Flag whether there are bounds checks in the graph. We can skip
700 // BCE if it's false. It's only best effort to keep it up to date in
701 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400702 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800703
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800704 // Flag whether there are try/catch blocks in the graph. We will skip
705 // try/catch-related passes if it's false. It's only best effort to keep
706 // it up to date in the presence of code elimination so there might be
707 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000708 bool has_try_catch_;
709
Artem Serov2808be82018-12-20 19:15:11 +0000710 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
711 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
712 bool has_monitor_operations_;
713
Aart Bikb13c65b2017-03-21 20:14:07 -0700714 // Flag whether SIMD instructions appear in the graph. If true, the
715 // code generators may have to be more careful spilling the wider
716 // contents of SIMD registers.
717 bool has_simd_;
718
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800719 // Flag whether there are any loops in the graph. We can skip loop
720 // optimization if it's false. It's only best effort to keep it up
721 // to date in the presence of code elimination so there might be false
722 // positives.
723 bool has_loops_;
724
725 // Flag whether there are any irreducible loops in the graph. It's only
726 // best effort to keep it up to date in the presence of code elimination
727 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000728 bool has_irreducible_loops_;
729
Hans Boehm206348c2018-12-05 11:11:33 -0800730 // Is the code known to be robust against eliminating dead references
731 // and the effects of early finalization? If false, dead reference variables
732 // are kept if they might be visible to the garbage collector.
733 // Currently this means that the class was declared to be dead-reference-safe,
734 // the method accesses no reachability-sensitive fields or data, and the same
735 // is true for any methods that were inlined into the current one.
736 bool dead_reference_safe_;
737
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000738 // Indicates whether the graph should be compiled in a way that
739 // ensures full debuggability. If false, we can apply more
740 // aggressive optimizations that may limit the level of debugging.
741 const bool debuggable_;
742
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000743 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000744 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000745
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100746 // The dex file from which the method is from.
747 const DexFile& dex_file_;
748
749 // The method index in the dex file.
750 const uint32_t method_idx_;
751
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100752 // If inlined, this encodes how the callee is being invoked.
753 const InvokeType invoke_type_;
754
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100755 // Whether the graph has been transformed to SSA form. Only used
756 // in debug mode to ensure we are not using properties only valid
757 // for non-SSA form (like the number of temporaries).
758 bool in_ssa_form_;
759
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800760 // Number of CHA guards in the graph. Used to short-circuit the
761 // CHA guard optimization pass when there is no CHA guard left.
762 uint32_t number_of_cha_guards_;
763
Mathieu Chartiere401d142015-04-22 13:56:20 -0700764 const InstructionSet instruction_set_;
765
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000766 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000767 HNullConstant* cached_null_constant_;
768 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000769 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000770 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000771 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000772
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100773 HCurrentMethod* cached_current_method_;
774
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100775 // The ArtMethod this graph is for. Note that for AOT, it may be null,
776 // for example for methods whose declaring class could not be resolved
777 // (such as when the superclass could not be found).
778 ArtMethod* art_method_;
779
David Brazdil4833f5a2015-12-16 10:37:39 +0000780 // Keep the RTI of inexact Object to avoid having to pass stack handle
781 // collection pointer to passes which may create NullConstant.
782 ReferenceTypeInfo inexact_object_rti_;
783
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000784 // Whether we are compiling this graph for on stack replacement: this will
785 // make all loops seen as irreducible and emit special stack maps to mark
786 // compiled code entries which the interpreter can directly jump to.
787 const bool osr_;
788
Mingyao Yang063fc772016-08-02 11:02:54 -0700789 // List of methods that are assumed to have single implementation.
790 ArenaSet<ArtMethod*> cha_single_implementation_list_;
791
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000792 // Whether we are JIT compiling in the shared region area, putting
793 // restrictions on, for example, how literals are being generated.
794 bool is_shared_jit_code_;
795
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000796 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100797 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000798 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000799 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800 DISALLOW_COPY_AND_ASSIGN(HGraph);
801};
802
Vladimir Markof9f64412015-09-02 14:05:49 +0100803class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000804 public:
805 HLoopInformation(HBasicBlock* header, HGraph* graph)
806 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100807 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000808 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100809 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100810 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100811 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100812 blocks_(graph->GetAllocator(),
813 graph->GetBlocks().size(),
814 true,
815 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100816 back_edges_.reserve(kDefaultNumberOfBackEdges);
817 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100818
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000819 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100820 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000821
822 void Dump(std::ostream& os);
823
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100824 HBasicBlock* GetHeader() const {
825 return header_;
826 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000827
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000828 void SetHeader(HBasicBlock* block) {
829 header_ = block;
830 }
831
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100832 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
833 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
834 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
835
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000836 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100837 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000838 }
839
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100840 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100841 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100842 }
843
David Brazdil46e2a392015-03-16 17:31:52 +0000844 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100845 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100846 }
847
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000848 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100849 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000850 }
851
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100852 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100853
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100854 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100855 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 }
857
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100858 // Returns the lifetime position of the back edge that has the
859 // greatest lifetime position.
860 size_t GetLifetimeEnd() const;
861
862 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100863 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100864 }
865
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000866 // Finds blocks that are part of this loop.
867 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100868
Artem Serov7f4aff62017-06-21 17:02:18 +0100869 // Updates blocks population of the loop and all of its outer' ones recursively after the
870 // population of the inner loop is updated.
871 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
872
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100873 // Returns whether this loop information contains `block`.
874 // Note that this loop information *must* be populated before entering this function.
875 bool Contains(const HBasicBlock& block) const;
876
877 // Returns whether this loop information is an inner loop of `other`.
878 // Note that `other` *must* be populated before entering this function.
879 bool IsIn(const HLoopInformation& other) const;
880
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800881 // Returns true if instruction is not defined within this loop.
882 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700883
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100884 const ArenaBitVector& GetBlocks() const { return blocks_; }
885
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000886 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000887 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000888
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000889 void ClearAllBlocks() {
890 blocks_.ClearAllBits();
891 }
892
David Brazdil3f4a5222016-05-06 12:46:21 +0100893 bool HasBackEdgeNotDominatedByHeader() const;
894
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100895 bool IsPopulated() const {
896 return blocks_.GetHighestBitSet() != -1;
897 }
898
Anton Shaminf89381f2016-05-16 16:44:13 +0600899 bool DominatesAllBackEdges(HBasicBlock* block);
900
David Sehrc757dec2016-11-04 15:48:34 -0700901 bool HasExitEdge() const;
902
Artem Serov7f4aff62017-06-21 17:02:18 +0100903 // Resets back edge and blocks-in-loop data.
904 void ResetBasicBlockData() {
905 back_edges_.clear();
906 ClearAllBlocks();
907 }
908
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000909 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100910 // Internal recursive implementation of `Populate`.
911 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100912 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100913
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000914 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100915 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000916 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100917 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100918 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100919 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000920
921 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
922};
923
David Brazdilec16f792015-08-19 15:04:01 +0100924// Stores try/catch information for basic blocks.
925// Note that HGraph is constructed so that catch blocks cannot simultaneously
926// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100927class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100928 public:
929 // Try block information constructor.
930 explicit TryCatchInformation(const HTryBoundary& try_entry)
931 : try_entry_(&try_entry),
932 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000933 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100934 DCHECK(try_entry_ != nullptr);
935 }
936
937 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800938 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100939 : try_entry_(nullptr),
940 catch_dex_file_(&dex_file),
941 catch_type_index_(catch_type_index) {}
942
943 bool IsTryBlock() const { return try_entry_ != nullptr; }
944
945 const HTryBoundary& GetTryEntry() const {
946 DCHECK(IsTryBlock());
947 return *try_entry_;
948 }
949
950 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
951
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000952 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100953 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000954 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100955 }
956
Andreas Gampea5b09a62016-11-17 15:21:22 -0800957 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100958 DCHECK(IsCatchBlock());
959 return catch_type_index_;
960 }
961
962 const DexFile& GetCatchDexFile() const {
963 DCHECK(IsCatchBlock());
964 return *catch_dex_file_;
965 }
966
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000967 void SetInvalidTypeIndex() {
968 catch_type_index_ = dex::TypeIndex::Invalid();
969 }
970
David Brazdilec16f792015-08-19 15:04:01 +0100971 private:
972 // One of possibly several TryBoundary instructions entering the block's try.
973 // Only set for try blocks.
974 const HTryBoundary* try_entry_;
975
976 // Exception type information. Only set for catch blocks.
977 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000978 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100979};
980
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100981static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100982static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100983
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000984// A block in a method. Contains the list of instructions represented
985// as a double linked list. Each block knows its predecessors and
986// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100987
Vladimir Markof9f64412015-09-02 14:05:49 +0100988class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000989 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700990 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000991 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100992 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
993 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000994 loop_information_(nullptr),
995 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100996 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100997 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100998 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100999 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001000 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001001 try_catch_information_(nullptr) {
1002 predecessors_.reserve(kDefaultNumberOfPredecessors);
1003 successors_.reserve(kDefaultNumberOfSuccessors);
1004 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1005 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001006
Vladimir Marko60584552015-09-03 13:35:12 +00001007 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001008 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009 }
1010
Vladimir Marko60584552015-09-03 13:35:12 +00001011 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001012 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013 }
1014
David Brazdild26a4112015-11-10 11:07:31 +00001015 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1016 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1017
Vladimir Marko60584552015-09-03 13:35:12 +00001018 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1019 return ContainsElement(successors_, block, start_from);
1020 }
1021
1022 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001023 return dominated_blocks_;
1024 }
1025
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001026 bool IsEntryBlock() const {
1027 return graph_->GetEntryBlock() == this;
1028 }
1029
1030 bool IsExitBlock() const {
1031 return graph_->GetExitBlock() == this;
1032 }
1033
David Brazdil46e2a392015-03-16 17:31:52 +00001034 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001035 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001036 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001037 bool IsSingleTryBoundary() const;
1038
1039 // Returns true if this block emits nothing but a jump.
1040 bool IsSingleJump() const {
1041 HLoopInformation* loop_info = GetLoopInformation();
1042 return (IsSingleGoto() || IsSingleTryBoundary())
1043 // Back edges generate a suspend check.
1044 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1045 }
David Brazdil46e2a392015-03-16 17:31:52 +00001046
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001047 void AddBackEdge(HBasicBlock* back_edge) {
1048 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001049 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001050 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001051 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001052 loop_information_->AddBackEdge(back_edge);
1053 }
1054
Artem Serov7f4aff62017-06-21 17:02:18 +01001055 // Registers a back edge; if the block was not a loop header before the call associates a newly
1056 // created loop info with it.
1057 //
1058 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1059 // info for all blocks during back edges recalculation.
1060 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1061 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1062 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1063 }
1064 loop_information_->AddBackEdge(back_edge);
1065 }
1066
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001067 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001068 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001070 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001071 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001072 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001073
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001074 HBasicBlock* GetDominator() const { return dominator_; }
1075 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001076 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1077
1078 void RemoveDominatedBlock(HBasicBlock* block) {
1079 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001080 }
Vladimir Marko60584552015-09-03 13:35:12 +00001081
1082 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1083 ReplaceElement(dominated_blocks_, existing, new_block);
1084 }
1085
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001086 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001087
1088 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001089 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001090 }
1091
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001092 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1093 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001094 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001095 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001096 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1097 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001098
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001099 HInstruction* GetFirstInstructionDisregardMoves() const;
1100
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001101 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001102 successors_.push_back(block);
1103 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001104 }
1105
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001106 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1107 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001108 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001109 new_block->predecessors_.push_back(this);
1110 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001111 }
1112
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001113 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1114 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001115 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001116 new_block->successors_.push_back(this);
1117 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001118 }
1119
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001120 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001121 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001122 // `predecessor` and `successor`.
1123 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1124 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001125 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001126 successor->predecessors_[predecessor_index] = this;
1127 predecessor->successors_[successor_index] = this;
1128 successors_.push_back(successor);
1129 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001130 }
1131
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001132 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001133 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001134 }
1135
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001136 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001137 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001138 }
1139
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001140 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001141 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001142 }
1143
1144 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001145 predecessors_.push_back(block);
1146 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001147 }
1148
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001149 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001150 DCHECK_EQ(predecessors_.size(), 2u);
1151 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001152 }
1153
David Brazdil769c9e52015-04-27 13:54:09 +01001154 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001155 DCHECK_EQ(successors_.size(), 2u);
1156 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001157 }
1158
David Brazdilfc6a86a2015-06-26 10:33:45 +00001159 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001160 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001161 }
1162
David Brazdilfc6a86a2015-06-26 10:33:45 +00001163 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001164 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001165 }
1166
David Brazdilfc6a86a2015-06-26 10:33:45 +00001167 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001168 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001169 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001170 }
1171
1172 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001173 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001174 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001175 }
1176
1177 // Returns whether the first occurrence of `predecessor` in the list of
1178 // predecessors is at index `idx`.
1179 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001180 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001181 return GetPredecessorIndexOf(predecessor) == idx;
1182 }
1183
David Brazdild7558da2015-09-22 13:04:14 +01001184 // Create a new block between this block and its predecessors. The new block
1185 // is added to the graph, all predecessor edges are relinked to it and an edge
1186 // is created to `this`. Returns the new empty block. Reverse post order or
1187 // loop and try/catch information are not updated.
1188 HBasicBlock* CreateImmediateDominator();
1189
David Brazdilfc6a86a2015-06-26 10:33:45 +00001190 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001191 // created, latter block. Note that this method will add the block to the
1192 // graph, create a Goto at the end of the former block and will create an edge
1193 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001194 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001195 HBasicBlock* SplitBefore(HInstruction* cursor);
1196
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001197 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001198 // created block. Note that this method just updates raw block information,
1199 // like predecessors, successors, dominators, and instruction list. It does not
1200 // update the graph, reverse post order, loop information, nor make sure the
1201 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001202 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1203
1204 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1205 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001206
1207 // Merge `other` at the end of `this`. Successors and dominated blocks of
1208 // `other` are changed to be successors and dominated blocks of `this`. Note
1209 // that this method does not update the graph, reverse post order, loop
1210 // information, nor make sure the blocks are consistent (for example ending
1211 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001212 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001213
1214 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1215 // of `this` are moved to `other`.
1216 // Note that this method does not update the graph, reverse post order, loop
1217 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001218 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001219 void ReplaceWith(HBasicBlock* other);
1220
Aart Bik6b69e0a2017-01-11 10:20:43 -08001221 // Merges the instructions of `other` at the end of `this`.
1222 void MergeInstructionsWith(HBasicBlock* other);
1223
David Brazdil2d7352b2015-04-20 14:52:42 +01001224 // Merge `other` at the end of `this`. This method updates loops, reverse post
1225 // order, links to predecessors, successors, dominators and deletes the block
1226 // from the graph. The two blocks must be successive, i.e. `this` the only
1227 // predecessor of `other` and vice versa.
1228 void MergeWith(HBasicBlock* other);
1229
1230 // Disconnects `this` from all its predecessors, successors and dominator,
1231 // removes it from all loops it is included in and eventually from the graph.
1232 // The block must not dominate any other block. Predecessors and successors
1233 // are safely updated.
1234 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001235
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001236 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001237 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001238 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001239 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001240 // Replace phi `initial` with `replacement` within this block.
1241 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001242 // Replace instruction `initial` with `replacement` within this block.
1243 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1244 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001245 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001246 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001247 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1248 // instruction list. With 'ensure_safety' set to true, it verifies that the
1249 // instruction is not in use and removes it from the use lists of its inputs.
1250 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1251 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001252 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001253
1254 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001255 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001256 }
1257
Roland Levillain6b879dd2014-09-22 17:13:44 +01001258 bool IsLoopPreHeaderFirstPredecessor() const {
1259 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001260 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001261 }
1262
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001263 bool IsFirstPredecessorBackEdge() const {
1264 DCHECK(IsLoopHeader());
1265 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1266 }
1267
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001268 HLoopInformation* GetLoopInformation() const {
1269 return loop_information_;
1270 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001271
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001272 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001273 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001274 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001275 void SetInLoop(HLoopInformation* info) {
1276 if (IsLoopHeader()) {
1277 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001278 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001279 loop_information_ = info;
1280 } else if (loop_information_->Contains(*info->GetHeader())) {
1281 // Block is currently part of an outer loop. Make it part of this inner loop.
1282 // Note that a non loop header having a loop information means this loop information
1283 // has already been populated
1284 loop_information_ = info;
1285 } else {
1286 // Block is part of an inner loop. Do not update the loop information.
1287 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1288 // at this point, because this method is being called while populating `info`.
1289 }
1290 }
1291
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001292 // Raw update of the loop information.
1293 void SetLoopInformation(HLoopInformation* info) {
1294 loop_information_ = info;
1295 }
1296
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001297 bool IsInLoop() const { return loop_information_ != nullptr; }
1298
David Brazdilec16f792015-08-19 15:04:01 +01001299 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1300
1301 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1302 try_catch_information_ = try_catch_information;
1303 }
1304
1305 bool IsTryBlock() const {
1306 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1307 }
1308
1309 bool IsCatchBlock() const {
1310 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1311 }
David Brazdilffee3d32015-07-06 11:48:53 +01001312
1313 // Returns the try entry that this block's successors should have. They will
1314 // be in the same try, unless the block ends in a try boundary. In that case,
1315 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001316 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001317
Aart Bik75ff2c92018-04-21 01:28:11 +00001318 bool HasThrowingInstructions() const;
1319
David Brazdila4b8c212015-05-07 09:59:30 +01001320 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001321 bool Dominates(HBasicBlock* block) const;
1322
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001323 size_t GetLifetimeStart() const { return lifetime_start_; }
1324 size_t GetLifetimeEnd() const { return lifetime_end_; }
1325
1326 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1327 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1328
David Brazdil8d5b8b22015-03-24 10:51:52 +00001329 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001330 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001331 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001332 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001333 bool HasSinglePhi() const;
1334
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001335 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001336 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001337 ArenaVector<HBasicBlock*> predecessors_;
1338 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001339 HInstructionList instructions_;
1340 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001341 HLoopInformation* loop_information_;
1342 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001343 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001344 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001345 // The dex program counter of the first instruction of this block.
1346 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001347 size_t lifetime_start_;
1348 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001349 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001350
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001351 friend class HGraph;
1352 friend class HInstruction;
1353
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001354 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1355};
1356
David Brazdilb2bd1c52015-03-25 11:17:37 +00001357// Iterates over the LoopInformation of all loops which contain 'block'
1358// from the innermost to the outermost.
1359class HLoopInformationOutwardIterator : public ValueObject {
1360 public:
1361 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1362 : current_(block.GetLoopInformation()) {}
1363
1364 bool Done() const { return current_ == nullptr; }
1365
1366 void Advance() {
1367 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001368 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001369 }
1370
1371 HLoopInformation* Current() const {
1372 DCHECK(!Done());
1373 return current_;
1374 }
1375
1376 private:
1377 HLoopInformation* current_;
1378
1379 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1380};
1381
Alexandre Ramesef20f712015-06-09 10:29:30 +01001382#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001383 M(Above, Condition) \
1384 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001385 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001386 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001387 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001388 M(ArrayGet, Instruction) \
1389 M(ArrayLength, Instruction) \
1390 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001391 M(Below, Condition) \
1392 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001393 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001394 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001395 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001396 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001397 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001398 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001399 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001400 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001401 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001402 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001403 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001404 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001405 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001406 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001407 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001408 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001409 M(Exit, Instruction) \
1410 M(FloatConstant, Constant) \
1411 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001412 M(GreaterThan, Condition) \
1413 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001415 M(InstanceFieldGet, Instruction) \
1416 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001417 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001418 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001419 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001420 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001421 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001422 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001424 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001425 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001426 M(LessThan, Condition) \
1427 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001428 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001429 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001430 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001431 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001432 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001433 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001434 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001435 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001436 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001437 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001438 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001439 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001440 M(Neg, UnaryOperation) \
1441 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001442 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001443 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001444 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001445 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001446 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001447 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001448 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001449 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001450 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001451 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001452 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001453 M(Return, Instruction) \
1454 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001455 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001456 M(Shl, BinaryOperation) \
1457 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001458 M(StaticFieldGet, Instruction) \
1459 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001460 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001461 M(UnresolvedInstanceFieldGet, Instruction) \
1462 M(UnresolvedInstanceFieldSet, Instruction) \
1463 M(UnresolvedStaticFieldGet, Instruction) \
1464 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001465 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001466 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001467 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001468 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001469 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001470 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001471 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001472 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001473 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001474 M(VecExtractScalar, VecUnaryOperation) \
1475 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001476 M(VecCnv, VecUnaryOperation) \
1477 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001478 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001479 M(VecNot, VecUnaryOperation) \
1480 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001481 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001482 M(VecSub, VecBinaryOperation) \
1483 M(VecMul, VecBinaryOperation) \
1484 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001485 M(VecMin, VecBinaryOperation) \
1486 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001487 M(VecAnd, VecBinaryOperation) \
1488 M(VecAndNot, VecBinaryOperation) \
1489 M(VecOr, VecBinaryOperation) \
1490 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001491 M(VecSaturationAdd, VecBinaryOperation) \
1492 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001493 M(VecShl, VecBinaryOperation) \
1494 M(VecShr, VecBinaryOperation) \
1495 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001496 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001497 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001498 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001499 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001500 M(VecLoad, VecMemoryOperation) \
1501 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001502
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001503/*
1504 * Instructions, shared across several (not all) architectures.
1505 */
1506#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1507#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1508#else
1509#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001510 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001511 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001512 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001513 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001514#endif
1515
Alexandre Ramesef20f712015-06-09 10:29:30 +01001516#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1517
1518#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1519
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001520#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001521#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001522#else
1523#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1524 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001525 M(MipsPackedSwitch, Instruction) \
1526 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001527#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001528
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001529#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1530
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001531#ifndef ART_ENABLE_CODEGEN_x86
1532#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1533#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001534#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1535 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001536 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001537 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001538 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001539#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001540
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301541#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1542#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301543 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301544 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301545#else
1546#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1547#endif
1548
Alexandre Ramesef20f712015-06-09 10:29:30 +01001549#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1550
1551#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1552 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001553 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001554 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1555 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001556 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001557 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001558 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301559 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1560 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001561
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001562#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1563 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001564 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001565 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001566 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001567 M(Invoke, Instruction) \
1568 M(VecOperation, Instruction) \
1569 M(VecUnaryOperation, VecOperation) \
1570 M(VecBinaryOperation, VecOperation) \
1571 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001572
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001573#define FOR_EACH_INSTRUCTION(M) \
1574 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1575 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1576
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001577#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001578FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1579#undef FORWARD_DECLARATION
1580
Vladimir Marko372f10e2016-05-17 16:30:10 +01001581#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001582 private: \
1583 H##type& operator=(const H##type&) = delete; \
1584 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001585 const char* DebugName() const override { return #type; } \
1586 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001587 DCHECK(IsClonable()); \
1588 return new (arena) H##type(*this->As##type()); \
1589 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001590 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001591
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001592#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001593 private: \
1594 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001595 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001596
Artem Serovcced8ba2017-07-19 18:18:09 +01001597#define DEFAULT_COPY_CONSTRUCTOR(type) \
1598 explicit H##type(const H##type& other) = default;
1599
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001600template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001601class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1602 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001603 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001604 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001605 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001606 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001607 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001608 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001609 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001610
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001611 private:
David Brazdiled596192015-01-23 10:39:45 +00001612 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001613 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001614
1615 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001616 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001617
Vladimir Marko46817b82016-03-29 12:21:58 +01001618 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001619
1620 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1621};
1622
David Brazdiled596192015-01-23 10:39:45 +00001623template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001624using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001625
David Brazdil1abb4192015-02-17 18:33:36 +00001626// This class is used by HEnvironment and HInstruction classes to record the
1627// instructions they use and pointers to the corresponding HUseListNodes kept
1628// by the used instructions.
1629template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001630class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001631 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001632 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1633 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001634
Vladimir Marko46817b82016-03-29 12:21:58 +01001635 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1636 : HUserRecord(old_record.instruction_, before_use_node) {}
1637 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1638 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001639 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001640 }
1641
1642 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001643 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1644 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001645
1646 private:
1647 // Instruction used by the user.
1648 HInstruction* instruction_;
1649
Vladimir Marko46817b82016-03-29 12:21:58 +01001650 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1651 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001652};
1653
Vladimir Markoe9004912016-06-16 16:50:52 +01001654// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1655// This is used for HInstruction::GetInputs() to return a container wrapper providing
1656// HInstruction* values even though the underlying container has HUserRecord<>s.
1657struct HInputExtractor {
1658 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1659 return record.GetInstruction();
1660 }
1661 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1662 return record.GetInstruction();
1663 }
1664};
1665
1666using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1667using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1668
Aart Bik854a02b2015-07-14 16:07:00 -07001669/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001670 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001671 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001672 * For write/read dependences on fields/arrays, the dependence analysis uses
1673 * type disambiguation (e.g. a float field write cannot modify the value of an
1674 * integer field read) and the access type (e.g. a reference array write cannot
1675 * modify the value of a reference field read [although it may modify the
1676 * reference fetch prior to reading the field, which is represented by its own
1677 * write/read dependence]). The analysis makes conservative points-to
1678 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1679 * the same, and any reference read depends on any reference read without
1680 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001681 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001682 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1683 * alive across the point where garbage collection might happen.
1684 *
1685 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1686 *
1687 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1688 * those instructions from the compiler perspective (between this instruction and the next one
1689 * in the IR).
1690 *
1691 * Note: Instructions which can cause GC only on a fatal slow path do not need
1692 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1693 * one. However the execution may return to compiled code if there is a catch block in the
1694 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1695 * set.
1696 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001697 * The internal representation uses 38-bit and is described in the table below.
1698 * The first line indicates the side effect, and for field/array accesses the
1699 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001700 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001701 * The two numbered lines below indicate the bit position in the bitfield (read
1702 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001703 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001704 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1705 * +-------------+---------+---------+--------------+---------+---------+
1706 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1707 * | 3 |333333322|222222221| 1 |111111110|000000000|
1708 * | 7 |654321098|765432109| 8 |765432109|876543210|
1709 *
1710 * Note that, to ease the implementation, 'changes' bits are least significant
1711 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001712 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001713class SideEffects : public ValueObject {
1714 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001715 SideEffects() : flags_(0) {}
1716
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001717 static SideEffects None() {
1718 return SideEffects(0);
1719 }
1720
1721 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001722 return SideEffects(kAllChangeBits | kAllDependOnBits);
1723 }
1724
1725 static SideEffects AllChanges() {
1726 return SideEffects(kAllChangeBits);
1727 }
1728
1729 static SideEffects AllDependencies() {
1730 return SideEffects(kAllDependOnBits);
1731 }
1732
1733 static SideEffects AllExceptGCDependency() {
1734 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1735 }
1736
1737 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001738 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001739 }
1740
Aart Bik34c3ba92015-07-20 14:08:59 -07001741 static SideEffects AllWrites() {
1742 return SideEffects(kAllWrites);
1743 }
1744
1745 static SideEffects AllReads() {
1746 return SideEffects(kAllReads);
1747 }
1748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001750 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001751 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001752 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001753 }
1754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001756 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001757 }
1758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001760 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001761 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001762 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001763 }
1764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001765 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001766 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001767 }
1768
Artem Serovd1aa7d02018-06-22 11:35:46 +01001769 // Returns whether GC might happen across this instruction from the compiler perspective so
1770 // the next instruction in the IR would see that.
1771 //
1772 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001773 static SideEffects CanTriggerGC() {
1774 return SideEffects(1ULL << kCanTriggerGCBit);
1775 }
1776
Artem Serovd1aa7d02018-06-22 11:35:46 +01001777 // Returns whether the instruction must not be alive across a GC point.
1778 //
1779 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001780 static SideEffects DependsOnGC() {
1781 return SideEffects(1ULL << kDependsOnGCBit);
1782 }
1783
Aart Bik854a02b2015-07-14 16:07:00 -07001784 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001785 SideEffects Union(SideEffects other) const {
1786 return SideEffects(flags_ | other.flags_);
1787 }
1788
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001789 SideEffects Exclusion(SideEffects other) const {
1790 return SideEffects(flags_ & ~other.flags_);
1791 }
1792
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001793 void Add(SideEffects other) {
1794 flags_ |= other.flags_;
1795 }
1796
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001797 bool Includes(SideEffects other) const {
1798 return (other.flags_ & flags_) == other.flags_;
1799 }
1800
1801 bool HasSideEffects() const {
1802 return (flags_ & kAllChangeBits);
1803 }
1804
1805 bool HasDependencies() const {
1806 return (flags_ & kAllDependOnBits);
1807 }
1808
1809 // Returns true if there are no side effects or dependencies.
1810 bool DoesNothing() const {
1811 return flags_ == 0;
1812 }
1813
Aart Bik854a02b2015-07-14 16:07:00 -07001814 // Returns true if something is written.
1815 bool DoesAnyWrite() const {
1816 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001817 }
1818
Aart Bik854a02b2015-07-14 16:07:00 -07001819 // Returns true if something is read.
1820 bool DoesAnyRead() const {
1821 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001822 }
1823
Aart Bik854a02b2015-07-14 16:07:00 -07001824 // Returns true if potentially everything is written and read
1825 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001826 bool DoesAllReadWrite() const {
1827 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1828 }
1829
Aart Bik854a02b2015-07-14 16:07:00 -07001830 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001831 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001832 }
1833
Roland Levillain0d5a2812015-11-13 10:07:31 +00001834 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001835 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001836 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1837 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001838 }
1839
1840 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001841 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001842 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001843 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001844 for (int s = kLastBit; s >= 0; s--) {
1845 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1846 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1847 // This is a bit for the GC side effect.
1848 if (current_bit_is_set) {
1849 flags += "GC";
1850 }
Aart Bik854a02b2015-07-14 16:07:00 -07001851 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001852 } else {
1853 // This is a bit for the array/field analysis.
1854 // The underscore character stands for the 'can trigger GC' bit.
1855 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1856 if (current_bit_is_set) {
1857 flags += kDebug[s];
1858 }
1859 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1860 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1861 flags += "|";
1862 }
1863 }
Aart Bik854a02b2015-07-14 16:07:00 -07001864 }
1865 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001866 }
1867
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001868 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001869
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001870 private:
1871 static constexpr int kFieldArrayAnalysisBits = 9;
1872
1873 static constexpr int kFieldWriteOffset = 0;
1874 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1875 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1876 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1877
1878 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1879
1880 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1881 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1882 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1883 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1884
1885 static constexpr int kLastBit = kDependsOnGCBit;
1886 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1887
1888 // Aliases.
1889
1890 static_assert(kChangeBits == kDependOnBits,
1891 "the 'change' bits should match the 'depend on' bits.");
1892
1893 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1894 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1895 static constexpr uint64_t kAllWrites =
1896 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1897 static constexpr uint64_t kAllReads =
1898 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001899
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001900 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001901 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001902 int shift;
1903 switch (type) {
1904 case DataType::Type::kReference: shift = 0; break;
1905 case DataType::Type::kBool: shift = 1; break;
1906 case DataType::Type::kInt8: shift = 2; break;
1907 case DataType::Type::kUint16: shift = 3; break;
1908 case DataType::Type::kInt16: shift = 4; break;
1909 case DataType::Type::kInt32: shift = 5; break;
1910 case DataType::Type::kInt64: shift = 6; break;
1911 case DataType::Type::kFloat32: shift = 7; break;
1912 case DataType::Type::kFloat64: shift = 8; break;
1913 default:
1914 LOG(FATAL) << "Unexpected data type " << type;
1915 UNREACHABLE();
1916 }
Aart Bik854a02b2015-07-14 16:07:00 -07001917 DCHECK_LE(kFieldWriteOffset, shift);
1918 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001919 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001920 }
1921
1922 // Private constructor on direct flags value.
1923 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1924
1925 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001926};
1927
David Brazdiled596192015-01-23 10:39:45 +00001928// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001929class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001930 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001931 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001932 size_t number_of_vregs,
1933 ArtMethod* method,
1934 uint32_t dex_pc,
1935 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001936 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1937 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001938 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001939 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001940 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001941 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001942 }
1943
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001944 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1945 const HEnvironment& to_copy,
1946 HInstruction* holder)
1947 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001948 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001949 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001950 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001951 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001952
Vladimir Markoec32f642017-06-02 10:51:55 +01001953 void AllocateLocations() {
1954 DCHECK(locations_.empty());
1955 locations_.resize(vregs_.size());
1956 }
1957
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001958 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001959 if (parent_ != nullptr) {
1960 parent_->SetAndCopyParentChain(allocator, parent);
1961 } else {
1962 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1963 parent_->CopyFrom(parent);
1964 if (parent->GetParent() != nullptr) {
1965 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1966 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001967 }
David Brazdiled596192015-01-23 10:39:45 +00001968 }
1969
Vladimir Marko69d310e2017-10-09 14:12:23 +01001970 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001971 void CopyFrom(HEnvironment* environment);
1972
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001973 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1974 // input to the loop phi instead. This is for inserting instructions that
1975 // require an environment (like HDeoptimization) in the loop pre-header.
1976 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001977
1978 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001979 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001980 }
1981
1982 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001983 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001984 }
1985
David Brazdil1abb4192015-02-17 18:33:36 +00001986 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001987
Artem Serovca210e32017-12-15 13:43:20 +00001988 // Replaces the input at the position 'index' with the replacement; the replacement and old
1989 // input instructions' env_uses_ lists are adjusted. The function works similar to
1990 // HInstruction::ReplaceInput.
1991 void ReplaceInput(HInstruction* replacement, size_t index);
1992
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001993 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001994
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001995 HEnvironment* GetParent() const { return parent_; }
1996
1997 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001998 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001999 }
2000
2001 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002002 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002003 }
2004
2005 uint32_t GetDexPc() const {
2006 return dex_pc_;
2007 }
2008
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002009 ArtMethod* GetMethod() const {
2010 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002011 }
2012
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002013 HInstruction* GetHolder() const {
2014 return holder_;
2015 }
2016
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002017
2018 bool IsFromInlinedInvoke() const {
2019 return GetParent() != nullptr;
2020 }
2021
David Brazdiled596192015-01-23 10:39:45 +00002022 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002023 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2024 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002025 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002026 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002027 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002028
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002029 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002030 HInstruction* const holder_;
2031
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002032 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002033
2034 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2035};
2036
Vladimir Markof9f64412015-09-02 14:05:49 +01002037class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002038 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302039#define DECLARE_KIND(type, super) k##type,
2040 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002041 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302042 kLastInstructionKind
2043 };
2044#undef DECLARE_KIND
2045
2046 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002047 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2048
2049 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002050 : previous_(nullptr),
2051 next_(nullptr),
2052 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002053 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002054 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002055 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002056 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002057 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002058 locations_(nullptr),
2059 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002060 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002061 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002062 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302063 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002064 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002065 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2066 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002067
Dave Allison20dfc792014-06-16 20:44:29 -07002068 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002070
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002071 HInstruction* GetNext() const { return next_; }
2072 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002073
Calin Juravle77520bc2015-01-12 18:45:46 +00002074 HInstruction* GetNextDisregardingMoves() const;
2075 HInstruction* GetPreviousDisregardingMoves() const;
2076
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002077 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002078 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002079 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002080 bool IsInBlock() const { return block_ != nullptr; }
2081 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002082 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2083 bool IsIrreducibleLoopHeaderPhi() const {
2084 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2085 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002086
Vladimir Marko372f10e2016-05-17 16:30:10 +01002087 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2088
2089 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2090 // One virtual method is enough, just const_cast<> and then re-add the const.
2091 return ArrayRef<const HUserRecord<HInstruction*>>(
2092 const_cast<HInstruction*>(this)->GetInputRecords());
2093 }
2094
Vladimir Markoe9004912016-06-16 16:50:52 +01002095 HInputsRef GetInputs() {
2096 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002097 }
2098
Vladimir Markoe9004912016-06-16 16:50:52 +01002099 HConstInputsRef GetInputs() const {
2100 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002101 }
2102
2103 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002104 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002105
Aart Bik2767f4b2016-10-28 15:03:53 -07002106 bool HasInput(HInstruction* input) const {
2107 for (const HInstruction* i : GetInputs()) {
2108 if (i == input) {
2109 return true;
2110 }
2111 }
2112 return false;
2113 }
2114
Vladimir Marko372f10e2016-05-17 16:30:10 +01002115 void SetRawInputAt(size_t index, HInstruction* input) {
2116 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2117 }
2118
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119 virtual void Accept(HGraphVisitor* visitor) = 0;
2120 virtual const char* DebugName() const = 0;
2121
Vladimir Markobd785672018-05-03 17:09:09 +01002122 DataType::Type GetType() const {
2123 return TypeField::Decode(GetPackedFields());
2124 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002125
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002126 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002127
2128 uint32_t GetDexPc() const { return dex_pc_; }
2129
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002130 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002131
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002132 // Can the instruction throw?
2133 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2134 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002135 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002136
2137 // Does the instruction always throw an exception unconditionally?
2138 virtual bool AlwaysThrows() const { return false; }
2139
David Brazdilec16f792015-08-19 15:04:01 +01002140 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002141
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002142 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002143 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002144
Calin Juravle10e244f2015-01-26 18:54:32 +00002145 // Does not apply for all instructions, but having this at top level greatly
2146 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002147 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002148 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002150 return true;
2151 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002152
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002153 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002154 return false;
2155 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002156
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002157 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2158 // with it. Otherwise return null.
2159 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002160 // Go over previous non-move instructions that are emitted at use site.
2161 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2162 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2163 if (prev_not_move->IsNullCheck()) {
2164 return prev_not_move->AsNullCheck();
2165 }
2166 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002167 }
2168 return nullptr;
2169 }
2170
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002171 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002172 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002173 }
2174
Calin Juravle2e768302015-07-28 14:41:11 +00002175 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002176
Calin Juravle61d544b2015-02-23 16:46:57 +00002177 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002178 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002179 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002180 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002181 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002182
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002183 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002184 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002185 // Note: fixup_end remains valid across push_front().
2186 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2187 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002188 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002189 uses_.push_front(*new_node);
2190 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002191 }
2192
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002194 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002195 // Note: env_fixup_end remains valid across push_front().
2196 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2197 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002198 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002199 env_uses_.push_front(*new_node);
2200 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002201 }
2202
David Brazdil1abb4192015-02-17 18:33:36 +00002203 void RemoveAsUserOfInput(size_t input) {
2204 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002205 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2206 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2207 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002208 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002209
Vladimir Marko372f10e2016-05-17 16:30:10 +01002210 void RemoveAsUserOfAllInputs() {
2211 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2212 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2213 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2214 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2215 }
2216 }
2217
David Brazdil1abb4192015-02-17 18:33:36 +00002218 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2219 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002220
Vladimir Marko46817b82016-03-29 12:21:58 +01002221 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2222 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2223 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002224 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002225 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002226 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227
Aart Bikcc42be02016-10-20 16:14:16 -07002228 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002229 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002230 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002231 !CanThrow() &&
2232 !IsSuspendCheck() &&
2233 !IsControlFlow() &&
2234 !IsNativeDebugInfo() &&
2235 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002236 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002237 !IsMemoryBarrier() &&
2238 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002239 }
2240
Aart Bikcc42be02016-10-20 16:14:16 -07002241 bool IsDeadAndRemovable() const {
2242 return IsRemovable() && !HasUses();
2243 }
2244
Roland Levillain6c82d402014-10-13 16:10:27 +01002245 // Does this instruction strictly dominate `other_instruction`?
2246 // Returns false if this instruction and `other_instruction` are the same.
2247 // Aborts if this instruction and `other_instruction` are both phis.
2248 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002249
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002250 int GetId() const { return id_; }
2251 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002252
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002253 int GetSsaIndex() const { return ssa_index_; }
2254 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2255 bool HasSsaIndex() const { return ssa_index_ != -1; }
2256
2257 bool HasEnvironment() const { return environment_ != nullptr; }
2258 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002259 // Set the `environment_` field. Raw because this method does not
2260 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002261 void SetRawEnvironment(HEnvironment* environment) {
2262 DCHECK(environment_ == nullptr);
2263 DCHECK_EQ(environment->GetHolder(), this);
2264 environment_ = environment;
2265 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002266
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002267 void InsertRawEnvironment(HEnvironment* environment) {
2268 DCHECK(environment_ != nullptr);
2269 DCHECK_EQ(environment->GetHolder(), this);
2270 DCHECK(environment->GetParent() == nullptr);
2271 environment->parent_ = environment_;
2272 environment_ = environment;
2273 }
2274
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002275 void RemoveEnvironment();
2276
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002277 // Set the environment of this instruction, copying it from `environment`. While
2278 // copying, the uses lists are being updated.
2279 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002280 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002281 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002282 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002283 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002284 if (environment->GetParent() != nullptr) {
2285 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2286 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002287 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002288
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002289 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2290 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002291 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002292 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002293 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002294 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002295 if (environment->GetParent() != nullptr) {
2296 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2297 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002298 }
2299
Nicolas Geoffray39468442014-09-02 15:17:15 +01002300 // Returns the number of entries in the environment. Typically, that is the
2301 // number of dex registers in a method. It could be more in case of inlining.
2302 size_t EnvironmentSize() const;
2303
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002304 LocationSummary* GetLocations() const { return locations_; }
2305 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002306
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002307 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002308 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002309 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002310 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002311
Alexandre Rames188d4312015-04-09 18:30:21 +01002312 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2313 // uses of this instruction by `other` are *not* updated.
2314 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2315 ReplaceWith(other);
2316 other->ReplaceInput(this, use_index);
2317 }
2318
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002319 // Move `this` instruction before `cursor`
2320 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002321
Vladimir Markofb337ea2015-11-25 15:25:10 +00002322 // Move `this` before its first user and out of any loops. If there is no
2323 // out-of-loop user that dominates all other users, move the instruction
2324 // to the end of the out-of-loop common dominator of the user's blocks.
2325 //
2326 // This can be used only on non-throwing instructions with no side effects that
2327 // have at least one use but no environment uses.
2328 void MoveBeforeFirstUserAndOutOfLoops();
2329
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002330#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002331 bool Is##type() const;
2332
2333 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2334#undef INSTRUCTION_TYPE_CHECK
2335
2336#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002337 const H##type* As##type() const; \
2338 H##type* As##type();
2339
Vladimir Markoa90dd512018-05-04 15:04:45 +01002340 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2341#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002342
Artem Serovcced8ba2017-07-19 18:18:09 +01002343 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2344 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2345 // the instruction then the behaviour of this function is undefined.
2346 //
2347 // Note: It is semantically valid to create a clone of the instruction only until
2348 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2349 // copied.
2350 //
2351 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2352 // 'explicit HInstruction(const HInstruction& other)' for details.
2353 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2354 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2355 DebugName() << " " << GetId();
2356 UNREACHABLE();
2357 }
2358
2359 // Return whether instruction can be cloned (copied).
2360 virtual bool IsClonable() const { return false; }
2361
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002362 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002363 // TODO: this method is used by LICM and GVN with possibly different
2364 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002365 virtual bool CanBeMoved() const { return false; }
2366
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002367 // Returns whether any data encoded in the two instructions is equal.
2368 // This method does not look at the inputs. Both instructions must be
2369 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002370 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002371 return false;
2372 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002373
2374 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002375 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002376 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002377 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002378
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302379 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002380
2381 virtual size_t ComputeHashCode() const {
2382 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002383 for (const HInstruction* input : GetInputs()) {
2384 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002385 }
2386 return result;
2387 }
2388
2389 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002390 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002391 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002392
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002393 size_t GetLifetimePosition() const { return lifetime_position_; }
2394 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2395 LiveInterval* GetLiveInterval() const { return live_interval_; }
2396 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2397 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2398
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002399 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2400
2401 // Returns whether the code generation of the instruction will require to have access
2402 // to the current method. Such instructions are:
2403 // (1): Instructions that require an environment, as calling the runtime requires
2404 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002405 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2406 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002407 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002408 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002409 }
2410
Vladimir Markodc151b22015-10-15 18:02:30 +01002411 // Returns whether the code generation of the instruction will require to have access
2412 // to the dex cache of the current method's declaring class via the current method.
2413 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002414
Mark Mendellc4701932015-04-10 13:18:51 -04002415 // Does this instruction have any use in an environment before
2416 // control flow hits 'other'?
2417 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2418
2419 // Remove all references to environment uses of this instruction.
2420 // The caller must ensure that this is safe to do.
2421 void RemoveEnvironmentUsers();
2422
Vladimir Markoa1de9182016-02-25 11:37:38 +00002423 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2424 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002425
David Brazdil1abb4192015-02-17 18:33:36 +00002426 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002427 // If set, the machine code for this instruction is assumed to be generated by
2428 // its users. Used by liveness analysis to compute use positions accordingly.
2429 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2430 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302431 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2432 static constexpr size_t kFieldInstructionKindSize =
2433 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002434 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302435 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002436 static constexpr size_t kFieldTypeSize =
2437 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2438 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002439 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2440
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302441 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2442 "Too many generic packed fields");
2443
Vladimir Markobd785672018-05-03 17:09:09 +01002444 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2445
Vladimir Marko372f10e2016-05-17 16:30:10 +01002446 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2447 return GetInputRecords()[i];
2448 }
2449
2450 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2451 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2452 input_records[index] = input;
2453 }
David Brazdil1abb4192015-02-17 18:33:36 +00002454
Vladimir Markoa1de9182016-02-25 11:37:38 +00002455 uint32_t GetPackedFields() const {
2456 return packed_fields_;
2457 }
2458
2459 template <size_t flag>
2460 bool GetPackedFlag() const {
2461 return (packed_fields_ & (1u << flag)) != 0u;
2462 }
2463
2464 template <size_t flag>
2465 void SetPackedFlag(bool value = true) {
2466 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2467 }
2468
2469 template <typename BitFieldType>
2470 typename BitFieldType::value_type GetPackedField() const {
2471 return BitFieldType::Decode(packed_fields_);
2472 }
2473
2474 template <typename BitFieldType>
2475 void SetPackedField(typename BitFieldType::value_type value) {
2476 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2477 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2478 }
2479
Artem Serovcced8ba2017-07-19 18:18:09 +01002480 // Copy construction for the instruction (used for Clone function).
2481 //
2482 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2483 // prepare_for_register_allocator are not copied (set to default values).
2484 //
2485 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2486 // fine for most of them. However for some of the instructions a custom copy constructor must be
2487 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2488 // for copying them).
2489 explicit HInstruction(const HInstruction& other)
2490 : previous_(nullptr),
2491 next_(nullptr),
2492 block_(nullptr),
2493 dex_pc_(other.dex_pc_),
2494 id_(-1),
2495 ssa_index_(-1),
2496 packed_fields_(other.packed_fields_),
2497 environment_(nullptr),
2498 locations_(nullptr),
2499 live_interval_(nullptr),
2500 lifetime_position_(kNoLifetime),
2501 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302502 reference_type_handle_(other.reference_type_handle_) {
2503 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002504
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002505 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302506 using InstructionKindField =
2507 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2508
Vladimir Marko46817b82016-03-29 12:21:58 +01002509 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2510 auto before_use_node = uses_.before_begin();
2511 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2512 HInstruction* user = use_node->GetUser();
2513 size_t input_index = use_node->GetIndex();
2514 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2515 before_use_node = use_node;
2516 }
2517 }
2518
2519 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2520 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2521 if (next != uses_.end()) {
2522 HInstruction* next_user = next->GetUser();
2523 size_t next_index = next->GetIndex();
2524 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2525 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2526 }
2527 }
2528
2529 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2530 auto before_env_use_node = env_uses_.before_begin();
2531 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2532 HEnvironment* user = env_use_node->GetUser();
2533 size_t input_index = env_use_node->GetIndex();
2534 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2535 before_env_use_node = env_use_node;
2536 }
2537 }
2538
2539 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2540 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2541 if (next != env_uses_.end()) {
2542 HEnvironment* next_user = next->GetUser();
2543 size_t next_index = next->GetIndex();
2544 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2545 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2546 }
2547 }
David Brazdil1abb4192015-02-17 18:33:36 +00002548
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002549 HInstruction* previous_;
2550 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002551 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002552 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002553
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002554 // An instruction gets an id when it is added to the graph.
2555 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002556 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002557 int id_;
2558
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002559 // When doing liveness analysis, instructions that have uses get an SSA index.
2560 int ssa_index_;
2561
Vladimir Markoa1de9182016-02-25 11:37:38 +00002562 // Packed fields.
2563 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002564
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002565 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002566 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002567
2568 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002569 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002570
Nicolas Geoffray39468442014-09-02 15:17:15 +01002571 // The environment associated with this instruction. Not null if the instruction
2572 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002573 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002574
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002575 // Set by the code generator.
2576 LocationSummary* locations_;
2577
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002578 // Set by the liveness analysis.
2579 LiveInterval* live_interval_;
2580
2581 // Set by the liveness analysis, this is the position in a linear
2582 // order of blocks where this instruction's live interval start.
2583 size_t lifetime_position_;
2584
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002585 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002586
Vladimir Markoa1de9182016-02-25 11:37:38 +00002587 // The reference handle part of the reference type info.
2588 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002589 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002590 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002591
David Brazdil1abb4192015-02-17 18:33:36 +00002592 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002593 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002594 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002595 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002596 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002597};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002598std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002599
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002600// Iterates over the instructions, while preserving the next instruction
2601// in case the current instruction gets removed from the list by the user
2602// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002603class HInstructionIterator : public ValueObject {
2604 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002605 explicit HInstructionIterator(const HInstructionList& instructions)
2606 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002607 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002608 }
2609
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002610 bool Done() const { return instruction_ == nullptr; }
2611 HInstruction* Current() const { return instruction_; }
2612 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002613 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002614 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002615 }
2616
2617 private:
2618 HInstruction* instruction_;
2619 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002620
2621 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002622};
2623
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002624// Iterates over the instructions without saving the next instruction,
2625// therefore handling changes in the graph potentially made by the user
2626// of this iterator.
2627class HInstructionIteratorHandleChanges : public ValueObject {
2628 public:
2629 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2630 : instruction_(instructions.first_instruction_) {
2631 }
2632
2633 bool Done() const { return instruction_ == nullptr; }
2634 HInstruction* Current() const { return instruction_; }
2635 void Advance() {
2636 instruction_ = instruction_->GetNext();
2637 }
2638
2639 private:
2640 HInstruction* instruction_;
2641
2642 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2643};
2644
2645
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002646class HBackwardInstructionIterator : public ValueObject {
2647 public:
2648 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2649 : instruction_(instructions.last_instruction_) {
2650 next_ = Done() ? nullptr : instruction_->GetPrevious();
2651 }
2652
2653 bool Done() const { return instruction_ == nullptr; }
2654 HInstruction* Current() const { return instruction_; }
2655 void Advance() {
2656 instruction_ = next_;
2657 next_ = Done() ? nullptr : instruction_->GetPrevious();
2658 }
2659
2660 private:
2661 HInstruction* instruction_;
2662 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002663
2664 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002665};
2666
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002667class HVariableInputSizeInstruction : public HInstruction {
2668 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002669 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002670 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002671 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2672 }
2673
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002674 void AddInput(HInstruction* input);
2675 void InsertInputAt(size_t index, HInstruction* input);
2676 void RemoveInputAt(size_t index);
2677
Igor Murashkind01745e2017-04-05 16:40:31 -07002678 // Removes all the inputs.
2679 // Also removes this instructions from each input's use list
2680 // (for non-environment uses only).
2681 void RemoveAllInputs();
2682
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002683 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302684 HVariableInputSizeInstruction(InstructionKind inst_kind,
2685 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002686 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002687 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002688 size_t number_of_inputs,
2689 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302690 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002691 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002692 HVariableInputSizeInstruction(InstructionKind inst_kind,
2693 DataType::Type type,
2694 SideEffects side_effects,
2695 uint32_t dex_pc,
2696 ArenaAllocator* allocator,
2697 size_t number_of_inputs,
2698 ArenaAllocKind kind)
2699 : HInstruction(inst_kind, type, side_effects, dex_pc),
2700 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002701
Artem Serovcced8ba2017-07-19 18:18:09 +01002702 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002703
Artem Serovcced8ba2017-07-19 18:18:09 +01002704 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002705};
2706
Vladimir Markof9f64412015-09-02 14:05:49 +01002707template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002708class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002709 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002710 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302711 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002712 HExpression<N>(InstructionKind kind,
2713 DataType::Type type,
2714 SideEffects side_effects,
2715 uint32_t dex_pc)
2716 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2717 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002718
Vladimir Marko372f10e2016-05-17 16:30:10 +01002719 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002720 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002721 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002722 }
2723
Artem Serovcced8ba2017-07-19 18:18:09 +01002724 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002725 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002726
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002727 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002728 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002729
2730 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002731};
2732
Vladimir Markobd785672018-05-03 17:09:09 +01002733// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002734template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002735class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002736 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002737 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002738
Vladimir Markobd785672018-05-03 17:09:09 +01002739 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002740
Vladimir Marko372f10e2016-05-17 16:30:10 +01002741 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002742 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002743 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002744 }
2745
Artem Serovcced8ba2017-07-19 18:18:09 +01002746 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002747 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002748
Vladimir Markof9f64412015-09-02 14:05:49 +01002749 private:
2750 friend class SsaBuilder;
2751};
2752
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002753// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2754// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002755class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002758 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302759 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002760
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002761 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002762
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002763 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002764
Artem Serovcced8ba2017-07-19 18:18:09 +01002765 protected:
2766 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002767};
2768
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002769// Represents dex's RETURN opcodes. A HReturn is a control flow
2770// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002771class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002772 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002773 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002774 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002775 SetRawInputAt(0, value);
2776 }
2777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002778 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002779
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002780 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002781
Artem Serovcced8ba2017-07-19 18:18:09 +01002782 protected:
2783 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002784};
2785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002786class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002787 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002788 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002789 uint32_t reg_number,
2790 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002792 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002793 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302794 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002795 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002796 SideEffects::None(),
2797 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002798 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002799 number_of_inputs,
2800 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002801 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002803 // Phis are constructed live and marked dead if conflicting or unused.
2804 // Individual steps of SsaBuilder should assume that if a phi has been
2805 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2806 SetPackedFlag<kFlagIsLive>(true);
2807 SetPackedFlag<kFlagCanBeNull>(true);
2808 }
2809
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002810 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002811
David Brazdildee58d62016-04-07 09:54:26 +00002812 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 static DataType::Type ToPhiType(DataType::Type type) {
2814 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002815 }
2816
2817 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002820 // Make sure that only valid type changes occur. The following are allowed:
2821 // (1) int -> float/ref (primitive type propagation),
2822 // (2) long -> double (primitive type propagation).
2823 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2825 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2826 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002827 SetPackedField<TypeField>(new_type);
2828 }
2829
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002830 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002831 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2832
2833 uint32_t GetRegNumber() const { return reg_number_; }
2834
2835 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2836 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2837 bool IsDead() const { return !IsLive(); }
2838 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2839
Vladimir Markoe9004912016-06-16 16:50:52 +01002840 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002841 return other != nullptr
2842 && other->IsPhi()
2843 && other->AsPhi()->GetBlock() == GetBlock()
2844 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2845 }
2846
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002847 bool HasEquivalentPhi() const {
2848 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2849 return true;
2850 }
2851 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2852 return true;
2853 }
2854 return false;
2855 }
2856
David Brazdildee58d62016-04-07 09:54:26 +00002857 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2858 // An equivalent phi is a phi having the same dex register and type.
2859 // It assumes that phis with the same dex register are adjacent.
2860 HPhi* GetNextEquivalentPhiWithSameType() {
2861 HInstruction* next = GetNext();
2862 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2863 if (next->GetType() == GetType()) {
2864 return next->AsPhi();
2865 }
2866 next = next->GetNext();
2867 }
2868 return nullptr;
2869 }
2870
2871 DECLARE_INSTRUCTION(Phi);
2872
Artem Serovcced8ba2017-07-19 18:18:09 +01002873 protected:
2874 DEFAULT_COPY_CONSTRUCTOR(Phi);
2875
David Brazdildee58d62016-04-07 09:54:26 +00002876 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002877 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002878 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2879 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2880 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002881
David Brazdildee58d62016-04-07 09:54:26 +00002882 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002883};
2884
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002885// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002886// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002887// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002888class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002889 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302890 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002891 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302892 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002893
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002894 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002895
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002896 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002897
Artem Serovcced8ba2017-07-19 18:18:09 +01002898 protected:
2899 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002900};
2901
2902// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002903class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002904 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302905 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002906 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302907 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002908
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002909 bool IsClonable() const override { return true; }
2910 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002911
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002912 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002913 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002914 }
2915
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002916 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002917
Artem Serovcced8ba2017-07-19 18:18:09 +01002918 protected:
2919 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002920};
2921
Roland Levillain9867bc72015-08-05 10:21:34 +01002922class HConstant : public HExpression<0> {
2923 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302924 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2925 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2926 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002927
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002928 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002929
Roland Levillain1a653882016-03-18 18:05:57 +00002930 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002931 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002932 // Is this constant 0 in the arithmetic sense?
2933 virtual bool IsArithmeticZero() const { return false; }
2934 // Is this constant a 0-bit pattern?
2935 virtual bool IsZeroBitPattern() const { return false; }
2936 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002937 virtual bool IsOne() const { return false; }
2938
David Brazdil77a48ae2015-09-15 12:34:04 +00002939 virtual uint64_t GetValueAsUint64() const = 0;
2940
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002941 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002942
Artem Serovcced8ba2017-07-19 18:18:09 +01002943 protected:
2944 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002945};
2946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002947class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002948 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002949 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002950 return true;
2951 }
2952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002953 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002955 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002956
Roland Levillain1a653882016-03-18 18:05:57 +00002957 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002958 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002959
Roland Levillain9867bc72015-08-05 10:21:34 +01002960 DECLARE_INSTRUCTION(NullConstant);
2961
Artem Serovcced8ba2017-07-19 18:18:09 +01002962 protected:
2963 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2964
Roland Levillain9867bc72015-08-05 10:21:34 +01002965 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302967 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2968 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002969
2970 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002971};
2972
2973// Constants of the type int. Those can be from Dex instructions, or
2974// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002975class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002976 public:
2977 int32_t GetValue() const { return value_; }
2978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002979 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002980 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2981 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002982
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002983 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002984 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002985 return other->AsIntConstant()->value_ == value_;
2986 }
2987
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002988 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002990 bool IsMinusOne() const override { return GetValue() == -1; }
2991 bool IsArithmeticZero() const override { return GetValue() == 0; }
2992 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2993 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002994
Roland Levillain1a653882016-03-18 18:05:57 +00002995 // Integer constants are used to encode Boolean values as well,
2996 // where 1 means true and 0 means false.
2997 bool IsTrue() const { return GetValue() == 1; }
2998 bool IsFalse() const { return GetValue() == 0; }
2999
Roland Levillain9867bc72015-08-05 10:21:34 +01003000 DECLARE_INSTRUCTION(IntConstant);
3001
Artem Serovcced8ba2017-07-19 18:18:09 +01003002 protected:
3003 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3004
Roland Levillain9867bc72015-08-05 10:21:34 +01003005 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003006 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303007 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3008 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003009 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303010 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3011 value_(value ? 1 : 0) {
3012 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003013
3014 const int32_t value_;
3015
3016 friend class HGraph;
3017 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3018 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003019};
3020
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003021class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003022 public:
3023 int64_t GetValue() const { return value_; }
3024
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003025 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003027 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003029 return other->AsLongConstant()->value_ == value_;
3030 }
3031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003032 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003033
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003034 bool IsMinusOne() const override { return GetValue() == -1; }
3035 bool IsArithmeticZero() const override { return GetValue() == 0; }
3036 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3037 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003038
3039 DECLARE_INSTRUCTION(LongConstant);
3040
Artem Serovcced8ba2017-07-19 18:18:09 +01003041 protected:
3042 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3043
Roland Levillain9867bc72015-08-05 10:21:34 +01003044 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003045 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303046 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3047 value_(value) {
3048 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003049
3050 const int64_t value_;
3051
3052 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003053};
Dave Allison20dfc792014-06-16 20:44:29 -07003054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003055class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003056 public:
3057 float GetValue() const { return value_; }
3058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003059 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003060 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3061 }
3062
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003063 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064 DCHECK(other->IsFloatConstant()) << other->DebugName();
3065 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3066 }
3067
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003068 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003070 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003071 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3072 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003073 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003074 return std::fpclassify(value_) == FP_ZERO;
3075 }
3076 bool IsArithmeticPositiveZero() const {
3077 return IsArithmeticZero() && !std::signbit(value_);
3078 }
3079 bool IsArithmeticNegativeZero() const {
3080 return IsArithmeticZero() && std::signbit(value_);
3081 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003082 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003083 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003084 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003085 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003086 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3087 }
3088 bool IsNaN() const {
3089 return std::isnan(value_);
3090 }
3091
3092 DECLARE_INSTRUCTION(FloatConstant);
3093
Artem Serovcced8ba2017-07-19 18:18:09 +01003094 protected:
3095 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3096
Roland Levillain31dd3d62016-02-16 12:21:02 +00003097 private:
3098 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303099 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3100 value_(value) {
3101 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003102 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303103 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3104 value_(bit_cast<float, int32_t>(value)) {
3105 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003106
3107 const float value_;
3108
3109 // Only the SsaBuilder and HGraph can create floating-point constants.
3110 friend class SsaBuilder;
3111 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003112};
3113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003114class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 public:
3116 double GetValue() const { return value_; }
3117
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003118 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003120 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003121 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3122 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3123 }
3124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003125 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003126
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003127 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003128 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3129 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003130 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003131 return std::fpclassify(value_) == FP_ZERO;
3132 }
3133 bool IsArithmeticPositiveZero() const {
3134 return IsArithmeticZero() && !std::signbit(value_);
3135 }
3136 bool IsArithmeticNegativeZero() const {
3137 return IsArithmeticZero() && std::signbit(value_);
3138 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003139 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003140 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003141 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003142 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003143 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3144 }
3145 bool IsNaN() const {
3146 return std::isnan(value_);
3147 }
3148
3149 DECLARE_INSTRUCTION(DoubleConstant);
3150
Artem Serovcced8ba2017-07-19 18:18:09 +01003151 protected:
3152 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3153
Roland Levillain31dd3d62016-02-16 12:21:02 +00003154 private:
3155 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303156 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3157 value_(value) {
3158 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003159 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303160 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3161 value_(bit_cast<double, int64_t>(value)) {
3162 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003163
3164 const double value_;
3165
3166 // Only the SsaBuilder and HGraph can create floating-point constants.
3167 friend class SsaBuilder;
3168 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003169};
3170
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003171// Conditional branch. A block ending with an HIf instruction must have
3172// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003173class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003174 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003175 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003176 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003177 SetRawInputAt(0, input);
3178 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003180 bool IsClonable() const override { return true; }
3181 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003182
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003183 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003184 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003185 }
3186
3187 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003188 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003189 }
3190
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003191 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003192
Artem Serovcced8ba2017-07-19 18:18:09 +01003193 protected:
3194 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003195};
3196
David Brazdilfc6a86a2015-06-26 10:33:45 +00003197
3198// Abstract instruction which marks the beginning and/or end of a try block and
3199// links it to the respective exception handlers. Behaves the same as a Goto in
3200// non-exceptional control flow.
3201// Normal-flow successor is stored at index zero, exception handlers under
3202// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003203class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003204 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003205 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003206 kEntry,
3207 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003208 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003209 };
3210
Artem Serovd1aa7d02018-06-22 11:35:46 +01003211 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3212 // across the catch block entering edges as GC might happen during throwing an exception.
3213 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3214 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003215 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003216 : HExpression(kTryBoundary,
3217 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3218 : SideEffects::None(),
3219 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003220 SetPackedField<BoundaryKindField>(kind);
3221 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003222
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003223 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003224
3225 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003226 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003227
David Brazdild26a4112015-11-10 11:07:31 +00003228 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3229 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3230 }
3231
David Brazdilfc6a86a2015-06-26 10:33:45 +00003232 // Returns whether `handler` is among its exception handlers (non-zero index
3233 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003234 bool HasExceptionHandler(const HBasicBlock& handler) const {
3235 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003236 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003237 }
3238
3239 // If not present already, adds `handler` to its block's list of exception
3240 // handlers.
3241 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003242 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003243 GetBlock()->AddSuccessor(handler);
3244 }
3245 }
3246
Vladimir Markoa1de9182016-02-25 11:37:38 +00003247 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3248 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003249
David Brazdilffee3d32015-07-06 11:48:53 +01003250 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3251
David Brazdilfc6a86a2015-06-26 10:33:45 +00003252 DECLARE_INSTRUCTION(TryBoundary);
3253
Artem Serovcced8ba2017-07-19 18:18:09 +01003254 protected:
3255 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3256
David Brazdilfc6a86a2015-06-26 10:33:45 +00003257 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003258 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3259 static constexpr size_t kFieldBoundaryKindSize =
3260 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3261 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3262 kFieldBoundaryKind + kFieldBoundaryKindSize;
3263 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3264 "Too many packed fields.");
3265 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003266};
3267
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003268// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003269class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003270 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003271 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3272 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003273 HDeoptimize(ArenaAllocator* allocator,
3274 HInstruction* cond,
3275 DeoptimizationKind kind,
3276 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003277 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303278 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003279 SideEffects::All(),
3280 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003281 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003282 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003283 kArenaAllocMisc) {
3284 SetPackedFlag<kFieldCanBeMoved>(false);
3285 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003286 SetRawInputAt(0, cond);
3287 }
3288
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003289 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003290
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003291 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3292 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3293 // instead of `guard`.
3294 // We set CanTriggerGC to prevent any intermediate address to be live
3295 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003296 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003297 HInstruction* cond,
3298 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003299 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003300 uint32_t dex_pc)
3301 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303302 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003303 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003304 SideEffects::CanTriggerGC(),
3305 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003306 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003307 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003308 kArenaAllocMisc) {
3309 SetPackedFlag<kFieldCanBeMoved>(true);
3310 SetPackedField<DeoptimizeKindField>(kind);
3311 SetRawInputAt(0, cond);
3312 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003313 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003314
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003315 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003316
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003317 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003318 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3319 }
3320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003321 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003323 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003324
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003325 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003326
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003327 bool GuardsAnInput() const {
3328 return InputCount() == 2;
3329 }
3330
3331 HInstruction* GuardedInput() const {
3332 DCHECK(GuardsAnInput());
3333 return InputAt(1);
3334 }
3335
3336 void RemoveGuard() {
3337 RemoveInputAt(1);
3338 }
3339
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003340 DECLARE_INSTRUCTION(Deoptimize);
3341
Artem Serovcced8ba2017-07-19 18:18:09 +01003342 protected:
3343 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3344
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003345 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003346 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3347 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3348 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003349 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003350 static constexpr size_t kNumberOfDeoptimizePackedBits =
3351 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3352 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3353 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003354 using DeoptimizeKindField =
3355 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003356};
3357
Mingyao Yang063fc772016-08-02 11:02:54 -07003358// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3359// The compiled code checks this flag value in a guard before devirtualized call and
3360// if it's true, starts to do deoptimization.
3361// It has a 4-byte slot on stack.
3362// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003363class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003364 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003365 // CHA guards are only optimized in a separate pass and it has no side effects
3366 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003367 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303368 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003369 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303370 SideEffects::None(),
3371 dex_pc,
3372 allocator,
3373 0,
3374 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003375 }
3376
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003377 // We do all CHA guard elimination/motion in a single pass, after which there is no
3378 // further guard elimination/motion since a guard might have been used for justification
3379 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3380 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003381 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003382
3383 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3384
Artem Serovcced8ba2017-07-19 18:18:09 +01003385 protected:
3386 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003387};
3388
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003389// Represents the ArtMethod that was passed as a first argument to
3390// the method. It is used by instructions that depend on it, like
3391// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003392class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003393 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003394 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303395 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3396 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003397
3398 DECLARE_INSTRUCTION(CurrentMethod);
3399
Artem Serovcced8ba2017-07-19 18:18:09 +01003400 protected:
3401 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003402};
3403
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003404// Fetches an ArtMethod from the virtual table or the interface method table
3405// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003406class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003407 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003408 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003409 kVTable,
3410 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003411 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003412 };
3413 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003414 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003415 TableKind kind,
3416 size_t index,
3417 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303418 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003419 index_(index) {
3420 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003421 SetRawInputAt(0, cls);
3422 }
3423
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003424 bool IsClonable() const override { return true; }
3425 bool CanBeMoved() const override { return true; }
3426 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003427 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003428 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003429 }
3430
Vladimir Markoa1de9182016-02-25 11:37:38 +00003431 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003432 size_t GetIndex() const { return index_; }
3433
3434 DECLARE_INSTRUCTION(ClassTableGet);
3435
Artem Serovcced8ba2017-07-19 18:18:09 +01003436 protected:
3437 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3438
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003439 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003440 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003441 static constexpr size_t kFieldTableKindSize =
3442 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3443 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3444 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3445 "Too many packed fields.");
3446 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3447
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003448 // The index of the ArtMethod in the table.
3449 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003450};
3451
Mark Mendellfe57faa2015-09-18 09:26:15 -04003452// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3453// have one successor for each entry in the switch table, and the final successor
3454// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003455class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003456 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003457 HPackedSwitch(int32_t start_value,
3458 uint32_t num_entries,
3459 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003460 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003461 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003462 start_value_(start_value),
3463 num_entries_(num_entries) {
3464 SetRawInputAt(0, input);
3465 }
3466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003467 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003468
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003469 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003470
3471 int32_t GetStartValue() const { return start_value_; }
3472
Vladimir Marko211c2112015-09-24 16:52:33 +01003473 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003474
3475 HBasicBlock* GetDefaultBlock() const {
3476 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003477 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003478 }
3479 DECLARE_INSTRUCTION(PackedSwitch);
3480
Artem Serovcced8ba2017-07-19 18:18:09 +01003481 protected:
3482 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3483
Mark Mendellfe57faa2015-09-18 09:26:15 -04003484 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003485 const int32_t start_value_;
3486 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003487};
3488
Roland Levillain88cb1752014-10-20 16:36:47 +01003489class HUnaryOperation : public HExpression<1> {
3490 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303491 HUnaryOperation(InstructionKind kind,
3492 DataType::Type result_type,
3493 HInstruction* input,
3494 uint32_t dex_pc = kNoDexPc)
3495 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003496 SetRawInputAt(0, input);
3497 }
3498
Artem Serovcced8ba2017-07-19 18:18:09 +01003499 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003500 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003501
Roland Levillain88cb1752014-10-20 16:36:47 +01003502 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003503 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003504
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003505 bool CanBeMoved() const override { return true; }
3506 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003507 return true;
3508 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003509
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 // Try to statically evaluate `this` and return a HConstant
3511 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003512 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003513 HConstant* TryStaticEvaluation() const;
3514
3515 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003516 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3517 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003518 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3519 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003520
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003521 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003522
Artem Serovcced8ba2017-07-19 18:18:09 +01003523 protected:
3524 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003525};
3526
Dave Allison20dfc792014-06-16 20:44:29 -07003527class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003528 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303529 HBinaryOperation(InstructionKind kind,
3530 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003531 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003532 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003533 SideEffects side_effects = SideEffects::None(),
3534 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303535 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003536 SetRawInputAt(0, left);
3537 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003538 }
3539
Artem Serovcced8ba2017-07-19 18:18:09 +01003540 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003541 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003542
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003543 HInstruction* GetLeft() const { return InputAt(0); }
3544 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003545 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003546
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003547 virtual bool IsCommutative() const { return false; }
3548
3549 // Put constant on the right.
3550 // Returns whether order is changed.
3551 bool OrderInputsWithConstantOnTheRight() {
3552 HInstruction* left = InputAt(0);
3553 HInstruction* right = InputAt(1);
3554 if (left->IsConstant() && !right->IsConstant()) {
3555 ReplaceInput(right, 0);
3556 ReplaceInput(left, 1);
3557 return true;
3558 }
3559 return false;
3560 }
3561
3562 // Order inputs by instruction id, but favor constant on the right side.
3563 // This helps GVN for commutative ops.
3564 void OrderInputs() {
3565 DCHECK(IsCommutative());
3566 HInstruction* left = InputAt(0);
3567 HInstruction* right = InputAt(1);
3568 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3569 return;
3570 }
3571 if (OrderInputsWithConstantOnTheRight()) {
3572 return;
3573 }
3574 // Order according to instruction id.
3575 if (left->GetId() > right->GetId()) {
3576 ReplaceInput(right, 0);
3577 ReplaceInput(left, 1);
3578 }
3579 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003580
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003581 bool CanBeMoved() const override { return true; }
3582 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003583 return true;
3584 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003585
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 // Try to statically evaluate `this` and return a HConstant
3587 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003588 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003589 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003590
3591 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003592 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3593 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003594 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3595 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003596 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003597 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3598 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003599 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3600 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003601 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3602 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003603 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003604 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3605 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003606
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003607 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003608 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003609 HConstant* GetConstantRight() const;
3610
3611 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003612 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003613 HInstruction* GetLeastConstantLeft() const;
3614
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003615 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003616
Artem Serovcced8ba2017-07-19 18:18:09 +01003617 protected:
3618 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003619};
3620
Mark Mendellc4701932015-04-10 13:18:51 -04003621// The comparison bias applies for floating point operations and indicates how NaN
3622// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003623enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003624 kNoBias, // bias is not applicable (i.e. for long operation)
3625 kGtBias, // return 1 for NaN comparisons
3626 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003627 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003628};
3629
Roland Levillain31dd3d62016-02-16 12:21:02 +00003630std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3631
Dave Allison20dfc792014-06-16 20:44:29 -07003632class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003633 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303634 HCondition(InstructionKind kind,
3635 HInstruction* first,
3636 HInstruction* second,
3637 uint32_t dex_pc = kNoDexPc)
3638 : HBinaryOperation(kind,
3639 DataType::Type::kBool,
3640 first,
3641 second,
3642 SideEffects::None(),
3643 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003644 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3645 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003646
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003647 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003648 // `instruction`, and disregard moves in between.
3649 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003650
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003651 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003652
3653 virtual IfCondition GetCondition() const = 0;
3654
Mark Mendellc4701932015-04-10 13:18:51 -04003655 virtual IfCondition GetOppositeCondition() const = 0;
3656
Vladimir Markoa1de9182016-02-25 11:37:38 +00003657 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003658 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3659
Vladimir Markoa1de9182016-02-25 11:37:38 +00003660 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3661 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003662
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003663 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003664 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003665 }
3666
Roland Levillain4fa13f62015-07-06 18:11:54 +01003667 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003668 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003669 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003670 if (if_cond == kCondNE) {
3671 return true;
3672 } else if (if_cond == kCondEQ) {
3673 return false;
3674 }
3675 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003676 }
3677
3678 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003679 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003680 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003681 if (if_cond == kCondEQ) {
3682 return true;
3683 } else if (if_cond == kCondNE) {
3684 return false;
3685 }
3686 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003687 }
3688
Roland Levillain31dd3d62016-02-16 12:21:02 +00003689 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003690 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003691 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003692 static constexpr size_t kFieldComparisonBiasSize =
3693 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3694 static constexpr size_t kNumberOfConditionPackedBits =
3695 kFieldComparisonBias + kFieldComparisonBiasSize;
3696 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3697 using ComparisonBiasField =
3698 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3699
Roland Levillain31dd3d62016-02-16 12:21:02 +00003700 template <typename T>
3701 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3702
3703 template <typename T>
3704 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003705 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003706 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3707 // Handle the bias.
3708 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3709 }
3710
3711 // Return an integer constant containing the result of a condition evaluated at compile time.
3712 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3713 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3714 }
3715
Artem Serovcced8ba2017-07-19 18:18:09 +01003716 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003717};
3718
3719// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003720class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003721 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003722 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303723 : HCondition(kEqual, first, second, dex_pc) {
3724 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003725
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003726 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003727
Vladimir Marko9e23df52015-11-10 17:14:35 +00003728 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003729 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003730 return MakeConstantCondition(true, GetDexPc());
3731 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003732 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003733 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3734 }
3735 // In the following Evaluate methods, a HCompare instruction has
3736 // been merged into this HEqual instruction; evaluate it as
3737 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003738 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003739 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3740 GetDexPc());
3741 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003742 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3744 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003745 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003746 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003747 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003748
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003749 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003750
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003751 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003752 return kCondEQ;
3753 }
3754
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003755 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003756 return kCondNE;
3757 }
3758
Artem Serovcced8ba2017-07-19 18:18:09 +01003759 protected:
3760 DEFAULT_COPY_CONSTRUCTOR(Equal);
3761
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003762 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003763 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003764};
3765
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003766class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003767 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303768 HNotEqual(HInstruction* first, HInstruction* second,
3769 uint32_t dex_pc = kNoDexPc)
3770 : HCondition(kNotEqual, first, second, dex_pc) {
3771 }
Dave Allison20dfc792014-06-16 20:44:29 -07003772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003773 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003774
Vladimir Marko9e23df52015-11-10 17:14:35 +00003775 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003776 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003777 return MakeConstantCondition(false, GetDexPc());
3778 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003779 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003780 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3781 }
3782 // In the following Evaluate methods, a HCompare instruction has
3783 // been merged into this HNotEqual instruction; evaluate it as
3784 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003785 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003786 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3787 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003788 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003789 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3790 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003792 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003793 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003794
Dave Allison20dfc792014-06-16 20:44:29 -07003795 DECLARE_INSTRUCTION(NotEqual);
3796
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003797 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003798 return kCondNE;
3799 }
3800
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003801 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003802 return kCondEQ;
3803 }
3804
Artem Serovcced8ba2017-07-19 18:18:09 +01003805 protected:
3806 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3807
Dave Allison20dfc792014-06-16 20:44:29 -07003808 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003809 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003810};
3811
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003812class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003813 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303814 HLessThan(HInstruction* first, HInstruction* second,
3815 uint32_t dex_pc = kNoDexPc)
3816 : HCondition(kLessThan, first, second, dex_pc) {
3817 }
Dave Allison20dfc792014-06-16 20:44:29 -07003818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003819 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003820 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003821 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003822 // In the following Evaluate methods, a HCompare instruction has
3823 // been merged into this HLessThan instruction; evaluate it as
3824 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003825 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003826 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3827 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003828 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003829 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3830 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003831 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003832 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003833 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003834
Dave Allison20dfc792014-06-16 20:44:29 -07003835 DECLARE_INSTRUCTION(LessThan);
3836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003837 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003838 return kCondLT;
3839 }
3840
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003841 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003842 return kCondGE;
3843 }
3844
Artem Serovcced8ba2017-07-19 18:18:09 +01003845 protected:
3846 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3847
Dave Allison20dfc792014-06-16 20:44:29 -07003848 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003849 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003850};
3851
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003852class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003853 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303854 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3855 uint32_t dex_pc = kNoDexPc)
3856 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3857 }
Dave Allison20dfc792014-06-16 20:44:29 -07003858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003859 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003860 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003861 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003862 // In the following Evaluate methods, a HCompare instruction has
3863 // been merged into this HLessThanOrEqual instruction; evaluate it as
3864 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003865 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003866 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3867 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003868 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003869 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3870 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003871 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003872 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003873 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003874
Dave Allison20dfc792014-06-16 20:44:29 -07003875 DECLARE_INSTRUCTION(LessThanOrEqual);
3876
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003877 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003878 return kCondLE;
3879 }
3880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003881 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003882 return kCondGT;
3883 }
3884
Artem Serovcced8ba2017-07-19 18:18:09 +01003885 protected:
3886 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3887
Dave Allison20dfc792014-06-16 20:44:29 -07003888 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003889 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003890};
3891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003892class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003893 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003894 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303895 : HCondition(kGreaterThan, first, second, dex_pc) {
3896 }
Dave Allison20dfc792014-06-16 20:44:29 -07003897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003898 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003899 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003900 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003901 // In the following Evaluate methods, a HCompare instruction has
3902 // been merged into this HGreaterThan instruction; evaluate it as
3903 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003904 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003905 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3906 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003907 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003908 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3909 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003910 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003911 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003912 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003913
Dave Allison20dfc792014-06-16 20:44:29 -07003914 DECLARE_INSTRUCTION(GreaterThan);
3915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003916 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003917 return kCondGT;
3918 }
3919
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003920 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003921 return kCondLE;
3922 }
3923
Artem Serovcced8ba2017-07-19 18:18:09 +01003924 protected:
3925 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3926
Dave Allison20dfc792014-06-16 20:44:29 -07003927 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003928 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003929};
3930
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003931class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003933 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303934 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3935 }
Dave Allison20dfc792014-06-16 20:44:29 -07003936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003937 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003938 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003939 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003940 // In the following Evaluate methods, a HCompare instruction has
3941 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3942 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003943 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003944 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3945 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003946 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003947 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3948 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003949 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003950 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003951 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003952
Dave Allison20dfc792014-06-16 20:44:29 -07003953 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003955 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003956 return kCondGE;
3957 }
3958
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003959 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003960 return kCondLT;
3961 }
3962
Artem Serovcced8ba2017-07-19 18:18:09 +01003963 protected:
3964 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3965
Dave Allison20dfc792014-06-16 20:44:29 -07003966 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003967 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003968};
3969
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003970class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003971 public:
3972 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303973 : HCondition(kBelow, first, second, dex_pc) {
3974 }
Aart Bike9f37602015-10-09 11:15:55 -07003975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003976 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003977 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003978 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003979 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003980 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3981 }
3982 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003983 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003984 LOG(FATAL) << DebugName() << " is not defined for float values";
3985 UNREACHABLE();
3986 }
3987 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003988 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003989 LOG(FATAL) << DebugName() << " is not defined for double values";
3990 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003991 }
3992
3993 DECLARE_INSTRUCTION(Below);
3994
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003995 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003996 return kCondB;
3997 }
3998
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003999 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004000 return kCondAE;
4001 }
4002
Artem Serovcced8ba2017-07-19 18:18:09 +01004003 protected:
4004 DEFAULT_COPY_CONSTRUCTOR(Below);
4005
Aart Bike9f37602015-10-09 11:15:55 -07004006 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004007 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004008 return MakeUnsigned(x) < MakeUnsigned(y);
4009 }
Aart Bike9f37602015-10-09 11:15:55 -07004010};
4011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004012class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004013 public:
4014 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304015 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4016 }
Aart Bike9f37602015-10-09 11:15:55 -07004017
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004018 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004019 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004020 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004021 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004022 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4023 }
4024 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004025 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004026 LOG(FATAL) << DebugName() << " is not defined for float values";
4027 UNREACHABLE();
4028 }
4029 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004030 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004031 LOG(FATAL) << DebugName() << " is not defined for double values";
4032 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004033 }
4034
4035 DECLARE_INSTRUCTION(BelowOrEqual);
4036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004037 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004038 return kCondBE;
4039 }
4040
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004041 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004042 return kCondA;
4043 }
4044
Artem Serovcced8ba2017-07-19 18:18:09 +01004045 protected:
4046 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4047
Aart Bike9f37602015-10-09 11:15:55 -07004048 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004049 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004050 return MakeUnsigned(x) <= MakeUnsigned(y);
4051 }
Aart Bike9f37602015-10-09 11:15:55 -07004052};
4053
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004054class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004055 public:
4056 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304057 : HCondition(kAbove, first, second, dex_pc) {
4058 }
Aart Bike9f37602015-10-09 11:15:55 -07004059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004060 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004061 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004062 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004063 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004064 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4065 }
4066 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004067 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004068 LOG(FATAL) << DebugName() << " is not defined for float values";
4069 UNREACHABLE();
4070 }
4071 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004072 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004073 LOG(FATAL) << DebugName() << " is not defined for double values";
4074 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004075 }
4076
4077 DECLARE_INSTRUCTION(Above);
4078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004079 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004080 return kCondA;
4081 }
4082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004083 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004084 return kCondBE;
4085 }
4086
Artem Serovcced8ba2017-07-19 18:18:09 +01004087 protected:
4088 DEFAULT_COPY_CONSTRUCTOR(Above);
4089
Aart Bike9f37602015-10-09 11:15:55 -07004090 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004091 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004092 return MakeUnsigned(x) > MakeUnsigned(y);
4093 }
Aart Bike9f37602015-10-09 11:15:55 -07004094};
4095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004096class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004097 public:
4098 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304099 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4100 }
Aart Bike9f37602015-10-09 11:15:55 -07004101
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004102 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004103 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004104 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004105 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004106 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4107 }
4108 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004109 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004110 LOG(FATAL) << DebugName() << " is not defined for float values";
4111 UNREACHABLE();
4112 }
4113 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004114 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004115 LOG(FATAL) << DebugName() << " is not defined for double values";
4116 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004117 }
4118
4119 DECLARE_INSTRUCTION(AboveOrEqual);
4120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004121 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004122 return kCondAE;
4123 }
4124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004125 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004126 return kCondB;
4127 }
4128
Artem Serovcced8ba2017-07-19 18:18:09 +01004129 protected:
4130 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4131
Aart Bike9f37602015-10-09 11:15:55 -07004132 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004133 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004134 return MakeUnsigned(x) >= MakeUnsigned(y);
4135 }
Aart Bike9f37602015-10-09 11:15:55 -07004136};
Dave Allison20dfc792014-06-16 20:44:29 -07004137
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004138// Instruction to check how two inputs compare to each other.
4139// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004140class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004141 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004142 // Note that `comparison_type` is the type of comparison performed
4143 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004144 // HCompare instruction (which is always DataType::Type::kInt).
4145 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004146 HInstruction* first,
4147 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004148 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004149 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304150 : HBinaryOperation(kCompare,
4151 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004152 first,
4153 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004154 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004155 dex_pc) {
4156 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004157 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4158 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004159 }
4160
Roland Levillain9867bc72015-08-05 10:21:34 +01004161 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004162 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4163
4164 template <typename T>
4165 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004166 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004167 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4168 // Handle the bias.
4169 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4170 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004172 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004173 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4174 // reach this code path when processing a freshly built HIR
4175 // graph. However HCompare integer instructions can be synthesized
4176 // by the instruction simplifier to implement IntegerCompare and
4177 // IntegerSignum intrinsics, so we have to handle this case.
4178 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004180 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004181 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4182 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004183 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004184 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4185 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004186 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004187 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004188 }
4189
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004190 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004191 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004192 }
4193
Vladimir Markoa1de9182016-02-25 11:37:38 +00004194 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004195
Roland Levillain31dd3d62016-02-16 12:21:02 +00004196 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004197 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004198 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004199 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004200 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004201 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004203 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004204 // Comparisons do not require a runtime call in any back end.
4205 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004206 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004207
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004208 DECLARE_INSTRUCTION(Compare);
4209
Roland Levillain31dd3d62016-02-16 12:21:02 +00004210 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004211 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004212 static constexpr size_t kFieldComparisonBiasSize =
4213 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4214 static constexpr size_t kNumberOfComparePackedBits =
4215 kFieldComparisonBias + kFieldComparisonBiasSize;
4216 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4217 using ComparisonBiasField =
4218 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4219
Roland Levillain31dd3d62016-02-16 12:21:02 +00004220 // Return an integer constant containing the result of a comparison evaluated at compile time.
4221 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4222 DCHECK(value == -1 || value == 0 || value == 1) << value;
4223 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4224 }
4225
Artem Serovcced8ba2017-07-19 18:18:09 +01004226 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004227};
4228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004229class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004230 public:
4231 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004232 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004233 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004234 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004235 bool finalizable,
4236 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304237 : HExpression(kNewInstance,
4238 DataType::Type::kReference,
4239 SideEffects::CanTriggerGC(),
4240 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004241 type_index_(type_index),
4242 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004243 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004244 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004245 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004246 }
4247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004248 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004249
Andreas Gampea5b09a62016-11-17 15:21:22 -08004250 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004251 const DexFile& GetDexFile() const { return dex_file_; }
4252
4253 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004254 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004255
Mingyao Yang062157f2016-03-02 10:15:36 -08004256 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004257 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004258
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004259 bool NeedsChecks() const {
4260 return entrypoint_ == kQuickAllocObjectWithChecks;
4261 }
David Brazdil6de19382016-01-08 17:37:10 +00004262
Vladimir Markoa1de9182016-02-25 11:37:38 +00004263 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004264
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004265 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004266
4267 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4268
4269 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4270 entrypoint_ = entrypoint;
4271 }
4272
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004273 HLoadClass* GetLoadClass() const {
4274 HInstruction* input = InputAt(0);
4275 if (input->IsClinitCheck()) {
4276 input = input->InputAt(0);
4277 }
4278 DCHECK(input->IsLoadClass());
4279 return input->AsLoadClass();
4280 }
4281
David Brazdil6de19382016-01-08 17:37:10 +00004282 bool IsStringAlloc() const;
4283
4284 DECLARE_INSTRUCTION(NewInstance);
4285
Artem Serovcced8ba2017-07-19 18:18:09 +01004286 protected:
4287 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4288
David Brazdil6de19382016-01-08 17:37:10 +00004289 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004290 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004291 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4292 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4293 "Too many packed fields.");
4294
Andreas Gampea5b09a62016-11-17 15:21:22 -08004295 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004296 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004297 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004298};
4299
Agi Csaki05f20562015-08-19 14:58:14 -07004300enum IntrinsicNeedsEnvironmentOrCache {
4301 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4302 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004303};
4304
Aart Bik5d75afe2015-12-14 11:57:01 -08004305enum IntrinsicSideEffects {
4306 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4307 kReadSideEffects, // Intrinsic may read heap memory.
4308 kWriteSideEffects, // Intrinsic may write heap memory.
4309 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4310};
4311
4312enum IntrinsicExceptions {
4313 kNoThrow, // Intrinsic does not throw any exceptions.
4314 kCanThrow // Intrinsic may throw exceptions.
4315};
4316
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004317class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004318 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004319 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004320
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004321 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004322 SetRawInputAt(index, argument);
4323 }
4324
Roland Levillain3e3d7332015-04-28 11:00:54 +01004325 // Return the number of arguments. This number can be lower than
4326 // the number of inputs returned by InputCount(), as some invoke
4327 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4328 // inputs at the end of their list of inputs.
4329 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4330
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004331 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4332
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004333 InvokeType GetInvokeType() const {
4334 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004335 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004336
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004337 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004338 return intrinsic_;
4339 }
4340
Aart Bik5d75afe2015-12-14 11:57:01 -08004341 void SetIntrinsic(Intrinsics intrinsic,
4342 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4343 IntrinsicSideEffects side_effects,
4344 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004345
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004346 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004347 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004348 }
4349
Aart Bikff7d89c2016-11-07 08:49:28 -08004350 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4351
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004352 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004353
Aart Bika8b8e9b2018-01-09 11:01:02 -08004354 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4355
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004356 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004357
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004358 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004359
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004360 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004361 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4362 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004363
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004364 uint32_t* GetIntrinsicOptimizations() {
4365 return &intrinsic_optimizations_;
4366 }
4367
4368 const uint32_t* GetIntrinsicOptimizations() const {
4369 return &intrinsic_optimizations_;
4370 }
4371
4372 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4373
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004374 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004375 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004376
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004377 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004378
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004379 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004380 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4381 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004382 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004383 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004384 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4385 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004386 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004387 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004388
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304389 HInvoke(InstructionKind kind,
4390 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004391 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004392 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004393 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004394 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004395 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004396 ArtMethod* resolved_method,
4397 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004398 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304399 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004400 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004401 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4402 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004403 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004404 number_of_arguments + number_of_other_inputs,
4405 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004406 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004407 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004408 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004409 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004410 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004411 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004412 // Check mutator lock, constructors lack annotalysis support.
4413 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4414 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004415 }
4416
Artem Serovcced8ba2017-07-19 18:18:09 +01004417 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4418
Roland Levillain3e3d7332015-04-28 11:00:54 +01004419 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004420 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004421 const uint32_t dex_method_index_;
4422 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004423
4424 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4425 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004426};
4427
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004428class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004429 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004430 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004431 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004432 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004433 uint32_t dex_pc,
4434 uint32_t dex_method_index,
4435 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304436 : HInvoke(kInvokeUnresolved,
4437 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004438 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004439 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004440 return_type,
4441 dex_pc,
4442 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004443 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004444 invoke_type) {
4445 }
4446
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004447 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004448
Calin Juravle175dc732015-08-25 15:42:32 +01004449 DECLARE_INSTRUCTION(InvokeUnresolved);
4450
Artem Serovcced8ba2017-07-19 18:18:09 +01004451 protected:
4452 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004453};
4454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004455class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004456 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004457 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004458 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004459 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004460 uint32_t dex_pc,
4461 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304462 : HInvoke(kInvokePolymorphic,
4463 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004464 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004465 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004466 return_type,
4467 dex_pc,
4468 dex_method_index,
4469 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304470 kVirtual) {
4471 }
Orion Hodsonac141392017-01-13 11:53:47 +00004472
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004473 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004474
Orion Hodsonac141392017-01-13 11:53:47 +00004475 DECLARE_INSTRUCTION(InvokePolymorphic);
4476
Artem Serovcced8ba2017-07-19 18:18:09 +01004477 protected:
4478 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004479};
4480
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004481class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004482 public:
4483 HInvokeCustom(ArenaAllocator* allocator,
4484 uint32_t number_of_arguments,
4485 uint32_t call_site_index,
4486 DataType::Type return_type,
4487 uint32_t dex_pc)
4488 : HInvoke(kInvokeCustom,
4489 allocator,
4490 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004491 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004492 return_type,
4493 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004494 /* dex_method_index= */ dex::kDexNoIndex,
4495 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004496 kStatic),
4497 call_site_index_(call_site_index) {
4498 }
4499
4500 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4501
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004502 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004503
4504 DECLARE_INSTRUCTION(InvokeCustom);
4505
4506 protected:
4507 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4508
4509 private:
4510 uint32_t call_site_index_;
4511};
4512
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004513class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004514 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004515 // Requirements of this method call regarding the class
4516 // initialization (clinit) check of its declaring class.
4517 enum class ClinitCheckRequirement {
4518 kNone, // Class already initialized.
4519 kExplicit, // Static call having explicit clinit check as last input.
4520 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004521 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004522 };
4523
Vladimir Marko58155012015-08-19 12:49:41 +00004524 // Determines how to load the target ArtMethod*.
4525 enum class MethodLoadKind {
4526 // Use a String init ArtMethod* loaded from Thread entrypoints.
4527 kStringInit,
4528
4529 // Use the method's own ArtMethod* loaded by the register allocator.
4530 kRecursive,
4531
Vladimir Marko65979462017-05-19 17:25:12 +01004532 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4533 // Used for boot image methods referenced by boot image code.
4534 kBootImageLinkTimePcRelative,
4535
Vladimir Markob066d432018-01-03 13:14:37 +00004536 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4537 // Used for app->boot calls with relocatable image.
4538 kBootImageRelRo,
4539
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004540 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004541 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004542 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004543
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004544 // Use ArtMethod* at a known address, embed the direct address in the code.
4545 // Used for for JIT-compiled calls.
4546 kJitDirectAddress,
4547
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004548 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4549 // used when other kinds are unimplemented on a particular architecture.
4550 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004551 };
4552
4553 // Determines the location of the code pointer.
4554 enum class CodePtrLocation {
4555 // Recursive call, use local PC-relative call instruction.
4556 kCallSelf,
4557
Vladimir Marko58155012015-08-19 12:49:41 +00004558 // Use code pointer from the ArtMethod*.
4559 // Used when we don't know the target code. This is also the last-resort-kind used when
4560 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4561 kCallArtMethod,
4562 };
4563
4564 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004565 MethodLoadKind method_load_kind;
4566 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004567 // The method load data holds
4568 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4569 // Note that there are multiple string init methods, each having its own offset.
4570 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004571 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004572 };
4573
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004574 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004575 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004576 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004577 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004578 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004579 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004580 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004581 InvokeType invoke_type,
4582 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004583 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304584 : HInvoke(kInvokeStaticOrDirect,
4585 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004586 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004587 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004588 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004589 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004590 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004591 return_type,
4592 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004593 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004594 resolved_method,
4595 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004596 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004597 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004598 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4599 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004600
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004601 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004602
Vladimir Markodc151b22015-10-15 18:02:30 +01004603 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004604 bool had_current_method_input = HasCurrentMethodInput();
4605 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4606
4607 // Using the current method is the default and once we find a better
4608 // method load kind, we should not go back to using the current method.
4609 DCHECK(had_current_method_input || !needs_current_method_input);
4610
4611 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004612 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4613 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004614 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004615 dispatch_info_ = dispatch_info;
4616 }
4617
Aart Bik6daebeb2017-04-03 14:35:41 -07004618 DispatchInfo GetDispatchInfo() const {
4619 return dispatch_info_;
4620 }
4621
Vladimir Markoc53c0792015-11-19 15:48:33 +00004622 void AddSpecialInput(HInstruction* input) {
4623 // We allow only one special input.
4624 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4625 DCHECK(InputCount() == GetSpecialInputIndex() ||
4626 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4627 InsertInputAt(GetSpecialInputIndex(), input);
4628 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004629
Vladimir Marko372f10e2016-05-17 16:30:10 +01004630 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004631 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004632 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4633 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4634 DCHECK(!input_records.empty());
4635 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4636 HInstruction* last_input = input_records.back().GetInstruction();
4637 // Note: `last_input` may be null during arguments setup.
4638 if (last_input != nullptr) {
4639 // `last_input` is the last input of a static invoke marked as having
4640 // an explicit clinit check. It must either be:
4641 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4642 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4643 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4644 }
4645 }
4646 return input_records;
4647 }
4648
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004649 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004650 // We access the method via the dex cache so we can't do an implicit null check.
4651 // TODO: for intrinsics we can generate implicit null checks.
4652 return false;
4653 }
4654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004655 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004656 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004657 }
4658
Vladimir Markoc53c0792015-11-19 15:48:33 +00004659 // Get the index of the special input, if any.
4660 //
David Brazdil6de19382016-01-08 17:37:10 +00004661 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4662 // method pointer; otherwise there may be one platform-specific special input,
4663 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004664 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004665 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004666
Vladimir Marko58155012015-08-19 12:49:41 +00004667 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4668 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4669 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004670 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004671 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004672 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004673 bool HasPcRelativeMethodLoadKind() const {
4674 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004675 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004676 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004677 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004678 bool HasCurrentMethodInput() const {
4679 // This function can be called only after the invoke has been fully initialized by the builder.
4680 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004681 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004682 return true;
4683 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004684 DCHECK(InputCount() == GetSpecialInputIndex() ||
4685 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004686 return false;
4687 }
4688 }
Vladimir Marko58155012015-08-19 12:49:41 +00004689
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004690 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004691 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004692 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004693 }
4694
4695 uint64_t GetMethodAddress() const {
4696 DCHECK(HasMethodAddress());
4697 return dispatch_info_.method_load_data;
4698 }
4699
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004700 const DexFile& GetDexFileForPcRelativeDexCache() const;
4701
Vladimir Markoa1de9182016-02-25 11:37:38 +00004702 ClinitCheckRequirement GetClinitCheckRequirement() const {
4703 return GetPackedField<ClinitCheckRequirementField>();
4704 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004705
Roland Levillain4c0eb422015-04-24 16:43:49 +01004706 // Is this instruction a call to a static method?
4707 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004708 return GetInvokeType() == kStatic;
4709 }
4710
4711 MethodReference GetTargetMethod() const {
4712 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004713 }
4714
Vladimir Markofbb184a2015-11-13 14:47:00 +00004715 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4716 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4717 // instruction; only relevant for static calls with explicit clinit check.
4718 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004719 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004720 size_t last_input_index = inputs_.size() - 1u;
4721 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004722 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004723 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004724 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004725 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004726 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004727 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004728 }
4729
4730 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004731 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004732 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004733 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004734 }
4735
4736 // Is this a call to a static method whose declaring class has an
4737 // implicit intialization check requirement?
4738 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004739 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004740 }
4741
Vladimir Markob554b5a2015-11-06 12:57:55 +00004742 // Does this method load kind need the current method as an input?
4743 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004744 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004745 }
4746
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004747 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004748
Artem Serovcced8ba2017-07-19 18:18:09 +01004749 protected:
4750 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4751
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004752 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004753 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004754 static constexpr size_t kFieldClinitCheckRequirementSize =
4755 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4756 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4757 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4758 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4759 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004760 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4761 kFieldClinitCheckRequirement,
4762 kFieldClinitCheckRequirementSize>;
4763
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004764 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004765 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004766 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004767};
Vladimir Markof64242a2015-12-01 14:58:23 +00004768std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004769std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004770
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004771class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004772 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004773 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004774 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004775 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004776 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004777 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004778 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004779 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304780 : HInvoke(kInvokeVirtual,
4781 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004782 number_of_arguments,
4783 0u,
4784 return_type,
4785 dex_pc,
4786 dex_method_index,
4787 resolved_method,
4788 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304789 vtable_index_(vtable_index) {
4790 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004791
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004792 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004793
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004794 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004795 switch (GetIntrinsic()) {
4796 case Intrinsics::kThreadCurrentThread:
4797 case Intrinsics::kStringBufferAppend:
4798 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004799 case Intrinsics::kStringBuilderAppendObject:
4800 case Intrinsics::kStringBuilderAppendString:
4801 case Intrinsics::kStringBuilderAppendCharSequence:
4802 case Intrinsics::kStringBuilderAppendCharArray:
4803 case Intrinsics::kStringBuilderAppendBoolean:
4804 case Intrinsics::kStringBuilderAppendChar:
4805 case Intrinsics::kStringBuilderAppendInt:
4806 case Intrinsics::kStringBuilderAppendLong:
4807 case Intrinsics::kStringBuilderAppendFloat:
4808 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004809 case Intrinsics::kStringBuilderToString:
4810 return false;
4811 default:
4812 return HInvoke::CanBeNull();
4813 }
4814 }
4815
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004816 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004817 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004818 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004819 }
4820
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004821 uint32_t GetVTableIndex() const { return vtable_index_; }
4822
4823 DECLARE_INSTRUCTION(InvokeVirtual);
4824
Artem Serovcced8ba2017-07-19 18:18:09 +01004825 protected:
4826 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4827
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004828 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004829 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004830 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004831};
4832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004833class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004834 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004835 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004836 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004837 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004838 uint32_t dex_pc,
4839 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004840 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004841 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304842 : HInvoke(kInvokeInterface,
4843 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004844 number_of_arguments,
4845 0u,
4846 return_type,
4847 dex_pc,
4848 dex_method_index,
4849 resolved_method,
4850 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304851 imt_index_(imt_index) {
4852 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004854 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004855
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004856 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004857 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004858 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004859 }
4860
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004861 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004862 // The assembly stub currently needs it.
4863 return true;
4864 }
4865
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004866 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004867
4868 DECLARE_INSTRUCTION(InvokeInterface);
4869
Artem Serovcced8ba2017-07-19 18:18:09 +01004870 protected:
4871 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4872
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004873 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004874 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004875 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004876};
4877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004878class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004879 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004880 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304881 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004882 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004883 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004884
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004885 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004886
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004887 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004888 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004889 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004890 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004891 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004892 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004893 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004894 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4895 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004896 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004897 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4898 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004899
Roland Levillain88cb1752014-10-20 16:36:47 +01004900 DECLARE_INSTRUCTION(Neg);
4901
Artem Serovcced8ba2017-07-19 18:18:09 +01004902 protected:
4903 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004904};
4905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004906class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004907 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004908 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304909 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004910 SetRawInputAt(0, cls);
4911 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004912 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004913 }
4914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004915 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004916
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004917 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004918 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004919
Mingyao Yang0c365e62015-03-31 15:09:29 -07004920 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004921 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004922
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004923 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004924
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004925 HLoadClass* GetLoadClass() const {
4926 DCHECK(InputAt(0)->IsLoadClass());
4927 return InputAt(0)->AsLoadClass();
4928 }
4929
4930 HInstruction* GetLength() const {
4931 return InputAt(1);
4932 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004933
Vladimir Markob5461632018-10-15 14:24:21 +01004934 size_t GetComponentSizeShift() {
4935 return GetPackedField<ComponentSizeShiftField>();
4936 }
4937
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004938 DECLARE_INSTRUCTION(NewArray);
4939
Artem Serovcced8ba2017-07-19 18:18:09 +01004940 protected:
4941 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004942
4943 private:
4944 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4945 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4946 static constexpr size_t kNumberOfNewArrayPackedBits =
4947 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4948 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4949 using ComponentSizeShiftField =
4950 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004951};
4952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004953class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004954 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004955 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004956 HInstruction* left,
4957 HInstruction* right,
4958 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304959 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4960 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004962 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004963
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004964 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004965
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004966 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004967 return GetBlock()->GetGraph()->GetIntConstant(
4968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004971 return GetBlock()->GetGraph()->GetLongConstant(
4972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004973 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004974 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004975 return GetBlock()->GetGraph()->GetFloatConstant(
4976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4977 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004978 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004979 return GetBlock()->GetGraph()->GetDoubleConstant(
4980 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4981 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004982
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004983 DECLARE_INSTRUCTION(Add);
4984
Artem Serovcced8ba2017-07-19 18:18:09 +01004985 protected:
4986 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004987};
4988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004989class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004990 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004991 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004992 HInstruction* left,
4993 HInstruction* right,
4994 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304995 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4996 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004997
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004998 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005000 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005001 return GetBlock()->GetGraph()->GetIntConstant(
5002 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005003 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005004 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005005 return GetBlock()->GetGraph()->GetLongConstant(
5006 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005007 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005008 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005009 return GetBlock()->GetGraph()->GetFloatConstant(
5010 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5011 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005012 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005013 return GetBlock()->GetGraph()->GetDoubleConstant(
5014 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5015 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005016
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005017 DECLARE_INSTRUCTION(Sub);
5018
Artem Serovcced8ba2017-07-19 18:18:09 +01005019 protected:
5020 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005021};
5022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005023class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005024 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005025 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005026 HInstruction* left,
5027 HInstruction* right,
5028 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305029 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5030 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005032 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005033
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005034 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005036 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005037 return GetBlock()->GetGraph()->GetIntConstant(
5038 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005039 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005040 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005041 return GetBlock()->GetGraph()->GetLongConstant(
5042 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005043 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005044 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005045 return GetBlock()->GetGraph()->GetFloatConstant(
5046 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5047 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005048 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005049 return GetBlock()->GetGraph()->GetDoubleConstant(
5050 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5051 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005052
5053 DECLARE_INSTRUCTION(Mul);
5054
Artem Serovcced8ba2017-07-19 18:18:09 +01005055 protected:
5056 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005057};
5058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005059class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005060 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005061 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005062 HInstruction* left,
5063 HInstruction* right,
5064 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305065 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5066 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005067
Roland Levillain9867bc72015-08-05 10:21:34 +01005068 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005069 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005070 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005071 // Our graph structure ensures we never have 0 for `y` during
5072 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005073 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005074 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005075 return (y == -1) ? -x : x / y;
5076 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005077
Roland Levillain31dd3d62016-02-16 12:21:02 +00005078 template <typename T>
5079 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005080 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005081 return x / y;
5082 }
5083
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005084 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005085 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005086 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005087 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005088 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005089 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005090 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5091 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005092 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005093 return GetBlock()->GetGraph()->GetFloatConstant(
5094 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5095 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005096 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005097 return GetBlock()->GetGraph()->GetDoubleConstant(
5098 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005099 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005100
5101 DECLARE_INSTRUCTION(Div);
5102
Artem Serovcced8ba2017-07-19 18:18:09 +01005103 protected:
5104 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005105};
5106
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005107class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005108 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005109 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005110 HInstruction* left,
5111 HInstruction* right,
5112 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305113 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5114 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005115
Roland Levillain9867bc72015-08-05 10:21:34 +01005116 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005117 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005118 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005119 // Our graph structure ensures we never have 0 for `y` during
5120 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005121 DCHECK_NE(y, 0);
5122 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5123 return (y == -1) ? 0 : x % y;
5124 }
5125
Roland Levillain31dd3d62016-02-16 12:21:02 +00005126 template <typename T>
5127 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005128 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005129 return std::fmod(x, y);
5130 }
5131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005132 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005133 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005134 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005135 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005136 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005137 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005138 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005139 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005140 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005141 return GetBlock()->GetGraph()->GetFloatConstant(
5142 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5143 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005144 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005145 return GetBlock()->GetGraph()->GetDoubleConstant(
5146 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5147 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005148
5149 DECLARE_INSTRUCTION(Rem);
5150
Artem Serovcced8ba2017-07-19 18:18:09 +01005151 protected:
5152 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005153};
5154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005155class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005156 public:
5157 HMin(DataType::Type result_type,
5158 HInstruction* left,
5159 HInstruction* right,
5160 uint32_t dex_pc)
5161 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5162
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005163 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005164
5165 // Evaluation for integral values.
5166 template <typename T> static T ComputeIntegral(T x, T y) {
5167 return (x <= y) ? x : y;
5168 }
5169
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005170 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005171 return GetBlock()->GetGraph()->GetIntConstant(
5172 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5173 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005174 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005175 return GetBlock()->GetGraph()->GetLongConstant(
5176 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5177 }
5178 // TODO: Evaluation for floating-point values.
5179 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005180 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005181 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005182 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005183
5184 DECLARE_INSTRUCTION(Min);
5185
5186 protected:
5187 DEFAULT_COPY_CONSTRUCTOR(Min);
5188};
5189
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005190class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005191 public:
5192 HMax(DataType::Type result_type,
5193 HInstruction* left,
5194 HInstruction* right,
5195 uint32_t dex_pc)
5196 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5197
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005198 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005199
5200 // Evaluation for integral values.
5201 template <typename T> static T ComputeIntegral(T x, T y) {
5202 return (x >= y) ? x : y;
5203 }
5204
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005205 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005206 return GetBlock()->GetGraph()->GetIntConstant(
5207 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5208 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005210 return GetBlock()->GetGraph()->GetLongConstant(
5211 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5212 }
5213 // TODO: Evaluation for floating-point values.
5214 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005215 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005216 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005217 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005218
5219 DECLARE_INSTRUCTION(Max);
5220
5221 protected:
5222 DEFAULT_COPY_CONSTRUCTOR(Max);
5223};
5224
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005225class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005226 public:
5227 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5228 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5229
5230 // Evaluation for integral values.
5231 template <typename T> static T ComputeIntegral(T x) {
5232 return x < 0 ? -x : x;
5233 }
5234
5235 // Evaluation for floating-point values.
5236 // Note, as a "quality of implementation", rather than pure "spec compliance",
5237 // we require that Math.abs() clears the sign bit (but changes nothing else)
5238 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5239 // http://b/30758343
5240 template <typename T, typename S> static T ComputeFP(T x) {
5241 S bits = bit_cast<S, T>(x);
5242 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5243 }
5244
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005245 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005246 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5247 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005248 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005249 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5250 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005251 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005252 return GetBlock()->GetGraph()->GetFloatConstant(
5253 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5254 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005255 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005256 return GetBlock()->GetGraph()->GetDoubleConstant(
5257 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5258 }
5259
5260 DECLARE_INSTRUCTION(Abs);
5261
5262 protected:
5263 DEFAULT_COPY_CONSTRUCTOR(Abs);
5264};
5265
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005266class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005267 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005268 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005269 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5270 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005271 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005272 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005273 SetRawInputAt(0, value);
5274 }
5275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005276 bool IsClonable() const override { return true; }
5277 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005278
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005279 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005280 return true;
5281 }
5282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005283 bool NeedsEnvironment() const override { return true; }
5284 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005285
Calin Juravled0d48522014-11-04 16:40:20 +00005286 DECLARE_INSTRUCTION(DivZeroCheck);
5287
Artem Serovcced8ba2017-07-19 18:18:09 +01005288 protected:
5289 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005290};
5291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005292class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005293 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005294 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005295 HInstruction* value,
5296 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005297 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305298 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005299 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5300 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005301 }
5302
Roland Levillain5b5b9312016-03-22 14:57:31 +00005303 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005304 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005305 return value << (distance & max_shift_distance);
5306 }
5307
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005308 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005309 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005310 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005311 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005312 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005313 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005314 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005315 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005316 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005317 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005318 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5319 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005320 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005321 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005322 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005323 LOG(FATAL) << DebugName() << " is not defined for float values";
5324 UNREACHABLE();
5325 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005326 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005327 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005328 LOG(FATAL) << DebugName() << " is not defined for double values";
5329 UNREACHABLE();
5330 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005331
5332 DECLARE_INSTRUCTION(Shl);
5333
Artem Serovcced8ba2017-07-19 18:18:09 +01005334 protected:
5335 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005336};
5337
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005338class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005339 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005340 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005341 HInstruction* value,
5342 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005343 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305344 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005345 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5346 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005347 }
5348
Roland Levillain5b5b9312016-03-22 14:57:31 +00005349 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005350 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005351 return value >> (distance & max_shift_distance);
5352 }
5353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005354 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005355 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005356 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005357 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005358 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005359 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005360 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005361 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005362 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005363 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005364 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5365 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005366 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005367 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005368 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005369 LOG(FATAL) << DebugName() << " is not defined for float values";
5370 UNREACHABLE();
5371 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005372 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005373 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005374 LOG(FATAL) << DebugName() << " is not defined for double values";
5375 UNREACHABLE();
5376 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005377
5378 DECLARE_INSTRUCTION(Shr);
5379
Artem Serovcced8ba2017-07-19 18:18:09 +01005380 protected:
5381 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005382};
5383
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005384class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005385 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005386 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005387 HInstruction* value,
5388 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005389 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305390 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005391 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5392 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005393 }
5394
Roland Levillain5b5b9312016-03-22 14:57:31 +00005395 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005396 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005397 typedef typename std::make_unsigned<T>::type V;
5398 V ux = static_cast<V>(value);
5399 return static_cast<T>(ux >> (distance & max_shift_distance));
5400 }
5401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005402 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005403 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005404 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005405 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005406 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005407 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005408 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005409 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005410 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005411 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005412 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5413 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005414 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005415 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005416 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005417 LOG(FATAL) << DebugName() << " is not defined for float values";
5418 UNREACHABLE();
5419 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005420 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005421 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005422 LOG(FATAL) << DebugName() << " is not defined for double values";
5423 UNREACHABLE();
5424 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005425
5426 DECLARE_INSTRUCTION(UShr);
5427
Artem Serovcced8ba2017-07-19 18:18:09 +01005428 protected:
5429 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005430};
5431
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005432class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005433 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005434 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005435 HInstruction* left,
5436 HInstruction* right,
5437 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305438 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5439 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005440
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005441 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005442
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005443 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005444
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005445 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005446 return GetBlock()->GetGraph()->GetIntConstant(
5447 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005448 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005449 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005450 return GetBlock()->GetGraph()->GetLongConstant(
5451 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005452 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005453 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005454 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005455 LOG(FATAL) << DebugName() << " is not defined for float values";
5456 UNREACHABLE();
5457 }
5458 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005459 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005460 LOG(FATAL) << DebugName() << " is not defined for double values";
5461 UNREACHABLE();
5462 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005463
5464 DECLARE_INSTRUCTION(And);
5465
Artem Serovcced8ba2017-07-19 18:18:09 +01005466 protected:
5467 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005468};
5469
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005470class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005471 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005472 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005473 HInstruction* left,
5474 HInstruction* right,
5475 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305476 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5477 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005480
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005481 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005482
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005483 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005484 return GetBlock()->GetGraph()->GetIntConstant(
5485 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005486 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005487 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005488 return GetBlock()->GetGraph()->GetLongConstant(
5489 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005490 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005491 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005492 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005493 LOG(FATAL) << DebugName() << " is not defined for float values";
5494 UNREACHABLE();
5495 }
5496 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005497 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005498 LOG(FATAL) << DebugName() << " is not defined for double values";
5499 UNREACHABLE();
5500 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005501
5502 DECLARE_INSTRUCTION(Or);
5503
Artem Serovcced8ba2017-07-19 18:18:09 +01005504 protected:
5505 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005506};
5507
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005508class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005509 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005510 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005511 HInstruction* left,
5512 HInstruction* right,
5513 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305514 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5515 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005516
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005517 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005518
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005519 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005520
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005521 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005522 return GetBlock()->GetGraph()->GetIntConstant(
5523 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005524 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005525 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005526 return GetBlock()->GetGraph()->GetLongConstant(
5527 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005528 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005529 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005530 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005531 LOG(FATAL) << DebugName() << " is not defined for float values";
5532 UNREACHABLE();
5533 }
5534 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005535 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005536 LOG(FATAL) << DebugName() << " is not defined for double values";
5537 UNREACHABLE();
5538 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005539
5540 DECLARE_INSTRUCTION(Xor);
5541
Artem Serovcced8ba2017-07-19 18:18:09 +01005542 protected:
5543 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005544};
5545
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005546class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005547 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005548 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305549 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005550 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5551 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005552 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005553
Roland Levillain5b5b9312016-03-22 14:57:31 +00005554 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005555 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005556 typedef typename std::make_unsigned<T>::type V;
5557 V ux = static_cast<V>(value);
5558 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005559 return static_cast<T>(ux);
5560 } else {
5561 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005562 return static_cast<T>(ux >> (distance & max_shift_value)) |
5563 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005564 }
5565 }
5566
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005567 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005568 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005569 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005570 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005571 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005572 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005573 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005574 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005575 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005576 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005577 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5578 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005579 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005580 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005581 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005582 LOG(FATAL) << DebugName() << " is not defined for float values";
5583 UNREACHABLE();
5584 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005585 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005586 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005587 LOG(FATAL) << DebugName() << " is not defined for double values";
5588 UNREACHABLE();
5589 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005590
5591 DECLARE_INSTRUCTION(Ror);
5592
Artem Serovcced8ba2017-07-19 18:18:09 +01005593 protected:
5594 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005595};
5596
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005597// The value of a parameter in this method. Its location depends on
5598// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005599class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005600 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005601 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005602 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005603 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005604 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005605 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305606 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005607 dex_file_(dex_file),
5608 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005609 index_(index) {
5610 SetPackedFlag<kFlagIsThis>(is_this);
5611 SetPackedFlag<kFlagCanBeNull>(!is_this);
5612 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005613
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005614 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005615 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005616 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005617 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005618
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005619 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005620 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005621
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005622 DECLARE_INSTRUCTION(ParameterValue);
5623
Artem Serovcced8ba2017-07-19 18:18:09 +01005624 protected:
5625 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5626
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005627 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005628 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005629 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005630 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5631 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5632 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5633 "Too many packed fields.");
5634
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005635 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005636 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005637 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005638 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005639 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005640};
5641
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005642class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005643 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005644 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305645 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5646 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005647
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005648 bool CanBeMoved() const override { return true; }
5649 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005650 return true;
5651 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005652
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005653 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005655 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005656 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005657 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005658 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005659 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005660 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005661 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005662 LOG(FATAL) << DebugName() << " is not defined for float values";
5663 UNREACHABLE();
5664 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005665 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005666 LOG(FATAL) << DebugName() << " is not defined for double values";
5667 UNREACHABLE();
5668 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005669
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005670 DECLARE_INSTRUCTION(Not);
5671
Artem Serovcced8ba2017-07-19 18:18:09 +01005672 protected:
5673 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005674};
5675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005676class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005677 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005678 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305679 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5680 }
David Brazdil66d126e2015-04-03 16:02:44 +01005681
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005682 bool CanBeMoved() const override { return true; }
5683 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005684 return true;
5685 }
5686
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005687 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005688 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005689 return !x;
5690 }
5691
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005692 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005693 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005694 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005695 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005696 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005697 UNREACHABLE();
5698 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005699 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005700 LOG(FATAL) << DebugName() << " is not defined for float values";
5701 UNREACHABLE();
5702 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005703 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005704 LOG(FATAL) << DebugName() << " is not defined for double values";
5705 UNREACHABLE();
5706 }
David Brazdil66d126e2015-04-03 16:02:44 +01005707
5708 DECLARE_INSTRUCTION(BooleanNot);
5709
Artem Serovcced8ba2017-07-19 18:18:09 +01005710 protected:
5711 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005712};
5713
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005714class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005715 public:
5716 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005717 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305718 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005719 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005720 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005721 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005722 }
5723
5724 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005725 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5726 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005727
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005728 bool IsClonable() const override { return true; }
5729 bool CanBeMoved() const override { return true; }
5730 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005731 return true;
5732 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005733 // Return whether the conversion is implicit. This includes conversion to the same type.
5734 bool IsImplicitConversion() const {
5735 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5736 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005737
Mark Mendelle82549b2015-05-06 10:55:34 -04005738 // Try to statically evaluate the conversion and return a HConstant
5739 // containing the result. If the input cannot be converted, return nullptr.
5740 HConstant* TryStaticEvaluation() const;
5741
Roland Levillaindff1f282014-11-05 14:15:05 +00005742 DECLARE_INSTRUCTION(TypeConversion);
5743
Artem Serovcced8ba2017-07-19 18:18:09 +01005744 protected:
5745 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005746};
5747
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005748static constexpr uint32_t kNoRegNumber = -1;
5749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005750class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005751 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005752 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005753 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5754 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005755 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005756 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005757 SetRawInputAt(0, value);
5758 }
5759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005760 bool IsClonable() const override { return true; }
5761 bool CanBeMoved() const override { return true; }
5762 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005763 return true;
5764 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005765
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005766 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005767
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005768 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005769
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005770 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005771
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005772 DECLARE_INSTRUCTION(NullCheck);
5773
Artem Serovcced8ba2017-07-19 18:18:09 +01005774 protected:
5775 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005776};
5777
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005778// Embeds an ArtField and all the information required by the compiler. We cache
5779// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005780class FieldInfo : public ValueObject {
5781 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005782 FieldInfo(ArtField* field,
5783 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005784 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005785 bool is_volatile,
5786 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005787 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005788 const DexFile& dex_file)
5789 : field_(field),
5790 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005791 field_type_(field_type),
5792 is_volatile_(is_volatile),
5793 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005794 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005795 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005796
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005797 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005798 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005799 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005800 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005801 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005802 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005803 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005804
5805 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005806 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005807 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005808 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005809 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005810 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005811 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005812 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005813};
5814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005815class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005816 public:
5817 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005818 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005820 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005821 bool is_volatile,
5822 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005823 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005824 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005825 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305826 : HExpression(kInstanceFieldGet,
5827 field_type,
5828 SideEffects::FieldReadOfType(field_type, is_volatile),
5829 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005830 field_info_(field,
5831 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005832 field_type,
5833 is_volatile,
5834 field_idx,
5835 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005836 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005837 SetRawInputAt(0, value);
5838 }
5839
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005840 bool IsClonable() const override { return true; }
5841 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005843 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005844 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005845 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005846 }
5847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005848 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005849 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005850 }
5851
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005852 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005853 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5854 }
5855
Calin Juravle52c48962014-12-16 17:02:57 +00005856 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005857 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005858 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005859 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005860
Vladimir Marko61b92282017-10-11 13:23:17 +01005861 void SetType(DataType::Type new_type) {
5862 DCHECK(DataType::IsIntegralType(GetType()));
5863 DCHECK(DataType::IsIntegralType(new_type));
5864 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5865 SetPackedField<TypeField>(new_type);
5866 }
5867
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005868 DECLARE_INSTRUCTION(InstanceFieldGet);
5869
Artem Serovcced8ba2017-07-19 18:18:09 +01005870 protected:
5871 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5872
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005873 private:
5874 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005875};
5876
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005877class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005878 public:
5879 HInstanceFieldSet(HInstruction* object,
5880 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005881 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005883 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005884 bool is_volatile,
5885 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005886 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005887 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005888 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005889 : HExpression(kInstanceFieldSet,
5890 SideEffects::FieldWriteOfType(field_type, is_volatile),
5891 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005892 field_info_(field,
5893 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005894 field_type,
5895 is_volatile,
5896 field_idx,
5897 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005898 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005899 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005900 SetRawInputAt(0, object);
5901 SetRawInputAt(1, value);
5902 }
5903
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005904 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005906 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005907 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005908 }
5909
Calin Juravle52c48962014-12-16 17:02:57 +00005910 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005911 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005912 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005913 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005914 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005915 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5916 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005917
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005918 DECLARE_INSTRUCTION(InstanceFieldSet);
5919
Artem Serovcced8ba2017-07-19 18:18:09 +01005920 protected:
5921 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5922
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005923 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005924 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5925 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5926 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5927 "Too many packed fields.");
5928
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005929 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005930};
5931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005932class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005933 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005934 HArrayGet(HInstruction* array,
5935 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005936 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005937 uint32_t dex_pc)
5938 : HArrayGet(array,
5939 index,
5940 type,
5941 SideEffects::ArrayReadOfType(type),
5942 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005943 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305944 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005945
5946 HArrayGet(HInstruction* array,
5947 HInstruction* index,
5948 DataType::Type type,
5949 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005950 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005951 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305952 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005953 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005954 SetRawInputAt(0, array);
5955 SetRawInputAt(1, index);
5956 }
5957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005958 bool IsClonable() const override { return true; }
5959 bool CanBeMoved() const override { return true; }
5960 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005961 return true;
5962 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005963 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005964 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005965 // Currently, unless the array is the result of NewArray, the array access is always
5966 // preceded by some form of null NullCheck necessary for the bounds check, usually
5967 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5968 // dynamic BCE. There are cases when these could be removed to produce better code.
5969 // If we ever add optimizations to do so we should allow an implicit check here
5970 // (as long as the address falls in the first page).
5971 //
5972 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5973 // a = cond ? new int[1] : null;
5974 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005975 return false;
5976 }
5977
David Brazdil4833f5a2015-12-16 10:37:39 +00005978 bool IsEquivalentOf(HArrayGet* other) const {
5979 bool result = (GetDexPc() == other->GetDexPc());
5980 if (kIsDebugBuild && result) {
5981 DCHECK_EQ(GetBlock(), other->GetBlock());
5982 DCHECK_EQ(GetArray(), other->GetArray());
5983 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005984 if (DataType::IsIntOrLongType(GetType())) {
5985 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005986 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005987 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5988 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005989 }
5990 }
5991 return result;
5992 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005993
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005994 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5995
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005996 HInstruction* GetArray() const { return InputAt(0); }
5997 HInstruction* GetIndex() const { return InputAt(1); }
5998
Vladimir Marko61b92282017-10-11 13:23:17 +01005999 void SetType(DataType::Type new_type) {
6000 DCHECK(DataType::IsIntegralType(GetType()));
6001 DCHECK(DataType::IsIntegralType(new_type));
6002 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6003 SetPackedField<TypeField>(new_type);
6004 }
6005
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006006 DECLARE_INSTRUCTION(ArrayGet);
6007
Artem Serovcced8ba2017-07-19 18:18:09 +01006008 protected:
6009 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6010
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006011 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006012 // We treat a String as an array, creating the HArrayGet from String.charAt()
6013 // intrinsic in the instruction simplifier. We can always determine whether
6014 // a particular HArrayGet is actually a String.charAt() by looking at the type
6015 // of the input but that requires holding the mutator lock, so we prefer to use
6016 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006017 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006018 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6019 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6020 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006021};
6022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006023class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006024 public:
6025 HArraySet(HInstruction* array,
6026 HInstruction* index,
6027 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006028 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006029 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006030 : HArraySet(array,
6031 index,
6032 value,
6033 expected_component_type,
6034 // Make a best guess for side effects now, may be refined during SSA building.
6035 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306036 dex_pc) {
6037 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006038
6039 HArraySet(HInstruction* array,
6040 HInstruction* index,
6041 HInstruction* value,
6042 DataType::Type expected_component_type,
6043 SideEffects side_effects,
6044 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006045 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006046 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006047 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006048 SetPackedFlag<kFlagValueCanBeNull>(true);
6049 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006050 SetRawInputAt(0, array);
6051 SetRawInputAt(1, index);
6052 SetRawInputAt(2, value);
6053 }
6054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006055 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006056
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006057 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006058 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006059 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006060 }
6061
Mingyao Yang81014cb2015-06-02 03:16:27 -07006062 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006063 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006065 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006066 // TODO: Same as for ArrayGet.
6067 return false;
6068 }
6069
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006070 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006071 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006072 }
6073
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006074 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006075 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006076 }
6077
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006078 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006079 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006080 }
6081
Vladimir Markoa1de9182016-02-25 11:37:38 +00006082 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6083 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6084 bool StaticTypeOfArrayIsObjectArray() const {
6085 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6086 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006087
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006088 HInstruction* GetArray() const { return InputAt(0); }
6089 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006090 HInstruction* GetValue() const { return InputAt(2); }
6091
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006092 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006093 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6094 }
6095
6096 static DataType::Type GetComponentType(DataType::Type value_type,
6097 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006098 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006099 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006100 // be correct, we also check what is the value type. If it is a floating
6101 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006102 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006103 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006104 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006105 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006107 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006108 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006109 }
6110
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006111 static SideEffects ComputeSideEffects(DataType::Type type) {
6112 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006113 }
6114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006115 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6116 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6117 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006118 }
6119
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006120 DECLARE_INSTRUCTION(ArraySet);
6121
Artem Serovcced8ba2017-07-19 18:18:09 +01006122 protected:
6123 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6124
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006125 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006126 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6127 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006128 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006129 static constexpr size_t kFlagNeedsTypeCheck =
6130 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6131 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006132 // Cached information for the reference_type_info_ so that codegen
6133 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6135 static constexpr size_t kNumberOfArraySetPackedBits =
6136 kFlagStaticTypeOfArrayIsObjectArray + 1;
6137 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6138 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006139 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006140};
6141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006142class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006143 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006144 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306145 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006146 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006147 // Note that arrays do not change length, so the instruction does not
6148 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006149 SetRawInputAt(0, array);
6150 }
6151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006152 bool IsClonable() const override { return true; }
6153 bool CanBeMoved() const override { return true; }
6154 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006155 return true;
6156 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006157 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006158 return obj == InputAt(0);
6159 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006160
Vladimir Markodce016e2016-04-28 13:10:02 +01006161 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006163 DECLARE_INSTRUCTION(ArrayLength);
6164
Artem Serovcced8ba2017-07-19 18:18:09 +01006165 protected:
6166 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6167
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006168 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006169 // We treat a String as an array, creating the HArrayLength from String.length()
6170 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6171 // determine whether a particular HArrayLength is actually a String.length() by
6172 // looking at the type of the input but that requires holding the mutator lock, so
6173 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006174 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006175 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6176 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6177 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006178};
6179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006180class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006181 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006182 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006183 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6184 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006185 HBoundsCheck(HInstruction* index,
6186 HInstruction* length,
6187 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006188 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006189 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006190 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006191 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006192 SetRawInputAt(0, index);
6193 SetRawInputAt(1, length);
6194 }
6195
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006196 bool IsClonable() const override { return true; }
6197 bool CanBeMoved() const override { return true; }
6198 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006199 return true;
6200 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006201
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006202 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006203
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006204 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006205
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006206 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006207
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006208 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006209
6210 DECLARE_INSTRUCTION(BoundsCheck);
6211
Artem Serovcced8ba2017-07-19 18:18:09 +01006212 protected:
6213 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6214
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006215 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006216 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006217 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6218 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6219 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006220};
6221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006222class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006223 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006224 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006225 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306226 slow_path_(nullptr) {
6227 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006229 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006230
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006231 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006232 return true;
6233 }
6234
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006235 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6236 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006237
6238 DECLARE_INSTRUCTION(SuspendCheck);
6239
Artem Serovcced8ba2017-07-19 18:18:09 +01006240 protected:
6241 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6242
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006243 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006244 // Only used for code generation, in order to share the same slow path between back edges
6245 // of a same loop.
6246 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006247};
6248
David Srbecky0cf44932015-12-09 14:09:59 +00006249// Pseudo-instruction which provides the native debugger with mapping information.
6250// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006251class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006252 public:
6253 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006254 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306255 }
David Srbecky0cf44932015-12-09 14:09:59 +00006256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006257 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006258 return true;
6259 }
6260
6261 DECLARE_INSTRUCTION(NativeDebugInfo);
6262
Artem Serovcced8ba2017-07-19 18:18:09 +01006263 protected:
6264 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006265};
6266
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006267/**
6268 * Instruction to load a Class object.
6269 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006270class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006271 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006272 // Determines how to load the Class.
6273 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006274 // We cannot load this class. See HSharpening::SharpenLoadClass.
6275 kInvalid = -1,
6276
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006277 // Use the Class* from the method's own ArtMethod*.
6278 kReferrersClass,
6279
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006280 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006281 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006282 kBootImageLinkTimePcRelative,
6283
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006284 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006285 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006286 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006287
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006288 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006289 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006290 kBssEntry,
6291
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006292 // Use a known boot image Class* address, embedded in the code by the codegen.
6293 // Used for boot image classes referenced by apps in JIT-compiled code.
6294 kJitBootImageAddress,
6295
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006296 // Load from the root table associated with the JIT compiled method.
6297 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006298
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006299 // Load using a simple runtime call. This is the fall-back load kind when
6300 // the codegen is unable to use another appropriate kind.
6301 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006302
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006303 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006304 };
6305
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006306 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006307 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006308 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006309 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006310 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006311 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006312 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006313 : HInstruction(kLoadClass,
6314 DataType::Type::kReference,
6315 SideEffectsForArchRuntimeCalls(),
6316 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006317 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006318 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006319 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006320 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006321 // Referrers class should not need access check. We never inline unverified
6322 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006323 DCHECK(!is_referrers_class || !needs_access_check);
6324
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006325 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006326 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006327 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006328 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006329 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006330 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006331 }
6332
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006333 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006334
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006335 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006336
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006337 LoadKind GetLoadKind() const {
6338 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006339 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006340
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006341 bool HasPcRelativeLoadKind() const {
6342 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6343 GetLoadKind() == LoadKind::kBootImageRelRo ||
6344 GetLoadKind() == LoadKind::kBssEntry;
6345 }
6346
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006347 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006348
Yi Kong39402542019-03-24 02:47:16 -07006349 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006350
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006351 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006352
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006353 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006355 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006356 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006357 }
6358
Calin Juravle0ba218d2015-05-19 18:46:01 +01006359 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006360 // The entrypoint the code generator is going to call does not do
6361 // clinit of the class.
6362 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006363 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006364 }
6365
6366 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006367 return NeedsAccessCheck() ||
6368 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006369 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006370 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006371 }
6372
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006373 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006374 return NeedsAccessCheck() ||
6375 MustGenerateClinitCheck() ||
6376 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006377 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006378 GetLoadKind() == LoadKind::kBssEntry) &&
6379 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006380 }
6381
Calin Juravleacf735c2015-02-12 15:25:22 +00006382 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006383 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6384 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006385 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006386 } else {
6387 return ReferenceTypeInfo::CreateInvalid();
6388 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006389 }
6390
Vladimir Marko175e7862018-03-27 09:03:13 +00006391 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6392 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6393 DCHECK(klass_ != nullptr);
6394 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006395 }
6396
Andreas Gampea5b09a62016-11-17 15:21:22 -08006397 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006398 const DexFile& GetDexFile() const { return dex_file_; }
6399
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006400 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006401 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006402 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006403
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006404 static SideEffects SideEffectsForArchRuntimeCalls() {
6405 return SideEffects::CanTriggerGC();
6406 }
6407
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006408 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006409 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006410 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006411 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006412
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006413 bool MustResolveTypeOnSlowPath() const {
6414 // Check that this instruction has a slow path.
6415 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6416 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6417 return GetLoadKind() == LoadKind::kBssEntry;
6418 }
6419
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006420 void MarkInBootImage() {
6421 SetPackedFlag<kFlagIsInBootImage>(true);
6422 }
6423
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006424 void AddSpecialInput(HInstruction* special_input);
6425
6426 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006427 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006428 return ArrayRef<HUserRecord<HInstruction*>>(
6429 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6430 }
6431
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006432 Handle<mirror::Class> GetClass() const {
6433 return klass_;
6434 }
6435
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006436 DECLARE_INSTRUCTION(LoadClass);
6437
Artem Serovcced8ba2017-07-19 18:18:09 +01006438 protected:
6439 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6440
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006441 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006442 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006443 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006444 // Whether this instruction must generate the initialization check.
6445 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006446 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006447 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6448 static constexpr size_t kFieldLoadKindSize =
6449 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006450 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6451 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006452 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006453 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6454
6455 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006456 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006457 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006458 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006459 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006460 }
6461
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006462 void SetLoadKindInternal(LoadKind load_kind);
6463
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006464 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006465 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006466 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006467 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006468
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006469 // A type index and dex file where the class can be accessed. The dex file can be:
6470 // - The compiling method's dex file if the class is defined there too.
6471 // - The compiling method's dex file if the class is referenced there.
6472 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006473 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006474 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006475 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006476
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006477 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006478};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006479std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6480
6481// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006482inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6483 // The load kind should be determined before inserting the instruction to the graph.
6484 DCHECK(GetBlock() == nullptr);
6485 DCHECK(GetEnvironment() == nullptr);
6486 SetPackedField<LoadKindField>(load_kind);
6487 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6488 special_input_ = HUserRecord<HInstruction*>(nullptr);
6489 }
6490 if (!NeedsEnvironment()) {
6491 SetSideEffects(SideEffects::None());
6492 }
6493}
6494
6495// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006496inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006497 // The special input is used for PC-relative loads on some architectures,
6498 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006499 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006500 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006501 GetLoadKind() == LoadKind::kBssEntry ||
6502 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006503 DCHECK(special_input_.GetInstruction() == nullptr);
6504 special_input_ = HUserRecord<HInstruction*>(special_input);
6505 special_input->AddUseAt(this, 0);
6506}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006507
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006508class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006509 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006510 // Determines how to load the String.
6511 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006512 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006513 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006514 kBootImageLinkTimePcRelative,
6515
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006516 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006517 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006518 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006519
Vladimir Markoaad75c62016-10-03 08:46:48 +00006520 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006521 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006522 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006523
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006524 // Use a known boot image String* address, embedded in the code by the codegen.
6525 // Used for boot image strings referenced by apps in JIT-compiled code.
6526 kJitBootImageAddress,
6527
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006528 // Load from the root table associated with the JIT compiled method.
6529 kJitTableAddress,
6530
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006531 // Load using a simple runtime call. This is the fall-back load kind when
6532 // the codegen is unable to use another appropriate kind.
6533 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006534
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006535 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006536 };
6537
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006538 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006539 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006540 const DexFile& dex_file,
6541 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006542 : HInstruction(kLoadString,
6543 DataType::Type::kReference,
6544 SideEffectsForArchRuntimeCalls(),
6545 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006546 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006547 string_index_(string_index),
6548 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006549 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006550 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006551
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006552 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006553
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006554 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006555
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006556 LoadKind GetLoadKind() const {
6557 return GetPackedField<LoadKindField>();
6558 }
6559
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006560 bool HasPcRelativeLoadKind() const {
6561 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6562 GetLoadKind() == LoadKind::kBootImageRelRo ||
6563 GetLoadKind() == LoadKind::kBssEntry;
6564 }
6565
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006566 const DexFile& GetDexFile() const {
6567 return dex_file_;
6568 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006569
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006570 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006571 return string_index_;
6572 }
6573
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006574 Handle<mirror::String> GetString() const {
6575 return string_;
6576 }
6577
6578 void SetString(Handle<mirror::String> str) {
6579 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006580 }
6581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006582 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006583
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006584 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006585
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006586 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006587
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006588 // Will call the runtime if we need to load the string through
6589 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006590 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006591 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006592 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006593 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006594 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006595 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006596 return false;
6597 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006598 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006599 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006600
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006601 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006602 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006603 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006604
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006605 bool CanBeNull() const override { return false; }
6606 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006607
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006608 static SideEffects SideEffectsForArchRuntimeCalls() {
6609 return SideEffects::CanTriggerGC();
6610 }
6611
Vladimir Marko372f10e2016-05-17 16:30:10 +01006612 void AddSpecialInput(HInstruction* special_input);
6613
6614 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006615 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006616 return ArrayRef<HUserRecord<HInstruction*>>(
6617 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006618 }
6619
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006620 DECLARE_INSTRUCTION(LoadString);
6621
Artem Serovcced8ba2017-07-19 18:18:09 +01006622 protected:
6623 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6624
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006625 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006626 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006627 static constexpr size_t kFieldLoadKindSize =
6628 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6629 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006630 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006631 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006632
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006633 void SetLoadKindInternal(LoadKind load_kind);
6634
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006635 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006636 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006637 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006638 HUserRecord<HInstruction*> special_input_;
6639
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006640 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006641 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006642
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006643 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006644};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006645std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6646
6647// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006648inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6649 // The load kind should be determined before inserting the instruction to the graph.
6650 DCHECK(GetBlock() == nullptr);
6651 DCHECK(GetEnvironment() == nullptr);
6652 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6653 SetPackedField<LoadKindField>(load_kind);
6654 if (load_kind != LoadKind::kRuntimeCall) {
6655 special_input_ = HUserRecord<HInstruction*>(nullptr);
6656 }
6657 if (!NeedsEnvironment()) {
6658 SetSideEffects(SideEffects::None());
6659 }
6660}
6661
6662// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006663inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006664 // The special input is used for PC-relative loads on some architectures,
6665 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006666 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006667 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006668 GetLoadKind() == LoadKind::kBssEntry ||
6669 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006670 // HLoadString::GetInputRecords() returns an empty array at this point,
6671 // so use the GetInputRecords() from the base class to set the input record.
6672 DCHECK(special_input_.GetInstruction() == nullptr);
6673 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006674 special_input->AddUseAt(this, 0);
6675}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006676
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006677class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006678 public:
6679 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006680 uint16_t method_handle_idx,
6681 const DexFile& dex_file,
6682 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006683 : HInstruction(kLoadMethodHandle,
6684 DataType::Type::kReference,
6685 SideEffectsForArchRuntimeCalls(),
6686 dex_pc),
6687 special_input_(HUserRecord<HInstruction*>(current_method)),
6688 method_handle_idx_(method_handle_idx),
6689 dex_file_(dex_file) {
6690 }
6691
6692 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006693 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006694 return ArrayRef<HUserRecord<HInstruction*>>(
6695 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6696 }
6697
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006698 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006699
6700 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6701
6702 const DexFile& GetDexFile() const { return dex_file_; }
6703
6704 static SideEffects SideEffectsForArchRuntimeCalls() {
6705 return SideEffects::CanTriggerGC();
6706 }
6707
6708 DECLARE_INSTRUCTION(LoadMethodHandle);
6709
6710 protected:
6711 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6712
6713 private:
6714 // The special input is the HCurrentMethod for kRuntimeCall.
6715 HUserRecord<HInstruction*> special_input_;
6716
6717 const uint16_t method_handle_idx_;
6718 const DexFile& dex_file_;
6719};
6720
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006721class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006722 public:
6723 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006724 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006725 const DexFile& dex_file,
6726 uint32_t dex_pc)
6727 : HInstruction(kLoadMethodType,
6728 DataType::Type::kReference,
6729 SideEffectsForArchRuntimeCalls(),
6730 dex_pc),
6731 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006732 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006733 dex_file_(dex_file) {
6734 }
6735
6736 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006737 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006738 return ArrayRef<HUserRecord<HInstruction*>>(
6739 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6740 }
6741
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006742 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006743
Orion Hodson06d10a72018-05-14 08:53:38 +01006744 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006745
6746 const DexFile& GetDexFile() const { return dex_file_; }
6747
6748 static SideEffects SideEffectsForArchRuntimeCalls() {
6749 return SideEffects::CanTriggerGC();
6750 }
6751
6752 DECLARE_INSTRUCTION(LoadMethodType);
6753
6754 protected:
6755 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6756
6757 private:
6758 // The special input is the HCurrentMethod for kRuntimeCall.
6759 HUserRecord<HInstruction*> special_input_;
6760
Orion Hodson06d10a72018-05-14 08:53:38 +01006761 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006762 const DexFile& dex_file_;
6763};
6764
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006765/**
6766 * Performs an initialization check on its Class object input.
6767 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006768class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006769 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006770 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006771 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306772 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006773 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006774 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006775 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006776 SetRawInputAt(0, constant);
6777 }
Artem Serova6e26142018-06-19 14:55:17 +01006778 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006779 bool CanBeMoved() const override { return true; }
6780 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006781 return true;
6782 }
6783
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006784 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006785 // May call runtime to initialize the class.
6786 return true;
6787 }
6788
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006789 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006790
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006791 HLoadClass* GetLoadClass() const {
6792 DCHECK(InputAt(0)->IsLoadClass());
6793 return InputAt(0)->AsLoadClass();
6794 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006795
6796 DECLARE_INSTRUCTION(ClinitCheck);
6797
Artem Serovcced8ba2017-07-19 18:18:09 +01006798
6799 protected:
6800 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006801};
6802
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006803class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006804 public:
6805 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006806 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006807 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006808 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006809 bool is_volatile,
6810 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006811 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006812 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006813 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306814 : HExpression(kStaticFieldGet,
6815 field_type,
6816 SideEffects::FieldReadOfType(field_type, is_volatile),
6817 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006818 field_info_(field,
6819 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006820 field_type,
6821 is_volatile,
6822 field_idx,
6823 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006824 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006825 SetRawInputAt(0, cls);
6826 }
6827
Calin Juravle52c48962014-12-16 17:02:57 +00006828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006829 bool IsClonable() const override { return true; }
6830 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006832 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006833 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006834 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006835 }
6836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006837 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006838 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6839 }
6840
Calin Juravle52c48962014-12-16 17:02:57 +00006841 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006842 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006843 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006844 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006845
Vladimir Marko61b92282017-10-11 13:23:17 +01006846 void SetType(DataType::Type new_type) {
6847 DCHECK(DataType::IsIntegralType(GetType()));
6848 DCHECK(DataType::IsIntegralType(new_type));
6849 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6850 SetPackedField<TypeField>(new_type);
6851 }
6852
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006853 DECLARE_INSTRUCTION(StaticFieldGet);
6854
Artem Serovcced8ba2017-07-19 18:18:09 +01006855 protected:
6856 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6857
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006858 private:
6859 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006860};
6861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006862class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006863 public:
6864 HStaticFieldSet(HInstruction* cls,
6865 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006866 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006867 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006868 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006869 bool is_volatile,
6870 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006871 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006872 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006873 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006874 : HExpression(kStaticFieldSet,
6875 SideEffects::FieldWriteOfType(field_type, is_volatile),
6876 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006877 field_info_(field,
6878 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006879 field_type,
6880 is_volatile,
6881 field_idx,
6882 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006883 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006884 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006885 SetRawInputAt(0, cls);
6886 SetRawInputAt(1, value);
6887 }
6888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006889 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006890 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006891 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006892 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006893 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006894
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006895 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006896 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6897 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006898
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006899 DECLARE_INSTRUCTION(StaticFieldSet);
6900
Artem Serovcced8ba2017-07-19 18:18:09 +01006901 protected:
6902 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6903
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006904 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006905 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6906 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6907 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6908 "Too many packed fields.");
6909
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006910 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006911};
6912
Vladimir Marko552a1342017-10-31 10:56:47 +00006913class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6914 public:
6915 HStringBuilderAppend(HIntConstant* format,
6916 uint32_t number_of_arguments,
6917 ArenaAllocator* allocator,
6918 uint32_t dex_pc)
6919 : HVariableInputSizeInstruction(
6920 kStringBuilderAppend,
6921 DataType::Type::kReference,
6922 // The runtime call may read memory from inputs. It never writes outside
6923 // of the newly allocated result object (or newly allocated helper objects).
6924 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6925 dex_pc,
6926 allocator,
6927 number_of_arguments + /* format */ 1u,
6928 kArenaAllocInvokeInputs) {
6929 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6930 SetRawInputAt(FormatIndex(), format);
6931 }
6932
6933 void SetArgumentAt(size_t index, HInstruction* argument) {
6934 DCHECK_LE(index, GetNumberOfArguments());
6935 SetRawInputAt(index, argument);
6936 }
6937
6938 // Return the number of arguments, excluding the format.
6939 size_t GetNumberOfArguments() const {
6940 DCHECK_GE(InputCount(), 1u);
6941 return InputCount() - 1u;
6942 }
6943
6944 size_t FormatIndex() const {
6945 return GetNumberOfArguments();
6946 }
6947
6948 HIntConstant* GetFormat() {
6949 return InputAt(FormatIndex())->AsIntConstant();
6950 }
6951
6952 bool NeedsEnvironment() const override { return true; }
6953
6954 bool CanThrow() const override { return true; }
6955
6956 DECLARE_INSTRUCTION(StringBuilderAppend);
6957
6958 protected:
6959 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
6960};
6961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006962class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006963 public:
6964 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006965 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006966 uint32_t field_index,
6967 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306968 : HExpression(kUnresolvedInstanceFieldGet,
6969 field_type,
6970 SideEffects::AllExceptGCDependency(),
6971 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006972 field_index_(field_index) {
6973 SetRawInputAt(0, obj);
6974 }
6975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006976 bool IsClonable() const override { return true; }
6977 bool NeedsEnvironment() const override { return true; }
6978 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006979
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006980 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006981 uint32_t GetFieldIndex() const { return field_index_; }
6982
6983 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6984
Artem Serovcced8ba2017-07-19 18:18:09 +01006985 protected:
6986 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6987
Calin Juravlee460d1d2015-09-29 04:52:17 +01006988 private:
6989 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006990};
6991
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006992class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006993 public:
6994 HUnresolvedInstanceFieldSet(HInstruction* obj,
6995 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006996 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006997 uint32_t field_index,
6998 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006999 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007000 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007001 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007002 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007003 SetRawInputAt(0, obj);
7004 SetRawInputAt(1, value);
7005 }
7006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007007 bool IsClonable() const override { return true; }
7008 bool NeedsEnvironment() const override { return true; }
7009 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007010
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007011 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007012 uint32_t GetFieldIndex() const { return field_index_; }
7013
7014 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7015
Artem Serovcced8ba2017-07-19 18:18:09 +01007016 protected:
7017 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7018
Calin Juravlee460d1d2015-09-29 04:52:17 +01007019 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007020 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7021 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007022 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007023 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7024 kFieldFieldType + kFieldFieldTypeSize;
7025 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7026 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007027 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007028
Calin Juravlee460d1d2015-09-29 04:52:17 +01007029 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007030};
7031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007032class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007033 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007034 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007035 uint32_t field_index,
7036 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307037 : HExpression(kUnresolvedStaticFieldGet,
7038 field_type,
7039 SideEffects::AllExceptGCDependency(),
7040 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007041 field_index_(field_index) {
7042 }
7043
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007044 bool IsClonable() const override { return true; }
7045 bool NeedsEnvironment() const override { return true; }
7046 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007048 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007049 uint32_t GetFieldIndex() const { return field_index_; }
7050
7051 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7052
Artem Serovcced8ba2017-07-19 18:18:09 +01007053 protected:
7054 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7055
Calin Juravlee460d1d2015-09-29 04:52:17 +01007056 private:
7057 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007058};
7059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007060class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007061 public:
7062 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007063 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007064 uint32_t field_index,
7065 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007066 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007067 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007068 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007069 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007070 SetRawInputAt(0, value);
7071 }
7072
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007073 bool IsClonable() const override { return true; }
7074 bool NeedsEnvironment() const override { return true; }
7075 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007077 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007078 uint32_t GetFieldIndex() const { return field_index_; }
7079
7080 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7081
Artem Serovcced8ba2017-07-19 18:18:09 +01007082 protected:
7083 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7084
Calin Juravlee460d1d2015-09-29 04:52:17 +01007085 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007086 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7087 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007088 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007089 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7090 kFieldFieldType + kFieldFieldTypeSize;
7091 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7092 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007093 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007094
Calin Juravlee460d1d2015-09-29 04:52:17 +01007095 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007096};
7097
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007098// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007099class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007100 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007101 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307102 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7103 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007104
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007105 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007106
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007107 DECLARE_INSTRUCTION(LoadException);
7108
Artem Serovcced8ba2017-07-19 18:18:09 +01007109 protected:
7110 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007111};
7112
David Brazdilcb1c0552015-08-04 16:22:25 +01007113// Implicit part of move-exception which clears thread-local exception storage.
7114// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007115class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007116 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007117 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007118 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307119 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007120
7121 DECLARE_INSTRUCTION(ClearException);
7122
Artem Serovcced8ba2017-07-19 18:18:09 +01007123 protected:
7124 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007125};
7126
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007127class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007128 public:
7129 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007130 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007131 SetRawInputAt(0, exception);
7132 }
7133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007134 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007135
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007136 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007137
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007138 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007139
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007140 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007141
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007142 DECLARE_INSTRUCTION(Throw);
7143
Artem Serovcced8ba2017-07-19 18:18:09 +01007144 protected:
7145 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007146};
7147
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007148/**
7149 * Implementation strategies for the code generator of a HInstanceOf
7150 * or `HCheckCast`.
7151 */
7152enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007153 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007154 kExactCheck, // Can do a single class compare.
7155 kClassHierarchyCheck, // Can just walk the super class chain.
7156 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7157 kInterfaceCheck, // No optimization yet when checking against an interface.
7158 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007159 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007160 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007161 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007162};
7163
Roland Levillain86503782016-02-11 19:07:30 +00007164std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7165
Vladimir Marko175e7862018-03-27 09:03:13 +00007166// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7167// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7168class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007169 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007170 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007171 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007172 HInstruction* object,
7173 HInstruction* target_class_or_null,
7174 TypeCheckKind check_kind,
7175 Handle<mirror::Class> klass,
7176 uint32_t dex_pc,
7177 ArenaAllocator* allocator,
7178 HIntConstant* bitstring_path_to_root,
7179 HIntConstant* bitstring_mask,
7180 SideEffects side_effects)
7181 : HVariableInputSizeInstruction(
7182 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007183 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007184 side_effects,
7185 dex_pc,
7186 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007187 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007188 kArenaAllocTypeCheckInputs),
7189 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007190 SetPackedField<TypeCheckKindField>(check_kind);
7191 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007192 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007193 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007194 SetRawInputAt(1, target_class_or_null);
7195 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7196 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7197 if (check_kind == TypeCheckKind::kBitstringCheck) {
7198 DCHECK(target_class_or_null->IsNullConstant());
7199 SetRawInputAt(2, bitstring_path_to_root);
7200 SetRawInputAt(3, bitstring_mask);
7201 } else {
7202 DCHECK(target_class_or_null->IsLoadClass());
7203 }
Vladimir Marko87584542017-12-12 17:47:52 +00007204 }
7205
7206 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007207 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007208 HInstruction* load_class = InputAt(1);
7209 DCHECK(load_class->IsLoadClass());
7210 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007211 }
7212
Vladimir Marko175e7862018-03-27 09:03:13 +00007213 uint32_t GetBitstringPathToRoot() const {
7214 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7215 HInstruction* path_to_root = InputAt(2);
7216 DCHECK(path_to_root->IsIntConstant());
7217 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7218 }
7219
7220 uint32_t GetBitstringMask() const {
7221 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7222 HInstruction* mask = InputAt(3);
7223 DCHECK(mask->IsIntConstant());
7224 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7225 }
7226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007227 bool IsClonable() const override { return true; }
7228 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007230 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007231 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7232 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007233 }
7234
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007235 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7236 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7237 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7238 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7239
Vladimir Marko175e7862018-03-27 09:03:13 +00007240 ReferenceTypeInfo GetTargetClassRTI() {
7241 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7242 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007243 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007244 } else {
7245 return ReferenceTypeInfo::CreateInvalid();
7246 }
7247 }
7248
7249 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7250 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7251 DCHECK(klass_ != nullptr);
7252 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7253 }
7254
7255 Handle<mirror::Class> GetClass() const {
7256 return klass_;
7257 }
7258
7259 protected:
7260 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7261
7262 private:
7263 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7264 static constexpr size_t kFieldTypeCheckKindSize =
7265 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7266 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7267 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7268 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7269 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7270 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7271
7272 Handle<mirror::Class> klass_;
7273};
7274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007275class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007276 public:
7277 HInstanceOf(HInstruction* object,
7278 HInstruction* target_class_or_null,
7279 TypeCheckKind check_kind,
7280 Handle<mirror::Class> klass,
7281 uint32_t dex_pc,
7282 ArenaAllocator* allocator,
7283 HIntConstant* bitstring_path_to_root,
7284 HIntConstant* bitstring_mask)
7285 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007286 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007287 object,
7288 target_class_or_null,
7289 check_kind,
7290 klass,
7291 dex_pc,
7292 allocator,
7293 bitstring_path_to_root,
7294 bitstring_mask,
7295 SideEffectsForArchRuntimeCalls(check_kind)) {}
7296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007297 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007299 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007300 return CanCallRuntime(GetTypeCheckKind());
7301 }
7302
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007303 static bool CanCallRuntime(TypeCheckKind check_kind) {
7304 // Mips currently does runtime calls for any other checks.
7305 return check_kind != TypeCheckKind::kExactCheck;
7306 }
7307
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007308 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007309 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007310 }
7311
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007312 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007313
Artem Serovcced8ba2017-07-19 18:18:09 +01007314 protected:
7315 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007316};
7317
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007318class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007319 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007320 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307321 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007322 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7323 SetPackedFlag<kFlagUpperCanBeNull>(true);
7324 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007325 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007326 SetRawInputAt(0, input);
7327 }
7328
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007329 bool InstructionDataEquals(const HInstruction* other) const override;
7330 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007331
David Brazdilf5552582015-12-27 13:36:12 +00007332 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007333 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007334 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007335 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007336
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007337 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007338 DCHECK(GetUpperCanBeNull() || !can_be_null);
7339 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007340 }
7341
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007342 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007343
Calin Juravleb1498f62015-02-16 13:13:29 +00007344 DECLARE_INSTRUCTION(BoundType);
7345
Artem Serovcced8ba2017-07-19 18:18:09 +01007346 protected:
7347 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7348
Calin Juravleb1498f62015-02-16 13:13:29 +00007349 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007350 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7351 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007352 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007353 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7354 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7355 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7356
Calin Juravleb1498f62015-02-16 13:13:29 +00007357 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007358 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7359 // It is used to bound the type in cases like:
7360 // if (x instanceof ClassX) {
7361 // // uper_bound_ will be ClassX
7362 // }
David Brazdilf5552582015-12-27 13:36:12 +00007363 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007364};
7365
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007366class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007367 public:
7368 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007369 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007370 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007371 Handle<mirror::Class> klass,
7372 uint32_t dex_pc,
7373 ArenaAllocator* allocator,
7374 HIntConstant* bitstring_path_to_root,
7375 HIntConstant* bitstring_mask)
7376 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007377 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007378 object,
7379 target_class_or_null,
7380 check_kind,
7381 klass,
7382 dex_pc,
7383 allocator,
7384 bitstring_path_to_root,
7385 bitstring_mask,
7386 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007388 bool IsClonable() const override { return true; }
7389 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007390 // Instruction may throw a CheckCastError.
7391 return true;
7392 }
7393
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007394 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007395
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007396 DECLARE_INSTRUCTION(CheckCast);
7397
Artem Serovcced8ba2017-07-19 18:18:09 +01007398 protected:
7399 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007400};
7401
Andreas Gampe26de38b2016-07-27 17:53:11 -07007402/**
7403 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7404 * @details We define the combined barrier types that are actually required
7405 * by the Java Memory Model, rather than using exactly the terminology from
7406 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7407 * primitives. Note that the JSR-133 cookbook generally does not deal with
7408 * store atomicity issues, and the recipes there are not always entirely sufficient.
7409 * The current recipe is as follows:
7410 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7411 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7412 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7413 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7414 * class has final fields.
7415 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7416 * store-to-memory instructions. Only generated together with non-temporal stores.
7417 */
7418enum MemBarrierKind {
7419 kAnyStore,
7420 kLoadAny,
7421 kStoreStore,
7422 kAnyAny,
7423 kNTStoreStore,
7424 kLastBarrierKind = kNTStoreStore
7425};
7426std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7427
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007428class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007429 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007430 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007431 : HExpression(kMemoryBarrier,
7432 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7433 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007434 SetPackedField<BarrierKindField>(barrier_kind);
7435 }
Calin Juravle27df7582015-04-17 19:12:31 +01007436
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007437 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007438
Vladimir Markoa1de9182016-02-25 11:37:38 +00007439 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007440
7441 DECLARE_INSTRUCTION(MemoryBarrier);
7442
Artem Serovcced8ba2017-07-19 18:18:09 +01007443 protected:
7444 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7445
Calin Juravle27df7582015-04-17 19:12:31 +01007446 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007447 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7448 static constexpr size_t kFieldBarrierKindSize =
7449 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7450 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7451 kFieldBarrierKind + kFieldBarrierKindSize;
7452 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7453 "Too many packed fields.");
7454 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007455};
7456
Igor Murashkind01745e2017-04-05 16:40:31 -07007457// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7458// the specified object(s), with respect to any subsequent store that might "publish"
7459// (i.e. make visible) the specified object to another thread.
7460//
7461// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7462// for all final fields (that were set) at the end of the invoked constructor.
7463//
7464// The constructor fence models the freeze actions for the final fields of an object
7465// being constructed (semantically at the end of the constructor). Constructor fences
7466// have a per-object affinity; two separate objects being constructed get two separate
7467// constructor fences.
7468//
7469// (Note: that if calling a super-constructor or forwarding to another constructor,
7470// the freezes would happen at the end of *that* constructor being invoked).
7471//
7472// The memory model guarantees that when the object being constructed is "published" after
7473// constructor completion (i.e. escapes the current thread via a store), then any final field
7474// writes must be observable on other threads (once they observe that publication).
7475//
7476// Further, anything written before the freeze, and read by dereferencing through the final field,
7477// must also be visible (so final object field could itself have an object with non-final fields;
7478// yet the freeze must also extend to them).
7479//
7480// Constructor example:
7481//
7482// class HasFinal {
7483// final int field; Optimizing IR for <init>()V:
7484// HasFinal() {
7485// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7486// // freeze(this.field); HConstructorFence(this)
7487// } HReturn
7488// }
7489//
7490// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7491// already-initialized classes; in that case the allocation must act as a "default-initializer"
7492// of the object which effectively writes the class pointer "final field".
7493//
7494// For example, we can model default-initialiation as roughly the equivalent of the following:
7495//
7496// class Object {
7497// private final Class header;
7498// }
7499//
7500// Java code: Optimizing IR:
7501//
7502// T new_instance<T>() {
7503// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7504// obj.header = T.class; // header write is done by above call.
7505// // freeze(obj.header) HConstructorFence(obj)
7506// return (T)obj;
7507// }
7508//
7509// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007510// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007511// * QuasiAtomic::ThreadFenceForConstructor
7512//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007513class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007514 // A fence has variable inputs because the inputs can be removed
7515 // after prepare_for_register_allocation phase.
7516 // (TODO: In the future a fence could freeze multiple objects
7517 // after merging two fences together.)
7518 public:
7519 // `fence_object` is the reference that needs to be protected for correct publication.
7520 //
7521 // It makes sense in the following situations:
7522 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7523 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7524 //
7525 // After construction the `fence_object` becomes the 0th input.
7526 // This is not an input in a real sense, but just a convenient place to stash the information
7527 // about the associated object.
7528 HConstructorFence(HInstruction* fence_object,
7529 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007530 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007531 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7532 // constraints described in the class header. We claim that these SideEffects constraints
7533 // enforce a superset of the real constraints.
7534 //
7535 // The ordering described above is conservatively modeled with SideEffects as follows:
7536 //
7537 // * To prevent reordering of the publication stores:
7538 // ----> "Reads of objects" is the initial SideEffect.
7539 // * For every primitive final field store in the constructor:
7540 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7541 // * If there are any stores to reference final fields in the constructor:
7542 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7543 // that are reachable from `fence_object` also need to be prevented for reordering
7544 // (and we do not want to do alias analysis to figure out what those stores are).
7545 //
7546 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7547 // even more conservative approach than the one described above, and prevents all of the
7548 // above reordering without analyzing any of the instructions in the constructor.
7549 //
7550 // If in a later phase we discover that there are no writes to reference final fields,
7551 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307552 : HVariableInputSizeInstruction(kConstructorFence,
7553 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007554 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007555 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007556 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007557 kArenaAllocConstructorFenceInputs) {
7558 DCHECK(fence_object != nullptr);
7559 SetRawInputAt(0, fence_object);
7560 }
7561
7562 // The object associated with this constructor fence.
7563 //
7564 // (Note: This will be null after the prepare_for_register_allocation phase,
7565 // as all constructor fence inputs are removed there).
7566 HInstruction* GetFenceObject() const {
7567 return InputAt(0);
7568 }
7569
7570 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7571 // - Delete `fence_use` from `this`'s use list.
7572 // - Delete `this` from `fence_use`'s inputs list.
7573 // - If the `fence_use` is dead, remove it from the graph.
7574 //
7575 // A fence is considered dead once it no longer has any uses
7576 // and all of the inputs are dead.
7577 //
7578 // This must *not* be called during/after prepare_for_register_allocation,
7579 // because that removes all the inputs to the fences but the fence is actually
7580 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007581 //
7582 // Returns how many HConstructorFence instructions were removed from graph.
7583 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007584
Igor Murashkindd018df2017-08-09 10:38:31 -07007585 // Combine all inputs of `this` and `other` instruction and remove
7586 // `other` from the graph.
7587 //
7588 // Inputs are unique after the merge.
7589 //
7590 // Requirement: `this` must not be the same as `other.
7591 void Merge(HConstructorFence* other);
7592
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007593 // Check if this constructor fence is protecting
7594 // an HNewInstance or HNewArray that is also the immediate
7595 // predecessor of `this`.
7596 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007597 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7598 // to be one of the inputs of `this`.
7599 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007600 // Returns the associated HNewArray or HNewInstance,
7601 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007602 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007603
Igor Murashkind01745e2017-04-05 16:40:31 -07007604 DECLARE_INSTRUCTION(ConstructorFence);
7605
Artem Serovcced8ba2017-07-19 18:18:09 +01007606 protected:
7607 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007608};
7609
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007610class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007611 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007612 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007613 kEnter,
7614 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007615 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007616 };
7617
7618 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007619 : HExpression(kMonitorOperation,
7620 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7621 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007622 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007623 SetRawInputAt(0, object);
7624 }
7625
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007626 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007627 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007628
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007629 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007630 // Verifier guarantees that monitor-exit cannot throw.
7631 // This is important because it allows the HGraphBuilder to remove
7632 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7633 return IsEnter();
7634 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007635
Vladimir Markoa1de9182016-02-25 11:37:38 +00007636 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7637 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007638
7639 DECLARE_INSTRUCTION(MonitorOperation);
7640
Artem Serovcced8ba2017-07-19 18:18:09 +01007641 protected:
7642 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7643
Calin Juravle52c48962014-12-16 17:02:57 +00007644 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007645 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7646 static constexpr size_t kFieldOperationKindSize =
7647 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7648 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7649 kFieldOperationKind + kFieldOperationKindSize;
7650 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7651 "Too many packed fields.");
7652 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007653};
7654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007655class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007656 public:
7657 HSelect(HInstruction* condition,
7658 HInstruction* true_value,
7659 HInstruction* false_value,
7660 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307661 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007662 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7663
7664 // First input must be `true_value` or `false_value` to allow codegens to
7665 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7666 // that architectures which implement HSelect as a conditional move also
7667 // will not need to invert the condition.
7668 SetRawInputAt(0, false_value);
7669 SetRawInputAt(1, true_value);
7670 SetRawInputAt(2, condition);
7671 }
7672
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007673 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007674 HInstruction* GetFalseValue() const { return InputAt(0); }
7675 HInstruction* GetTrueValue() const { return InputAt(1); }
7676 HInstruction* GetCondition() const { return InputAt(2); }
7677
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007678 bool CanBeMoved() const override { return true; }
7679 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007680 return true;
7681 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007682
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007683 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007684 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7685 }
7686
7687 DECLARE_INSTRUCTION(Select);
7688
Artem Serovcced8ba2017-07-19 18:18:09 +01007689 protected:
7690 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007691};
7692
Vladimir Markof9f64412015-09-02 14:05:49 +01007693class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007694 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007695 MoveOperands(Location source,
7696 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007697 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007698 HInstruction* instruction)
7699 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007700
7701 Location GetSource() const { return source_; }
7702 Location GetDestination() const { return destination_; }
7703
7704 void SetSource(Location value) { source_ = value; }
7705 void SetDestination(Location value) { destination_ = value; }
7706
7707 // The parallel move resolver marks moves as "in-progress" by clearing the
7708 // destination (but not the source).
7709 Location MarkPending() {
7710 DCHECK(!IsPending());
7711 Location dest = destination_;
7712 destination_ = Location::NoLocation();
7713 return dest;
7714 }
7715
7716 void ClearPending(Location dest) {
7717 DCHECK(IsPending());
7718 destination_ = dest;
7719 }
7720
7721 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007722 DCHECK(source_.IsValid() || destination_.IsInvalid());
7723 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007724 }
7725
7726 // True if this blocks a move from the given location.
7727 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007728 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007729 }
7730
7731 // A move is redundant if it's been eliminated, if its source and
7732 // destination are the same, or if its destination is unneeded.
7733 bool IsRedundant() const {
7734 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7735 }
7736
7737 // We clear both operands to indicate move that's been eliminated.
7738 void Eliminate() {
7739 source_ = destination_ = Location::NoLocation();
7740 }
7741
7742 bool IsEliminated() const {
7743 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7744 return source_.IsInvalid();
7745 }
7746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007747 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007748
Nicolas Geoffray90218252015-04-15 11:56:51 +01007749 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007750 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007751 }
7752
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007753 HInstruction* GetInstruction() const { return instruction_; }
7754
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007755 private:
7756 Location source_;
7757 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007758 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007759 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007760 // The instruction this move is assocatied with. Null when this move is
7761 // for moving an input in the expected locations of user (including a phi user).
7762 // This is only used in debug mode, to ensure we do not connect interval siblings
7763 // in the same parallel move.
7764 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007765};
7766
Roland Levillainc9285912015-12-18 10:38:42 +00007767std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7768
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007769static constexpr size_t kDefaultNumberOfMoves = 4;
7770
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007771class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007772 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007773 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007774 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007775 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007776 moves_.reserve(kDefaultNumberOfMoves);
7777 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007778
Nicolas Geoffray90218252015-04-15 11:56:51 +01007779 void AddMove(Location source,
7780 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007781 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007782 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007783 DCHECK(source.IsValid());
7784 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007785 if (kIsDebugBuild) {
7786 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007787 for (const MoveOperands& move : moves_) {
7788 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007789 // Special case the situation where the move is for the spill slot
7790 // of the instruction.
7791 if ((GetPrevious() == instruction)
7792 || ((GetPrevious() == nullptr)
7793 && instruction->IsPhi()
7794 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007795 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007796 << "Doing parallel moves for the same instruction.";
7797 } else {
7798 DCHECK(false) << "Doing parallel moves for the same instruction.";
7799 }
7800 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007801 }
7802 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007803 for (const MoveOperands& move : moves_) {
7804 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007805 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007806 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007807 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007808 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007809 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007810 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007811 }
7812
Vladimir Marko225b6462015-09-28 12:17:40 +01007813 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007814 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007815 }
7816
Vladimir Marko225b6462015-09-28 12:17:40 +01007817 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007818
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007819 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007820
Artem Serovcced8ba2017-07-19 18:18:09 +01007821 protected:
7822 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7823
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007824 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007825 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007826};
7827
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007828// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7829// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7830// never used across anything that can trigger GC.
7831// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7832// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007833class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007834 public:
7835 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307836 : HExpression(kIntermediateAddress,
7837 DataType::Type::kInt32,
7838 SideEffects::DependsOnGC(),
7839 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007840 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7841 DataType::Size(DataType::Type::kReference))
7842 << "kPrimInt and kPrimNot have different sizes.";
7843 SetRawInputAt(0, base_address);
7844 SetRawInputAt(1, offset);
7845 }
7846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007847 bool IsClonable() const override { return true; }
7848 bool CanBeMoved() const override { return true; }
7849 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007850 return true;
7851 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007852 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007853
7854 HInstruction* GetBaseAddress() const { return InputAt(0); }
7855 HInstruction* GetOffset() const { return InputAt(1); }
7856
7857 DECLARE_INSTRUCTION(IntermediateAddress);
7858
Artem Serovcced8ba2017-07-19 18:18:09 +01007859 protected:
7860 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007861};
7862
7863
Mark Mendell0616ae02015-04-17 12:49:27 -04007864} // namespace art
7865
Aart Bikf8f5a162017-02-06 15:35:29 -08007866#include "nodes_vector.h"
7867
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007868#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7869#include "nodes_shared.h"
7870#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007871#ifdef ART_ENABLE_CODEGEN_mips
7872#include "nodes_mips.h"
7873#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307874#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007875#include "nodes_x86.h"
7876#endif
7877
Vladimir Marko0a516052019-10-14 13:00:44 +00007878namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007879
Igor Murashkin6ef45672017-08-08 13:59:55 -07007880class OptimizingCompilerStats;
7881
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007882class HGraphVisitor : public ValueObject {
7883 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007884 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7885 : stats_(stats),
7886 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007887 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007888
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007889 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007890 virtual void VisitBasicBlock(HBasicBlock* block);
7891
Roland Levillain633021e2014-10-01 14:12:25 +01007892 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007893 void VisitInsertionOrder();
7894
Roland Levillain633021e2014-10-01 14:12:25 +01007895 // Visit the graph following dominator tree reverse post-order.
7896 void VisitReversePostOrder();
7897
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007898 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007899
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007900 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007901#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007902 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7903
7904 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7905
7906#undef DECLARE_VISIT_INSTRUCTION
7907
Igor Murashkin6ef45672017-08-08 13:59:55 -07007908 protected:
7909 OptimizingCompilerStats* stats_;
7910
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007911 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007912 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007913
7914 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7915};
7916
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007917class HGraphDelegateVisitor : public HGraphVisitor {
7918 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007919 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7920 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007921 virtual ~HGraphDelegateVisitor() {}
7922
7923 // Visit functions that delegate to to super class.
7924#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007925 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007926
7927 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7928
7929#undef DECLARE_VISIT_INSTRUCTION
7930
7931 private:
7932 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7933};
7934
Artem Serovcced8ba2017-07-19 18:18:09 +01007935// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7936// and remove the old instruction.
7937HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7938
7939// Create a clone for each clonable instructions/phis and replace the original with the clone.
7940//
7941// Used for testing individual instruction cloner.
7942class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7943 public:
7944 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007945 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007947 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007948 if (instruction->IsClonable()) {
7949 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007950 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007951 }
7952 }
7953
Artem Serov7f4aff62017-06-21 17:02:18 +01007954 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007955
7956 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007957 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007958
7959 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7960};
7961
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007962// Iterator over the blocks that art part of the loop. Includes blocks part
7963// of an inner loop. The order in which the blocks are iterated is on their
7964// block id.
7965class HBlocksInLoopIterator : public ValueObject {
7966 public:
7967 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7968 : blocks_in_loop_(info.GetBlocks()),
7969 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7970 index_(0) {
7971 if (!blocks_in_loop_.IsBitSet(index_)) {
7972 Advance();
7973 }
7974 }
7975
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007976 bool Done() const { return index_ == blocks_.size(); }
7977 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007978 void Advance() {
7979 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007980 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007981 if (blocks_in_loop_.IsBitSet(index_)) {
7982 break;
7983 }
7984 }
7985 }
7986
7987 private:
7988 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007989 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007990 size_t index_;
7991
7992 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7993};
7994
Mingyao Yang3584bce2015-05-19 16:01:59 -07007995// Iterator over the blocks that art part of the loop. Includes blocks part
7996// of an inner loop. The order in which the blocks are iterated is reverse
7997// post order.
7998class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7999 public:
8000 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8001 : blocks_in_loop_(info.GetBlocks()),
8002 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8003 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008004 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008005 Advance();
8006 }
8007 }
8008
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008009 bool Done() const { return index_ == blocks_.size(); }
8010 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008011 void Advance() {
8012 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008013 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8014 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008015 break;
8016 }
8017 }
8018 }
8019
8020 private:
8021 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008022 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008023 size_t index_;
8024
8025 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8026};
8027
Aart Bikf3e61ee2017-04-12 17:09:20 -07008028// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008029inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008030 if (constant->IsIntConstant()) {
8031 return constant->AsIntConstant()->GetValue();
8032 } else if (constant->IsLongConstant()) {
8033 return constant->AsLongConstant()->GetValue();
8034 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008035 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008036 return 0;
8037 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008038}
8039
Aart Bikf3e61ee2017-04-12 17:09:20 -07008040// Returns true iff instruction is an integral constant (and sets value on success).
8041inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8042 if (instruction->IsIntConstant()) {
8043 *value = instruction->AsIntConstant()->GetValue();
8044 return true;
8045 } else if (instruction->IsLongConstant()) {
8046 *value = instruction->AsLongConstant()->GetValue();
8047 return true;
8048 } else if (instruction->IsNullConstant()) {
8049 *value = 0;
8050 return true;
8051 }
8052 return false;
8053}
8054
Aart Bik0148de42017-09-05 09:25:01 -07008055// Returns true iff instruction is the given integral constant.
8056inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8057 int64_t val = 0;
8058 return IsInt64AndGet(instruction, &val) && val == value;
8059}
8060
8061// Returns true iff instruction is a zero bit pattern.
8062inline bool IsZeroBitPattern(HInstruction* instruction) {
8063 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8064}
8065
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008066// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008067#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008068 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8069 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8070#undef INSTRUCTION_TYPE_CHECK
8071
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008072// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008073#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8074 std::is_base_of<BaseType, H##type>::value,
8075#define INSTRUCTION_TYPE_CHECK(type, super) \
8076 inline bool HInstruction::Is##type() const { \
8077 DCHECK_LT(GetKind(), kLastInstructionKind); \
8078 using BaseType = H##type; \
8079 static constexpr bool results[] = { \
8080 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8081 }; \
8082 return results[static_cast<size_t>(GetKind())]; \
8083 }
8084
8085 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8086#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008087#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008088
8089#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008090 inline const H##type* HInstruction::As##type() const { \
8091 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8092 } \
8093 inline H##type* HInstruction::As##type() { \
8094 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8095 }
8096
Vladimir Markoa90dd512018-05-04 15:04:45 +01008097 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8098#undef INSTRUCTION_TYPE_CAST
8099
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008100
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008101// Create space in `blocks` for adding `number_of_new_blocks` entries
8102// starting at location `at`. Blocks after `at` are moved accordingly.
8103inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8104 size_t number_of_new_blocks,
8105 size_t after) {
8106 DCHECK_LT(after, blocks->size());
8107 size_t old_size = blocks->size();
8108 size_t new_size = old_size + number_of_new_blocks;
8109 blocks->resize(new_size);
8110 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8111}
8112
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008113/*
8114 * Hunt "under the hood" of array lengths (leading to array references),
8115 * null checks (also leading to array references), and new arrays
8116 * (leading to the actual length). This makes it more likely related
8117 * instructions become actually comparable.
8118 */
8119inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8120 while (instruction->IsArrayLength() ||
8121 instruction->IsNullCheck() ||
8122 instruction->IsNewArray()) {
8123 instruction = instruction->IsNewArray()
8124 ? instruction->AsNewArray()->GetLength()
8125 : instruction->InputAt(0);
8126 }
8127 return instruction;
8128}
8129
Artem Serov21c7e6f2017-07-27 16:04:42 +01008130void RemoveEnvironmentUses(HInstruction* instruction);
8131bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8132void ResetEnvironmentInputRecords(HInstruction* instruction);
8133
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008134} // namespace art
8135
8136#endif // ART_COMPILER_OPTIMIZING_NODES_H_