blob: 043338466f9b5ef3fadab320695a3b288b4192d8 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko3fae1292019-06-07 11:26:25 +010028#include "base/intrusive_forward_list.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010029#include "base/iteration_range.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080030#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000032#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010033#include "base/transform_array_ref.h"
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +010034#include "art_method.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010035#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080037#include "dex/dex_file.h"
38#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080039#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070040#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000041#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000042#include "handle.h"
43#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010044#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010045#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000046#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010047#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010048#include "offsets.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049
Vladimir Marko0a516052019-10-14 13:00:44 +000050namespace art {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051
Vladimir Markoca6fff82017-10-03 14:49:14 +010052class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000053class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070055class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010056class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010058class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000060class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000063class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000064class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000065class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000066class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070067class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010068class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010069class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010070class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010071class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000072class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010073class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000074class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075
Mathieu Chartier736b5602015-09-02 14:54:11 -070076namespace mirror {
77class DexCache;
78} // namespace mirror
79
Nicolas Geoffray818f2102014-02-18 16:43:35 +000080static const int kDefaultNumberOfBlocks = 8;
81static const int kDefaultNumberOfSuccessors = 2;
82static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010083static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010084static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000086
Roland Levillain5b5b9312016-03-22 14:57:31 +000087// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
88static constexpr int32_t kMaxIntShiftDistance = 0x1f;
89// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
90static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000091
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010092static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070093static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010094
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010095static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
96
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060097static constexpr uint32_t kNoDexPc = -1;
98
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010099inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
100 // For the purposes of the compiler, the dex files must actually be the same object
101 // if we want to safely treat them as the same. This is especially important for JIT
102 // as custom class loaders can open the same underlying file (or memory) multiple
103 // times and provide different class resolution but no two class loaders should ever
104 // use the same DexFile object - doing so is an unsupported hack that can lead to
105 // all sorts of weird failures.
106 return &lhs == &rhs;
107}
108
Dave Allison20dfc792014-06-16 20:44:29 -0700109enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700110 // All types.
111 kCondEQ, // ==
112 kCondNE, // !=
113 // Signed integers and floating-point numbers.
114 kCondLT, // <
115 kCondLE, // <=
116 kCondGT, // >
117 kCondGE, // >=
118 // Unsigned integers.
119 kCondB, // <
120 kCondBE, // <=
121 kCondA, // >
122 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100123 // First and last aliases.
124 kCondFirst = kCondEQ,
125 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700126};
127
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000128enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000129 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000130 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000131 kAnalysisFailThrowCatchLoop,
132 kAnalysisFailAmbiguousArrayOp,
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100133 kAnalysisFailIrreducibleLoopAndStringInit,
Nicolas Geoffray7cfc8f52019-08-07 10:41:09 +0100134 kAnalysisFailPhiEquivalentInOsr,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000135 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000136};
137
Andreas Gampe9186ced2016-12-12 14:28:21 -0800138template <typename T>
139static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
140 return static_cast<typename std::make_unsigned<T>::type>(x);
141}
142
Vladimir Markof9f64412015-09-02 14:05:49 +0100143class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100144 public:
145 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
146
147 void AddInstruction(HInstruction* instruction);
148 void RemoveInstruction(HInstruction* instruction);
149
David Brazdilc3d743f2015-04-22 13:40:50 +0100150 // Insert `instruction` before/after an existing instruction `cursor`.
151 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
152 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
153
Roland Levillain6b469232014-09-25 10:10:38 +0100154 // Return true if this list contains `instruction`.
155 bool Contains(HInstruction* instruction) const;
156
Roland Levillainccc07a92014-09-16 14:48:16 +0100157 // Return true if `instruction1` is found before `instruction2` in
158 // this instruction list and false otherwise. Abort if none
159 // of these instructions is found.
160 bool FoundBefore(const HInstruction* instruction1,
161 const HInstruction* instruction2) const;
162
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000163 bool IsEmpty() const { return first_instruction_ == nullptr; }
164 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
165
166 // Update the block of all instructions to be `block`.
167 void SetBlockOfInstructions(HBasicBlock* block) const;
168
169 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000170 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000171 void Add(const HInstructionList& instruction_list);
172
David Brazdil2d7352b2015-04-20 14:52:42 +0100173 // Return the number of instructions in the list. This is an expensive operation.
174 size_t CountSize() const;
175
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176 private:
177 HInstruction* first_instruction_;
178 HInstruction* last_instruction_;
179
180 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000181 friend class HGraph;
182 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100183 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000184 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100185 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100186
187 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
188};
189
David Brazdil4833f5a2015-12-16 10:37:39 +0000190class ReferenceTypeInfo : ValueObject {
191 public:
192 typedef Handle<mirror::Class> TypeHandle;
193
Vladimir Markoa1de9182016-02-25 11:37:38 +0000194 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
195
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700196 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100197 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
198 }
199
Vladimir Markoa1de9182016-02-25 11:37:38 +0000200 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return ReferenceTypeInfo(type_handle, is_exact);
202 }
203
204 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
205
Vladimir Markof39745e2016-01-26 12:16:55 +0000206 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 return handle.GetReference() != nullptr;
208 }
209
Vladimir Marko456307a2016-04-19 14:12:13 +0000210 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 return IsValidHandle(type_handle_);
212 }
213
214 bool IsExact() const { return is_exact_; }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return GetTypeHandle()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsStringClass();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsInterface();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 return GetTypeHandle()->IsPrimitiveArray();
244 }
245
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000247 DCHECK(IsValid());
248 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
249 }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 if (!IsExact()) return false;
254 if (!IsArrayClass()) return false;
255 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
256 }
257
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700258 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000259 DCHECK(IsValid());
260 if (!IsExact()) return false;
261 if (!IsArrayClass()) return false;
262 if (!rti.IsArrayClass()) return false;
263 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
264 rti.GetTypeHandle()->GetComponentType());
265 }
266
267 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
268
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700269 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000270 DCHECK(IsValid());
271 DCHECK(rti.IsValid());
272 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
273 }
274
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 DCHECK(IsValid());
277 DCHECK(rti.IsValid());
278 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
279 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
280 }
281
282 // Returns true if the type information provide the same amount of details.
283 // Note that it does not mean that the instructions have the same actual type
284 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700285 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000286 if (!IsValid() && !rti.IsValid()) {
287 // Invalid types are equal.
288 return true;
289 }
290 if (!IsValid() || !rti.IsValid()) {
291 // One is valid, the other not.
292 return false;
293 }
294 return IsExact() == rti.IsExact()
295 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
296 }
297
298 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000299 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
300 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
301 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000302
303 // The class of the object.
304 TypeHandle type_handle_;
305 // Whether or not the type is exact or a superclass of the actual type.
306 // Whether or not we have any information about this type.
307 bool is_exact_;
308};
309
310std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
311
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100313class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100315 HGraph(ArenaAllocator* allocator,
316 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 const DexFile& dex_file,
318 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100320 InvokeType invoke_type = kInvalidInvokeType,
Hans Boehm206348c2018-12-05 11:11:33 -0800321 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100322 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000323 bool osr = false,
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000324 bool is_shared_jit_code = false,
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000325 bool baseline = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100326 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100327 : allocator_(allocator),
328 arena_stack_(arena_stack),
329 blocks_(allocator->Adapter(kArenaAllocBlockList)),
330 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
331 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700332 entry_block_(nullptr),
333 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100334 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100335 number_of_vregs_(0),
336 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000337 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400338 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000339 has_try_catch_(false),
Artem Serov2808be82018-12-20 19:15:11 +0000340 has_monitor_operations_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700341 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800342 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000343 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800344 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000345 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000346 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100347 dex_file_(dex_file),
348 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100349 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100350 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800351 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000353 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100354 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
355 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
356 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
357 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000358 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100359 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000360 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700361 osr_(osr),
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000362 baseline_(baseline),
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000363 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)),
364 is_shared_jit_code_(is_shared_jit_code) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100365 blocks_.reserve(kDefaultNumberOfBlocks);
366 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000367
David Brazdilbadd8262016-02-02 16:28:56 +0000368 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700369 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000370
Vladimir Markoca6fff82017-10-03 14:49:14 +0100371 ArenaAllocator* GetAllocator() const { return allocator_; }
372 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100373 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
374
David Brazdil69ba7b72015-06-23 18:27:30 +0100375 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000376 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100377
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000378 HBasicBlock* GetEntryBlock() const { return entry_block_; }
379 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100380 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000381
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000382 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
383 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000384
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000385 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100386
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100387 void ComputeDominanceInformation();
388 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000389 void ClearLoopInformation();
390 void FindBackEdges(ArenaBitVector* visited);
391 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100392 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100393 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000394
David Brazdil4833f5a2015-12-16 10:37:39 +0000395 // Analyze all natural loops in this graph. Returns a code specifying that it
396 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000397 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000398 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100399
David Brazdilffee3d32015-07-06 11:48:53 +0100400 // Iterate over blocks to compute try block membership. Needs reverse post
401 // order and loop information.
402 void ComputeTryBlockInformation();
403
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000404 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000405 // Returns the instruction to replace the invoke expression or null if the
406 // invoke is for a void method. Note that the caller is responsible for replacing
407 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000408 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000409
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000410 // Update the loop and try membership of `block`, which was spawned from `reference`.
411 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
412 // should be the new back edge.
413 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
414 HBasicBlock* reference,
415 bool replace_if_back_edge);
416
Mingyao Yang3584bce2015-05-19 16:01:59 -0700417 // Need to add a couple of blocks to test if the loop body is entered and
418 // put deoptimization instructions, etc.
419 void TransformLoopHeaderForBCE(HBasicBlock* header);
420
Aart Bikf8f5a162017-02-06 15:35:29 -0800421 // Adds a new loop directly after the loop with the given header and exit.
422 // Returns the new preheader.
423 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
424 HBasicBlock* body,
425 HBasicBlock* exit);
426
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000427 // Removes `block` from the graph. Assumes `block` has been disconnected from
428 // other blocks and has no instructions or phis.
429 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000430
David Brazdilfc6a86a2015-06-26 10:33:45 +0000431 // Splits the edge between `block` and `successor` while preserving the
432 // indices in the predecessor/successor lists. If there are multiple edges
433 // between the blocks, the lowest indices are used.
434 // Returns the new block which is empty and has the same dex pc as `successor`.
435 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
436
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100437 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100438 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000439
440 // Transform a loop into a format with a single preheader.
441 //
442 // Each phi in the header should be split: original one in the header should only hold
443 // inputs reachable from the back edges and a single input from the preheader. The newly created
444 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
445 //
446 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
447 // that no longer have this property.
448 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
449
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100450 void SimplifyLoop(HBasicBlock* header);
451
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000452 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100453 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000454 return current_instruction_id_++;
455 }
456
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000457 int32_t GetCurrentInstructionId() const {
458 return current_instruction_id_;
459 }
460
461 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100462 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000463 current_instruction_id_ = id;
464 }
465
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100466 uint16_t GetMaximumNumberOfOutVRegs() const {
467 return maximum_number_of_out_vregs_;
468 }
469
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000470 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
471 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100472 }
473
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100474 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
475 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
476 }
477
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000478 void UpdateTemporariesVRegSlots(size_t slots) {
479 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100480 }
481
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000482 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100483 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000484 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100485 }
486
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100487 void SetNumberOfVRegs(uint16_t number_of_vregs) {
488 number_of_vregs_ = number_of_vregs;
489 }
490
491 uint16_t GetNumberOfVRegs() const {
492 return number_of_vregs_;
493 }
494
495 void SetNumberOfInVRegs(uint16_t value) {
496 number_of_in_vregs_ = value;
497 }
498
David Brazdildee58d62016-04-07 09:54:26 +0000499 uint16_t GetNumberOfInVRegs() const {
500 return number_of_in_vregs_;
501 }
502
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100503 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100504 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100505 return number_of_vregs_ - number_of_in_vregs_;
506 }
507
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100508 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100510 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100511
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100512 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
513 DCHECK(GetReversePostOrder()[0] == entry_block_);
514 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
515 }
516
517 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
518 return ReverseRange(GetReversePostOrder());
519 }
520
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100521 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100522 return linear_order_;
523 }
524
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100525 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
526 return ReverseRange(GetLinearOrder());
527 }
528
Mark Mendell1152c922015-04-24 17:06:35 -0400529 bool HasBoundsChecks() const {
530 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800531 }
532
Mark Mendell1152c922015-04-24 17:06:35 -0400533 void SetHasBoundsChecks(bool value) {
534 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800535 }
536
Hans Boehm206348c2018-12-05 11:11:33 -0800537 // Is the code known to be robust against eliminating dead references
538 // and the effects of early finalization?
539 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
540
541 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
542
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000543 bool IsDebuggable() const { return debuggable_; }
544
David Brazdil8d5b8b22015-03-24 10:51:52 +0000545 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000546 // already, it is created and inserted into the graph. This method is only for
547 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100548 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000549
550 // TODO: This is problematic for the consistency of reference type propagation
551 // because it can be created anytime after the pass and thus it will be left
552 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600553 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000554
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600555 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
556 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000557 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600558 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
559 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000560 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600561 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
562 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000563 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600564 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
565 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000566 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000567
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100568 HCurrentMethod* GetCurrentMethod();
569
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100570 const DexFile& GetDexFile() const {
571 return dex_file_;
572 }
573
574 uint32_t GetMethodIdx() const {
575 return method_idx_;
576 }
577
Igor Murashkind01745e2017-04-05 16:40:31 -0700578 // Get the method name (without the signature), e.g. "<init>"
579 const char* GetMethodName() const;
580
581 // Get the pretty method name (class + name + optionally signature).
582 std::string PrettyMethod(bool with_signature = true) const;
583
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100584 InvokeType GetInvokeType() const {
585 return invoke_type_;
586 }
587
Mark Mendellc4701932015-04-10 13:18:51 -0400588 InstructionSet GetInstructionSet() const {
589 return instruction_set_;
590 }
591
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000592 bool IsCompilingOsr() const { return osr_; }
593
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000594 bool IsCompilingBaseline() const { return baseline_; }
595
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000596 bool IsCompilingForSharedJitCode() const {
597 return is_shared_jit_code_;
598 }
599
Mingyao Yang063fc772016-08-02 11:02:54 -0700600 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
601 return cha_single_implementation_list_;
602 }
603
604 void AddCHASingleImplementationDependency(ArtMethod* method) {
605 cha_single_implementation_list_.insert(method);
606 }
607
608 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800609 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700610 }
611
David Brazdil77a48ae2015-09-15 12:34:04 +0000612 bool HasTryCatch() const { return has_try_catch_; }
613 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100614
Artem Serov2808be82018-12-20 19:15:11 +0000615 bool HasMonitorOperations() const { return has_monitor_operations_; }
616 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
617
Aart Bikb13c65b2017-03-21 20:14:07 -0700618 bool HasSIMD() const { return has_simd_; }
619 void SetHasSIMD(bool value) { has_simd_ = value; }
620
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800621 bool HasLoops() const { return has_loops_; }
622 void SetHasLoops(bool value) { has_loops_ = value; }
623
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000624 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
625 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
626
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100627 ArtMethod* GetArtMethod() const { return art_method_; }
628 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
629
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100630 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500631 // The instruction has been inserted into the graph, either as a constant, or
632 // before cursor.
633 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
634
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000635 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
636
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800637 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
638 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
639 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
640
David Brazdil2d7352b2015-04-20 14:52:42 +0100641 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000642 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100643 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000644
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000645 template <class InstructionType, typename ValueType>
646 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600647 ArenaSafeMap<ValueType, InstructionType*>* cache,
648 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000649 // Try to find an existing constant of the given value.
650 InstructionType* constant = nullptr;
651 auto cached_constant = cache->find(value);
652 if (cached_constant != cache->end()) {
653 constant = cached_constant->second;
654 }
655
656 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100657 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000658 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100659 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000660 cache->Overwrite(value, constant);
661 InsertConstant(constant);
662 }
663 return constant;
664 }
665
David Brazdil8d5b8b22015-03-24 10:51:52 +0000666 void InsertConstant(HConstant* instruction);
667
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000668 // Cache a float constant into the graph. This method should only be
669 // called by the SsaBuilder when creating "equivalent" instructions.
670 void CacheFloatConstant(HFloatConstant* constant);
671
672 // See CacheFloatConstant comment.
673 void CacheDoubleConstant(HDoubleConstant* constant);
674
Vladimir Markoca6fff82017-10-03 14:49:14 +0100675 ArenaAllocator* const allocator_;
676 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677
678 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000680
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100682 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000683
Aart Bik281c6812016-08-26 11:31:48 -0700684 // List of blocks to perform a linear order tree traversal. Unlike the reverse
685 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100686 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100687
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000688 HBasicBlock* entry_block_;
689 HBasicBlock* exit_block_;
690
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100691 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100692 uint16_t maximum_number_of_out_vregs_;
693
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100694 // The number of virtual registers in this method. Contains the parameters.
695 uint16_t number_of_vregs_;
696
697 // The number of virtual registers used by parameters of this method.
698 uint16_t number_of_in_vregs_;
699
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000700 // Number of vreg size slots that the temporaries use (used in baseline compiler).
701 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100702
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800703 // Flag whether there are bounds checks in the graph. We can skip
704 // BCE if it's false. It's only best effort to keep it up to date in
705 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400706 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800707
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800708 // Flag whether there are try/catch blocks in the graph. We will skip
709 // try/catch-related passes if it's false. It's only best effort to keep
710 // it up to date in the presence of code elimination so there might be
711 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000712 bool has_try_catch_;
713
Artem Serov2808be82018-12-20 19:15:11 +0000714 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
715 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
716 bool has_monitor_operations_;
717
Aart Bikb13c65b2017-03-21 20:14:07 -0700718 // Flag whether SIMD instructions appear in the graph. If true, the
719 // code generators may have to be more careful spilling the wider
720 // contents of SIMD registers.
721 bool has_simd_;
722
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800723 // Flag whether there are any loops in the graph. We can skip loop
724 // optimization if it's false. It's only best effort to keep it up
725 // to date in the presence of code elimination so there might be false
726 // positives.
727 bool has_loops_;
728
729 // Flag whether there are any irreducible loops in the graph. It's only
730 // best effort to keep it up to date in the presence of code elimination
731 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000732 bool has_irreducible_loops_;
733
Hans Boehm206348c2018-12-05 11:11:33 -0800734 // Is the code known to be robust against eliminating dead references
735 // and the effects of early finalization? If false, dead reference variables
736 // are kept if they might be visible to the garbage collector.
737 // Currently this means that the class was declared to be dead-reference-safe,
738 // the method accesses no reachability-sensitive fields or data, and the same
739 // is true for any methods that were inlined into the current one.
740 bool dead_reference_safe_;
741
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000742 // Indicates whether the graph should be compiled in a way that
743 // ensures full debuggability. If false, we can apply more
744 // aggressive optimizations that may limit the level of debugging.
745 const bool debuggable_;
746
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000747 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000748 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000749
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100750 // The dex file from which the method is from.
751 const DexFile& dex_file_;
752
753 // The method index in the dex file.
754 const uint32_t method_idx_;
755
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100756 // If inlined, this encodes how the callee is being invoked.
757 const InvokeType invoke_type_;
758
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100759 // Whether the graph has been transformed to SSA form. Only used
760 // in debug mode to ensure we are not using properties only valid
761 // for non-SSA form (like the number of temporaries).
762 bool in_ssa_form_;
763
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800764 // Number of CHA guards in the graph. Used to short-circuit the
765 // CHA guard optimization pass when there is no CHA guard left.
766 uint32_t number_of_cha_guards_;
767
Mathieu Chartiere401d142015-04-22 13:56:20 -0700768 const InstructionSet instruction_set_;
769
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000770 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000771 HNullConstant* cached_null_constant_;
772 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000773 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000774 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000775 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000776
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100777 HCurrentMethod* cached_current_method_;
778
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100779 // The ArtMethod this graph is for. Note that for AOT, it may be null,
780 // for example for methods whose declaring class could not be resolved
781 // (such as when the superclass could not be found).
782 ArtMethod* art_method_;
783
David Brazdil4833f5a2015-12-16 10:37:39 +0000784 // Keep the RTI of inexact Object to avoid having to pass stack handle
785 // collection pointer to passes which may create NullConstant.
786 ReferenceTypeInfo inexact_object_rti_;
787
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000788 // Whether we are compiling this graph for on stack replacement: this will
789 // make all loops seen as irreducible and emit special stack maps to mark
790 // compiled code entries which the interpreter can directly jump to.
791 const bool osr_;
792
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000793 // Whether we are compiling baseline (not running optimizations). This affects
794 // the code being generated.
795 const bool baseline_;
796
Mingyao Yang063fc772016-08-02 11:02:54 -0700797 // List of methods that are assumed to have single implementation.
798 ArenaSet<ArtMethod*> cha_single_implementation_list_;
799
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000800 // Whether we are JIT compiling in the shared region area, putting
801 // restrictions on, for example, how literals are being generated.
802 bool is_shared_jit_code_;
803
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000804 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100805 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000806 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000807 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000808 DISALLOW_COPY_AND_ASSIGN(HGraph);
809};
810
Vladimir Markof9f64412015-09-02 14:05:49 +0100811class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000812 public:
813 HLoopInformation(HBasicBlock* header, HGraph* graph)
814 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100815 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000816 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100817 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100818 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100819 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100820 blocks_(graph->GetAllocator(),
821 graph->GetBlocks().size(),
822 true,
823 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100824 back_edges_.reserve(kDefaultNumberOfBackEdges);
825 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100826
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000827 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100828 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000829
830 void Dump(std::ostream& os);
831
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100832 HBasicBlock* GetHeader() const {
833 return header_;
834 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000835
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000836 void SetHeader(HBasicBlock* block) {
837 header_ = block;
838 }
839
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100840 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
841 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
842 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
843
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000844 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100845 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000846 }
847
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100848 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100849 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100850 }
851
David Brazdil46e2a392015-03-16 17:31:52 +0000852 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100853 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100854 }
855
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000856 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100857 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000858 }
859
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100860 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100861
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100862 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100863 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100864 }
865
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100866 // Returns the lifetime position of the back edge that has the
867 // greatest lifetime position.
868 size_t GetLifetimeEnd() const;
869
870 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100871 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100872 }
873
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000874 // Finds blocks that are part of this loop.
875 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100876
Artem Serov7f4aff62017-06-21 17:02:18 +0100877 // Updates blocks population of the loop and all of its outer' ones recursively after the
878 // population of the inner loop is updated.
879 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
880
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100881 // Returns whether this loop information contains `block`.
882 // Note that this loop information *must* be populated before entering this function.
883 bool Contains(const HBasicBlock& block) const;
884
885 // Returns whether this loop information is an inner loop of `other`.
886 // Note that `other` *must* be populated before entering this function.
887 bool IsIn(const HLoopInformation& other) const;
888
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800889 // Returns true if instruction is not defined within this loop.
890 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700891
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100892 const ArenaBitVector& GetBlocks() const { return blocks_; }
893
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000894 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000895 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000896
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000897 void ClearAllBlocks() {
898 blocks_.ClearAllBits();
899 }
900
David Brazdil3f4a5222016-05-06 12:46:21 +0100901 bool HasBackEdgeNotDominatedByHeader() const;
902
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100903 bool IsPopulated() const {
904 return blocks_.GetHighestBitSet() != -1;
905 }
906
Anton Shaminf89381f2016-05-16 16:44:13 +0600907 bool DominatesAllBackEdges(HBasicBlock* block);
908
David Sehrc757dec2016-11-04 15:48:34 -0700909 bool HasExitEdge() const;
910
Artem Serov7f4aff62017-06-21 17:02:18 +0100911 // Resets back edge and blocks-in-loop data.
912 void ResetBasicBlockData() {
913 back_edges_.clear();
914 ClearAllBlocks();
915 }
916
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000917 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100918 // Internal recursive implementation of `Populate`.
919 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100920 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100921
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000922 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100923 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000924 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100925 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100926 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100927 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000928
929 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
930};
931
David Brazdilec16f792015-08-19 15:04:01 +0100932// Stores try/catch information for basic blocks.
933// Note that HGraph is constructed so that catch blocks cannot simultaneously
934// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100935class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100936 public:
937 // Try block information constructor.
938 explicit TryCatchInformation(const HTryBoundary& try_entry)
939 : try_entry_(&try_entry),
940 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000941 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100942 DCHECK(try_entry_ != nullptr);
943 }
944
945 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800946 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100947 : try_entry_(nullptr),
948 catch_dex_file_(&dex_file),
949 catch_type_index_(catch_type_index) {}
950
951 bool IsTryBlock() const { return try_entry_ != nullptr; }
952
953 const HTryBoundary& GetTryEntry() const {
954 DCHECK(IsTryBlock());
955 return *try_entry_;
956 }
957
958 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
959
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000960 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100961 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000962 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100963 }
964
Andreas Gampea5b09a62016-11-17 15:21:22 -0800965 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100966 DCHECK(IsCatchBlock());
967 return catch_type_index_;
968 }
969
970 const DexFile& GetCatchDexFile() const {
971 DCHECK(IsCatchBlock());
972 return *catch_dex_file_;
973 }
974
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000975 void SetInvalidTypeIndex() {
976 catch_type_index_ = dex::TypeIndex::Invalid();
977 }
978
David Brazdilec16f792015-08-19 15:04:01 +0100979 private:
980 // One of possibly several TryBoundary instructions entering the block's try.
981 // Only set for try blocks.
982 const HTryBoundary* try_entry_;
983
984 // Exception type information. Only set for catch blocks.
985 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000986 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100987};
988
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100989static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100990static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100991
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992// A block in a method. Contains the list of instructions represented
993// as a double linked list. Each block knows its predecessors and
994// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100995
Vladimir Markof9f64412015-09-02 14:05:49 +0100996class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700998 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000999 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001000 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
1001 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001002 loop_information_(nullptr),
1003 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001004 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001005 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001006 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001007 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001008 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001009 try_catch_information_(nullptr) {
1010 predecessors_.reserve(kDefaultNumberOfPredecessors);
1011 successors_.reserve(kDefaultNumberOfSuccessors);
1012 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1013 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001014
Vladimir Marko60584552015-09-03 13:35:12 +00001015 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001016 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 }
1018
Vladimir Marko60584552015-09-03 13:35:12 +00001019 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001020 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001021 }
1022
David Brazdild26a4112015-11-10 11:07:31 +00001023 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1024 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1025
Vladimir Marko60584552015-09-03 13:35:12 +00001026 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1027 return ContainsElement(successors_, block, start_from);
1028 }
1029
1030 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001031 return dominated_blocks_;
1032 }
1033
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001034 bool IsEntryBlock() const {
1035 return graph_->GetEntryBlock() == this;
1036 }
1037
1038 bool IsExitBlock() const {
1039 return graph_->GetExitBlock() == this;
1040 }
1041
David Brazdil46e2a392015-03-16 17:31:52 +00001042 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001043 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001044 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001045 bool IsSingleTryBoundary() const;
1046
1047 // Returns true if this block emits nothing but a jump.
1048 bool IsSingleJump() const {
1049 HLoopInformation* loop_info = GetLoopInformation();
1050 return (IsSingleGoto() || IsSingleTryBoundary())
1051 // Back edges generate a suspend check.
1052 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1053 }
David Brazdil46e2a392015-03-16 17:31:52 +00001054
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001055 void AddBackEdge(HBasicBlock* back_edge) {
1056 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001057 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001058 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001059 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001060 loop_information_->AddBackEdge(back_edge);
1061 }
1062
Artem Serov7f4aff62017-06-21 17:02:18 +01001063 // Registers a back edge; if the block was not a loop header before the call associates a newly
1064 // created loop info with it.
1065 //
1066 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1067 // info for all blocks during back edges recalculation.
1068 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1069 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1070 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1071 }
1072 loop_information_->AddBackEdge(back_edge);
1073 }
1074
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001075 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001076 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001077
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001078 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001079 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001080 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001081
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001082 HBasicBlock* GetDominator() const { return dominator_; }
1083 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001084 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1085
1086 void RemoveDominatedBlock(HBasicBlock* block) {
1087 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001088 }
Vladimir Marko60584552015-09-03 13:35:12 +00001089
1090 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1091 ReplaceElement(dominated_blocks_, existing, new_block);
1092 }
1093
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001094 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001095
1096 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001097 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001098 }
1099
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001100 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1101 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001102 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001103 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001104 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1105 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001106
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001107 HInstruction* GetFirstInstructionDisregardMoves() const;
1108
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001109 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001110 successors_.push_back(block);
1111 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001112 }
1113
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001114 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1115 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001116 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001117 new_block->predecessors_.push_back(this);
1118 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001119 }
1120
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001121 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1122 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001123 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001124 new_block->successors_.push_back(this);
1125 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001126 }
1127
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001128 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001129 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001130 // `predecessor` and `successor`.
1131 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1132 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001133 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001134 successor->predecessors_[predecessor_index] = this;
1135 predecessor->successors_[successor_index] = this;
1136 successors_.push_back(successor);
1137 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001138 }
1139
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001140 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001141 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001142 }
1143
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001144 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001145 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001146 }
1147
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001148 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001149 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001150 }
1151
1152 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001153 predecessors_.push_back(block);
1154 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001155 }
1156
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001157 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001158 DCHECK_EQ(predecessors_.size(), 2u);
1159 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001160 }
1161
David Brazdil769c9e52015-04-27 13:54:09 +01001162 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001163 DCHECK_EQ(successors_.size(), 2u);
1164 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001165 }
1166
David Brazdilfc6a86a2015-06-26 10:33:45 +00001167 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001168 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001169 }
1170
David Brazdilfc6a86a2015-06-26 10:33:45 +00001171 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001172 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001173 }
1174
David Brazdilfc6a86a2015-06-26 10:33:45 +00001175 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001176 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001177 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001178 }
1179
1180 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001181 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001182 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001183 }
1184
1185 // Returns whether the first occurrence of `predecessor` in the list of
1186 // predecessors is at index `idx`.
1187 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001188 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001189 return GetPredecessorIndexOf(predecessor) == idx;
1190 }
1191
David Brazdild7558da2015-09-22 13:04:14 +01001192 // Create a new block between this block and its predecessors. The new block
1193 // is added to the graph, all predecessor edges are relinked to it and an edge
1194 // is created to `this`. Returns the new empty block. Reverse post order or
1195 // loop and try/catch information are not updated.
1196 HBasicBlock* CreateImmediateDominator();
1197
David Brazdilfc6a86a2015-06-26 10:33:45 +00001198 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001199 // created, latter block. Note that this method will add the block to the
1200 // graph, create a Goto at the end of the former block and will create an edge
1201 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001202 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001203 HBasicBlock* SplitBefore(HInstruction* cursor);
1204
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001205 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001206 // created block. Note that this method just updates raw block information,
1207 // like predecessors, successors, dominators, and instruction list. It does not
1208 // update the graph, reverse post order, loop information, nor make sure the
1209 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001210 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1211
1212 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1213 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001214
1215 // Merge `other` at the end of `this`. Successors and dominated blocks of
1216 // `other` are changed to be successors and dominated blocks of `this`. Note
1217 // that this method does not update the graph, reverse post order, loop
1218 // information, nor make sure the blocks are consistent (for example ending
1219 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001220 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001221
1222 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1223 // of `this` are moved to `other`.
1224 // Note that this method does not update the graph, reverse post order, loop
1225 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001226 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001227 void ReplaceWith(HBasicBlock* other);
1228
Aart Bik6b69e0a2017-01-11 10:20:43 -08001229 // Merges the instructions of `other` at the end of `this`.
1230 void MergeInstructionsWith(HBasicBlock* other);
1231
David Brazdil2d7352b2015-04-20 14:52:42 +01001232 // Merge `other` at the end of `this`. This method updates loops, reverse post
1233 // order, links to predecessors, successors, dominators and deletes the block
1234 // from the graph. The two blocks must be successive, i.e. `this` the only
1235 // predecessor of `other` and vice versa.
1236 void MergeWith(HBasicBlock* other);
1237
1238 // Disconnects `this` from all its predecessors, successors and dominator,
1239 // removes it from all loops it is included in and eventually from the graph.
1240 // The block must not dominate any other block. Predecessors and successors
1241 // are safely updated.
1242 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001243
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001244 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001245 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001246 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001247 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001248 // Replace phi `initial` with `replacement` within this block.
1249 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001250 // Replace instruction `initial` with `replacement` within this block.
1251 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1252 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001253 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001254 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001255 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1256 // instruction list. With 'ensure_safety' set to true, it verifies that the
1257 // instruction is not in use and removes it from the use lists of its inputs.
1258 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1259 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001260 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001261
1262 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001263 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001264 }
1265
Roland Levillain6b879dd2014-09-22 17:13:44 +01001266 bool IsLoopPreHeaderFirstPredecessor() const {
1267 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001268 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001269 }
1270
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001271 bool IsFirstPredecessorBackEdge() const {
1272 DCHECK(IsLoopHeader());
1273 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1274 }
1275
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001276 HLoopInformation* GetLoopInformation() const {
1277 return loop_information_;
1278 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001279
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001280 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001281 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001282 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001283 void SetInLoop(HLoopInformation* info) {
1284 if (IsLoopHeader()) {
1285 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001286 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001287 loop_information_ = info;
1288 } else if (loop_information_->Contains(*info->GetHeader())) {
1289 // Block is currently part of an outer loop. Make it part of this inner loop.
1290 // Note that a non loop header having a loop information means this loop information
1291 // has already been populated
1292 loop_information_ = info;
1293 } else {
1294 // Block is part of an inner loop. Do not update the loop information.
1295 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1296 // at this point, because this method is being called while populating `info`.
1297 }
1298 }
1299
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001300 // Raw update of the loop information.
1301 void SetLoopInformation(HLoopInformation* info) {
1302 loop_information_ = info;
1303 }
1304
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001305 bool IsInLoop() const { return loop_information_ != nullptr; }
1306
David Brazdilec16f792015-08-19 15:04:01 +01001307 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1308
1309 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1310 try_catch_information_ = try_catch_information;
1311 }
1312
1313 bool IsTryBlock() const {
1314 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1315 }
1316
1317 bool IsCatchBlock() const {
1318 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1319 }
David Brazdilffee3d32015-07-06 11:48:53 +01001320
1321 // Returns the try entry that this block's successors should have. They will
1322 // be in the same try, unless the block ends in a try boundary. In that case,
1323 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001324 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001325
Aart Bik75ff2c92018-04-21 01:28:11 +00001326 bool HasThrowingInstructions() const;
1327
David Brazdila4b8c212015-05-07 09:59:30 +01001328 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001329 bool Dominates(HBasicBlock* block) const;
1330
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001331 size_t GetLifetimeStart() const { return lifetime_start_; }
1332 size_t GetLifetimeEnd() const { return lifetime_end_; }
1333
1334 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1335 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1336
David Brazdil8d5b8b22015-03-24 10:51:52 +00001337 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001338 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001339 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001340 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001341 bool HasSinglePhi() const;
1342
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001343 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001344 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001345 ArenaVector<HBasicBlock*> predecessors_;
1346 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347 HInstructionList instructions_;
1348 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001349 HLoopInformation* loop_information_;
1350 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001351 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001352 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001353 // The dex program counter of the first instruction of this block.
1354 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001355 size_t lifetime_start_;
1356 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001357 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001358
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001359 friend class HGraph;
1360 friend class HInstruction;
1361
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001362 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1363};
1364
David Brazdilb2bd1c52015-03-25 11:17:37 +00001365// Iterates over the LoopInformation of all loops which contain 'block'
1366// from the innermost to the outermost.
1367class HLoopInformationOutwardIterator : public ValueObject {
1368 public:
1369 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1370 : current_(block.GetLoopInformation()) {}
1371
1372 bool Done() const { return current_ == nullptr; }
1373
1374 void Advance() {
1375 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001376 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001377 }
1378
1379 HLoopInformation* Current() const {
1380 DCHECK(!Done());
1381 return current_;
1382 }
1383
1384 private:
1385 HLoopInformation* current_;
1386
1387 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1388};
1389
Alexandre Ramesef20f712015-06-09 10:29:30 +01001390#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001391 M(Above, Condition) \
1392 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001393 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001394 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001395 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(ArrayGet, Instruction) \
1397 M(ArrayLength, Instruction) \
1398 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001399 M(Below, Condition) \
1400 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001401 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001402 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001403 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001404 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001405 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001406 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001407 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001408 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001409 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001410 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001411 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001412 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001413 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001414 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001415 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001416 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001417 M(Exit, Instruction) \
1418 M(FloatConstant, Constant) \
1419 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001420 M(GreaterThan, Condition) \
1421 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001422 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001423 M(InstanceFieldGet, Instruction) \
1424 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001425 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001426 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001427 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001428 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001429 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001430 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001431 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001432 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001433 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001434 M(LessThan, Condition) \
1435 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001436 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001437 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001438 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001439 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001440 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001441 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001442 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001443 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001444 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001445 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001446 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001447 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001448 M(Neg, UnaryOperation) \
1449 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001450 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001451 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001452 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001453 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001454 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001455 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001456 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001457 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001458 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001459 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001460 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001461 M(Return, Instruction) \
1462 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001463 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001464 M(Shl, BinaryOperation) \
1465 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001466 M(StaticFieldGet, Instruction) \
1467 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001468 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001469 M(UnresolvedInstanceFieldGet, Instruction) \
1470 M(UnresolvedInstanceFieldSet, Instruction) \
1471 M(UnresolvedStaticFieldGet, Instruction) \
1472 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001473 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001474 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001475 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001476 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001477 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001478 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001479 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001480 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001481 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001482 M(VecExtractScalar, VecUnaryOperation) \
1483 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001484 M(VecCnv, VecUnaryOperation) \
1485 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001486 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001487 M(VecNot, VecUnaryOperation) \
1488 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001489 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001490 M(VecSub, VecBinaryOperation) \
1491 M(VecMul, VecBinaryOperation) \
1492 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001493 M(VecMin, VecBinaryOperation) \
1494 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001495 M(VecAnd, VecBinaryOperation) \
1496 M(VecAndNot, VecBinaryOperation) \
1497 M(VecOr, VecBinaryOperation) \
1498 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001499 M(VecSaturationAdd, VecBinaryOperation) \
1500 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001501 M(VecShl, VecBinaryOperation) \
1502 M(VecShr, VecBinaryOperation) \
1503 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001504 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001505 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001506 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001507 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001508 M(VecLoad, VecMemoryOperation) \
1509 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001510
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001511/*
1512 * Instructions, shared across several (not all) architectures.
1513 */
1514#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1515#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1516#else
1517#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001518 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001519 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001520 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001521 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001522#endif
1523
Alexandre Ramesef20f712015-06-09 10:29:30 +01001524#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1525
1526#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1527
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001528#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001529#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001530#else
1531#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1532 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001533 M(MipsPackedSwitch, Instruction) \
1534 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001535#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001536
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001537#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1538
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001539#ifndef ART_ENABLE_CODEGEN_x86
1540#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1541#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001542#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1543 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001544 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001545 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001546 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001547#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001548
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301549#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1550#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301551 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301552 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301553#else
1554#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1555#endif
1556
Alexandre Ramesef20f712015-06-09 10:29:30 +01001557#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1558
1559#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1560 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001561 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001562 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1563 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001564 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001565 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001566 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301567 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1568 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001569
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001570#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1571 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001572 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001573 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001574 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001575 M(Invoke, Instruction) \
1576 M(VecOperation, Instruction) \
1577 M(VecUnaryOperation, VecOperation) \
1578 M(VecBinaryOperation, VecOperation) \
1579 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001580
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001581#define FOR_EACH_INSTRUCTION(M) \
1582 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1583 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1584
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001585#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001586FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1587#undef FORWARD_DECLARATION
1588
Vladimir Marko372f10e2016-05-17 16:30:10 +01001589#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001590 private: \
1591 H##type& operator=(const H##type&) = delete; \
1592 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001593 const char* DebugName() const override { return #type; } \
1594 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001595 DCHECK(IsClonable()); \
1596 return new (arena) H##type(*this->As##type()); \
1597 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001598 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001599
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001600#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001601 private: \
1602 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001603 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001604
Artem Serovcced8ba2017-07-19 18:18:09 +01001605#define DEFAULT_COPY_CONSTRUCTOR(type) \
1606 explicit H##type(const H##type& other) = default;
1607
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001608template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001609class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1610 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001611 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001612 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001613 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001614 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001615 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001616 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001617 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001618
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001619 private:
David Brazdiled596192015-01-23 10:39:45 +00001620 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001621 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001622
1623 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001624 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001625
Vladimir Marko46817b82016-03-29 12:21:58 +01001626 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001627
1628 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1629};
1630
David Brazdiled596192015-01-23 10:39:45 +00001631template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001632using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001633
David Brazdil1abb4192015-02-17 18:33:36 +00001634// This class is used by HEnvironment and HInstruction classes to record the
1635// instructions they use and pointers to the corresponding HUseListNodes kept
1636// by the used instructions.
1637template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001638class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001639 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001640 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1641 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001642
Vladimir Marko46817b82016-03-29 12:21:58 +01001643 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1644 : HUserRecord(old_record.instruction_, before_use_node) {}
1645 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1646 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001647 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001648 }
1649
1650 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001651 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1652 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001653
1654 private:
1655 // Instruction used by the user.
1656 HInstruction* instruction_;
1657
Vladimir Marko46817b82016-03-29 12:21:58 +01001658 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1659 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001660};
1661
Vladimir Markoe9004912016-06-16 16:50:52 +01001662// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1663// This is used for HInstruction::GetInputs() to return a container wrapper providing
1664// HInstruction* values even though the underlying container has HUserRecord<>s.
1665struct HInputExtractor {
1666 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1667 return record.GetInstruction();
1668 }
1669 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1670 return record.GetInstruction();
1671 }
1672};
1673
1674using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1675using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1676
Aart Bik854a02b2015-07-14 16:07:00 -07001677/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001678 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001679 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001680 * For write/read dependences on fields/arrays, the dependence analysis uses
1681 * type disambiguation (e.g. a float field write cannot modify the value of an
1682 * integer field read) and the access type (e.g. a reference array write cannot
1683 * modify the value of a reference field read [although it may modify the
1684 * reference fetch prior to reading the field, which is represented by its own
1685 * write/read dependence]). The analysis makes conservative points-to
1686 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1687 * the same, and any reference read depends on any reference read without
1688 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001689 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001690 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1691 * alive across the point where garbage collection might happen.
1692 *
1693 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1694 *
1695 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1696 * those instructions from the compiler perspective (between this instruction and the next one
1697 * in the IR).
1698 *
1699 * Note: Instructions which can cause GC only on a fatal slow path do not need
1700 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1701 * one. However the execution may return to compiled code if there is a catch block in the
1702 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1703 * set.
1704 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001705 * The internal representation uses 38-bit and is described in the table below.
1706 * The first line indicates the side effect, and for field/array accesses the
1707 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001708 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001709 * The two numbered lines below indicate the bit position in the bitfield (read
1710 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001711 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001712 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1713 * +-------------+---------+---------+--------------+---------+---------+
1714 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1715 * | 3 |333333322|222222221| 1 |111111110|000000000|
1716 * | 7 |654321098|765432109| 8 |765432109|876543210|
1717 *
1718 * Note that, to ease the implementation, 'changes' bits are least significant
1719 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001720 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001721class SideEffects : public ValueObject {
1722 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001723 SideEffects() : flags_(0) {}
1724
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001725 static SideEffects None() {
1726 return SideEffects(0);
1727 }
1728
1729 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001730 return SideEffects(kAllChangeBits | kAllDependOnBits);
1731 }
1732
1733 static SideEffects AllChanges() {
1734 return SideEffects(kAllChangeBits);
1735 }
1736
1737 static SideEffects AllDependencies() {
1738 return SideEffects(kAllDependOnBits);
1739 }
1740
1741 static SideEffects AllExceptGCDependency() {
1742 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1743 }
1744
1745 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001746 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001747 }
1748
Aart Bik34c3ba92015-07-20 14:08:59 -07001749 static SideEffects AllWrites() {
1750 return SideEffects(kAllWrites);
1751 }
1752
1753 static SideEffects AllReads() {
1754 return SideEffects(kAllReads);
1755 }
1756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001757 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001758 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001759 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001760 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001761 }
1762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001763 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001764 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001765 }
1766
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001767 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001768 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001769 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001770 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001771 }
1772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001773 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001774 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001775 }
1776
Artem Serovd1aa7d02018-06-22 11:35:46 +01001777 // Returns whether GC might happen across this instruction from the compiler perspective so
1778 // the next instruction in the IR would see that.
1779 //
1780 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001781 static SideEffects CanTriggerGC() {
1782 return SideEffects(1ULL << kCanTriggerGCBit);
1783 }
1784
Artem Serovd1aa7d02018-06-22 11:35:46 +01001785 // Returns whether the instruction must not be alive across a GC point.
1786 //
1787 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001788 static SideEffects DependsOnGC() {
1789 return SideEffects(1ULL << kDependsOnGCBit);
1790 }
1791
Aart Bik854a02b2015-07-14 16:07:00 -07001792 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001793 SideEffects Union(SideEffects other) const {
1794 return SideEffects(flags_ | other.flags_);
1795 }
1796
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001797 SideEffects Exclusion(SideEffects other) const {
1798 return SideEffects(flags_ & ~other.flags_);
1799 }
1800
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001801 void Add(SideEffects other) {
1802 flags_ |= other.flags_;
1803 }
1804
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001805 bool Includes(SideEffects other) const {
1806 return (other.flags_ & flags_) == other.flags_;
1807 }
1808
1809 bool HasSideEffects() const {
1810 return (flags_ & kAllChangeBits);
1811 }
1812
1813 bool HasDependencies() const {
1814 return (flags_ & kAllDependOnBits);
1815 }
1816
1817 // Returns true if there are no side effects or dependencies.
1818 bool DoesNothing() const {
1819 return flags_ == 0;
1820 }
1821
Aart Bik854a02b2015-07-14 16:07:00 -07001822 // Returns true if something is written.
1823 bool DoesAnyWrite() const {
1824 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001825 }
1826
Aart Bik854a02b2015-07-14 16:07:00 -07001827 // Returns true if something is read.
1828 bool DoesAnyRead() const {
1829 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001830 }
1831
Aart Bik854a02b2015-07-14 16:07:00 -07001832 // Returns true if potentially everything is written and read
1833 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001834 bool DoesAllReadWrite() const {
1835 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1836 }
1837
Aart Bik854a02b2015-07-14 16:07:00 -07001838 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001839 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001840 }
1841
Roland Levillain0d5a2812015-11-13 10:07:31 +00001842 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001843 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001844 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1845 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001846 }
1847
1848 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001849 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001850 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001851 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001852 for (int s = kLastBit; s >= 0; s--) {
1853 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1854 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1855 // This is a bit for the GC side effect.
1856 if (current_bit_is_set) {
1857 flags += "GC";
1858 }
Aart Bik854a02b2015-07-14 16:07:00 -07001859 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001860 } else {
1861 // This is a bit for the array/field analysis.
1862 // The underscore character stands for the 'can trigger GC' bit.
1863 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1864 if (current_bit_is_set) {
1865 flags += kDebug[s];
1866 }
1867 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1868 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1869 flags += "|";
1870 }
1871 }
Aart Bik854a02b2015-07-14 16:07:00 -07001872 }
1873 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001874 }
1875
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001876 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001877
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001878 private:
1879 static constexpr int kFieldArrayAnalysisBits = 9;
1880
1881 static constexpr int kFieldWriteOffset = 0;
1882 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1883 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1884 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1885
1886 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1887
1888 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1889 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1890 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1891 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1892
1893 static constexpr int kLastBit = kDependsOnGCBit;
1894 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1895
1896 // Aliases.
1897
1898 static_assert(kChangeBits == kDependOnBits,
1899 "the 'change' bits should match the 'depend on' bits.");
1900
1901 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1902 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1903 static constexpr uint64_t kAllWrites =
1904 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1905 static constexpr uint64_t kAllReads =
1906 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001907
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001908 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001909 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001910 int shift;
1911 switch (type) {
1912 case DataType::Type::kReference: shift = 0; break;
1913 case DataType::Type::kBool: shift = 1; break;
1914 case DataType::Type::kInt8: shift = 2; break;
1915 case DataType::Type::kUint16: shift = 3; break;
1916 case DataType::Type::kInt16: shift = 4; break;
1917 case DataType::Type::kInt32: shift = 5; break;
1918 case DataType::Type::kInt64: shift = 6; break;
1919 case DataType::Type::kFloat32: shift = 7; break;
1920 case DataType::Type::kFloat64: shift = 8; break;
1921 default:
1922 LOG(FATAL) << "Unexpected data type " << type;
1923 UNREACHABLE();
1924 }
Aart Bik854a02b2015-07-14 16:07:00 -07001925 DCHECK_LE(kFieldWriteOffset, shift);
1926 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001927 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001928 }
1929
1930 // Private constructor on direct flags value.
1931 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1932
1933 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001934};
1935
David Brazdiled596192015-01-23 10:39:45 +00001936// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001937class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001938 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001939 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001940 size_t number_of_vregs,
1941 ArtMethod* method,
1942 uint32_t dex_pc,
1943 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001944 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1945 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001946 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001947 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001948 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001950 }
1951
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001952 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1953 const HEnvironment& to_copy,
1954 HInstruction* holder)
1955 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001956 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001957 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001958 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001959 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001960
Vladimir Markoec32f642017-06-02 10:51:55 +01001961 void AllocateLocations() {
1962 DCHECK(locations_.empty());
1963 locations_.resize(vregs_.size());
1964 }
1965
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001966 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001967 if (parent_ != nullptr) {
1968 parent_->SetAndCopyParentChain(allocator, parent);
1969 } else {
1970 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1971 parent_->CopyFrom(parent);
1972 if (parent->GetParent() != nullptr) {
1973 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1974 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001975 }
David Brazdiled596192015-01-23 10:39:45 +00001976 }
1977
Vladimir Marko69d310e2017-10-09 14:12:23 +01001978 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001979 void CopyFrom(HEnvironment* environment);
1980
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001981 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1982 // input to the loop phi instead. This is for inserting instructions that
1983 // require an environment (like HDeoptimization) in the loop pre-header.
1984 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001985
1986 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001987 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001988 }
1989
1990 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001991 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001992 }
1993
David Brazdil1abb4192015-02-17 18:33:36 +00001994 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001995
Artem Serovca210e32017-12-15 13:43:20 +00001996 // Replaces the input at the position 'index' with the replacement; the replacement and old
1997 // input instructions' env_uses_ lists are adjusted. The function works similar to
1998 // HInstruction::ReplaceInput.
1999 void ReplaceInput(HInstruction* replacement, size_t index);
2000
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002001 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00002002
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002003 HEnvironment* GetParent() const { return parent_; }
2004
2005 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002006 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002007 }
2008
2009 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002010 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002011 }
2012
2013 uint32_t GetDexPc() const {
2014 return dex_pc_;
2015 }
2016
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002017 ArtMethod* GetMethod() const {
2018 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002019 }
2020
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002021 HInstruction* GetHolder() const {
2022 return holder_;
2023 }
2024
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002025
2026 bool IsFromInlinedInvoke() const {
2027 return GetParent() != nullptr;
2028 }
2029
David Brazdiled596192015-01-23 10:39:45 +00002030 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002031 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2032 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002033 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002034 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002035 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002036
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002037 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002038 HInstruction* const holder_;
2039
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002040 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002041
2042 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2043};
2044
Vladimir Markof9f64412015-09-02 14:05:49 +01002045class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002046 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302047#define DECLARE_KIND(type, super) k##type,
2048 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002049 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302050 kLastInstructionKind
2051 };
2052#undef DECLARE_KIND
2053
2054 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002055 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2056
2057 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002058 : previous_(nullptr),
2059 next_(nullptr),
2060 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002061 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002062 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002063 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002064 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002065 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002066 locations_(nullptr),
2067 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002068 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002069 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002070 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302071 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002072 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002073 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2074 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002075
Dave Allison20dfc792014-06-16 20:44:29 -07002076 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002077
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002078
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002079 HInstruction* GetNext() const { return next_; }
2080 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002081
Calin Juravle77520bc2015-01-12 18:45:46 +00002082 HInstruction* GetNextDisregardingMoves() const;
2083 HInstruction* GetPreviousDisregardingMoves() const;
2084
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002085 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002086 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002087 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002088 bool IsInBlock() const { return block_ != nullptr; }
2089 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002090 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2091 bool IsIrreducibleLoopHeaderPhi() const {
2092 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2093 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002094
Vladimir Marko372f10e2016-05-17 16:30:10 +01002095 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2096
2097 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2098 // One virtual method is enough, just const_cast<> and then re-add the const.
2099 return ArrayRef<const HUserRecord<HInstruction*>>(
2100 const_cast<HInstruction*>(this)->GetInputRecords());
2101 }
2102
Vladimir Markoe9004912016-06-16 16:50:52 +01002103 HInputsRef GetInputs() {
2104 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002105 }
2106
Vladimir Markoe9004912016-06-16 16:50:52 +01002107 HConstInputsRef GetInputs() const {
2108 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002109 }
2110
2111 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002112 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113
Aart Bik2767f4b2016-10-28 15:03:53 -07002114 bool HasInput(HInstruction* input) const {
2115 for (const HInstruction* i : GetInputs()) {
2116 if (i == input) {
2117 return true;
2118 }
2119 }
2120 return false;
2121 }
2122
Vladimir Marko372f10e2016-05-17 16:30:10 +01002123 void SetRawInputAt(size_t index, HInstruction* input) {
2124 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2125 }
2126
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127 virtual void Accept(HGraphVisitor* visitor) = 0;
2128 virtual const char* DebugName() const = 0;
2129
Vladimir Markobd785672018-05-03 17:09:09 +01002130 DataType::Type GetType() const {
2131 return TypeField::Decode(GetPackedFields());
2132 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002134 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002135
2136 uint32_t GetDexPc() const { return dex_pc_; }
2137
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002138 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002139
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002140 // Can the instruction throw?
2141 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2142 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002143 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002144
2145 // Does the instruction always throw an exception unconditionally?
2146 virtual bool AlwaysThrows() const { return false; }
2147
David Brazdilec16f792015-08-19 15:04:01 +01002148 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002149
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002150 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002151 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002152
Calin Juravle10e244f2015-01-26 18:54:32 +00002153 // Does not apply for all instructions, but having this at top level greatly
2154 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002155 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002156 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002157 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002158 return true;
2159 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002160
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002161 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002162 return false;
2163 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002164
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002165 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2166 // with it. Otherwise return null.
2167 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002168 // Go over previous non-move instructions that are emitted at use site.
2169 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2170 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2171 if (prev_not_move->IsNullCheck()) {
2172 return prev_not_move->AsNullCheck();
2173 }
2174 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002175 }
2176 return nullptr;
2177 }
2178
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002179 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002180 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002181 }
2182
Calin Juravle2e768302015-07-28 14:41:11 +00002183 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002184
Calin Juravle61d544b2015-02-23 16:46:57 +00002185 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002186 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002187 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002188 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002189 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002190
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002191 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002192 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002193 // Note: fixup_end remains valid across push_front().
2194 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2195 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002196 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002197 uses_.push_front(*new_node);
2198 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002199 }
2200
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002201 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002202 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002203 // Note: env_fixup_end remains valid across push_front().
2204 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2205 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002206 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002207 env_uses_.push_front(*new_node);
2208 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002209 }
2210
David Brazdil1abb4192015-02-17 18:33:36 +00002211 void RemoveAsUserOfInput(size_t input) {
2212 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002213 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2214 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2215 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002216 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002217
Vladimir Marko372f10e2016-05-17 16:30:10 +01002218 void RemoveAsUserOfAllInputs() {
2219 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2220 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2221 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2222 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2223 }
2224 }
2225
David Brazdil1abb4192015-02-17 18:33:36 +00002226 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2227 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002228
Vladimir Marko46817b82016-03-29 12:21:58 +01002229 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2230 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2231 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002232 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002233 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002234 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002235
Aart Bikcc42be02016-10-20 16:14:16 -07002236 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002237 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002238 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002239 !CanThrow() &&
2240 !IsSuspendCheck() &&
2241 !IsControlFlow() &&
2242 !IsNativeDebugInfo() &&
2243 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002244 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002245 !IsMemoryBarrier() &&
2246 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002247 }
2248
Aart Bikcc42be02016-10-20 16:14:16 -07002249 bool IsDeadAndRemovable() const {
2250 return IsRemovable() && !HasUses();
2251 }
2252
Roland Levillain6c82d402014-10-13 16:10:27 +01002253 // Does this instruction strictly dominate `other_instruction`?
2254 // Returns false if this instruction and `other_instruction` are the same.
2255 // Aborts if this instruction and `other_instruction` are both phis.
2256 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002257
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002258 int GetId() const { return id_; }
2259 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002260
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002261 int GetSsaIndex() const { return ssa_index_; }
2262 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2263 bool HasSsaIndex() const { return ssa_index_ != -1; }
2264
2265 bool HasEnvironment() const { return environment_ != nullptr; }
2266 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002267 // Set the `environment_` field. Raw because this method does not
2268 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002269 void SetRawEnvironment(HEnvironment* environment) {
2270 DCHECK(environment_ == nullptr);
2271 DCHECK_EQ(environment->GetHolder(), this);
2272 environment_ = environment;
2273 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002274
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002275 void InsertRawEnvironment(HEnvironment* environment) {
2276 DCHECK(environment_ != nullptr);
2277 DCHECK_EQ(environment->GetHolder(), this);
2278 DCHECK(environment->GetParent() == nullptr);
2279 environment->parent_ = environment_;
2280 environment_ = environment;
2281 }
2282
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002283 void RemoveEnvironment();
2284
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002285 // Set the environment of this instruction, copying it from `environment`. While
2286 // copying, the uses lists are being updated.
2287 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002288 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002289 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002290 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002291 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002292 if (environment->GetParent() != nullptr) {
2293 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2294 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002295 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002296
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002297 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2298 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002299 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002300 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002301 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002302 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002303 if (environment->GetParent() != nullptr) {
2304 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2305 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002306 }
2307
Nicolas Geoffray39468442014-09-02 15:17:15 +01002308 // Returns the number of entries in the environment. Typically, that is the
2309 // number of dex registers in a method. It could be more in case of inlining.
2310 size_t EnvironmentSize() const;
2311
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002312 LocationSummary* GetLocations() const { return locations_; }
2313 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002314
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002315 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002316 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002317 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002318 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002319
Alexandre Rames188d4312015-04-09 18:30:21 +01002320 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2321 // uses of this instruction by `other` are *not* updated.
2322 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2323 ReplaceWith(other);
2324 other->ReplaceInput(this, use_index);
2325 }
2326
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002327 // Move `this` instruction before `cursor`
2328 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002329
Vladimir Markofb337ea2015-11-25 15:25:10 +00002330 // Move `this` before its first user and out of any loops. If there is no
2331 // out-of-loop user that dominates all other users, move the instruction
2332 // to the end of the out-of-loop common dominator of the user's blocks.
2333 //
2334 // This can be used only on non-throwing instructions with no side effects that
2335 // have at least one use but no environment uses.
2336 void MoveBeforeFirstUserAndOutOfLoops();
2337
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002338#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002339 bool Is##type() const;
2340
2341 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2342#undef INSTRUCTION_TYPE_CHECK
2343
2344#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002345 const H##type* As##type() const; \
2346 H##type* As##type();
2347
Vladimir Markoa90dd512018-05-04 15:04:45 +01002348 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2349#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002350
Artem Serovcced8ba2017-07-19 18:18:09 +01002351 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2352 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2353 // the instruction then the behaviour of this function is undefined.
2354 //
2355 // Note: It is semantically valid to create a clone of the instruction only until
2356 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2357 // copied.
2358 //
2359 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2360 // 'explicit HInstruction(const HInstruction& other)' for details.
2361 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2362 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2363 DebugName() << " " << GetId();
2364 UNREACHABLE();
2365 }
2366
2367 // Return whether instruction can be cloned (copied).
2368 virtual bool IsClonable() const { return false; }
2369
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002370 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002371 // TODO: this method is used by LICM and GVN with possibly different
2372 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002373 virtual bool CanBeMoved() const { return false; }
2374
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002375 // Returns whether any data encoded in the two instructions is equal.
2376 // This method does not look at the inputs. Both instructions must be
2377 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002378 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002379 return false;
2380 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002381
2382 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002383 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002384 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002385 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002386
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302387 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002388
2389 virtual size_t ComputeHashCode() const {
2390 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002391 for (const HInstruction* input : GetInputs()) {
2392 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002393 }
2394 return result;
2395 }
2396
2397 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002398 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002399 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002400
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002401 size_t GetLifetimePosition() const { return lifetime_position_; }
2402 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2403 LiveInterval* GetLiveInterval() const { return live_interval_; }
2404 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2405 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2406
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002407 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2408
2409 // Returns whether the code generation of the instruction will require to have access
2410 // to the current method. Such instructions are:
2411 // (1): Instructions that require an environment, as calling the runtime requires
2412 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002413 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2414 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002415 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002416 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002417 }
2418
Vladimir Markodc151b22015-10-15 18:02:30 +01002419 // Returns whether the code generation of the instruction will require to have access
2420 // to the dex cache of the current method's declaring class via the current method.
2421 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002422
Mark Mendellc4701932015-04-10 13:18:51 -04002423 // Does this instruction have any use in an environment before
2424 // control flow hits 'other'?
2425 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2426
2427 // Remove all references to environment uses of this instruction.
2428 // The caller must ensure that this is safe to do.
2429 void RemoveEnvironmentUsers();
2430
Vladimir Markoa1de9182016-02-25 11:37:38 +00002431 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2432 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002433
David Brazdil1abb4192015-02-17 18:33:36 +00002434 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002435 // If set, the machine code for this instruction is assumed to be generated by
2436 // its users. Used by liveness analysis to compute use positions accordingly.
2437 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2438 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302439 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2440 static constexpr size_t kFieldInstructionKindSize =
2441 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002442 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302443 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002444 static constexpr size_t kFieldTypeSize =
2445 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2446 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002447 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2448
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302449 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2450 "Too many generic packed fields");
2451
Vladimir Markobd785672018-05-03 17:09:09 +01002452 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2453
Vladimir Marko372f10e2016-05-17 16:30:10 +01002454 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2455 return GetInputRecords()[i];
2456 }
2457
2458 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2459 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2460 input_records[index] = input;
2461 }
David Brazdil1abb4192015-02-17 18:33:36 +00002462
Vladimir Markoa1de9182016-02-25 11:37:38 +00002463 uint32_t GetPackedFields() const {
2464 return packed_fields_;
2465 }
2466
2467 template <size_t flag>
2468 bool GetPackedFlag() const {
2469 return (packed_fields_ & (1u << flag)) != 0u;
2470 }
2471
2472 template <size_t flag>
2473 void SetPackedFlag(bool value = true) {
2474 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2475 }
2476
2477 template <typename BitFieldType>
2478 typename BitFieldType::value_type GetPackedField() const {
2479 return BitFieldType::Decode(packed_fields_);
2480 }
2481
2482 template <typename BitFieldType>
2483 void SetPackedField(typename BitFieldType::value_type value) {
2484 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2485 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2486 }
2487
Artem Serovcced8ba2017-07-19 18:18:09 +01002488 // Copy construction for the instruction (used for Clone function).
2489 //
2490 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2491 // prepare_for_register_allocator are not copied (set to default values).
2492 //
2493 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2494 // fine for most of them. However for some of the instructions a custom copy constructor must be
2495 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2496 // for copying them).
2497 explicit HInstruction(const HInstruction& other)
2498 : previous_(nullptr),
2499 next_(nullptr),
2500 block_(nullptr),
2501 dex_pc_(other.dex_pc_),
2502 id_(-1),
2503 ssa_index_(-1),
2504 packed_fields_(other.packed_fields_),
2505 environment_(nullptr),
2506 locations_(nullptr),
2507 live_interval_(nullptr),
2508 lifetime_position_(kNoLifetime),
2509 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302510 reference_type_handle_(other.reference_type_handle_) {
2511 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002512
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002513 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302514 using InstructionKindField =
2515 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2516
Vladimir Marko46817b82016-03-29 12:21:58 +01002517 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2518 auto before_use_node = uses_.before_begin();
2519 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2520 HInstruction* user = use_node->GetUser();
2521 size_t input_index = use_node->GetIndex();
2522 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2523 before_use_node = use_node;
2524 }
2525 }
2526
2527 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2528 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2529 if (next != uses_.end()) {
2530 HInstruction* next_user = next->GetUser();
2531 size_t next_index = next->GetIndex();
2532 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2533 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2534 }
2535 }
2536
2537 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2538 auto before_env_use_node = env_uses_.before_begin();
2539 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2540 HEnvironment* user = env_use_node->GetUser();
2541 size_t input_index = env_use_node->GetIndex();
2542 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2543 before_env_use_node = env_use_node;
2544 }
2545 }
2546
2547 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2548 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2549 if (next != env_uses_.end()) {
2550 HEnvironment* next_user = next->GetUser();
2551 size_t next_index = next->GetIndex();
2552 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2553 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2554 }
2555 }
David Brazdil1abb4192015-02-17 18:33:36 +00002556
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002557 HInstruction* previous_;
2558 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002559 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002560 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002561
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002562 // An instruction gets an id when it is added to the graph.
2563 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002564 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002565 int id_;
2566
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002567 // When doing liveness analysis, instructions that have uses get an SSA index.
2568 int ssa_index_;
2569
Vladimir Markoa1de9182016-02-25 11:37:38 +00002570 // Packed fields.
2571 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002572
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002573 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002574 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002575
2576 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002577 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002578
Nicolas Geoffray39468442014-09-02 15:17:15 +01002579 // The environment associated with this instruction. Not null if the instruction
2580 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002581 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002582
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002583 // Set by the code generator.
2584 LocationSummary* locations_;
2585
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002586 // Set by the liveness analysis.
2587 LiveInterval* live_interval_;
2588
2589 // Set by the liveness analysis, this is the position in a linear
2590 // order of blocks where this instruction's live interval start.
2591 size_t lifetime_position_;
2592
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002593 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002594
Vladimir Markoa1de9182016-02-25 11:37:38 +00002595 // The reference handle part of the reference type info.
2596 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002597 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002598 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002599
David Brazdil1abb4192015-02-17 18:33:36 +00002600 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002601 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002602 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002603 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002604 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002605};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002606std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002607
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002608// Iterates over the instructions, while preserving the next instruction
2609// in case the current instruction gets removed from the list by the user
2610// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002611class HInstructionIterator : public ValueObject {
2612 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002613 explicit HInstructionIterator(const HInstructionList& instructions)
2614 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002615 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002616 }
2617
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002618 bool Done() const { return instruction_ == nullptr; }
2619 HInstruction* Current() const { return instruction_; }
2620 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002621 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002622 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002623 }
2624
2625 private:
2626 HInstruction* instruction_;
2627 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002628
2629 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002630};
2631
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002632// Iterates over the instructions without saving the next instruction,
2633// therefore handling changes in the graph potentially made by the user
2634// of this iterator.
2635class HInstructionIteratorHandleChanges : public ValueObject {
2636 public:
2637 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2638 : instruction_(instructions.first_instruction_) {
2639 }
2640
2641 bool Done() const { return instruction_ == nullptr; }
2642 HInstruction* Current() const { return instruction_; }
2643 void Advance() {
2644 instruction_ = instruction_->GetNext();
2645 }
2646
2647 private:
2648 HInstruction* instruction_;
2649
2650 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2651};
2652
2653
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002654class HBackwardInstructionIterator : public ValueObject {
2655 public:
2656 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2657 : instruction_(instructions.last_instruction_) {
2658 next_ = Done() ? nullptr : instruction_->GetPrevious();
2659 }
2660
2661 bool Done() const { return instruction_ == nullptr; }
2662 HInstruction* Current() const { return instruction_; }
2663 void Advance() {
2664 instruction_ = next_;
2665 next_ = Done() ? nullptr : instruction_->GetPrevious();
2666 }
2667
2668 private:
2669 HInstruction* instruction_;
2670 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002671
2672 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002673};
2674
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002675class HVariableInputSizeInstruction : public HInstruction {
2676 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002677 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002678 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002679 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2680 }
2681
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002682 void AddInput(HInstruction* input);
2683 void InsertInputAt(size_t index, HInstruction* input);
2684 void RemoveInputAt(size_t index);
2685
Igor Murashkind01745e2017-04-05 16:40:31 -07002686 // Removes all the inputs.
2687 // Also removes this instructions from each input's use list
2688 // (for non-environment uses only).
2689 void RemoveAllInputs();
2690
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002691 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302692 HVariableInputSizeInstruction(InstructionKind inst_kind,
2693 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002694 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002695 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002696 size_t number_of_inputs,
2697 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302698 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002699 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002700 HVariableInputSizeInstruction(InstructionKind inst_kind,
2701 DataType::Type type,
2702 SideEffects side_effects,
2703 uint32_t dex_pc,
2704 ArenaAllocator* allocator,
2705 size_t number_of_inputs,
2706 ArenaAllocKind kind)
2707 : HInstruction(inst_kind, type, side_effects, dex_pc),
2708 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002709
Artem Serovcced8ba2017-07-19 18:18:09 +01002710 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002711
Artem Serovcced8ba2017-07-19 18:18:09 +01002712 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002713};
2714
Vladimir Markof9f64412015-09-02 14:05:49 +01002715template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002716class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002717 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002718 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302719 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002720 HExpression<N>(InstructionKind kind,
2721 DataType::Type type,
2722 SideEffects side_effects,
2723 uint32_t dex_pc)
2724 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2725 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002726
Vladimir Marko372f10e2016-05-17 16:30:10 +01002727 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002728 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002729 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002730 }
2731
Artem Serovcced8ba2017-07-19 18:18:09 +01002732 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002733 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002734
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002735 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002736 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002737
2738 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002739};
2740
Vladimir Markobd785672018-05-03 17:09:09 +01002741// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002742template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002743class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002744 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002745 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002746
Vladimir Markobd785672018-05-03 17:09:09 +01002747 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002748
Vladimir Marko372f10e2016-05-17 16:30:10 +01002749 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002750 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002751 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002752 }
2753
Artem Serovcced8ba2017-07-19 18:18:09 +01002754 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002755 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002756
Vladimir Markof9f64412015-09-02 14:05:49 +01002757 private:
2758 friend class SsaBuilder;
2759};
2760
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002761// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2762// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002763class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002764 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002765 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002766 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302767 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002769 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002770
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002771 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002772
Artem Serovcced8ba2017-07-19 18:18:09 +01002773 protected:
2774 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002775};
2776
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002777// Represents dex's RETURN opcodes. A HReturn is a control flow
2778// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002779class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002780 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002781 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002782 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002783 SetRawInputAt(0, value);
2784 }
2785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002786 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002787
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002788 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002789
Artem Serovcced8ba2017-07-19 18:18:09 +01002790 protected:
2791 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002792};
2793
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002794class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002795 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002796 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002797 uint32_t reg_number,
2798 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002800 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002801 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302802 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002803 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002804 SideEffects::None(),
2805 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002806 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002807 number_of_inputs,
2808 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002809 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002811 // Phis are constructed live and marked dead if conflicting or unused.
2812 // Individual steps of SsaBuilder should assume that if a phi has been
2813 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2814 SetPackedFlag<kFlagIsLive>(true);
2815 SetPackedFlag<kFlagCanBeNull>(true);
2816 }
2817
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002818 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002819
David Brazdildee58d62016-04-07 09:54:26 +00002820 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 static DataType::Type ToPhiType(DataType::Type type) {
2822 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002823 }
2824
2825 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002828 // Make sure that only valid type changes occur. The following are allowed:
2829 // (1) int -> float/ref (primitive type propagation),
2830 // (2) long -> double (primitive type propagation).
2831 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002832 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2833 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2834 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002835 SetPackedField<TypeField>(new_type);
2836 }
2837
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002838 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002839 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2840
2841 uint32_t GetRegNumber() const { return reg_number_; }
2842
2843 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2844 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2845 bool IsDead() const { return !IsLive(); }
2846 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2847
Vladimir Markoe9004912016-06-16 16:50:52 +01002848 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002849 return other != nullptr
2850 && other->IsPhi()
2851 && other->AsPhi()->GetBlock() == GetBlock()
2852 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2853 }
2854
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002855 bool HasEquivalentPhi() const {
2856 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2857 return true;
2858 }
2859 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2860 return true;
2861 }
2862 return false;
2863 }
2864
David Brazdildee58d62016-04-07 09:54:26 +00002865 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2866 // An equivalent phi is a phi having the same dex register and type.
2867 // It assumes that phis with the same dex register are adjacent.
2868 HPhi* GetNextEquivalentPhiWithSameType() {
2869 HInstruction* next = GetNext();
2870 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2871 if (next->GetType() == GetType()) {
2872 return next->AsPhi();
2873 }
2874 next = next->GetNext();
2875 }
2876 return nullptr;
2877 }
2878
2879 DECLARE_INSTRUCTION(Phi);
2880
Artem Serovcced8ba2017-07-19 18:18:09 +01002881 protected:
2882 DEFAULT_COPY_CONSTRUCTOR(Phi);
2883
David Brazdildee58d62016-04-07 09:54:26 +00002884 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002885 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002886 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2887 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2888 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002889
David Brazdildee58d62016-04-07 09:54:26 +00002890 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002891};
2892
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002893// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002894// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002895// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002896class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002897 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302898 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002899 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302900 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002901
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002902 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002903
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002904 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002905
Artem Serovcced8ba2017-07-19 18:18:09 +01002906 protected:
2907 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002908};
2909
2910// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002911class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002912 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302913 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002914 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302915 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002916
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002917 bool IsClonable() const override { return true; }
2918 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002919
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002920 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002921 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002922 }
2923
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002924 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002925
Artem Serovcced8ba2017-07-19 18:18:09 +01002926 protected:
2927 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002928};
2929
Roland Levillain9867bc72015-08-05 10:21:34 +01002930class HConstant : public HExpression<0> {
2931 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302932 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2933 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2934 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002936 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002937
Roland Levillain1a653882016-03-18 18:05:57 +00002938 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002939 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002940 // Is this constant 0 in the arithmetic sense?
2941 virtual bool IsArithmeticZero() const { return false; }
2942 // Is this constant a 0-bit pattern?
2943 virtual bool IsZeroBitPattern() const { return false; }
2944 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002945 virtual bool IsOne() const { return false; }
2946
David Brazdil77a48ae2015-09-15 12:34:04 +00002947 virtual uint64_t GetValueAsUint64() const = 0;
2948
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002949 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002950
Artem Serovcced8ba2017-07-19 18:18:09 +01002951 protected:
2952 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002953};
2954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002955class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002956 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002957 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002958 return true;
2959 }
2960
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002961 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002962
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002963 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002964
Roland Levillain1a653882016-03-18 18:05:57 +00002965 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07002966 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00002967
Roland Levillain9867bc72015-08-05 10:21:34 +01002968 DECLARE_INSTRUCTION(NullConstant);
2969
Artem Serovcced8ba2017-07-19 18:18:09 +01002970 protected:
2971 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2972
Roland Levillain9867bc72015-08-05 10:21:34 +01002973 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302975 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2976 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002977
2978 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002979};
2980
2981// Constants of the type int. Those can be from Dex instructions, or
2982// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002983class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002984 public:
2985 int32_t GetValue() const { return value_; }
2986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002987 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002988 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2989 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002991 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002992 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002993 return other->AsIntConstant()->value_ == value_;
2994 }
2995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002996 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002997
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002998 bool IsMinusOne() const override { return GetValue() == -1; }
2999 bool IsArithmeticZero() const override { return GetValue() == 0; }
3000 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3001 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003002
Roland Levillain1a653882016-03-18 18:05:57 +00003003 // Integer constants are used to encode Boolean values as well,
3004 // where 1 means true and 0 means false.
3005 bool IsTrue() const { return GetValue() == 1; }
3006 bool IsFalse() const { return GetValue() == 0; }
3007
Roland Levillain9867bc72015-08-05 10:21:34 +01003008 DECLARE_INSTRUCTION(IntConstant);
3009
Artem Serovcced8ba2017-07-19 18:18:09 +01003010 protected:
3011 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3012
Roland Levillain9867bc72015-08-05 10:21:34 +01003013 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003014 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303015 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3016 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003017 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303018 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3019 value_(value ? 1 : 0) {
3020 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003021
3022 const int32_t value_;
3023
3024 friend class HGraph;
3025 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3026 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003027};
3028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003029class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003030 public:
3031 int64_t GetValue() const { return value_; }
3032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003033 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003035 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003037 return other->AsLongConstant()->value_ == value_;
3038 }
3039
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003040 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003042 bool IsMinusOne() const override { return GetValue() == -1; }
3043 bool IsArithmeticZero() const override { return GetValue() == 0; }
3044 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3045 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003046
3047 DECLARE_INSTRUCTION(LongConstant);
3048
Artem Serovcced8ba2017-07-19 18:18:09 +01003049 protected:
3050 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3051
Roland Levillain9867bc72015-08-05 10:21:34 +01003052 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003053 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303054 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3055 value_(value) {
3056 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003057
3058 const int64_t value_;
3059
3060 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003061};
Dave Allison20dfc792014-06-16 20:44:29 -07003062
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003063class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064 public:
3065 float GetValue() const { return value_; }
3066
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003067 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3069 }
3070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003071 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003072 DCHECK(other->IsFloatConstant()) << other->DebugName();
3073 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3074 }
3075
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003076 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003078 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3080 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003081 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003082 return std::fpclassify(value_) == FP_ZERO;
3083 }
3084 bool IsArithmeticPositiveZero() const {
3085 return IsArithmeticZero() && !std::signbit(value_);
3086 }
3087 bool IsArithmeticNegativeZero() const {
3088 return IsArithmeticZero() && std::signbit(value_);
3089 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003090 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003091 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003092 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003093 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3095 }
3096 bool IsNaN() const {
3097 return std::isnan(value_);
3098 }
3099
3100 DECLARE_INSTRUCTION(FloatConstant);
3101
Artem Serovcced8ba2017-07-19 18:18:09 +01003102 protected:
3103 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3104
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105 private:
3106 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303107 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3108 value_(value) {
3109 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303111 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3112 value_(bit_cast<float, int32_t>(value)) {
3113 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003114
3115 const float value_;
3116
3117 // Only the SsaBuilder and HGraph can create floating-point constants.
3118 friend class SsaBuilder;
3119 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120};
3121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003122class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003123 public:
3124 double GetValue() const { return value_; }
3125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003126 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003128 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003129 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3130 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3131 }
3132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003133 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003135 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003136 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3137 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003138 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003139 return std::fpclassify(value_) == FP_ZERO;
3140 }
3141 bool IsArithmeticPositiveZero() const {
3142 return IsArithmeticZero() && !std::signbit(value_);
3143 }
3144 bool IsArithmeticNegativeZero() const {
3145 return IsArithmeticZero() && std::signbit(value_);
3146 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003147 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003148 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003149 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003150 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003151 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3152 }
3153 bool IsNaN() const {
3154 return std::isnan(value_);
3155 }
3156
3157 DECLARE_INSTRUCTION(DoubleConstant);
3158
Artem Serovcced8ba2017-07-19 18:18:09 +01003159 protected:
3160 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3161
Roland Levillain31dd3d62016-02-16 12:21:02 +00003162 private:
3163 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303164 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3165 value_(value) {
3166 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003167 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303168 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3169 value_(bit_cast<double, int64_t>(value)) {
3170 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003171
3172 const double value_;
3173
3174 // Only the SsaBuilder and HGraph can create floating-point constants.
3175 friend class SsaBuilder;
3176 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003177};
3178
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003179// Conditional branch. A block ending with an HIf instruction must have
3180// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003181class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003182 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003183 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003184 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003185 SetRawInputAt(0, input);
3186 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003187
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003188 bool IsClonable() const override { return true; }
3189 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003190
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003191 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003192 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003193 }
3194
3195 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003196 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003197 }
3198
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003199 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003200
Artem Serovcced8ba2017-07-19 18:18:09 +01003201 protected:
3202 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003203};
3204
David Brazdilfc6a86a2015-06-26 10:33:45 +00003205
3206// Abstract instruction which marks the beginning and/or end of a try block and
3207// links it to the respective exception handlers. Behaves the same as a Goto in
3208// non-exceptional control flow.
3209// Normal-flow successor is stored at index zero, exception handlers under
3210// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003211class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003212 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003213 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003214 kEntry,
3215 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003216 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003217 };
3218
Artem Serovd1aa7d02018-06-22 11:35:46 +01003219 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3220 // across the catch block entering edges as GC might happen during throwing an exception.
3221 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3222 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003223 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003224 : HExpression(kTryBoundary,
3225 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3226 : SideEffects::None(),
3227 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003228 SetPackedField<BoundaryKindField>(kind);
3229 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003230
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003231 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003232
3233 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003234 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003235
David Brazdild26a4112015-11-10 11:07:31 +00003236 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3237 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3238 }
3239
David Brazdilfc6a86a2015-06-26 10:33:45 +00003240 // Returns whether `handler` is among its exception handlers (non-zero index
3241 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003242 bool HasExceptionHandler(const HBasicBlock& handler) const {
3243 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003244 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003245 }
3246
3247 // If not present already, adds `handler` to its block's list of exception
3248 // handlers.
3249 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003250 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003251 GetBlock()->AddSuccessor(handler);
3252 }
3253 }
3254
Vladimir Markoa1de9182016-02-25 11:37:38 +00003255 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3256 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003257
David Brazdilffee3d32015-07-06 11:48:53 +01003258 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3259
David Brazdilfc6a86a2015-06-26 10:33:45 +00003260 DECLARE_INSTRUCTION(TryBoundary);
3261
Artem Serovcced8ba2017-07-19 18:18:09 +01003262 protected:
3263 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3264
David Brazdilfc6a86a2015-06-26 10:33:45 +00003265 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003266 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3267 static constexpr size_t kFieldBoundaryKindSize =
3268 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3269 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3270 kFieldBoundaryKind + kFieldBoundaryKindSize;
3271 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3272 "Too many packed fields.");
3273 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003274};
3275
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003276// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003277class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003278 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003279 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3280 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003281 HDeoptimize(ArenaAllocator* allocator,
3282 HInstruction* cond,
3283 DeoptimizationKind kind,
3284 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003285 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303286 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003287 SideEffects::All(),
3288 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003289 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003290 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003291 kArenaAllocMisc) {
3292 SetPackedFlag<kFieldCanBeMoved>(false);
3293 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003294 SetRawInputAt(0, cond);
3295 }
3296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003297 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003298
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003299 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3300 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3301 // instead of `guard`.
3302 // We set CanTriggerGC to prevent any intermediate address to be live
3303 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003304 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003305 HInstruction* cond,
3306 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003307 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003308 uint32_t dex_pc)
3309 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303310 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003311 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003312 SideEffects::CanTriggerGC(),
3313 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003314 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003315 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003316 kArenaAllocMisc) {
3317 SetPackedFlag<kFieldCanBeMoved>(true);
3318 SetPackedField<DeoptimizeKindField>(kind);
3319 SetRawInputAt(0, cond);
3320 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003321 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003323 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003324
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003325 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003326 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3327 }
3328
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003329 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003330
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003331 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003332
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003333 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003334
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003335 bool GuardsAnInput() const {
3336 return InputCount() == 2;
3337 }
3338
3339 HInstruction* GuardedInput() const {
3340 DCHECK(GuardsAnInput());
3341 return InputAt(1);
3342 }
3343
3344 void RemoveGuard() {
3345 RemoveInputAt(1);
3346 }
3347
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003348 DECLARE_INSTRUCTION(Deoptimize);
3349
Artem Serovcced8ba2017-07-19 18:18:09 +01003350 protected:
3351 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3352
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003353 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003354 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3355 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3356 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003357 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003358 static constexpr size_t kNumberOfDeoptimizePackedBits =
3359 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3360 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3361 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003362 using DeoptimizeKindField =
3363 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003364};
3365
Mingyao Yang063fc772016-08-02 11:02:54 -07003366// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3367// The compiled code checks this flag value in a guard before devirtualized call and
3368// if it's true, starts to do deoptimization.
3369// It has a 4-byte slot on stack.
3370// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003371class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003372 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003373 // CHA guards are only optimized in a separate pass and it has no side effects
3374 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003375 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303376 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003377 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303378 SideEffects::None(),
3379 dex_pc,
3380 allocator,
3381 0,
3382 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003383 }
3384
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003385 // We do all CHA guard elimination/motion in a single pass, after which there is no
3386 // further guard elimination/motion since a guard might have been used for justification
3387 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3388 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003389 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003390
3391 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3392
Artem Serovcced8ba2017-07-19 18:18:09 +01003393 protected:
3394 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003395};
3396
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003397// Represents the ArtMethod that was passed as a first argument to
3398// the method. It is used by instructions that depend on it, like
3399// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003400class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003401 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003402 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303403 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3404 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003405
3406 DECLARE_INSTRUCTION(CurrentMethod);
3407
Artem Serovcced8ba2017-07-19 18:18:09 +01003408 protected:
3409 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003410};
3411
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003412// Fetches an ArtMethod from the virtual table or the interface method table
3413// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003414class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003415 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003416 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003417 kVTable,
3418 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003419 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003420 };
3421 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003422 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003423 TableKind kind,
3424 size_t index,
3425 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303426 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003427 index_(index) {
3428 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003429 SetRawInputAt(0, cls);
3430 }
3431
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003432 bool IsClonable() const override { return true; }
3433 bool CanBeMoved() const override { return true; }
3434 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003435 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003436 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003437 }
3438
Vladimir Markoa1de9182016-02-25 11:37:38 +00003439 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003440 size_t GetIndex() const { return index_; }
3441
3442 DECLARE_INSTRUCTION(ClassTableGet);
3443
Artem Serovcced8ba2017-07-19 18:18:09 +01003444 protected:
3445 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3446
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003447 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003448 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003449 static constexpr size_t kFieldTableKindSize =
3450 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3451 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3452 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3453 "Too many packed fields.");
3454 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3455
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003456 // The index of the ArtMethod in the table.
3457 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003458};
3459
Mark Mendellfe57faa2015-09-18 09:26:15 -04003460// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3461// have one successor for each entry in the switch table, and the final successor
3462// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003463class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003464 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003465 HPackedSwitch(int32_t start_value,
3466 uint32_t num_entries,
3467 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003468 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003469 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003470 start_value_(start_value),
3471 num_entries_(num_entries) {
3472 SetRawInputAt(0, input);
3473 }
3474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003475 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003476
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003477 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003478
3479 int32_t GetStartValue() const { return start_value_; }
3480
Vladimir Marko211c2112015-09-24 16:52:33 +01003481 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003482
3483 HBasicBlock* GetDefaultBlock() const {
3484 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003485 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003486 }
3487 DECLARE_INSTRUCTION(PackedSwitch);
3488
Artem Serovcced8ba2017-07-19 18:18:09 +01003489 protected:
3490 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3491
Mark Mendellfe57faa2015-09-18 09:26:15 -04003492 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003493 const int32_t start_value_;
3494 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003495};
3496
Roland Levillain88cb1752014-10-20 16:36:47 +01003497class HUnaryOperation : public HExpression<1> {
3498 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303499 HUnaryOperation(InstructionKind kind,
3500 DataType::Type result_type,
3501 HInstruction* input,
3502 uint32_t dex_pc = kNoDexPc)
3503 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003504 SetRawInputAt(0, input);
3505 }
3506
Artem Serovcced8ba2017-07-19 18:18:09 +01003507 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003508 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003509
Roland Levillain88cb1752014-10-20 16:36:47 +01003510 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003511 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003512
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003513 bool CanBeMoved() const override { return true; }
3514 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003515 return true;
3516 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003517
Roland Levillain31dd3d62016-02-16 12:21:02 +00003518 // Try to statically evaluate `this` and return a HConstant
3519 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003520 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003521 HConstant* TryStaticEvaluation() const;
3522
3523 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003524 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3525 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003526 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3527 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003528
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003529 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003530
Artem Serovcced8ba2017-07-19 18:18:09 +01003531 protected:
3532 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003533};
3534
Dave Allison20dfc792014-06-16 20:44:29 -07003535class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003536 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303537 HBinaryOperation(InstructionKind kind,
3538 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003539 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003540 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003541 SideEffects side_effects = SideEffects::None(),
3542 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303543 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003544 SetRawInputAt(0, left);
3545 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003546 }
3547
Artem Serovcced8ba2017-07-19 18:18:09 +01003548 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003549 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003550
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003551 HInstruction* GetLeft() const { return InputAt(0); }
3552 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003553 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003554
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003555 virtual bool IsCommutative() const { return false; }
3556
3557 // Put constant on the right.
3558 // Returns whether order is changed.
3559 bool OrderInputsWithConstantOnTheRight() {
3560 HInstruction* left = InputAt(0);
3561 HInstruction* right = InputAt(1);
3562 if (left->IsConstant() && !right->IsConstant()) {
3563 ReplaceInput(right, 0);
3564 ReplaceInput(left, 1);
3565 return true;
3566 }
3567 return false;
3568 }
3569
3570 // Order inputs by instruction id, but favor constant on the right side.
3571 // This helps GVN for commutative ops.
3572 void OrderInputs() {
3573 DCHECK(IsCommutative());
3574 HInstruction* left = InputAt(0);
3575 HInstruction* right = InputAt(1);
3576 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3577 return;
3578 }
3579 if (OrderInputsWithConstantOnTheRight()) {
3580 return;
3581 }
3582 // Order according to instruction id.
3583 if (left->GetId() > right->GetId()) {
3584 ReplaceInput(right, 0);
3585 ReplaceInput(left, 1);
3586 }
3587 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003588
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003589 bool CanBeMoved() const override { return true; }
3590 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003591 return true;
3592 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003593
Roland Levillain31dd3d62016-02-16 12:21:02 +00003594 // Try to statically evaluate `this` and return a HConstant
3595 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003596 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003597 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003598
3599 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003600 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3601 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003602 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3603 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003604 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003605 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3606 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003607 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3608 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003609 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3610 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003611 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3613 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003614
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003615 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003616 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003617 HConstant* GetConstantRight() const;
3618
3619 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003620 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003621 HInstruction* GetLeastConstantLeft() const;
3622
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003623 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003624
Artem Serovcced8ba2017-07-19 18:18:09 +01003625 protected:
3626 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003627};
3628
Mark Mendellc4701932015-04-10 13:18:51 -04003629// The comparison bias applies for floating point operations and indicates how NaN
3630// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003631enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003632 kNoBias, // bias is not applicable (i.e. for long operation)
3633 kGtBias, // return 1 for NaN comparisons
3634 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003635 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003636};
3637
Roland Levillain31dd3d62016-02-16 12:21:02 +00003638std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3639
Dave Allison20dfc792014-06-16 20:44:29 -07003640class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003641 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303642 HCondition(InstructionKind kind,
3643 HInstruction* first,
3644 HInstruction* second,
3645 uint32_t dex_pc = kNoDexPc)
3646 : HBinaryOperation(kind,
3647 DataType::Type::kBool,
3648 first,
3649 second,
3650 SideEffects::None(),
3651 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003652 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3653 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003654
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003655 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003656 // `instruction`, and disregard moves in between.
3657 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003658
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003659 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003660
3661 virtual IfCondition GetCondition() const = 0;
3662
Mark Mendellc4701932015-04-10 13:18:51 -04003663 virtual IfCondition GetOppositeCondition() const = 0;
3664
Vladimir Markoa1de9182016-02-25 11:37:38 +00003665 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003666 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3667
Vladimir Markoa1de9182016-02-25 11:37:38 +00003668 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3669 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003670
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003671 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003672 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003673 }
3674
Roland Levillain4fa13f62015-07-06 18:11:54 +01003675 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003676 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003677 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003678 if (if_cond == kCondNE) {
3679 return true;
3680 } else if (if_cond == kCondEQ) {
3681 return false;
3682 }
3683 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003684 }
3685
3686 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003688 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003689 if (if_cond == kCondEQ) {
3690 return true;
3691 } else if (if_cond == kCondNE) {
3692 return false;
3693 }
3694 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003695 }
3696
Roland Levillain31dd3d62016-02-16 12:21:02 +00003697 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003698 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003699 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003700 static constexpr size_t kFieldComparisonBiasSize =
3701 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3702 static constexpr size_t kNumberOfConditionPackedBits =
3703 kFieldComparisonBias + kFieldComparisonBiasSize;
3704 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3705 using ComparisonBiasField =
3706 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3707
Roland Levillain31dd3d62016-02-16 12:21:02 +00003708 template <typename T>
3709 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3710
3711 template <typename T>
3712 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003713 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003714 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3715 // Handle the bias.
3716 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3717 }
3718
3719 // Return an integer constant containing the result of a condition evaluated at compile time.
3720 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3721 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3722 }
3723
Artem Serovcced8ba2017-07-19 18:18:09 +01003724 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003725};
3726
3727// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003728class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003729 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003730 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303731 : HCondition(kEqual, first, second, dex_pc) {
3732 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003733
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003734 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003735
Vladimir Marko9e23df52015-11-10 17:14:35 +00003736 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003737 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003738 return MakeConstantCondition(true, GetDexPc());
3739 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003740 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003741 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3742 }
3743 // In the following Evaluate methods, a HCompare instruction has
3744 // been merged into this HEqual instruction; evaluate it as
3745 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003746 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003747 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3748 GetDexPc());
3749 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003750 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003751 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3752 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003753 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003754 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003755 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003756
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003757 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003759 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003760 return kCondEQ;
3761 }
3762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003763 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003764 return kCondNE;
3765 }
3766
Artem Serovcced8ba2017-07-19 18:18:09 +01003767 protected:
3768 DEFAULT_COPY_CONSTRUCTOR(Equal);
3769
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003770 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003771 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003772};
3773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003774class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003775 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303776 HNotEqual(HInstruction* first, HInstruction* second,
3777 uint32_t dex_pc = kNoDexPc)
3778 : HCondition(kNotEqual, first, second, dex_pc) {
3779 }
Dave Allison20dfc792014-06-16 20:44:29 -07003780
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003781 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003782
Vladimir Marko9e23df52015-11-10 17:14:35 +00003783 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003784 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003785 return MakeConstantCondition(false, GetDexPc());
3786 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003787 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003788 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3789 }
3790 // In the following Evaluate methods, a HCompare instruction has
3791 // been merged into this HNotEqual instruction; evaluate it as
3792 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003793 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003794 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3795 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003796 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003797 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3798 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003799 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003800 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003801 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003802
Dave Allison20dfc792014-06-16 20:44:29 -07003803 DECLARE_INSTRUCTION(NotEqual);
3804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003805 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003806 return kCondNE;
3807 }
3808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003809 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003810 return kCondEQ;
3811 }
3812
Artem Serovcced8ba2017-07-19 18:18:09 +01003813 protected:
3814 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3815
Dave Allison20dfc792014-06-16 20:44:29 -07003816 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003817 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003818};
3819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003820class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003821 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303822 HLessThan(HInstruction* first, HInstruction* second,
3823 uint32_t dex_pc = kNoDexPc)
3824 : HCondition(kLessThan, first, second, dex_pc) {
3825 }
Dave Allison20dfc792014-06-16 20:44:29 -07003826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003827 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003828 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003829 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003830 // In the following Evaluate methods, a HCompare instruction has
3831 // been merged into this HLessThan instruction; evaluate it as
3832 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003833 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003834 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3835 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003836 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003837 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3838 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003839 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003841 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003842
Dave Allison20dfc792014-06-16 20:44:29 -07003843 DECLARE_INSTRUCTION(LessThan);
3844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003845 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003846 return kCondLT;
3847 }
3848
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003849 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003850 return kCondGE;
3851 }
3852
Artem Serovcced8ba2017-07-19 18:18:09 +01003853 protected:
3854 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3855
Dave Allison20dfc792014-06-16 20:44:29 -07003856 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003857 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003858};
3859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003861 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303862 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3863 uint32_t dex_pc = kNoDexPc)
3864 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3865 }
Dave Allison20dfc792014-06-16 20:44:29 -07003866
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003867 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003868 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003869 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003870 // In the following Evaluate methods, a HCompare instruction has
3871 // been merged into this HLessThanOrEqual instruction; evaluate it as
3872 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003873 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003874 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3875 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003876 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003877 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3878 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003879 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003880 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003881 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003882
Dave Allison20dfc792014-06-16 20:44:29 -07003883 DECLARE_INSTRUCTION(LessThanOrEqual);
3884
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003885 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003886 return kCondLE;
3887 }
3888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003889 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003890 return kCondGT;
3891 }
3892
Artem Serovcced8ba2017-07-19 18:18:09 +01003893 protected:
3894 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3895
Dave Allison20dfc792014-06-16 20:44:29 -07003896 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003897 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003898};
3899
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003900class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003902 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303903 : HCondition(kGreaterThan, first, second, dex_pc) {
3904 }
Dave Allison20dfc792014-06-16 20:44:29 -07003905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003906 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003907 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003908 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003909 // In the following Evaluate methods, a HCompare instruction has
3910 // been merged into this HGreaterThan instruction; evaluate it as
3911 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003912 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003913 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3914 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003915 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003916 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3917 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003918 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003919 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003920 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003921
Dave Allison20dfc792014-06-16 20:44:29 -07003922 DECLARE_INSTRUCTION(GreaterThan);
3923
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003924 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003925 return kCondGT;
3926 }
3927
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003928 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003929 return kCondLE;
3930 }
3931
Artem Serovcced8ba2017-07-19 18:18:09 +01003932 protected:
3933 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3934
Dave Allison20dfc792014-06-16 20:44:29 -07003935 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003936 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003937};
3938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003939class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003940 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003941 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303942 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3943 }
Dave Allison20dfc792014-06-16 20:44:29 -07003944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003945 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003946 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003947 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003948 // In the following Evaluate methods, a HCompare instruction has
3949 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3950 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003951 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003952 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3953 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003954 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003955 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3956 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003957 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003958 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003959 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003960
Dave Allison20dfc792014-06-16 20:44:29 -07003961 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3962
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003963 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003964 return kCondGE;
3965 }
3966
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003967 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003968 return kCondLT;
3969 }
3970
Artem Serovcced8ba2017-07-19 18:18:09 +01003971 protected:
3972 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3973
Dave Allison20dfc792014-06-16 20:44:29 -07003974 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003975 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003976};
3977
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003978class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003979 public:
3980 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303981 : HCondition(kBelow, first, second, dex_pc) {
3982 }
Aart Bike9f37602015-10-09 11:15:55 -07003983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003984 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003985 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003986 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003987 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003988 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3989 }
3990 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003991 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003992 LOG(FATAL) << DebugName() << " is not defined for float values";
3993 UNREACHABLE();
3994 }
3995 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003996 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003997 LOG(FATAL) << DebugName() << " is not defined for double values";
3998 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003999 }
4000
4001 DECLARE_INSTRUCTION(Below);
4002
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004003 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004004 return kCondB;
4005 }
4006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004007 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004008 return kCondAE;
4009 }
4010
Artem Serovcced8ba2017-07-19 18:18:09 +01004011 protected:
4012 DEFAULT_COPY_CONSTRUCTOR(Below);
4013
Aart Bike9f37602015-10-09 11:15:55 -07004014 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004015 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004016 return MakeUnsigned(x) < MakeUnsigned(y);
4017 }
Aart Bike9f37602015-10-09 11:15:55 -07004018};
4019
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004020class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004021 public:
4022 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304023 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4024 }
Aart Bike9f37602015-10-09 11:15:55 -07004025
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004026 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004027 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004028 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004029 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004030 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4031 }
4032 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004033 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004034 LOG(FATAL) << DebugName() << " is not defined for float values";
4035 UNREACHABLE();
4036 }
4037 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004038 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004039 LOG(FATAL) << DebugName() << " is not defined for double values";
4040 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004041 }
4042
4043 DECLARE_INSTRUCTION(BelowOrEqual);
4044
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004045 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004046 return kCondBE;
4047 }
4048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004049 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004050 return kCondA;
4051 }
4052
Artem Serovcced8ba2017-07-19 18:18:09 +01004053 protected:
4054 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4055
Aart Bike9f37602015-10-09 11:15:55 -07004056 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004057 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004058 return MakeUnsigned(x) <= MakeUnsigned(y);
4059 }
Aart Bike9f37602015-10-09 11:15:55 -07004060};
4061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004062class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004063 public:
4064 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304065 : HCondition(kAbove, first, second, dex_pc) {
4066 }
Aart Bike9f37602015-10-09 11:15:55 -07004067
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004068 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004069 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004070 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004071 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004072 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4073 }
4074 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004075 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004076 LOG(FATAL) << DebugName() << " is not defined for float values";
4077 UNREACHABLE();
4078 }
4079 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004080 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004081 LOG(FATAL) << DebugName() << " is not defined for double values";
4082 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004083 }
4084
4085 DECLARE_INSTRUCTION(Above);
4086
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004087 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004088 return kCondA;
4089 }
4090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004091 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004092 return kCondBE;
4093 }
4094
Artem Serovcced8ba2017-07-19 18:18:09 +01004095 protected:
4096 DEFAULT_COPY_CONSTRUCTOR(Above);
4097
Aart Bike9f37602015-10-09 11:15:55 -07004098 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004099 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004100 return MakeUnsigned(x) > MakeUnsigned(y);
4101 }
Aart Bike9f37602015-10-09 11:15:55 -07004102};
4103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004104class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004105 public:
4106 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304107 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4108 }
Aart Bike9f37602015-10-09 11:15:55 -07004109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004110 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004111 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004112 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004113 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004114 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4115 }
4116 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004117 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004118 LOG(FATAL) << DebugName() << " is not defined for float values";
4119 UNREACHABLE();
4120 }
4121 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004122 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004123 LOG(FATAL) << DebugName() << " is not defined for double values";
4124 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004125 }
4126
4127 DECLARE_INSTRUCTION(AboveOrEqual);
4128
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004129 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004130 return kCondAE;
4131 }
4132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004133 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004134 return kCondB;
4135 }
4136
Artem Serovcced8ba2017-07-19 18:18:09 +01004137 protected:
4138 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4139
Aart Bike9f37602015-10-09 11:15:55 -07004140 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004141 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004142 return MakeUnsigned(x) >= MakeUnsigned(y);
4143 }
Aart Bike9f37602015-10-09 11:15:55 -07004144};
Dave Allison20dfc792014-06-16 20:44:29 -07004145
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004146// Instruction to check how two inputs compare to each other.
4147// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004148class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004149 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004150 // Note that `comparison_type` is the type of comparison performed
4151 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004152 // HCompare instruction (which is always DataType::Type::kInt).
4153 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004154 HInstruction* first,
4155 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004156 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004157 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304158 : HBinaryOperation(kCompare,
4159 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004160 first,
4161 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004162 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004163 dex_pc) {
4164 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004165 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4166 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004167 }
4168
Roland Levillain9867bc72015-08-05 10:21:34 +01004169 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004170 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4171
4172 template <typename T>
4173 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004174 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004175 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4176 // Handle the bias.
4177 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4178 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004180 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004181 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4182 // reach this code path when processing a freshly built HIR
4183 // graph. However HCompare integer instructions can be synthesized
4184 // by the instruction simplifier to implement IntegerCompare and
4185 // IntegerSignum intrinsics, so we have to handle this case.
4186 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004187 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004188 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004189 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4190 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004191 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004192 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4193 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004194 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004195 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004196 }
4197
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004198 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004199 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004200 }
4201
Vladimir Markoa1de9182016-02-25 11:37:38 +00004202 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004203
Roland Levillain31dd3d62016-02-16 12:21:02 +00004204 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004205 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004206 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004207 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004208 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004209 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004210
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004211 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004212 // Comparisons do not require a runtime call in any back end.
4213 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004214 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004215
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004216 DECLARE_INSTRUCTION(Compare);
4217
Roland Levillain31dd3d62016-02-16 12:21:02 +00004218 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004219 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004220 static constexpr size_t kFieldComparisonBiasSize =
4221 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4222 static constexpr size_t kNumberOfComparePackedBits =
4223 kFieldComparisonBias + kFieldComparisonBiasSize;
4224 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4225 using ComparisonBiasField =
4226 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4227
Roland Levillain31dd3d62016-02-16 12:21:02 +00004228 // Return an integer constant containing the result of a comparison evaluated at compile time.
4229 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4230 DCHECK(value == -1 || value == 0 || value == 1) << value;
4231 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4232 }
4233
Artem Serovcced8ba2017-07-19 18:18:09 +01004234 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004235};
4236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004237class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004238 public:
4239 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004240 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004241 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004242 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004243 bool finalizable,
4244 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304245 : HExpression(kNewInstance,
4246 DataType::Type::kReference,
4247 SideEffects::CanTriggerGC(),
4248 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004249 type_index_(type_index),
4250 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004251 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004252 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004253 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004254 }
4255
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004256 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004257
Andreas Gampea5b09a62016-11-17 15:21:22 -08004258 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004259 const DexFile& GetDexFile() const { return dex_file_; }
4260
4261 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004262 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004263
Mingyao Yang062157f2016-03-02 10:15:36 -08004264 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004265 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004266
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004267 bool NeedsChecks() const {
4268 return entrypoint_ == kQuickAllocObjectWithChecks;
4269 }
David Brazdil6de19382016-01-08 17:37:10 +00004270
Vladimir Markoa1de9182016-02-25 11:37:38 +00004271 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004272
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004273 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004274
4275 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4276
4277 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4278 entrypoint_ = entrypoint;
4279 }
4280
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004281 HLoadClass* GetLoadClass() const {
4282 HInstruction* input = InputAt(0);
4283 if (input->IsClinitCheck()) {
4284 input = input->InputAt(0);
4285 }
4286 DCHECK(input->IsLoadClass());
4287 return input->AsLoadClass();
4288 }
4289
David Brazdil6de19382016-01-08 17:37:10 +00004290 bool IsStringAlloc() const;
4291
4292 DECLARE_INSTRUCTION(NewInstance);
4293
Artem Serovcced8ba2017-07-19 18:18:09 +01004294 protected:
4295 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4296
David Brazdil6de19382016-01-08 17:37:10 +00004297 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004298 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004299 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4300 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4301 "Too many packed fields.");
4302
Andreas Gampea5b09a62016-11-17 15:21:22 -08004303 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004304 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004305 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004306};
4307
Agi Csaki05f20562015-08-19 14:58:14 -07004308enum IntrinsicNeedsEnvironmentOrCache {
4309 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4310 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004311};
4312
Aart Bik5d75afe2015-12-14 11:57:01 -08004313enum IntrinsicSideEffects {
4314 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4315 kReadSideEffects, // Intrinsic may read heap memory.
4316 kWriteSideEffects, // Intrinsic may write heap memory.
4317 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4318};
4319
4320enum IntrinsicExceptions {
4321 kNoThrow, // Intrinsic does not throw any exceptions.
4322 kCanThrow // Intrinsic may throw exceptions.
4323};
4324
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004325class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004326 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004327 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004328
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004329 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004330 SetRawInputAt(index, argument);
4331 }
4332
Roland Levillain3e3d7332015-04-28 11:00:54 +01004333 // Return the number of arguments. This number can be lower than
4334 // the number of inputs returned by InputCount(), as some invoke
4335 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4336 // inputs at the end of their list of inputs.
4337 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4338
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004339 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4340
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004341 InvokeType GetInvokeType() const {
4342 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004343 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004344
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004345 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004346 return intrinsic_;
4347 }
4348
Aart Bik5d75afe2015-12-14 11:57:01 -08004349 void SetIntrinsic(Intrinsics intrinsic,
4350 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4351 IntrinsicSideEffects side_effects,
4352 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004353
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004354 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004355 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004356 }
4357
Aart Bikff7d89c2016-11-07 08:49:28 -08004358 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4359
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004360 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004361
Aart Bika8b8e9b2018-01-09 11:01:02 -08004362 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4363
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004364 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004365
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004366 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004367
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004368 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004369 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4370 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004371
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004372 uint32_t* GetIntrinsicOptimizations() {
4373 return &intrinsic_optimizations_;
4374 }
4375
4376 const uint32_t* GetIntrinsicOptimizations() const {
4377 return &intrinsic_optimizations_;
4378 }
4379
4380 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4381
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004382 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004383 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004384
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004385 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004386
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004387 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004388 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4389 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004390 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004391 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004392 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4393 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004394 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004395 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004396
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304397 HInvoke(InstructionKind kind,
4398 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004399 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004400 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004401 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004402 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004403 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004404 ArtMethod* resolved_method,
4405 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004406 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304407 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004408 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004409 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4410 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004411 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004412 number_of_arguments + number_of_other_inputs,
4413 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004414 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004415 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004416 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004417 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004418 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004419 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004420 // Check mutator lock, constructors lack annotalysis support.
4421 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4422 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004423 }
4424
Artem Serovcced8ba2017-07-19 18:18:09 +01004425 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4426
Roland Levillain3e3d7332015-04-28 11:00:54 +01004427 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004428 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004429 const uint32_t dex_method_index_;
4430 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004431
4432 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4433 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004434};
4435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004436class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004437 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004438 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004439 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004440 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004441 uint32_t dex_pc,
4442 uint32_t dex_method_index,
4443 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304444 : HInvoke(kInvokeUnresolved,
4445 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004446 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004447 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004448 return_type,
4449 dex_pc,
4450 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004451 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004452 invoke_type) {
4453 }
4454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004455 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004456
Calin Juravle175dc732015-08-25 15:42:32 +01004457 DECLARE_INSTRUCTION(InvokeUnresolved);
4458
Artem Serovcced8ba2017-07-19 18:18:09 +01004459 protected:
4460 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004461};
4462
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004463class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004464 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004465 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004466 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004467 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004468 uint32_t dex_pc,
4469 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304470 : HInvoke(kInvokePolymorphic,
4471 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004472 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004473 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004474 return_type,
4475 dex_pc,
4476 dex_method_index,
4477 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304478 kVirtual) {
4479 }
Orion Hodsonac141392017-01-13 11:53:47 +00004480
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004481 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004482
Orion Hodsonac141392017-01-13 11:53:47 +00004483 DECLARE_INSTRUCTION(InvokePolymorphic);
4484
Artem Serovcced8ba2017-07-19 18:18:09 +01004485 protected:
4486 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004487};
4488
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004489class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004490 public:
4491 HInvokeCustom(ArenaAllocator* allocator,
4492 uint32_t number_of_arguments,
4493 uint32_t call_site_index,
4494 DataType::Type return_type,
4495 uint32_t dex_pc)
4496 : HInvoke(kInvokeCustom,
4497 allocator,
4498 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004499 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004500 return_type,
4501 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004502 /* dex_method_index= */ dex::kDexNoIndex,
4503 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004504 kStatic),
4505 call_site_index_(call_site_index) {
4506 }
4507
4508 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4509
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004510 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004511
4512 DECLARE_INSTRUCTION(InvokeCustom);
4513
4514 protected:
4515 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4516
4517 private:
4518 uint32_t call_site_index_;
4519};
4520
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004521class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004522 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004523 // Requirements of this method call regarding the class
4524 // initialization (clinit) check of its declaring class.
4525 enum class ClinitCheckRequirement {
4526 kNone, // Class already initialized.
4527 kExplicit, // Static call having explicit clinit check as last input.
4528 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004529 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004530 };
4531
Vladimir Marko58155012015-08-19 12:49:41 +00004532 // Determines how to load the target ArtMethod*.
4533 enum class MethodLoadKind {
4534 // Use a String init ArtMethod* loaded from Thread entrypoints.
4535 kStringInit,
4536
4537 // Use the method's own ArtMethod* loaded by the register allocator.
4538 kRecursive,
4539
Vladimir Marko65979462017-05-19 17:25:12 +01004540 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4541 // Used for boot image methods referenced by boot image code.
4542 kBootImageLinkTimePcRelative,
4543
Vladimir Markob066d432018-01-03 13:14:37 +00004544 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4545 // Used for app->boot calls with relocatable image.
4546 kBootImageRelRo,
4547
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004548 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004549 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004550 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004551
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004552 // Use ArtMethod* at a known address, embed the direct address in the code.
4553 // Used for for JIT-compiled calls.
4554 kJitDirectAddress,
4555
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004556 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4557 // used when other kinds are unimplemented on a particular architecture.
4558 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004559 };
4560
4561 // Determines the location of the code pointer.
4562 enum class CodePtrLocation {
4563 // Recursive call, use local PC-relative call instruction.
4564 kCallSelf,
4565
Vladimir Marko58155012015-08-19 12:49:41 +00004566 // Use code pointer from the ArtMethod*.
4567 // Used when we don't know the target code. This is also the last-resort-kind used when
4568 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4569 kCallArtMethod,
4570 };
4571
4572 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004573 MethodLoadKind method_load_kind;
4574 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004575 // The method load data holds
4576 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4577 // Note that there are multiple string init methods, each having its own offset.
4578 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004579 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004580 };
4581
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004582 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004583 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004584 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004585 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004586 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004587 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004588 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004589 InvokeType invoke_type,
4590 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004591 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304592 : HInvoke(kInvokeStaticOrDirect,
4593 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004594 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004595 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004596 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004597 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004598 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004599 return_type,
4600 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004601 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004602 resolved_method,
4603 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004604 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004605 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004606 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4607 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004608
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004609 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004610
Vladimir Markodc151b22015-10-15 18:02:30 +01004611 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004612 bool had_current_method_input = HasCurrentMethodInput();
4613 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4614
4615 // Using the current method is the default and once we find a better
4616 // method load kind, we should not go back to using the current method.
4617 DCHECK(had_current_method_input || !needs_current_method_input);
4618
4619 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004620 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4621 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004622 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004623 dispatch_info_ = dispatch_info;
4624 }
4625
Aart Bik6daebeb2017-04-03 14:35:41 -07004626 DispatchInfo GetDispatchInfo() const {
4627 return dispatch_info_;
4628 }
4629
Vladimir Markoc53c0792015-11-19 15:48:33 +00004630 void AddSpecialInput(HInstruction* input) {
4631 // We allow only one special input.
4632 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4633 DCHECK(InputCount() == GetSpecialInputIndex() ||
4634 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4635 InsertInputAt(GetSpecialInputIndex(), input);
4636 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004637
Vladimir Marko372f10e2016-05-17 16:30:10 +01004638 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004639 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004640 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4641 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4642 DCHECK(!input_records.empty());
4643 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4644 HInstruction* last_input = input_records.back().GetInstruction();
4645 // Note: `last_input` may be null during arguments setup.
4646 if (last_input != nullptr) {
4647 // `last_input` is the last input of a static invoke marked as having
4648 // an explicit clinit check. It must either be:
4649 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4650 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4651 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4652 }
4653 }
4654 return input_records;
4655 }
4656
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004657 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004658 // We access the method via the dex cache so we can't do an implicit null check.
4659 // TODO: for intrinsics we can generate implicit null checks.
4660 return false;
4661 }
4662
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004663 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004664 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004665 }
4666
Vladimir Markoc53c0792015-11-19 15:48:33 +00004667 // Get the index of the special input, if any.
4668 //
David Brazdil6de19382016-01-08 17:37:10 +00004669 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4670 // method pointer; otherwise there may be one platform-specific special input,
4671 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004672 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004673 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004674
Vladimir Marko58155012015-08-19 12:49:41 +00004675 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4676 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4677 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004678 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004679 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004680 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004681 bool HasPcRelativeMethodLoadKind() const {
4682 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004683 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004684 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004685 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004686 bool HasCurrentMethodInput() const {
4687 // This function can be called only after the invoke has been fully initialized by the builder.
4688 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004689 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004690 return true;
4691 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004692 DCHECK(InputCount() == GetSpecialInputIndex() ||
4693 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004694 return false;
4695 }
4696 }
Vladimir Marko58155012015-08-19 12:49:41 +00004697
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004698 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004699 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004700 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004701 }
4702
4703 uint64_t GetMethodAddress() const {
4704 DCHECK(HasMethodAddress());
4705 return dispatch_info_.method_load_data;
4706 }
4707
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004708 const DexFile& GetDexFileForPcRelativeDexCache() const;
4709
Vladimir Markoa1de9182016-02-25 11:37:38 +00004710 ClinitCheckRequirement GetClinitCheckRequirement() const {
4711 return GetPackedField<ClinitCheckRequirementField>();
4712 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004713
Roland Levillain4c0eb422015-04-24 16:43:49 +01004714 // Is this instruction a call to a static method?
4715 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004716 return GetInvokeType() == kStatic;
4717 }
4718
4719 MethodReference GetTargetMethod() const {
4720 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004721 }
4722
Vladimir Markofbb184a2015-11-13 14:47:00 +00004723 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4724 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4725 // instruction; only relevant for static calls with explicit clinit check.
4726 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004727 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004728 size_t last_input_index = inputs_.size() - 1u;
4729 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004730 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004731 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004732 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004733 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004734 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004735 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004736 }
4737
4738 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004739 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004740 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004741 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004742 }
4743
4744 // Is this a call to a static method whose declaring class has an
4745 // implicit intialization check requirement?
4746 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004747 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004748 }
4749
Vladimir Markob554b5a2015-11-06 12:57:55 +00004750 // Does this method load kind need the current method as an input?
4751 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004752 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004753 }
4754
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004755 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004756
Artem Serovcced8ba2017-07-19 18:18:09 +01004757 protected:
4758 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4759
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004760 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004761 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004762 static constexpr size_t kFieldClinitCheckRequirementSize =
4763 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4764 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4765 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4766 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4767 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004768 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4769 kFieldClinitCheckRequirement,
4770 kFieldClinitCheckRequirementSize>;
4771
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004772 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004773 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004774 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004775};
Vladimir Markof64242a2015-12-01 14:58:23 +00004776std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004777std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004778
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004779class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004780 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004781 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004782 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004783 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004784 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004785 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004786 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004787 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304788 : HInvoke(kInvokeVirtual,
4789 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004790 number_of_arguments,
4791 0u,
4792 return_type,
4793 dex_pc,
4794 dex_method_index,
4795 resolved_method,
4796 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304797 vtable_index_(vtable_index) {
4798 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004800 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004802 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004803 switch (GetIntrinsic()) {
4804 case Intrinsics::kThreadCurrentThread:
4805 case Intrinsics::kStringBufferAppend:
4806 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004807 case Intrinsics::kStringBuilderAppendObject:
4808 case Intrinsics::kStringBuilderAppendString:
4809 case Intrinsics::kStringBuilderAppendCharSequence:
4810 case Intrinsics::kStringBuilderAppendCharArray:
4811 case Intrinsics::kStringBuilderAppendBoolean:
4812 case Intrinsics::kStringBuilderAppendChar:
4813 case Intrinsics::kStringBuilderAppendInt:
4814 case Intrinsics::kStringBuilderAppendLong:
4815 case Intrinsics::kStringBuilderAppendFloat:
4816 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004817 case Intrinsics::kStringBuilderToString:
4818 return false;
4819 default:
4820 return HInvoke::CanBeNull();
4821 }
4822 }
4823
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004824 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004825 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004826 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004827 }
4828
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004829 uint32_t GetVTableIndex() const { return vtable_index_; }
4830
4831 DECLARE_INSTRUCTION(InvokeVirtual);
4832
Artem Serovcced8ba2017-07-19 18:18:09 +01004833 protected:
4834 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4835
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004836 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004837 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004838 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004839};
4840
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004841class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004842 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004843 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004844 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004845 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004846 uint32_t dex_pc,
4847 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004848 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004849 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304850 : HInvoke(kInvokeInterface,
4851 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004852 number_of_arguments,
4853 0u,
4854 return_type,
4855 dex_pc,
4856 dex_method_index,
4857 resolved_method,
4858 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304859 imt_index_(imt_index) {
4860 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004862 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004863
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004864 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004865 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004866 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004867 }
4868
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004869 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004870 // The assembly stub currently needs it.
4871 return true;
4872 }
4873
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004874 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004875
4876 DECLARE_INSTRUCTION(InvokeInterface);
4877
Artem Serovcced8ba2017-07-19 18:18:09 +01004878 protected:
4879 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4880
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004881 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004882 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004883 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004884};
4885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004886class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004887 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004888 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304889 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004890 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004891 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004892
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004893 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004894
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004895 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004896 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004897 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004898 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004899 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004900 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004901 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004902 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4903 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004904 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004905 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4906 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004907
Roland Levillain88cb1752014-10-20 16:36:47 +01004908 DECLARE_INSTRUCTION(Neg);
4909
Artem Serovcced8ba2017-07-19 18:18:09 +01004910 protected:
4911 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004912};
4913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004914class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004915 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004916 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304917 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004918 SetRawInputAt(0, cls);
4919 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004920 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004921 }
4922
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004923 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004924
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004925 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004926 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004927
Mingyao Yang0c365e62015-03-31 15:09:29 -07004928 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004929 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004930
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004931 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004932
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004933 HLoadClass* GetLoadClass() const {
4934 DCHECK(InputAt(0)->IsLoadClass());
4935 return InputAt(0)->AsLoadClass();
4936 }
4937
4938 HInstruction* GetLength() const {
4939 return InputAt(1);
4940 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004941
Vladimir Markob5461632018-10-15 14:24:21 +01004942 size_t GetComponentSizeShift() {
4943 return GetPackedField<ComponentSizeShiftField>();
4944 }
4945
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004946 DECLARE_INSTRUCTION(NewArray);
4947
Artem Serovcced8ba2017-07-19 18:18:09 +01004948 protected:
4949 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004950
4951 private:
4952 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4953 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4954 static constexpr size_t kNumberOfNewArrayPackedBits =
4955 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4956 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4957 using ComponentSizeShiftField =
4958 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004959};
4960
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004961class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004962 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004963 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004964 HInstruction* left,
4965 HInstruction* right,
4966 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304967 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4968 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004969
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004971
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004972 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004973
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004974 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004975 return GetBlock()->GetGraph()->GetIntConstant(
4976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004977 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004978 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004979 return GetBlock()->GetGraph()->GetLongConstant(
4980 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004981 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004982 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004983 return GetBlock()->GetGraph()->GetFloatConstant(
4984 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4985 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004986 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004987 return GetBlock()->GetGraph()->GetDoubleConstant(
4988 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4989 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004990
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004991 DECLARE_INSTRUCTION(Add);
4992
Artem Serovcced8ba2017-07-19 18:18:09 +01004993 protected:
4994 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004995};
4996
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004997class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004998 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004999 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005000 HInstruction* left,
5001 HInstruction* right,
5002 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305003 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5004 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005005
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005006 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005008 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005009 return GetBlock()->GetGraph()->GetIntConstant(
5010 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005011 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005012 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005013 return GetBlock()->GetGraph()->GetLongConstant(
5014 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005015 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005016 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005017 return GetBlock()->GetGraph()->GetFloatConstant(
5018 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5019 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005020 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005021 return GetBlock()->GetGraph()->GetDoubleConstant(
5022 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5023 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005024
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005025 DECLARE_INSTRUCTION(Sub);
5026
Artem Serovcced8ba2017-07-19 18:18:09 +01005027 protected:
5028 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005029};
5030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005031class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005032 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005033 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005034 HInstruction* left,
5035 HInstruction* right,
5036 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305037 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5038 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005039
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005040 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005041
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005042 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005043
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005044 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005045 return GetBlock()->GetGraph()->GetIntConstant(
5046 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005047 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005048 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005049 return GetBlock()->GetGraph()->GetLongConstant(
5050 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005051 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005052 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005053 return GetBlock()->GetGraph()->GetFloatConstant(
5054 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5055 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005056 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005057 return GetBlock()->GetGraph()->GetDoubleConstant(
5058 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5059 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005060
5061 DECLARE_INSTRUCTION(Mul);
5062
Artem Serovcced8ba2017-07-19 18:18:09 +01005063 protected:
5064 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005065};
5066
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005067class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005068 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005069 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005070 HInstruction* left,
5071 HInstruction* right,
5072 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305073 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5074 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005075
Roland Levillain9867bc72015-08-05 10:21:34 +01005076 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005077 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005078 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005079 // Our graph structure ensures we never have 0 for `y` during
5080 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005081 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005082 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005083 return (y == -1) ? -x : x / y;
5084 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005085
Roland Levillain31dd3d62016-02-16 12:21:02 +00005086 template <typename T>
5087 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005088 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005089 return x / y;
5090 }
5091
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005092 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005093 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005094 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005095 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005096 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005097 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005098 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5099 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005100 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005101 return GetBlock()->GetGraph()->GetFloatConstant(
5102 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5103 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005104 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005105 return GetBlock()->GetGraph()->GetDoubleConstant(
5106 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005107 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005108
5109 DECLARE_INSTRUCTION(Div);
5110
Artem Serovcced8ba2017-07-19 18:18:09 +01005111 protected:
5112 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005113};
5114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005115class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005116 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005117 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005118 HInstruction* left,
5119 HInstruction* right,
5120 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305121 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5122 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005123
Roland Levillain9867bc72015-08-05 10:21:34 +01005124 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005125 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005126 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005127 // Our graph structure ensures we never have 0 for `y` during
5128 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005129 DCHECK_NE(y, 0);
5130 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5131 return (y == -1) ? 0 : x % y;
5132 }
5133
Roland Levillain31dd3d62016-02-16 12:21:02 +00005134 template <typename T>
5135 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005136 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005137 return std::fmod(x, y);
5138 }
5139
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005140 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005142 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005143 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005144 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005145 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005146 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005147 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005148 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005149 return GetBlock()->GetGraph()->GetFloatConstant(
5150 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5151 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005152 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005153 return GetBlock()->GetGraph()->GetDoubleConstant(
5154 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5155 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005156
5157 DECLARE_INSTRUCTION(Rem);
5158
Artem Serovcced8ba2017-07-19 18:18:09 +01005159 protected:
5160 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005161};
5162
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005163class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005164 public:
5165 HMin(DataType::Type result_type,
5166 HInstruction* left,
5167 HInstruction* right,
5168 uint32_t dex_pc)
5169 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5170
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005171 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005172
5173 // Evaluation for integral values.
5174 template <typename T> static T ComputeIntegral(T x, T y) {
5175 return (x <= y) ? x : y;
5176 }
5177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005178 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005179 return GetBlock()->GetGraph()->GetIntConstant(
5180 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5181 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005182 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005183 return GetBlock()->GetGraph()->GetLongConstant(
5184 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5185 }
5186 // TODO: Evaluation for floating-point values.
5187 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005188 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005189 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005190 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005191
5192 DECLARE_INSTRUCTION(Min);
5193
5194 protected:
5195 DEFAULT_COPY_CONSTRUCTOR(Min);
5196};
5197
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005198class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005199 public:
5200 HMax(DataType::Type result_type,
5201 HInstruction* left,
5202 HInstruction* right,
5203 uint32_t dex_pc)
5204 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005206 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005207
5208 // Evaluation for integral values.
5209 template <typename T> static T ComputeIntegral(T x, T y) {
5210 return (x >= y) ? x : y;
5211 }
5212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005213 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005214 return GetBlock()->GetGraph()->GetIntConstant(
5215 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5216 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005217 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005218 return GetBlock()->GetGraph()->GetLongConstant(
5219 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5220 }
5221 // TODO: Evaluation for floating-point values.
5222 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005223 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005224 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005225 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005226
5227 DECLARE_INSTRUCTION(Max);
5228
5229 protected:
5230 DEFAULT_COPY_CONSTRUCTOR(Max);
5231};
5232
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005233class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005234 public:
5235 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5236 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5237
5238 // Evaluation for integral values.
5239 template <typename T> static T ComputeIntegral(T x) {
5240 return x < 0 ? -x : x;
5241 }
5242
5243 // Evaluation for floating-point values.
5244 // Note, as a "quality of implementation", rather than pure "spec compliance",
5245 // we require that Math.abs() clears the sign bit (but changes nothing else)
5246 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5247 // http://b/30758343
5248 template <typename T, typename S> static T ComputeFP(T x) {
5249 S bits = bit_cast<S, T>(x);
5250 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5251 }
5252
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005253 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005254 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5255 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005256 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005257 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5258 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005259 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005260 return GetBlock()->GetGraph()->GetFloatConstant(
5261 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5262 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005263 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005264 return GetBlock()->GetGraph()->GetDoubleConstant(
5265 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5266 }
5267
5268 DECLARE_INSTRUCTION(Abs);
5269
5270 protected:
5271 DEFAULT_COPY_CONSTRUCTOR(Abs);
5272};
5273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005274class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005275 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005276 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005277 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5278 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005279 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005280 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005281 SetRawInputAt(0, value);
5282 }
5283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284 bool IsClonable() const override { return true; }
5285 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005286
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005287 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005288 return true;
5289 }
5290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005291 bool NeedsEnvironment() const override { return true; }
5292 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005293
Calin Juravled0d48522014-11-04 16:40:20 +00005294 DECLARE_INSTRUCTION(DivZeroCheck);
5295
Artem Serovcced8ba2017-07-19 18:18:09 +01005296 protected:
5297 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005298};
5299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005300class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005301 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005302 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005303 HInstruction* value,
5304 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005305 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305306 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005307 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5308 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005309 }
5310
Roland Levillain5b5b9312016-03-22 14:57:31 +00005311 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005312 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005313 return value << (distance & max_shift_distance);
5314 }
5315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005316 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005317 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005318 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005319 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005320 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005321 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005322 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005323 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005324 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005325 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005326 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5327 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005328 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005329 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005330 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005331 LOG(FATAL) << DebugName() << " is not defined for float values";
5332 UNREACHABLE();
5333 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005334 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005335 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005336 LOG(FATAL) << DebugName() << " is not defined for double values";
5337 UNREACHABLE();
5338 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005339
5340 DECLARE_INSTRUCTION(Shl);
5341
Artem Serovcced8ba2017-07-19 18:18:09 +01005342 protected:
5343 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005344};
5345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005346class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005347 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005348 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005349 HInstruction* value,
5350 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005351 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305352 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005353 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5354 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005355 }
5356
Roland Levillain5b5b9312016-03-22 14:57:31 +00005357 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005358 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005359 return value >> (distance & max_shift_distance);
5360 }
5361
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005362 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005363 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005364 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005365 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005366 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005367 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005368 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005369 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005370 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005371 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005372 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5373 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005374 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005375 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005376 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005377 LOG(FATAL) << DebugName() << " is not defined for float values";
5378 UNREACHABLE();
5379 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005380 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005381 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005382 LOG(FATAL) << DebugName() << " is not defined for double values";
5383 UNREACHABLE();
5384 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005385
5386 DECLARE_INSTRUCTION(Shr);
5387
Artem Serovcced8ba2017-07-19 18:18:09 +01005388 protected:
5389 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005390};
5391
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005392class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005393 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005394 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005395 HInstruction* value,
5396 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005397 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305398 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005399 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5400 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005401 }
5402
Roland Levillain5b5b9312016-03-22 14:57:31 +00005403 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005404 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005405 typedef typename std::make_unsigned<T>::type V;
5406 V ux = static_cast<V>(value);
5407 return static_cast<T>(ux >> (distance & max_shift_distance));
5408 }
5409
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005410 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005411 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005412 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005413 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005414 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005415 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005416 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005417 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005418 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005419 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005420 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5421 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005422 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005423 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005424 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005425 LOG(FATAL) << DebugName() << " is not defined for float values";
5426 UNREACHABLE();
5427 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005428 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005429 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005430 LOG(FATAL) << DebugName() << " is not defined for double values";
5431 UNREACHABLE();
5432 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005433
5434 DECLARE_INSTRUCTION(UShr);
5435
Artem Serovcced8ba2017-07-19 18:18:09 +01005436 protected:
5437 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005438};
5439
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005440class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005441 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005442 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005443 HInstruction* left,
5444 HInstruction* right,
5445 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305446 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5447 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005449 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005450
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005451 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005452
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005453 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005454 return GetBlock()->GetGraph()->GetIntConstant(
5455 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005456 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005457 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005458 return GetBlock()->GetGraph()->GetLongConstant(
5459 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005460 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005461 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005463 LOG(FATAL) << DebugName() << " is not defined for float values";
5464 UNREACHABLE();
5465 }
5466 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005467 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005468 LOG(FATAL) << DebugName() << " is not defined for double values";
5469 UNREACHABLE();
5470 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005471
5472 DECLARE_INSTRUCTION(And);
5473
Artem Serovcced8ba2017-07-19 18:18:09 +01005474 protected:
5475 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005476};
5477
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005478class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005479 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005481 HInstruction* left,
5482 HInstruction* right,
5483 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305484 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5485 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005486
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005487 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005488
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005489 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005490
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005491 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005492 return GetBlock()->GetGraph()->GetIntConstant(
5493 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005494 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005495 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005496 return GetBlock()->GetGraph()->GetLongConstant(
5497 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005498 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005499 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005500 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005501 LOG(FATAL) << DebugName() << " is not defined for float values";
5502 UNREACHABLE();
5503 }
5504 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005505 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005506 LOG(FATAL) << DebugName() << " is not defined for double values";
5507 UNREACHABLE();
5508 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005509
5510 DECLARE_INSTRUCTION(Or);
5511
Artem Serovcced8ba2017-07-19 18:18:09 +01005512 protected:
5513 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005514};
5515
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005516class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005517 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005518 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005519 HInstruction* left,
5520 HInstruction* right,
5521 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305522 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5523 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005524
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005525 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005526
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005527 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005528
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005529 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005530 return GetBlock()->GetGraph()->GetIntConstant(
5531 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005532 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005533 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005534 return GetBlock()->GetGraph()->GetLongConstant(
5535 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005536 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005537 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005538 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005539 LOG(FATAL) << DebugName() << " is not defined for float values";
5540 UNREACHABLE();
5541 }
5542 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005543 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005544 LOG(FATAL) << DebugName() << " is not defined for double values";
5545 UNREACHABLE();
5546 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005547
5548 DECLARE_INSTRUCTION(Xor);
5549
Artem Serovcced8ba2017-07-19 18:18:09 +01005550 protected:
5551 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005552};
5553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005554class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005555 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005556 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305557 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005558 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5559 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005560 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005561
Roland Levillain5b5b9312016-03-22 14:57:31 +00005562 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005563 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005564 typedef typename std::make_unsigned<T>::type V;
5565 V ux = static_cast<V>(value);
5566 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005567 return static_cast<T>(ux);
5568 } else {
5569 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005570 return static_cast<T>(ux >> (distance & max_shift_value)) |
5571 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005572 }
5573 }
5574
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005575 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005576 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005577 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005578 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005579 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005580 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005581 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005582 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005583 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005584 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005585 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5586 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005587 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005588 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005589 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005590 LOG(FATAL) << DebugName() << " is not defined for float values";
5591 UNREACHABLE();
5592 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005593 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005594 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005595 LOG(FATAL) << DebugName() << " is not defined for double values";
5596 UNREACHABLE();
5597 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005598
5599 DECLARE_INSTRUCTION(Ror);
5600
Artem Serovcced8ba2017-07-19 18:18:09 +01005601 protected:
5602 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005603};
5604
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005605// The value of a parameter in this method. Its location depends on
5606// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005607class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005608 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005609 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005610 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005611 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005612 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005613 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305614 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005615 dex_file_(dex_file),
5616 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005617 index_(index) {
5618 SetPackedFlag<kFlagIsThis>(is_this);
5619 SetPackedFlag<kFlagCanBeNull>(!is_this);
5620 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005621
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005622 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005623 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005624 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005625 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005626
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005627 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005628 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005629
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005630 DECLARE_INSTRUCTION(ParameterValue);
5631
Artem Serovcced8ba2017-07-19 18:18:09 +01005632 protected:
5633 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5634
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005635 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005636 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005637 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005638 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5639 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5640 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5641 "Too many packed fields.");
5642
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005643 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005644 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005645 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005646 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005647 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005648};
5649
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005650class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005651 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305653 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5654 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005655
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005656 bool CanBeMoved() const override { return true; }
5657 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005658 return true;
5659 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005660
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005661 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005662
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005663 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005664 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005665 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005666 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005667 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005668 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005669 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005670 LOG(FATAL) << DebugName() << " is not defined for float values";
5671 UNREACHABLE();
5672 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005673 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005674 LOG(FATAL) << DebugName() << " is not defined for double values";
5675 UNREACHABLE();
5676 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005677
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005678 DECLARE_INSTRUCTION(Not);
5679
Artem Serovcced8ba2017-07-19 18:18:09 +01005680 protected:
5681 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005682};
5683
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005684class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005685 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005686 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305687 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5688 }
David Brazdil66d126e2015-04-03 16:02:44 +01005689
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005690 bool CanBeMoved() const override { return true; }
5691 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005692 return true;
5693 }
5694
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005695 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005696 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005697 return !x;
5698 }
5699
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005700 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005701 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005702 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005703 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005704 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005705 UNREACHABLE();
5706 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005707 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005708 LOG(FATAL) << DebugName() << " is not defined for float values";
5709 UNREACHABLE();
5710 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005711 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005712 LOG(FATAL) << DebugName() << " is not defined for double values";
5713 UNREACHABLE();
5714 }
David Brazdil66d126e2015-04-03 16:02:44 +01005715
5716 DECLARE_INSTRUCTION(BooleanNot);
5717
Artem Serovcced8ba2017-07-19 18:18:09 +01005718 protected:
5719 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005720};
5721
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005722class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005723 public:
5724 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005725 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305726 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005727 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005728 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005729 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005730 }
5731
5732 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005733 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5734 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005735
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005736 bool IsClonable() const override { return true; }
5737 bool CanBeMoved() const override { return true; }
5738 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005739 return true;
5740 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005741 // Return whether the conversion is implicit. This includes conversion to the same type.
5742 bool IsImplicitConversion() const {
5743 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5744 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005745
Mark Mendelle82549b2015-05-06 10:55:34 -04005746 // Try to statically evaluate the conversion and return a HConstant
5747 // containing the result. If the input cannot be converted, return nullptr.
5748 HConstant* TryStaticEvaluation() const;
5749
Roland Levillaindff1f282014-11-05 14:15:05 +00005750 DECLARE_INSTRUCTION(TypeConversion);
5751
Artem Serovcced8ba2017-07-19 18:18:09 +01005752 protected:
5753 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005754};
5755
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005756static constexpr uint32_t kNoRegNumber = -1;
5757
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005758class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005759 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005760 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005761 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5762 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005763 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005764 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005765 SetRawInputAt(0, value);
5766 }
5767
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005768 bool IsClonable() const override { return true; }
5769 bool CanBeMoved() const override { return true; }
5770 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005771 return true;
5772 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005774 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005775
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005776 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005778 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005779
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005780 DECLARE_INSTRUCTION(NullCheck);
5781
Artem Serovcced8ba2017-07-19 18:18:09 +01005782 protected:
5783 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005784};
5785
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005786// Embeds an ArtField and all the information required by the compiler. We cache
5787// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005788class FieldInfo : public ValueObject {
5789 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005790 FieldInfo(ArtField* field,
5791 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005792 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005793 bool is_volatile,
5794 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005795 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005796 const DexFile& dex_file)
5797 : field_(field),
5798 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005799 field_type_(field_type),
5800 is_volatile_(is_volatile),
5801 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005802 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005803 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005804
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005805 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005806 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005807 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005808 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005809 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005810 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005811 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005812
5813 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005814 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005815 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005816 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005817 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005818 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005819 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005820 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005821};
5822
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005823class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005824 public:
5825 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005826 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005827 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005828 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005829 bool is_volatile,
5830 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005831 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005832 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005833 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305834 : HExpression(kInstanceFieldGet,
5835 field_type,
5836 SideEffects::FieldReadOfType(field_type, is_volatile),
5837 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005838 field_info_(field,
5839 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005840 field_type,
5841 is_volatile,
5842 field_idx,
5843 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005844 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005845 SetRawInputAt(0, value);
5846 }
5847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005848 bool IsClonable() const override { return true; }
5849 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005850
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005851 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005852 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005853 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005854 }
5855
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005856 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005857 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005858 }
5859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005860 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005861 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5862 }
5863
Calin Juravle52c48962014-12-16 17:02:57 +00005864 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005865 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005866 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005867 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005868
Vladimir Marko61b92282017-10-11 13:23:17 +01005869 void SetType(DataType::Type new_type) {
5870 DCHECK(DataType::IsIntegralType(GetType()));
5871 DCHECK(DataType::IsIntegralType(new_type));
5872 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5873 SetPackedField<TypeField>(new_type);
5874 }
5875
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005876 DECLARE_INSTRUCTION(InstanceFieldGet);
5877
Artem Serovcced8ba2017-07-19 18:18:09 +01005878 protected:
5879 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5880
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005881 private:
5882 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005883};
5884
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005885class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005886 public:
5887 HInstanceFieldSet(HInstruction* object,
5888 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005889 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005890 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005891 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005892 bool is_volatile,
5893 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005894 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005895 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005896 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005897 : HExpression(kInstanceFieldSet,
5898 SideEffects::FieldWriteOfType(field_type, is_volatile),
5899 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005900 field_info_(field,
5901 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005902 field_type,
5903 is_volatile,
5904 field_idx,
5905 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005906 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005907 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005908 SetRawInputAt(0, object);
5909 SetRawInputAt(1, value);
5910 }
5911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005912 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005914 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005915 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005916 }
5917
Calin Juravle52c48962014-12-16 17:02:57 +00005918 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005919 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005920 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005921 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005922 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005923 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5924 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005925
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005926 DECLARE_INSTRUCTION(InstanceFieldSet);
5927
Artem Serovcced8ba2017-07-19 18:18:09 +01005928 protected:
5929 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5930
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005931 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005932 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5933 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5934 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5935 "Too many packed fields.");
5936
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005937 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005938};
5939
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005940class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005941 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005942 HArrayGet(HInstruction* array,
5943 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005945 uint32_t dex_pc)
5946 : HArrayGet(array,
5947 index,
5948 type,
5949 SideEffects::ArrayReadOfType(type),
5950 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005951 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305952 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005953
5954 HArrayGet(HInstruction* array,
5955 HInstruction* index,
5956 DataType::Type type,
5957 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005958 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005959 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305960 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005961 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005962 SetRawInputAt(0, array);
5963 SetRawInputAt(1, index);
5964 }
5965
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005966 bool IsClonable() const override { return true; }
5967 bool CanBeMoved() const override { return true; }
5968 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005969 return true;
5970 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005971 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005972 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005973 // Currently, unless the array is the result of NewArray, the array access is always
5974 // preceded by some form of null NullCheck necessary for the bounds check, usually
5975 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5976 // dynamic BCE. There are cases when these could be removed to produce better code.
5977 // If we ever add optimizations to do so we should allow an implicit check here
5978 // (as long as the address falls in the first page).
5979 //
5980 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5981 // a = cond ? new int[1] : null;
5982 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005983 return false;
5984 }
5985
David Brazdil4833f5a2015-12-16 10:37:39 +00005986 bool IsEquivalentOf(HArrayGet* other) const {
5987 bool result = (GetDexPc() == other->GetDexPc());
5988 if (kIsDebugBuild && result) {
5989 DCHECK_EQ(GetBlock(), other->GetBlock());
5990 DCHECK_EQ(GetArray(), other->GetArray());
5991 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005992 if (DataType::IsIntOrLongType(GetType())) {
5993 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005994 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005995 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5996 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005997 }
5998 }
5999 return result;
6000 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006001
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006002 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6003
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006004 HInstruction* GetArray() const { return InputAt(0); }
6005 HInstruction* GetIndex() const { return InputAt(1); }
6006
Vladimir Marko61b92282017-10-11 13:23:17 +01006007 void SetType(DataType::Type new_type) {
6008 DCHECK(DataType::IsIntegralType(GetType()));
6009 DCHECK(DataType::IsIntegralType(new_type));
6010 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6011 SetPackedField<TypeField>(new_type);
6012 }
6013
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006014 DECLARE_INSTRUCTION(ArrayGet);
6015
Artem Serovcced8ba2017-07-19 18:18:09 +01006016 protected:
6017 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6018
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006019 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006020 // We treat a String as an array, creating the HArrayGet from String.charAt()
6021 // intrinsic in the instruction simplifier. We can always determine whether
6022 // a particular HArrayGet is actually a String.charAt() by looking at the type
6023 // of the input but that requires holding the mutator lock, so we prefer to use
6024 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006025 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006026 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6027 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6028 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006029};
6030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006031class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006032 public:
6033 HArraySet(HInstruction* array,
6034 HInstruction* index,
6035 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006036 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006037 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006038 : HArraySet(array,
6039 index,
6040 value,
6041 expected_component_type,
6042 // Make a best guess for side effects now, may be refined during SSA building.
6043 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306044 dex_pc) {
6045 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006046
6047 HArraySet(HInstruction* array,
6048 HInstruction* index,
6049 HInstruction* value,
6050 DataType::Type expected_component_type,
6051 SideEffects side_effects,
6052 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006053 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006054 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006055 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006056 SetPackedFlag<kFlagValueCanBeNull>(true);
6057 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006058 SetRawInputAt(0, array);
6059 SetRawInputAt(1, index);
6060 SetRawInputAt(2, value);
6061 }
6062
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006063 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006065 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006066 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006067 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006068 }
6069
Mingyao Yang81014cb2015-06-02 03:16:27 -07006070 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006071 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006072
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006073 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006074 // TODO: Same as for ArrayGet.
6075 return false;
6076 }
6077
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006078 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006079 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006080 }
6081
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006082 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006083 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006084 }
6085
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006086 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006087 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006088 }
6089
Vladimir Markoa1de9182016-02-25 11:37:38 +00006090 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6091 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6092 bool StaticTypeOfArrayIsObjectArray() const {
6093 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6094 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006095
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006096 HInstruction* GetArray() const { return InputAt(0); }
6097 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006098 HInstruction* GetValue() const { return InputAt(2); }
6099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006100 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006101 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6102 }
6103
6104 static DataType::Type GetComponentType(DataType::Type value_type,
6105 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006106 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006107 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006108 // be correct, we also check what is the value type. If it is a floating
6109 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006110 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006111 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006112 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006113 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006115 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006117 }
6118
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006119 static SideEffects ComputeSideEffects(DataType::Type type) {
6120 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006121 }
6122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006123 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6124 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6125 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006126 }
6127
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006128 DECLARE_INSTRUCTION(ArraySet);
6129
Artem Serovcced8ba2017-07-19 18:18:09 +01006130 protected:
6131 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6132
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006133 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6135 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006136 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006137 static constexpr size_t kFlagNeedsTypeCheck =
6138 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6139 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006140 // Cached information for the reference_type_info_ so that codegen
6141 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006142 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6143 static constexpr size_t kNumberOfArraySetPackedBits =
6144 kFlagStaticTypeOfArrayIsObjectArray + 1;
6145 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6146 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006147 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006148};
6149
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006150class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006151 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006152 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306153 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006154 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006155 // Note that arrays do not change length, so the instruction does not
6156 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006157 SetRawInputAt(0, array);
6158 }
6159
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006160 bool IsClonable() const override { return true; }
6161 bool CanBeMoved() const override { return true; }
6162 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006163 return true;
6164 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006165 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006166 return obj == InputAt(0);
6167 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006168
Vladimir Markodce016e2016-04-28 13:10:02 +01006169 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6170
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006171 DECLARE_INSTRUCTION(ArrayLength);
6172
Artem Serovcced8ba2017-07-19 18:18:09 +01006173 protected:
6174 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006176 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006177 // We treat a String as an array, creating the HArrayLength from String.length()
6178 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6179 // determine whether a particular HArrayLength is actually a String.length() by
6180 // looking at the type of the input but that requires holding the mutator lock, so
6181 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006182 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006183 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6184 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6185 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006186};
6187
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006188class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006189 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006190 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006191 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6192 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006193 HBoundsCheck(HInstruction* index,
6194 HInstruction* length,
6195 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006196 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006197 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006198 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006199 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006200 SetRawInputAt(0, index);
6201 SetRawInputAt(1, length);
6202 }
6203
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006204 bool IsClonable() const override { return true; }
6205 bool CanBeMoved() const override { return true; }
6206 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006207 return true;
6208 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006210 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006211
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006212 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006213
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006214 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006215
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006216 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006217
6218 DECLARE_INSTRUCTION(BoundsCheck);
6219
Artem Serovcced8ba2017-07-19 18:18:09 +01006220 protected:
6221 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6222
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006223 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006224 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006225 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6226 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6227 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006228};
6229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006230class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006231 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006232 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006233 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306234 slow_path_(nullptr) {
6235 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006237 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006239 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006240 return true;
6241 }
6242
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006243 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6244 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006245
6246 DECLARE_INSTRUCTION(SuspendCheck);
6247
Artem Serovcced8ba2017-07-19 18:18:09 +01006248 protected:
6249 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6250
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006251 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006252 // Only used for code generation, in order to share the same slow path between back edges
6253 // of a same loop.
6254 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006255};
6256
David Srbecky0cf44932015-12-09 14:09:59 +00006257// Pseudo-instruction which provides the native debugger with mapping information.
6258// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006259class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006260 public:
6261 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006262 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306263 }
David Srbecky0cf44932015-12-09 14:09:59 +00006264
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006265 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006266 return true;
6267 }
6268
6269 DECLARE_INSTRUCTION(NativeDebugInfo);
6270
Artem Serovcced8ba2017-07-19 18:18:09 +01006271 protected:
6272 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006273};
6274
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006275/**
6276 * Instruction to load a Class object.
6277 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006278class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006279 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006280 // Determines how to load the Class.
6281 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006282 // We cannot load this class. See HSharpening::SharpenLoadClass.
6283 kInvalid = -1,
6284
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006285 // Use the Class* from the method's own ArtMethod*.
6286 kReferrersClass,
6287
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006288 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006289 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006290 kBootImageLinkTimePcRelative,
6291
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006292 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006293 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006294 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006295
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006296 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006297 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006298 kBssEntry,
6299
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006300 // Use a known boot image Class* address, embedded in the code by the codegen.
6301 // Used for boot image classes referenced by apps in JIT-compiled code.
6302 kJitBootImageAddress,
6303
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006304 // Load from the root table associated with the JIT compiled method.
6305 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006306
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006307 // Load using a simple runtime call. This is the fall-back load kind when
6308 // the codegen is unable to use another appropriate kind.
6309 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006310
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006311 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006312 };
6313
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006314 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006315 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006316 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006317 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006318 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006319 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006320 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006321 : HInstruction(kLoadClass,
6322 DataType::Type::kReference,
6323 SideEffectsForArchRuntimeCalls(),
6324 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006325 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006326 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006327 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006328 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006329 // Referrers class should not need access check. We never inline unverified
6330 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006331 DCHECK(!is_referrers_class || !needs_access_check);
6332
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006333 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006334 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006335 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006336 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006337 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006338 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006339 }
6340
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006341 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006342
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006343 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006344
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006345 LoadKind GetLoadKind() const {
6346 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006347 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006348
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006349 bool HasPcRelativeLoadKind() const {
6350 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6351 GetLoadKind() == LoadKind::kBootImageRelRo ||
6352 GetLoadKind() == LoadKind::kBssEntry;
6353 }
6354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006355 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006356
Yi Kong39402542019-03-24 02:47:16 -07006357 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006358
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006359 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006360
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006361 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006362
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006363 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006364 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006365 }
6366
Calin Juravle0ba218d2015-05-19 18:46:01 +01006367 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006368 // The entrypoint the code generator is going to call does not do
6369 // clinit of the class.
6370 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006371 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006372 }
6373
6374 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006375 return NeedsAccessCheck() ||
6376 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006377 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006378 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006379 }
6380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006381 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006382 return NeedsAccessCheck() ||
6383 MustGenerateClinitCheck() ||
6384 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006385 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006386 GetLoadKind() == LoadKind::kBssEntry) &&
6387 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006388 }
6389
Calin Juravleacf735c2015-02-12 15:25:22 +00006390 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006391 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6392 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006393 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006394 } else {
6395 return ReferenceTypeInfo::CreateInvalid();
6396 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006397 }
6398
Vladimir Marko175e7862018-03-27 09:03:13 +00006399 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6400 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6401 DCHECK(klass_ != nullptr);
6402 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006403 }
6404
Andreas Gampea5b09a62016-11-17 15:21:22 -08006405 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006406 const DexFile& GetDexFile() const { return dex_file_; }
6407
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006408 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006409 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006410 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006411
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006412 static SideEffects SideEffectsForArchRuntimeCalls() {
6413 return SideEffects::CanTriggerGC();
6414 }
6415
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006416 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006417 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006418 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006419 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006420
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006421 bool MustResolveTypeOnSlowPath() const {
6422 // Check that this instruction has a slow path.
6423 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6424 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6425 return GetLoadKind() == LoadKind::kBssEntry;
6426 }
6427
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006428 void MarkInBootImage() {
6429 SetPackedFlag<kFlagIsInBootImage>(true);
6430 }
6431
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006432 void AddSpecialInput(HInstruction* special_input);
6433
6434 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006435 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006436 return ArrayRef<HUserRecord<HInstruction*>>(
6437 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6438 }
6439
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006440 Handle<mirror::Class> GetClass() const {
6441 return klass_;
6442 }
6443
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006444 DECLARE_INSTRUCTION(LoadClass);
6445
Artem Serovcced8ba2017-07-19 18:18:09 +01006446 protected:
6447 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6448
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006449 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006450 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006451 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006452 // Whether this instruction must generate the initialization check.
6453 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006454 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006455 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6456 static constexpr size_t kFieldLoadKindSize =
6457 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006458 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6459 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006460 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006461 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6462
6463 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006464 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006465 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006466 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006467 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006468 }
6469
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006470 void SetLoadKindInternal(LoadKind load_kind);
6471
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006472 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006473 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006474 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006475 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006476
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006477 // A type index and dex file where the class can be accessed. The dex file can be:
6478 // - The compiling method's dex file if the class is defined there too.
6479 // - The compiling method's dex file if the class is referenced there.
6480 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006481 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006482 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006483 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006484
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006485 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006486};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006487std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6488
6489// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006490inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6491 // The load kind should be determined before inserting the instruction to the graph.
6492 DCHECK(GetBlock() == nullptr);
6493 DCHECK(GetEnvironment() == nullptr);
6494 SetPackedField<LoadKindField>(load_kind);
6495 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6496 special_input_ = HUserRecord<HInstruction*>(nullptr);
6497 }
6498 if (!NeedsEnvironment()) {
6499 SetSideEffects(SideEffects::None());
6500 }
6501}
6502
6503// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006504inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006505 // The special input is used for PC-relative loads on some architectures,
6506 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006507 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006508 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006509 GetLoadKind() == LoadKind::kBssEntry ||
6510 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006511 DCHECK(special_input_.GetInstruction() == nullptr);
6512 special_input_ = HUserRecord<HInstruction*>(special_input);
6513 special_input->AddUseAt(this, 0);
6514}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006515
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006516class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006517 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006518 // Determines how to load the String.
6519 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006520 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006521 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006522 kBootImageLinkTimePcRelative,
6523
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006524 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006525 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006526 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006527
Vladimir Markoaad75c62016-10-03 08:46:48 +00006528 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006529 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006530 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006531
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006532 // Use a known boot image String* address, embedded in the code by the codegen.
6533 // Used for boot image strings referenced by apps in JIT-compiled code.
6534 kJitBootImageAddress,
6535
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006536 // Load from the root table associated with the JIT compiled method.
6537 kJitTableAddress,
6538
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006539 // Load using a simple runtime call. This is the fall-back load kind when
6540 // the codegen is unable to use another appropriate kind.
6541 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006542
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006543 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006544 };
6545
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006546 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006547 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006548 const DexFile& dex_file,
6549 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006550 : HInstruction(kLoadString,
6551 DataType::Type::kReference,
6552 SideEffectsForArchRuntimeCalls(),
6553 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006554 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006555 string_index_(string_index),
6556 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006557 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006558 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006559
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006560 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006561
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006562 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006563
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006564 LoadKind GetLoadKind() const {
6565 return GetPackedField<LoadKindField>();
6566 }
6567
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006568 bool HasPcRelativeLoadKind() const {
6569 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6570 GetLoadKind() == LoadKind::kBootImageRelRo ||
6571 GetLoadKind() == LoadKind::kBssEntry;
6572 }
6573
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006574 const DexFile& GetDexFile() const {
6575 return dex_file_;
6576 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006577
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006578 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006579 return string_index_;
6580 }
6581
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006582 Handle<mirror::String> GetString() const {
6583 return string_;
6584 }
6585
6586 void SetString(Handle<mirror::String> str) {
6587 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006588 }
6589
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006590 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006591
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006592 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006593
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006594 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006595
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006596 // Will call the runtime if we need to load the string through
6597 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006598 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006599 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006600 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006601 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006602 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006603 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006604 return false;
6605 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006606 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006607 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006608
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006609 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006610 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006611 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006612
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006613 bool CanBeNull() const override { return false; }
6614 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006615
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006616 static SideEffects SideEffectsForArchRuntimeCalls() {
6617 return SideEffects::CanTriggerGC();
6618 }
6619
Vladimir Marko372f10e2016-05-17 16:30:10 +01006620 void AddSpecialInput(HInstruction* special_input);
6621
6622 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006623 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006624 return ArrayRef<HUserRecord<HInstruction*>>(
6625 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006626 }
6627
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006628 DECLARE_INSTRUCTION(LoadString);
6629
Artem Serovcced8ba2017-07-19 18:18:09 +01006630 protected:
6631 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6632
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006633 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006634 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006635 static constexpr size_t kFieldLoadKindSize =
6636 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6637 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006638 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006639 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006640
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006641 void SetLoadKindInternal(LoadKind load_kind);
6642
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006643 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006644 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006645 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006646 HUserRecord<HInstruction*> special_input_;
6647
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006648 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006649 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006650
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006651 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006652};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006653std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6654
6655// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006656inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6657 // The load kind should be determined before inserting the instruction to the graph.
6658 DCHECK(GetBlock() == nullptr);
6659 DCHECK(GetEnvironment() == nullptr);
6660 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6661 SetPackedField<LoadKindField>(load_kind);
6662 if (load_kind != LoadKind::kRuntimeCall) {
6663 special_input_ = HUserRecord<HInstruction*>(nullptr);
6664 }
6665 if (!NeedsEnvironment()) {
6666 SetSideEffects(SideEffects::None());
6667 }
6668}
6669
6670// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006671inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006672 // The special input is used for PC-relative loads on some architectures,
6673 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006674 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006675 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006676 GetLoadKind() == LoadKind::kBssEntry ||
6677 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006678 // HLoadString::GetInputRecords() returns an empty array at this point,
6679 // so use the GetInputRecords() from the base class to set the input record.
6680 DCHECK(special_input_.GetInstruction() == nullptr);
6681 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006682 special_input->AddUseAt(this, 0);
6683}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006684
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006685class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006686 public:
6687 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006688 uint16_t method_handle_idx,
6689 const DexFile& dex_file,
6690 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006691 : HInstruction(kLoadMethodHandle,
6692 DataType::Type::kReference,
6693 SideEffectsForArchRuntimeCalls(),
6694 dex_pc),
6695 special_input_(HUserRecord<HInstruction*>(current_method)),
6696 method_handle_idx_(method_handle_idx),
6697 dex_file_(dex_file) {
6698 }
6699
6700 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006701 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006702 return ArrayRef<HUserRecord<HInstruction*>>(
6703 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6704 }
6705
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006706 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006707
6708 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6709
6710 const DexFile& GetDexFile() const { return dex_file_; }
6711
6712 static SideEffects SideEffectsForArchRuntimeCalls() {
6713 return SideEffects::CanTriggerGC();
6714 }
6715
6716 DECLARE_INSTRUCTION(LoadMethodHandle);
6717
6718 protected:
6719 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6720
6721 private:
6722 // The special input is the HCurrentMethod for kRuntimeCall.
6723 HUserRecord<HInstruction*> special_input_;
6724
6725 const uint16_t method_handle_idx_;
6726 const DexFile& dex_file_;
6727};
6728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006729class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006730 public:
6731 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006732 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006733 const DexFile& dex_file,
6734 uint32_t dex_pc)
6735 : HInstruction(kLoadMethodType,
6736 DataType::Type::kReference,
6737 SideEffectsForArchRuntimeCalls(),
6738 dex_pc),
6739 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006740 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006741 dex_file_(dex_file) {
6742 }
6743
6744 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006745 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006746 return ArrayRef<HUserRecord<HInstruction*>>(
6747 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6748 }
6749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006750 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006751
Orion Hodson06d10a72018-05-14 08:53:38 +01006752 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006753
6754 const DexFile& GetDexFile() const { return dex_file_; }
6755
6756 static SideEffects SideEffectsForArchRuntimeCalls() {
6757 return SideEffects::CanTriggerGC();
6758 }
6759
6760 DECLARE_INSTRUCTION(LoadMethodType);
6761
6762 protected:
6763 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6764
6765 private:
6766 // The special input is the HCurrentMethod for kRuntimeCall.
6767 HUserRecord<HInstruction*> special_input_;
6768
Orion Hodson06d10a72018-05-14 08:53:38 +01006769 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006770 const DexFile& dex_file_;
6771};
6772
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006773/**
6774 * Performs an initialization check on its Class object input.
6775 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006776class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006777 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006778 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006779 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306780 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006782 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006783 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006784 SetRawInputAt(0, constant);
6785 }
Artem Serova6e26142018-06-19 14:55:17 +01006786 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006787 bool CanBeMoved() const override { return true; }
6788 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006789 return true;
6790 }
6791
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006792 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006793 // May call runtime to initialize the class.
6794 return true;
6795 }
6796
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006797 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006798
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006799 HLoadClass* GetLoadClass() const {
6800 DCHECK(InputAt(0)->IsLoadClass());
6801 return InputAt(0)->AsLoadClass();
6802 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006803
6804 DECLARE_INSTRUCTION(ClinitCheck);
6805
Artem Serovcced8ba2017-07-19 18:18:09 +01006806
6807 protected:
6808 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006809};
6810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006811class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006812 public:
6813 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006814 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006815 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006816 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006817 bool is_volatile,
6818 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006819 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006820 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006821 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306822 : HExpression(kStaticFieldGet,
6823 field_type,
6824 SideEffects::FieldReadOfType(field_type, is_volatile),
6825 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006826 field_info_(field,
6827 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006828 field_type,
6829 is_volatile,
6830 field_idx,
6831 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006832 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006833 SetRawInputAt(0, cls);
6834 }
6835
Calin Juravle52c48962014-12-16 17:02:57 +00006836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006837 bool IsClonable() const override { return true; }
6838 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006839
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006840 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006841 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006842 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006843 }
6844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006845 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006846 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6847 }
6848
Calin Juravle52c48962014-12-16 17:02:57 +00006849 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006850 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006851 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006852 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006853
Vladimir Marko61b92282017-10-11 13:23:17 +01006854 void SetType(DataType::Type new_type) {
6855 DCHECK(DataType::IsIntegralType(GetType()));
6856 DCHECK(DataType::IsIntegralType(new_type));
6857 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6858 SetPackedField<TypeField>(new_type);
6859 }
6860
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006861 DECLARE_INSTRUCTION(StaticFieldGet);
6862
Artem Serovcced8ba2017-07-19 18:18:09 +01006863 protected:
6864 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6865
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006866 private:
6867 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006868};
6869
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006870class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006871 public:
6872 HStaticFieldSet(HInstruction* cls,
6873 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006874 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006875 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006876 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006877 bool is_volatile,
6878 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006879 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006880 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006881 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006882 : HExpression(kStaticFieldSet,
6883 SideEffects::FieldWriteOfType(field_type, is_volatile),
6884 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006885 field_info_(field,
6886 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006887 field_type,
6888 is_volatile,
6889 field_idx,
6890 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006891 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006892 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006893 SetRawInputAt(0, cls);
6894 SetRawInputAt(1, value);
6895 }
6896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006897 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006898 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006899 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006900 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006901 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006902
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006903 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006904 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6905 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006906
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006907 DECLARE_INSTRUCTION(StaticFieldSet);
6908
Artem Serovcced8ba2017-07-19 18:18:09 +01006909 protected:
6910 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6911
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006912 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006913 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6914 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6915 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6916 "Too many packed fields.");
6917
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006918 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006919};
6920
Vladimir Marko552a1342017-10-31 10:56:47 +00006921class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6922 public:
6923 HStringBuilderAppend(HIntConstant* format,
6924 uint32_t number_of_arguments,
6925 ArenaAllocator* allocator,
6926 uint32_t dex_pc)
6927 : HVariableInputSizeInstruction(
6928 kStringBuilderAppend,
6929 DataType::Type::kReference,
6930 // The runtime call may read memory from inputs. It never writes outside
6931 // of the newly allocated result object (or newly allocated helper objects).
6932 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6933 dex_pc,
6934 allocator,
6935 number_of_arguments + /* format */ 1u,
6936 kArenaAllocInvokeInputs) {
6937 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6938 SetRawInputAt(FormatIndex(), format);
6939 }
6940
6941 void SetArgumentAt(size_t index, HInstruction* argument) {
6942 DCHECK_LE(index, GetNumberOfArguments());
6943 SetRawInputAt(index, argument);
6944 }
6945
6946 // Return the number of arguments, excluding the format.
6947 size_t GetNumberOfArguments() const {
6948 DCHECK_GE(InputCount(), 1u);
6949 return InputCount() - 1u;
6950 }
6951
6952 size_t FormatIndex() const {
6953 return GetNumberOfArguments();
6954 }
6955
6956 HIntConstant* GetFormat() {
6957 return InputAt(FormatIndex())->AsIntConstant();
6958 }
6959
6960 bool NeedsEnvironment() const override { return true; }
6961
6962 bool CanThrow() const override { return true; }
6963
6964 DECLARE_INSTRUCTION(StringBuilderAppend);
6965
6966 protected:
6967 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
6968};
6969
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006970class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006971 public:
6972 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006973 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006974 uint32_t field_index,
6975 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306976 : HExpression(kUnresolvedInstanceFieldGet,
6977 field_type,
6978 SideEffects::AllExceptGCDependency(),
6979 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006980 field_index_(field_index) {
6981 SetRawInputAt(0, obj);
6982 }
6983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006984 bool IsClonable() const override { return true; }
6985 bool NeedsEnvironment() const override { return true; }
6986 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006987
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006988 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006989 uint32_t GetFieldIndex() const { return field_index_; }
6990
6991 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6992
Artem Serovcced8ba2017-07-19 18:18:09 +01006993 protected:
6994 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6995
Calin Juravlee460d1d2015-09-29 04:52:17 +01006996 private:
6997 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006998};
6999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007000class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007001 public:
7002 HUnresolvedInstanceFieldSet(HInstruction* obj,
7003 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007004 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007005 uint32_t field_index,
7006 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007007 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007008 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007009 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007010 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007011 SetRawInputAt(0, obj);
7012 SetRawInputAt(1, value);
7013 }
7014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007015 bool IsClonable() const override { return true; }
7016 bool NeedsEnvironment() const override { return true; }
7017 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007019 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007020 uint32_t GetFieldIndex() const { return field_index_; }
7021
7022 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7023
Artem Serovcced8ba2017-07-19 18:18:09 +01007024 protected:
7025 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7026
Calin Juravlee460d1d2015-09-29 04:52:17 +01007027 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007028 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7029 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007030 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007031 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7032 kFieldFieldType + kFieldFieldTypeSize;
7033 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7034 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007035 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007036
Calin Juravlee460d1d2015-09-29 04:52:17 +01007037 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007038};
7039
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007040class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007041 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007042 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007043 uint32_t field_index,
7044 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307045 : HExpression(kUnresolvedStaticFieldGet,
7046 field_type,
7047 SideEffects::AllExceptGCDependency(),
7048 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007049 field_index_(field_index) {
7050 }
7051
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007052 bool IsClonable() const override { return true; }
7053 bool NeedsEnvironment() const override { return true; }
7054 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007055
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007056 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007057 uint32_t GetFieldIndex() const { return field_index_; }
7058
7059 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7060
Artem Serovcced8ba2017-07-19 18:18:09 +01007061 protected:
7062 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7063
Calin Juravlee460d1d2015-09-29 04:52:17 +01007064 private:
7065 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007066};
7067
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007068class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007069 public:
7070 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007071 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007072 uint32_t field_index,
7073 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007074 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007075 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007076 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007077 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007078 SetRawInputAt(0, value);
7079 }
7080
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007081 bool IsClonable() const override { return true; }
7082 bool NeedsEnvironment() const override { return true; }
7083 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007085 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007086 uint32_t GetFieldIndex() const { return field_index_; }
7087
7088 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7089
Artem Serovcced8ba2017-07-19 18:18:09 +01007090 protected:
7091 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7092
Calin Juravlee460d1d2015-09-29 04:52:17 +01007093 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007094 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7095 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007096 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007097 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7098 kFieldFieldType + kFieldFieldTypeSize;
7099 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7100 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007101 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007102
Calin Juravlee460d1d2015-09-29 04:52:17 +01007103 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007104};
7105
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007106// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007107class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007108 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007109 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307110 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7111 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007113 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007114
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007115 DECLARE_INSTRUCTION(LoadException);
7116
Artem Serovcced8ba2017-07-19 18:18:09 +01007117 protected:
7118 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007119};
7120
David Brazdilcb1c0552015-08-04 16:22:25 +01007121// Implicit part of move-exception which clears thread-local exception storage.
7122// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007123class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007124 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007125 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007126 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307127 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007128
7129 DECLARE_INSTRUCTION(ClearException);
7130
Artem Serovcced8ba2017-07-19 18:18:09 +01007131 protected:
7132 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007133};
7134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007135class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007136 public:
7137 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007138 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007139 SetRawInputAt(0, exception);
7140 }
7141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007142 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007143
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007144 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007145
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007146 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007148 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007149
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007150 DECLARE_INSTRUCTION(Throw);
7151
Artem Serovcced8ba2017-07-19 18:18:09 +01007152 protected:
7153 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007154};
7155
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007156/**
7157 * Implementation strategies for the code generator of a HInstanceOf
7158 * or `HCheckCast`.
7159 */
7160enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007161 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007162 kExactCheck, // Can do a single class compare.
7163 kClassHierarchyCheck, // Can just walk the super class chain.
7164 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7165 kInterfaceCheck, // No optimization yet when checking against an interface.
7166 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007167 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007168 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007169 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007170};
7171
Roland Levillain86503782016-02-11 19:07:30 +00007172std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7173
Vladimir Marko175e7862018-03-27 09:03:13 +00007174// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7175// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7176class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007177 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007178 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007179 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007180 HInstruction* object,
7181 HInstruction* target_class_or_null,
7182 TypeCheckKind check_kind,
7183 Handle<mirror::Class> klass,
7184 uint32_t dex_pc,
7185 ArenaAllocator* allocator,
7186 HIntConstant* bitstring_path_to_root,
7187 HIntConstant* bitstring_mask,
7188 SideEffects side_effects)
7189 : HVariableInputSizeInstruction(
7190 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007191 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007192 side_effects,
7193 dex_pc,
7194 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007195 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007196 kArenaAllocTypeCheckInputs),
7197 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007198 SetPackedField<TypeCheckKindField>(check_kind);
7199 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007200 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007201 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007202 SetRawInputAt(1, target_class_or_null);
7203 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7204 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7205 if (check_kind == TypeCheckKind::kBitstringCheck) {
7206 DCHECK(target_class_or_null->IsNullConstant());
7207 SetRawInputAt(2, bitstring_path_to_root);
7208 SetRawInputAt(3, bitstring_mask);
7209 } else {
7210 DCHECK(target_class_or_null->IsLoadClass());
7211 }
Vladimir Marko87584542017-12-12 17:47:52 +00007212 }
7213
7214 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007215 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007216 HInstruction* load_class = InputAt(1);
7217 DCHECK(load_class->IsLoadClass());
7218 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007219 }
7220
Vladimir Marko175e7862018-03-27 09:03:13 +00007221 uint32_t GetBitstringPathToRoot() const {
7222 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7223 HInstruction* path_to_root = InputAt(2);
7224 DCHECK(path_to_root->IsIntConstant());
7225 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7226 }
7227
7228 uint32_t GetBitstringMask() const {
7229 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7230 HInstruction* mask = InputAt(3);
7231 DCHECK(mask->IsIntConstant());
7232 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7233 }
7234
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007235 bool IsClonable() const override { return true; }
7236 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007237
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007238 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007239 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7240 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007241 }
7242
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007243 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7244 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7245 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7246 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7247
Vladimir Marko175e7862018-03-27 09:03:13 +00007248 ReferenceTypeInfo GetTargetClassRTI() {
7249 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7250 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007251 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007252 } else {
7253 return ReferenceTypeInfo::CreateInvalid();
7254 }
7255 }
7256
7257 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7258 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7259 DCHECK(klass_ != nullptr);
7260 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7261 }
7262
7263 Handle<mirror::Class> GetClass() const {
7264 return klass_;
7265 }
7266
7267 protected:
7268 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7269
7270 private:
7271 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7272 static constexpr size_t kFieldTypeCheckKindSize =
7273 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7274 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7275 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7276 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7277 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7278 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7279
7280 Handle<mirror::Class> klass_;
7281};
7282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007283class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007284 public:
7285 HInstanceOf(HInstruction* object,
7286 HInstruction* target_class_or_null,
7287 TypeCheckKind check_kind,
7288 Handle<mirror::Class> klass,
7289 uint32_t dex_pc,
7290 ArenaAllocator* allocator,
7291 HIntConstant* bitstring_path_to_root,
7292 HIntConstant* bitstring_mask)
7293 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007294 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007295 object,
7296 target_class_or_null,
7297 check_kind,
7298 klass,
7299 dex_pc,
7300 allocator,
7301 bitstring_path_to_root,
7302 bitstring_mask,
7303 SideEffectsForArchRuntimeCalls(check_kind)) {}
7304
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007305 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007307 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007308 return CanCallRuntime(GetTypeCheckKind());
7309 }
7310
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007311 static bool CanCallRuntime(TypeCheckKind check_kind) {
7312 // Mips currently does runtime calls for any other checks.
7313 return check_kind != TypeCheckKind::kExactCheck;
7314 }
7315
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007316 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007317 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007318 }
7319
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007320 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007321
Artem Serovcced8ba2017-07-19 18:18:09 +01007322 protected:
7323 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007324};
7325
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007326class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007327 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007328 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307329 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007330 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7331 SetPackedFlag<kFlagUpperCanBeNull>(true);
7332 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007333 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007334 SetRawInputAt(0, input);
7335 }
7336
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007337 bool InstructionDataEquals(const HInstruction* other) const override;
7338 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007339
David Brazdilf5552582015-12-27 13:36:12 +00007340 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007341 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007342 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007343 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007344
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007345 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007346 DCHECK(GetUpperCanBeNull() || !can_be_null);
7347 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007348 }
7349
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007350 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007351
Calin Juravleb1498f62015-02-16 13:13:29 +00007352 DECLARE_INSTRUCTION(BoundType);
7353
Artem Serovcced8ba2017-07-19 18:18:09 +01007354 protected:
7355 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7356
Calin Juravleb1498f62015-02-16 13:13:29 +00007357 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007358 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7359 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007360 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007361 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7362 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7363 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7364
Calin Juravleb1498f62015-02-16 13:13:29 +00007365 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007366 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7367 // It is used to bound the type in cases like:
7368 // if (x instanceof ClassX) {
7369 // // uper_bound_ will be ClassX
7370 // }
David Brazdilf5552582015-12-27 13:36:12 +00007371 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007372};
7373
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007374class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007375 public:
7376 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007377 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007378 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007379 Handle<mirror::Class> klass,
7380 uint32_t dex_pc,
7381 ArenaAllocator* allocator,
7382 HIntConstant* bitstring_path_to_root,
7383 HIntConstant* bitstring_mask)
7384 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007385 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007386 object,
7387 target_class_or_null,
7388 check_kind,
7389 klass,
7390 dex_pc,
7391 allocator,
7392 bitstring_path_to_root,
7393 bitstring_mask,
7394 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007396 bool IsClonable() const override { return true; }
7397 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007398 // Instruction may throw a CheckCastError.
7399 return true;
7400 }
7401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007402 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007403
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007404 DECLARE_INSTRUCTION(CheckCast);
7405
Artem Serovcced8ba2017-07-19 18:18:09 +01007406 protected:
7407 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007408};
7409
Andreas Gampe26de38b2016-07-27 17:53:11 -07007410/**
7411 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7412 * @details We define the combined barrier types that are actually required
7413 * by the Java Memory Model, rather than using exactly the terminology from
7414 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7415 * primitives. Note that the JSR-133 cookbook generally does not deal with
7416 * store atomicity issues, and the recipes there are not always entirely sufficient.
7417 * The current recipe is as follows:
7418 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7419 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7420 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7421 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7422 * class has final fields.
7423 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7424 * store-to-memory instructions. Only generated together with non-temporal stores.
7425 */
7426enum MemBarrierKind {
7427 kAnyStore,
7428 kLoadAny,
7429 kStoreStore,
7430 kAnyAny,
7431 kNTStoreStore,
7432 kLastBarrierKind = kNTStoreStore
7433};
7434std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007436class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007437 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007438 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007439 : HExpression(kMemoryBarrier,
7440 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7441 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007442 SetPackedField<BarrierKindField>(barrier_kind);
7443 }
Calin Juravle27df7582015-04-17 19:12:31 +01007444
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007445 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007446
Vladimir Markoa1de9182016-02-25 11:37:38 +00007447 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007448
7449 DECLARE_INSTRUCTION(MemoryBarrier);
7450
Artem Serovcced8ba2017-07-19 18:18:09 +01007451 protected:
7452 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7453
Calin Juravle27df7582015-04-17 19:12:31 +01007454 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007455 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7456 static constexpr size_t kFieldBarrierKindSize =
7457 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7458 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7459 kFieldBarrierKind + kFieldBarrierKindSize;
7460 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7461 "Too many packed fields.");
7462 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007463};
7464
Igor Murashkind01745e2017-04-05 16:40:31 -07007465// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7466// the specified object(s), with respect to any subsequent store that might "publish"
7467// (i.e. make visible) the specified object to another thread.
7468//
7469// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7470// for all final fields (that were set) at the end of the invoked constructor.
7471//
7472// The constructor fence models the freeze actions for the final fields of an object
7473// being constructed (semantically at the end of the constructor). Constructor fences
7474// have a per-object affinity; two separate objects being constructed get two separate
7475// constructor fences.
7476//
7477// (Note: that if calling a super-constructor or forwarding to another constructor,
7478// the freezes would happen at the end of *that* constructor being invoked).
7479//
7480// The memory model guarantees that when the object being constructed is "published" after
7481// constructor completion (i.e. escapes the current thread via a store), then any final field
7482// writes must be observable on other threads (once they observe that publication).
7483//
7484// Further, anything written before the freeze, and read by dereferencing through the final field,
7485// must also be visible (so final object field could itself have an object with non-final fields;
7486// yet the freeze must also extend to them).
7487//
7488// Constructor example:
7489//
7490// class HasFinal {
7491// final int field; Optimizing IR for <init>()V:
7492// HasFinal() {
7493// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7494// // freeze(this.field); HConstructorFence(this)
7495// } HReturn
7496// }
7497//
7498// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7499// already-initialized classes; in that case the allocation must act as a "default-initializer"
7500// of the object which effectively writes the class pointer "final field".
7501//
7502// For example, we can model default-initialiation as roughly the equivalent of the following:
7503//
7504// class Object {
7505// private final Class header;
7506// }
7507//
7508// Java code: Optimizing IR:
7509//
7510// T new_instance<T>() {
7511// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7512// obj.header = T.class; // header write is done by above call.
7513// // freeze(obj.header) HConstructorFence(obj)
7514// return (T)obj;
7515// }
7516//
7517// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007518// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007519// * QuasiAtomic::ThreadFenceForConstructor
7520//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007521class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007522 // A fence has variable inputs because the inputs can be removed
7523 // after prepare_for_register_allocation phase.
7524 // (TODO: In the future a fence could freeze multiple objects
7525 // after merging two fences together.)
7526 public:
7527 // `fence_object` is the reference that needs to be protected for correct publication.
7528 //
7529 // It makes sense in the following situations:
7530 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7531 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7532 //
7533 // After construction the `fence_object` becomes the 0th input.
7534 // This is not an input in a real sense, but just a convenient place to stash the information
7535 // about the associated object.
7536 HConstructorFence(HInstruction* fence_object,
7537 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007538 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007539 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7540 // constraints described in the class header. We claim that these SideEffects constraints
7541 // enforce a superset of the real constraints.
7542 //
7543 // The ordering described above is conservatively modeled with SideEffects as follows:
7544 //
7545 // * To prevent reordering of the publication stores:
7546 // ----> "Reads of objects" is the initial SideEffect.
7547 // * For every primitive final field store in the constructor:
7548 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7549 // * If there are any stores to reference final fields in the constructor:
7550 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7551 // that are reachable from `fence_object` also need to be prevented for reordering
7552 // (and we do not want to do alias analysis to figure out what those stores are).
7553 //
7554 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7555 // even more conservative approach than the one described above, and prevents all of the
7556 // above reordering without analyzing any of the instructions in the constructor.
7557 //
7558 // If in a later phase we discover that there are no writes to reference final fields,
7559 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307560 : HVariableInputSizeInstruction(kConstructorFence,
7561 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007562 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007563 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007564 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007565 kArenaAllocConstructorFenceInputs) {
7566 DCHECK(fence_object != nullptr);
7567 SetRawInputAt(0, fence_object);
7568 }
7569
7570 // The object associated with this constructor fence.
7571 //
7572 // (Note: This will be null after the prepare_for_register_allocation phase,
7573 // as all constructor fence inputs are removed there).
7574 HInstruction* GetFenceObject() const {
7575 return InputAt(0);
7576 }
7577
7578 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7579 // - Delete `fence_use` from `this`'s use list.
7580 // - Delete `this` from `fence_use`'s inputs list.
7581 // - If the `fence_use` is dead, remove it from the graph.
7582 //
7583 // A fence is considered dead once it no longer has any uses
7584 // and all of the inputs are dead.
7585 //
7586 // This must *not* be called during/after prepare_for_register_allocation,
7587 // because that removes all the inputs to the fences but the fence is actually
7588 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007589 //
7590 // Returns how many HConstructorFence instructions were removed from graph.
7591 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007592
Igor Murashkindd018df2017-08-09 10:38:31 -07007593 // Combine all inputs of `this` and `other` instruction and remove
7594 // `other` from the graph.
7595 //
7596 // Inputs are unique after the merge.
7597 //
7598 // Requirement: `this` must not be the same as `other.
7599 void Merge(HConstructorFence* other);
7600
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007601 // Check if this constructor fence is protecting
7602 // an HNewInstance or HNewArray that is also the immediate
7603 // predecessor of `this`.
7604 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007605 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7606 // to be one of the inputs of `this`.
7607 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007608 // Returns the associated HNewArray or HNewInstance,
7609 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007610 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007611
Igor Murashkind01745e2017-04-05 16:40:31 -07007612 DECLARE_INSTRUCTION(ConstructorFence);
7613
Artem Serovcced8ba2017-07-19 18:18:09 +01007614 protected:
7615 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007616};
7617
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007618class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007619 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007620 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007621 kEnter,
7622 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007623 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007624 };
7625
7626 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007627 : HExpression(kMonitorOperation,
7628 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7629 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007630 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007631 SetRawInputAt(0, object);
7632 }
7633
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007634 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007635 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007636
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007637 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007638 // Verifier guarantees that monitor-exit cannot throw.
7639 // This is important because it allows the HGraphBuilder to remove
7640 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7641 return IsEnter();
7642 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007643
Vladimir Markoa1de9182016-02-25 11:37:38 +00007644 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7645 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007646
7647 DECLARE_INSTRUCTION(MonitorOperation);
7648
Artem Serovcced8ba2017-07-19 18:18:09 +01007649 protected:
7650 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7651
Calin Juravle52c48962014-12-16 17:02:57 +00007652 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007653 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7654 static constexpr size_t kFieldOperationKindSize =
7655 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7656 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7657 kFieldOperationKind + kFieldOperationKindSize;
7658 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7659 "Too many packed fields.");
7660 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007661};
7662
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007663class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007664 public:
7665 HSelect(HInstruction* condition,
7666 HInstruction* true_value,
7667 HInstruction* false_value,
7668 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307669 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007670 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7671
7672 // First input must be `true_value` or `false_value` to allow codegens to
7673 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7674 // that architectures which implement HSelect as a conditional move also
7675 // will not need to invert the condition.
7676 SetRawInputAt(0, false_value);
7677 SetRawInputAt(1, true_value);
7678 SetRawInputAt(2, condition);
7679 }
7680
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007681 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007682 HInstruction* GetFalseValue() const { return InputAt(0); }
7683 HInstruction* GetTrueValue() const { return InputAt(1); }
7684 HInstruction* GetCondition() const { return InputAt(2); }
7685
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007686 bool CanBeMoved() const override { return true; }
7687 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007688 return true;
7689 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007690
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007691 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007692 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7693 }
7694
7695 DECLARE_INSTRUCTION(Select);
7696
Artem Serovcced8ba2017-07-19 18:18:09 +01007697 protected:
7698 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007699};
7700
Vladimir Markof9f64412015-09-02 14:05:49 +01007701class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007702 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007703 MoveOperands(Location source,
7704 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007705 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007706 HInstruction* instruction)
7707 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007708
7709 Location GetSource() const { return source_; }
7710 Location GetDestination() const { return destination_; }
7711
7712 void SetSource(Location value) { source_ = value; }
7713 void SetDestination(Location value) { destination_ = value; }
7714
7715 // The parallel move resolver marks moves as "in-progress" by clearing the
7716 // destination (but not the source).
7717 Location MarkPending() {
7718 DCHECK(!IsPending());
7719 Location dest = destination_;
7720 destination_ = Location::NoLocation();
7721 return dest;
7722 }
7723
7724 void ClearPending(Location dest) {
7725 DCHECK(IsPending());
7726 destination_ = dest;
7727 }
7728
7729 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007730 DCHECK(source_.IsValid() || destination_.IsInvalid());
7731 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007732 }
7733
7734 // True if this blocks a move from the given location.
7735 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007736 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007737 }
7738
7739 // A move is redundant if it's been eliminated, if its source and
7740 // destination are the same, or if its destination is unneeded.
7741 bool IsRedundant() const {
7742 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7743 }
7744
7745 // We clear both operands to indicate move that's been eliminated.
7746 void Eliminate() {
7747 source_ = destination_ = Location::NoLocation();
7748 }
7749
7750 bool IsEliminated() const {
7751 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7752 return source_.IsInvalid();
7753 }
7754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007755 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007756
Nicolas Geoffray90218252015-04-15 11:56:51 +01007757 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007758 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007759 }
7760
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007761 HInstruction* GetInstruction() const { return instruction_; }
7762
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007763 private:
7764 Location source_;
7765 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007766 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007767 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007768 // The instruction this move is assocatied with. Null when this move is
7769 // for moving an input in the expected locations of user (including a phi user).
7770 // This is only used in debug mode, to ensure we do not connect interval siblings
7771 // in the same parallel move.
7772 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007773};
7774
Roland Levillainc9285912015-12-18 10:38:42 +00007775std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7776
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007777static constexpr size_t kDefaultNumberOfMoves = 4;
7778
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007779class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007780 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007781 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007782 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007783 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007784 moves_.reserve(kDefaultNumberOfMoves);
7785 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007786
Nicolas Geoffray90218252015-04-15 11:56:51 +01007787 void AddMove(Location source,
7788 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007789 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007790 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007791 DCHECK(source.IsValid());
7792 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007793 if (kIsDebugBuild) {
7794 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007795 for (const MoveOperands& move : moves_) {
7796 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007797 // Special case the situation where the move is for the spill slot
7798 // of the instruction.
7799 if ((GetPrevious() == instruction)
7800 || ((GetPrevious() == nullptr)
7801 && instruction->IsPhi()
7802 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007803 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007804 << "Doing parallel moves for the same instruction.";
7805 } else {
7806 DCHECK(false) << "Doing parallel moves for the same instruction.";
7807 }
7808 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007809 }
7810 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007811 for (const MoveOperands& move : moves_) {
7812 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007813 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007814 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007815 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007816 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007817 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007818 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007819 }
7820
Vladimir Marko225b6462015-09-28 12:17:40 +01007821 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007822 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007823 }
7824
Vladimir Marko225b6462015-09-28 12:17:40 +01007825 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007826
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007827 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007828
Artem Serovcced8ba2017-07-19 18:18:09 +01007829 protected:
7830 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7831
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007832 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007833 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007834};
7835
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007836// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7837// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7838// never used across anything that can trigger GC.
7839// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7840// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007841class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007842 public:
7843 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307844 : HExpression(kIntermediateAddress,
7845 DataType::Type::kInt32,
7846 SideEffects::DependsOnGC(),
7847 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007848 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7849 DataType::Size(DataType::Type::kReference))
7850 << "kPrimInt and kPrimNot have different sizes.";
7851 SetRawInputAt(0, base_address);
7852 SetRawInputAt(1, offset);
7853 }
7854
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007855 bool IsClonable() const override { return true; }
7856 bool CanBeMoved() const override { return true; }
7857 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007858 return true;
7859 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007860 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007861
7862 HInstruction* GetBaseAddress() const { return InputAt(0); }
7863 HInstruction* GetOffset() const { return InputAt(1); }
7864
7865 DECLARE_INSTRUCTION(IntermediateAddress);
7866
Artem Serovcced8ba2017-07-19 18:18:09 +01007867 protected:
7868 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007869};
7870
7871
Mark Mendell0616ae02015-04-17 12:49:27 -04007872} // namespace art
7873
Aart Bikf8f5a162017-02-06 15:35:29 -08007874#include "nodes_vector.h"
7875
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007876#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7877#include "nodes_shared.h"
7878#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007879#ifdef ART_ENABLE_CODEGEN_mips
7880#include "nodes_mips.h"
7881#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307882#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007883#include "nodes_x86.h"
7884#endif
7885
Vladimir Marko0a516052019-10-14 13:00:44 +00007886namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007887
Igor Murashkin6ef45672017-08-08 13:59:55 -07007888class OptimizingCompilerStats;
7889
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007890class HGraphVisitor : public ValueObject {
7891 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007892 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7893 : stats_(stats),
7894 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007895 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007896
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007897 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007898 virtual void VisitBasicBlock(HBasicBlock* block);
7899
Roland Levillain633021e2014-10-01 14:12:25 +01007900 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007901 void VisitInsertionOrder();
7902
Roland Levillain633021e2014-10-01 14:12:25 +01007903 // Visit the graph following dominator tree reverse post-order.
7904 void VisitReversePostOrder();
7905
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007906 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007907
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007908 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007909#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007910 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7911
7912 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7913
7914#undef DECLARE_VISIT_INSTRUCTION
7915
Igor Murashkin6ef45672017-08-08 13:59:55 -07007916 protected:
7917 OptimizingCompilerStats* stats_;
7918
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007919 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007920 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007921
7922 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7923};
7924
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007925class HGraphDelegateVisitor : public HGraphVisitor {
7926 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007927 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7928 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007929 virtual ~HGraphDelegateVisitor() {}
7930
7931 // Visit functions that delegate to to super class.
7932#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007933 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007934
7935 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7936
7937#undef DECLARE_VISIT_INSTRUCTION
7938
7939 private:
7940 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7941};
7942
Artem Serovcced8ba2017-07-19 18:18:09 +01007943// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7944// and remove the old instruction.
7945HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7946
7947// Create a clone for each clonable instructions/phis and replace the original with the clone.
7948//
7949// Used for testing individual instruction cloner.
7950class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7951 public:
7952 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007953 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007955 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007956 if (instruction->IsClonable()) {
7957 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007958 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007959 }
7960 }
7961
Artem Serov7f4aff62017-06-21 17:02:18 +01007962 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007963
7964 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007965 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007966
7967 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7968};
7969
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007970// Iterator over the blocks that art part of the loop. Includes blocks part
7971// of an inner loop. The order in which the blocks are iterated is on their
7972// block id.
7973class HBlocksInLoopIterator : public ValueObject {
7974 public:
7975 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7976 : blocks_in_loop_(info.GetBlocks()),
7977 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7978 index_(0) {
7979 if (!blocks_in_loop_.IsBitSet(index_)) {
7980 Advance();
7981 }
7982 }
7983
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007984 bool Done() const { return index_ == blocks_.size(); }
7985 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007986 void Advance() {
7987 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007988 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007989 if (blocks_in_loop_.IsBitSet(index_)) {
7990 break;
7991 }
7992 }
7993 }
7994
7995 private:
7996 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007997 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007998 size_t index_;
7999
8000 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8001};
8002
Mingyao Yang3584bce2015-05-19 16:01:59 -07008003// Iterator over the blocks that art part of the loop. Includes blocks part
8004// of an inner loop. The order in which the blocks are iterated is reverse
8005// post order.
8006class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8007 public:
8008 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8009 : blocks_in_loop_(info.GetBlocks()),
8010 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8011 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008012 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008013 Advance();
8014 }
8015 }
8016
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008017 bool Done() const { return index_ == blocks_.size(); }
8018 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008019 void Advance() {
8020 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008021 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8022 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008023 break;
8024 }
8025 }
8026 }
8027
8028 private:
8029 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008030 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008031 size_t index_;
8032
8033 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8034};
8035
Aart Bikf3e61ee2017-04-12 17:09:20 -07008036// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008037inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008038 if (constant->IsIntConstant()) {
8039 return constant->AsIntConstant()->GetValue();
8040 } else if (constant->IsLongConstant()) {
8041 return constant->AsLongConstant()->GetValue();
8042 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008043 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008044 return 0;
8045 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008046}
8047
Aart Bikf3e61ee2017-04-12 17:09:20 -07008048// Returns true iff instruction is an integral constant (and sets value on success).
8049inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8050 if (instruction->IsIntConstant()) {
8051 *value = instruction->AsIntConstant()->GetValue();
8052 return true;
8053 } else if (instruction->IsLongConstant()) {
8054 *value = instruction->AsLongConstant()->GetValue();
8055 return true;
8056 } else if (instruction->IsNullConstant()) {
8057 *value = 0;
8058 return true;
8059 }
8060 return false;
8061}
8062
Aart Bik0148de42017-09-05 09:25:01 -07008063// Returns true iff instruction is the given integral constant.
8064inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8065 int64_t val = 0;
8066 return IsInt64AndGet(instruction, &val) && val == value;
8067}
8068
8069// Returns true iff instruction is a zero bit pattern.
8070inline bool IsZeroBitPattern(HInstruction* instruction) {
8071 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8072}
8073
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008074// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008075#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008076 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8077 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8078#undef INSTRUCTION_TYPE_CHECK
8079
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008080// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008081#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8082 std::is_base_of<BaseType, H##type>::value,
8083#define INSTRUCTION_TYPE_CHECK(type, super) \
8084 inline bool HInstruction::Is##type() const { \
8085 DCHECK_LT(GetKind(), kLastInstructionKind); \
8086 using BaseType = H##type; \
8087 static constexpr bool results[] = { \
8088 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8089 }; \
8090 return results[static_cast<size_t>(GetKind())]; \
8091 }
8092
8093 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8094#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008095#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008096
8097#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008098 inline const H##type* HInstruction::As##type() const { \
8099 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8100 } \
8101 inline H##type* HInstruction::As##type() { \
8102 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8103 }
8104
Vladimir Markoa90dd512018-05-04 15:04:45 +01008105 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8106#undef INSTRUCTION_TYPE_CAST
8107
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008108
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008109// Create space in `blocks` for adding `number_of_new_blocks` entries
8110// starting at location `at`. Blocks after `at` are moved accordingly.
8111inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8112 size_t number_of_new_blocks,
8113 size_t after) {
8114 DCHECK_LT(after, blocks->size());
8115 size_t old_size = blocks->size();
8116 size_t new_size = old_size + number_of_new_blocks;
8117 blocks->resize(new_size);
8118 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8119}
8120
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008121/*
8122 * Hunt "under the hood" of array lengths (leading to array references),
8123 * null checks (also leading to array references), and new arrays
8124 * (leading to the actual length). This makes it more likely related
8125 * instructions become actually comparable.
8126 */
8127inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8128 while (instruction->IsArrayLength() ||
8129 instruction->IsNullCheck() ||
8130 instruction->IsNewArray()) {
8131 instruction = instruction->IsNewArray()
8132 ? instruction->AsNewArray()->GetLength()
8133 : instruction->InputAt(0);
8134 }
8135 return instruction;
8136}
8137
Artem Serov21c7e6f2017-07-27 16:04:42 +01008138void RemoveEnvironmentUses(HInstruction* instruction);
8139bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8140void ResetEnvironmentInputRecords(HInstruction* instruction);
8141
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008142} // namespace art
8143
8144#endif // ART_COMPILER_OPTIMIZING_NODES_H_