blob: 68f1a2406a3612c29209ae848fe8c55575f7ce6b [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 Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000030#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010031#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010032#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file.h"
35#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080036#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070037#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000038#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000039#include "handle.h"
40#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010041#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010042#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000043#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010044#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010045#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010046#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047
48namespace art {
49
Vladimir Markoca6fff82017-10-03 14:49:14 +010050class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000051class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070053class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010054class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000058class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000061class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000062class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000064class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070065class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010067class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010068class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010069class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000070class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010071class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000072class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Mathieu Chartier736b5602015-09-02 14:54:11 -070074namespace mirror {
75class DexCache;
76} // namespace mirror
77
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078static const int kDefaultNumberOfBlocks = 8;
79static const int kDefaultNumberOfSuccessors = 2;
80static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010081static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010082static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084
Roland Levillain5b5b9312016-03-22 14:57:31 +000085// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
86static constexpr int32_t kMaxIntShiftDistance = 0x1f;
87// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
88static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000089
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070091static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010092
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010093static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
94
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060095static constexpr uint32_t kNoDexPc = -1;
96
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010097inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
98 // For the purposes of the compiler, the dex files must actually be the same object
99 // if we want to safely treat them as the same. This is especially important for JIT
100 // as custom class loaders can open the same underlying file (or memory) multiple
101 // times and provide different class resolution but no two class loaders should ever
102 // use the same DexFile object - doing so is an unsupported hack that can lead to
103 // all sorts of weird failures.
104 return &lhs == &rhs;
105}
106
Dave Allison20dfc792014-06-16 20:44:29 -0700107enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700108 // All types.
109 kCondEQ, // ==
110 kCondNE, // !=
111 // Signed integers and floating-point numbers.
112 kCondLT, // <
113 kCondLE, // <=
114 kCondGT, // >
115 kCondGE, // >=
116 // Unsigned integers.
117 kCondB, // <
118 kCondBE, // <=
119 kCondA, // >
120 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100121 // First and last aliases.
122 kCondFirst = kCondEQ,
123 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700124};
125
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000127 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000128 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000129 kAnalysisFailThrowCatchLoop,
130 kAnalysisFailAmbiguousArrayOp,
131 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000132};
133
Andreas Gampe9186ced2016-12-12 14:28:21 -0800134template <typename T>
135static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
136 return static_cast<typename std::make_unsigned<T>::type>(x);
137}
138
Vladimir Markof9f64412015-09-02 14:05:49 +0100139class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 public:
141 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
142
143 void AddInstruction(HInstruction* instruction);
144 void RemoveInstruction(HInstruction* instruction);
145
David Brazdilc3d743f2015-04-22 13:40:50 +0100146 // Insert `instruction` before/after an existing instruction `cursor`.
147 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
148 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
149
Roland Levillain6b469232014-09-25 10:10:38 +0100150 // Return true if this list contains `instruction`.
151 bool Contains(HInstruction* instruction) const;
152
Roland Levillainccc07a92014-09-16 14:48:16 +0100153 // Return true if `instruction1` is found before `instruction2` in
154 // this instruction list and false otherwise. Abort if none
155 // of these instructions is found.
156 bool FoundBefore(const HInstruction* instruction1,
157 const HInstruction* instruction2) const;
158
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000159 bool IsEmpty() const { return first_instruction_ == nullptr; }
160 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
161
162 // Update the block of all instructions to be `block`.
163 void SetBlockOfInstructions(HBasicBlock* block) const;
164
165 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000166 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000167 void Add(const HInstructionList& instruction_list);
168
David Brazdil2d7352b2015-04-20 14:52:42 +0100169 // Return the number of instructions in the list. This is an expensive operation.
170 size_t CountSize() const;
171
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100172 private:
173 HInstruction* first_instruction_;
174 HInstruction* last_instruction_;
175
176 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 friend class HGraph;
178 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000180 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100181 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182
183 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
184};
185
David Brazdil4833f5a2015-12-16 10:37:39 +0000186class ReferenceTypeInfo : ValueObject {
187 public:
188 typedef Handle<mirror::Class> TypeHandle;
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
191
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100193 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
194 }
195
Vladimir Markoa1de9182016-02-25 11:37:38 +0000196 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return ReferenceTypeInfo(type_handle, is_exact);
198 }
199
200 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
201
Vladimir Markof39745e2016-01-26 12:16:55 +0000202 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000203 return handle.GetReference() != nullptr;
204 }
205
Vladimir Marko456307a2016-04-19 14:12:13 +0000206 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 return IsValidHandle(type_handle_);
208 }
209
210 bool IsExact() const { return is_exact_; }
211
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000213 DCHECK(IsValid());
214 return GetTypeHandle()->IsObjectClass();
215 }
216
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000218 DCHECK(IsValid());
219 return GetTypeHandle()->IsStringClass();
220 }
221
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000223 DCHECK(IsValid());
224 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
225 }
226
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000228 DCHECK(IsValid());
229 return GetTypeHandle()->IsInterface();
230 }
231
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000233 DCHECK(IsValid());
234 return GetTypeHandle()->IsArrayClass();
235 }
236
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000238 DCHECK(IsValid());
239 return GetTypeHandle()->IsPrimitiveArray();
240 }
241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 DCHECK(IsValid());
244 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
245 }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 if (!IsExact()) return false;
250 if (!IsArrayClass()) return false;
251 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
252 }
253
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000255 DCHECK(IsValid());
256 if (!IsExact()) return false;
257 if (!IsArrayClass()) return false;
258 if (!rti.IsArrayClass()) return false;
259 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
260 rti.GetTypeHandle()->GetComponentType());
261 }
262
263 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
269 }
270
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700271 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000272 DCHECK(IsValid());
273 DCHECK(rti.IsValid());
274 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
275 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
276 }
277
278 // Returns true if the type information provide the same amount of details.
279 // Note that it does not mean that the instructions have the same actual type
280 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700281 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000282 if (!IsValid() && !rti.IsValid()) {
283 // Invalid types are equal.
284 return true;
285 }
286 if (!IsValid() || !rti.IsValid()) {
287 // One is valid, the other not.
288 return false;
289 }
290 return IsExact() == rti.IsExact()
291 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
292 }
293
294 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000295 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
296 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
297 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000298
299 // The class of the object.
300 TypeHandle type_handle_;
301 // Whether or not the type is exact or a superclass of the actual type.
302 // Whether or not we have any information about this type.
303 bool is_exact_;
304};
305
306std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
307
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100309class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100311 HGraph(ArenaAllocator* allocator,
312 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 const DexFile& dex_file,
314 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100316 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000318 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100320 : allocator_(allocator),
321 arena_stack_(arena_stack),
322 blocks_(allocator->Adapter(kArenaAllocBlockList)),
323 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
324 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 entry_block_(nullptr),
326 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100327 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100328 number_of_vregs_(0),
329 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000330 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400331 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000332 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700333 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800334 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000335 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000336 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000337 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100338 dex_file_(dex_file),
339 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100340 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800342 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000344 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100345 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
347 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
348 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000349 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100350 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000351 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700352 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100353 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100354 blocks_.reserve(kDefaultNumberOfBlocks);
355 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
David Brazdilbadd8262016-02-02 16:28:56 +0000357 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700358 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000359
Vladimir Markoca6fff82017-10-03 14:49:14 +0100360 ArenaAllocator* GetAllocator() const { return allocator_; }
361 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100362 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
363
David Brazdil69ba7b72015-06-23 18:27:30 +0100364 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000365 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100366
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000367 HBasicBlock* GetEntryBlock() const { return entry_block_; }
368 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100369 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000370
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
372 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000373
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000374 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100376 void ComputeDominanceInformation();
377 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000378 void ClearLoopInformation();
379 void FindBackEdges(ArenaBitVector* visited);
380 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100381 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100382 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // Analyze all natural loops in this graph. Returns a code specifying that it
385 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000386 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000387 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388
David Brazdilffee3d32015-07-06 11:48:53 +0100389 // Iterate over blocks to compute try block membership. Needs reverse post
390 // order and loop information.
391 void ComputeTryBlockInformation();
392
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000393 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000394 // Returns the instruction to replace the invoke expression or null if the
395 // invoke is for a void method. Note that the caller is responsible for replacing
396 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000397 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000398
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000399 // Update the loop and try membership of `block`, which was spawned from `reference`.
400 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
401 // should be the new back edge.
402 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
403 HBasicBlock* reference,
404 bool replace_if_back_edge);
405
Mingyao Yang3584bce2015-05-19 16:01:59 -0700406 // Need to add a couple of blocks to test if the loop body is entered and
407 // put deoptimization instructions, etc.
408 void TransformLoopHeaderForBCE(HBasicBlock* header);
409
Aart Bikf8f5a162017-02-06 15:35:29 -0800410 // Adds a new loop directly after the loop with the given header and exit.
411 // Returns the new preheader.
412 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
413 HBasicBlock* body,
414 HBasicBlock* exit);
415
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000416 // Removes `block` from the graph. Assumes `block` has been disconnected from
417 // other blocks and has no instructions or phis.
418 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000419
David Brazdilfc6a86a2015-06-26 10:33:45 +0000420 // Splits the edge between `block` and `successor` while preserving the
421 // indices in the predecessor/successor lists. If there are multiple edges
422 // between the blocks, the lowest indices are used.
423 // Returns the new block which is empty and has the same dex pc as `successor`.
424 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
425
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100427 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000428
429 // Transform a loop into a format with a single preheader.
430 //
431 // Each phi in the header should be split: original one in the header should only hold
432 // inputs reachable from the back edges and a single input from the preheader. The newly created
433 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
434 //
435 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
436 // that no longer have this property.
437 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
438
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100439 void SimplifyLoop(HBasicBlock* header);
440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000441 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100442 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000443 return current_instruction_id_++;
444 }
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t GetCurrentInstructionId() const {
447 return current_instruction_id_;
448 }
449
450 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100451 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000452 current_instruction_id_ = id;
453 }
454
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100455 uint16_t GetMaximumNumberOfOutVRegs() const {
456 return maximum_number_of_out_vregs_;
457 }
458
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
460 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100461 }
462
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100463 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
464 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
465 }
466
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000467 void UpdateTemporariesVRegSlots(size_t slots) {
468 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100469 }
470
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100472 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000473 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 }
475
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100476 void SetNumberOfVRegs(uint16_t number_of_vregs) {
477 number_of_vregs_ = number_of_vregs;
478 }
479
480 uint16_t GetNumberOfVRegs() const {
481 return number_of_vregs_;
482 }
483
484 void SetNumberOfInVRegs(uint16_t value) {
485 number_of_in_vregs_ = value;
486 }
487
David Brazdildee58d62016-04-07 09:54:26 +0000488 uint16_t GetNumberOfInVRegs() const {
489 return number_of_in_vregs_;
490 }
491
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100492 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100493 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100494 return number_of_vregs_ - number_of_in_vregs_;
495 }
496
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100497 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100499 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100500
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100501 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
502 DCHECK(GetReversePostOrder()[0] == entry_block_);
503 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
504 }
505
506 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
507 return ReverseRange(GetReversePostOrder());
508 }
509
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100511 return linear_order_;
512 }
513
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100514 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
515 return ReverseRange(GetLinearOrder());
516 }
517
Mark Mendell1152c922015-04-24 17:06:35 -0400518 bool HasBoundsChecks() const {
519 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800520 }
521
Mark Mendell1152c922015-04-24 17:06:35 -0400522 void SetHasBoundsChecks(bool value) {
523 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800524 }
525
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000526 bool IsDebuggable() const { return debuggable_; }
527
David Brazdil8d5b8b22015-03-24 10:51:52 +0000528 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 // already, it is created and inserted into the graph. This method is only for
530 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100531 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000532
533 // TODO: This is problematic for the consistency of reference type propagation
534 // because it can be created anytime after the pass and thus it will be left
535 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600536 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000537
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600538 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
539 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000540 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600541 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
542 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000543 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600544 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
545 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000546 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600547 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
548 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000549 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000550
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100551 HCurrentMethod* GetCurrentMethod();
552
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100553 const DexFile& GetDexFile() const {
554 return dex_file_;
555 }
556
557 uint32_t GetMethodIdx() const {
558 return method_idx_;
559 }
560
Igor Murashkind01745e2017-04-05 16:40:31 -0700561 // Get the method name (without the signature), e.g. "<init>"
562 const char* GetMethodName() const;
563
564 // Get the pretty method name (class + name + optionally signature).
565 std::string PrettyMethod(bool with_signature = true) const;
566
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100567 InvokeType GetInvokeType() const {
568 return invoke_type_;
569 }
570
Mark Mendellc4701932015-04-10 13:18:51 -0400571 InstructionSet GetInstructionSet() const {
572 return instruction_set_;
573 }
574
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000575 bool IsCompilingOsr() const { return osr_; }
576
Mingyao Yang063fc772016-08-02 11:02:54 -0700577 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
578 return cha_single_implementation_list_;
579 }
580
581 void AddCHASingleImplementationDependency(ArtMethod* method) {
582 cha_single_implementation_list_.insert(method);
583 }
584
585 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800586 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700587 }
588
David Brazdil77a48ae2015-09-15 12:34:04 +0000589 bool HasTryCatch() const { return has_try_catch_; }
590 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100591
Aart Bikb13c65b2017-03-21 20:14:07 -0700592 bool HasSIMD() const { return has_simd_; }
593 void SetHasSIMD(bool value) { has_simd_ = value; }
594
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800595 bool HasLoops() const { return has_loops_; }
596 void SetHasLoops(bool value) { has_loops_ = value; }
597
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000598 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
599 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
600
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100601 ArtMethod* GetArtMethod() const { return art_method_; }
602 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
603
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100604 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500605 // The instruction has been inserted into the graph, either as a constant, or
606 // before cursor.
607 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
608
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000609 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
610
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800611 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
612 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
613 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
614
David Brazdil2d7352b2015-04-20 14:52:42 +0100615 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000616 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100617 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 template <class InstructionType, typename ValueType>
620 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600621 ArenaSafeMap<ValueType, InstructionType*>* cache,
622 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000623 // Try to find an existing constant of the given value.
624 InstructionType* constant = nullptr;
625 auto cached_constant = cache->find(value);
626 if (cached_constant != cache->end()) {
627 constant = cached_constant->second;
628 }
629
630 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100631 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100633 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000634 cache->Overwrite(value, constant);
635 InsertConstant(constant);
636 }
637 return constant;
638 }
639
David Brazdil8d5b8b22015-03-24 10:51:52 +0000640 void InsertConstant(HConstant* instruction);
641
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 // Cache a float constant into the graph. This method should only be
643 // called by the SsaBuilder when creating "equivalent" instructions.
644 void CacheFloatConstant(HFloatConstant* constant);
645
646 // See CacheFloatConstant comment.
647 void CacheDoubleConstant(HDoubleConstant* constant);
648
Vladimir Markoca6fff82017-10-03 14:49:14 +0100649 ArenaAllocator* const allocator_;
650 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000651
652 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657
Aart Bik281c6812016-08-26 11:31:48 -0700658 // List of blocks to perform a linear order tree traversal. Unlike the reverse
659 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100660 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100661
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000662 HBasicBlock* entry_block_;
663 HBasicBlock* exit_block_;
664
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100665 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100666 uint16_t maximum_number_of_out_vregs_;
667
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100668 // The number of virtual registers in this method. Contains the parameters.
669 uint16_t number_of_vregs_;
670
671 // The number of virtual registers used by parameters of this method.
672 uint16_t number_of_in_vregs_;
673
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000674 // Number of vreg size slots that the temporaries use (used in baseline compiler).
675 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100676
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800677 // Flag whether there are bounds checks in the graph. We can skip
678 // BCE if it's false. It's only best effort to keep it up to date in
679 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400680 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800681
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800682 // Flag whether there are try/catch blocks in the graph. We will skip
683 // try/catch-related passes if it's false. It's only best effort to keep
684 // it up to date in the presence of code elimination so there might be
685 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000686 bool has_try_catch_;
687
Aart Bikb13c65b2017-03-21 20:14:07 -0700688 // Flag whether SIMD instructions appear in the graph. If true, the
689 // code generators may have to be more careful spilling the wider
690 // contents of SIMD registers.
691 bool has_simd_;
692
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800693 // Flag whether there are any loops in the graph. We can skip loop
694 // optimization if it's false. It's only best effort to keep it up
695 // to date in the presence of code elimination so there might be false
696 // positives.
697 bool has_loops_;
698
699 // Flag whether there are any irreducible loops in the graph. It's only
700 // best effort to keep it up to date in the presence of code elimination
701 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000702 bool has_irreducible_loops_;
703
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000704 // Indicates whether the graph should be compiled in a way that
705 // ensures full debuggability. If false, we can apply more
706 // aggressive optimizations that may limit the level of debugging.
707 const bool debuggable_;
708
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000710 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000711
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100712 // The dex file from which the method is from.
713 const DexFile& dex_file_;
714
715 // The method index in the dex file.
716 const uint32_t method_idx_;
717
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100718 // If inlined, this encodes how the callee is being invoked.
719 const InvokeType invoke_type_;
720
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100721 // Whether the graph has been transformed to SSA form. Only used
722 // in debug mode to ensure we are not using properties only valid
723 // for non-SSA form (like the number of temporaries).
724 bool in_ssa_form_;
725
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800726 // Number of CHA guards in the graph. Used to short-circuit the
727 // CHA guard optimization pass when there is no CHA guard left.
728 uint32_t number_of_cha_guards_;
729
Mathieu Chartiere401d142015-04-22 13:56:20 -0700730 const InstructionSet instruction_set_;
731
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000732 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000733 HNullConstant* cached_null_constant_;
734 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000736 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000737 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000738
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100739 HCurrentMethod* cached_current_method_;
740
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100741 // The ArtMethod this graph is for. Note that for AOT, it may be null,
742 // for example for methods whose declaring class could not be resolved
743 // (such as when the superclass could not be found).
744 ArtMethod* art_method_;
745
David Brazdil4833f5a2015-12-16 10:37:39 +0000746 // Keep the RTI of inexact Object to avoid having to pass stack handle
747 // collection pointer to passes which may create NullConstant.
748 ReferenceTypeInfo inexact_object_rti_;
749
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000750 // Whether we are compiling this graph for on stack replacement: this will
751 // make all loops seen as irreducible and emit special stack maps to mark
752 // compiled code entries which the interpreter can directly jump to.
753 const bool osr_;
754
Mingyao Yang063fc772016-08-02 11:02:54 -0700755 // List of methods that are assumed to have single implementation.
756 ArenaSet<ArtMethod*> cha_single_implementation_list_;
757
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000758 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100759 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000760 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000761 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762 DISALLOW_COPY_AND_ASSIGN(HGraph);
763};
764
Vladimir Markof9f64412015-09-02 14:05:49 +0100765class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000766 public:
767 HLoopInformation(HBasicBlock* header, HGraph* graph)
768 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100769 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000770 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100771 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100772 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100773 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100774 blocks_(graph->GetAllocator(),
775 graph->GetBlocks().size(),
776 true,
777 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778 back_edges_.reserve(kDefaultNumberOfBackEdges);
779 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100782 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000783
784 void Dump(std::ostream& os);
785
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 HBasicBlock* GetHeader() const {
787 return header_;
788 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000789
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000790 void SetHeader(HBasicBlock* block) {
791 header_ = block;
792 }
793
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100794 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
795 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
796 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
797
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100799 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000800 }
801
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 }
805
David Brazdil46e2a392015-03-16 17:31:52 +0000806 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100807 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 }
809
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000810 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100811 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000812 }
813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100814 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100816 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100818 }
819
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100820 // Returns the lifetime position of the back edge that has the
821 // greatest lifetime position.
822 size_t GetLifetimeEnd() const;
823
824 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100825 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100826 }
827
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000828 // Finds blocks that are part of this loop.
829 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100830
Artem Serov7f4aff62017-06-21 17:02:18 +0100831 // Updates blocks population of the loop and all of its outer' ones recursively after the
832 // population of the inner loop is updated.
833 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
834
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100835 // Returns whether this loop information contains `block`.
836 // Note that this loop information *must* be populated before entering this function.
837 bool Contains(const HBasicBlock& block) const;
838
839 // Returns whether this loop information is an inner loop of `other`.
840 // Note that `other` *must* be populated before entering this function.
841 bool IsIn(const HLoopInformation& other) const;
842
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800843 // Returns true if instruction is not defined within this loop.
844 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700845
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100846 const ArenaBitVector& GetBlocks() const { return blocks_; }
847
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000849 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000851 void ClearAllBlocks() {
852 blocks_.ClearAllBits();
853 }
854
David Brazdil3f4a5222016-05-06 12:46:21 +0100855 bool HasBackEdgeNotDominatedByHeader() const;
856
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100857 bool IsPopulated() const {
858 return blocks_.GetHighestBitSet() != -1;
859 }
860
Anton Shaminf89381f2016-05-16 16:44:13 +0600861 bool DominatesAllBackEdges(HBasicBlock* block);
862
David Sehrc757dec2016-11-04 15:48:34 -0700863 bool HasExitEdge() const;
864
Artem Serov7f4aff62017-06-21 17:02:18 +0100865 // Resets back edge and blocks-in-loop data.
866 void ResetBasicBlockData() {
867 back_edges_.clear();
868 ClearAllBlocks();
869 }
870
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872 // Internal recursive implementation of `Populate`.
873 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100874 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100875
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000876 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100877 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000878 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100879 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100880 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100881 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000882
883 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
884};
885
David Brazdilec16f792015-08-19 15:04:01 +0100886// Stores try/catch information for basic blocks.
887// Note that HGraph is constructed so that catch blocks cannot simultaneously
888// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100889class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100890 public:
891 // Try block information constructor.
892 explicit TryCatchInformation(const HTryBoundary& try_entry)
893 : try_entry_(&try_entry),
894 catch_dex_file_(nullptr),
895 catch_type_index_(DexFile::kDexNoIndex16) {
896 DCHECK(try_entry_ != nullptr);
897 }
898
899 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800900 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100901 : try_entry_(nullptr),
902 catch_dex_file_(&dex_file),
903 catch_type_index_(catch_type_index) {}
904
905 bool IsTryBlock() const { return try_entry_ != nullptr; }
906
907 const HTryBoundary& GetTryEntry() const {
908 DCHECK(IsTryBlock());
909 return *try_entry_;
910 }
911
912 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
913
914 bool IsCatchAllTypeIndex() const {
915 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800916 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100917 }
918
Andreas Gampea5b09a62016-11-17 15:21:22 -0800919 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100920 DCHECK(IsCatchBlock());
921 return catch_type_index_;
922 }
923
924 const DexFile& GetCatchDexFile() const {
925 DCHECK(IsCatchBlock());
926 return *catch_dex_file_;
927 }
928
929 private:
930 // One of possibly several TryBoundary instructions entering the block's try.
931 // Only set for try blocks.
932 const HTryBoundary* try_entry_;
933
934 // Exception type information. Only set for catch blocks.
935 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800936 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100937};
938
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100940static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100941
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942// A block in a method. Contains the list of instructions represented
943// as a double linked list. Each block knows its predecessors and
944// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100945
Vladimir Markof9f64412015-09-02 14:05:49 +0100946class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700948 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100950 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
951 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000952 loop_information_(nullptr),
953 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100954 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100955 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100956 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100957 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000958 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000959 try_catch_information_(nullptr) {
960 predecessors_.reserve(kDefaultNumberOfPredecessors);
961 successors_.reserve(kDefaultNumberOfSuccessors);
962 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
963 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964
Vladimir Marko60584552015-09-03 13:35:12 +0000965 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 }
968
Vladimir Marko60584552015-09-03 13:35:12 +0000969 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100970 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971 }
972
David Brazdild26a4112015-11-10 11:07:31 +0000973 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
974 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
975
Vladimir Marko60584552015-09-03 13:35:12 +0000976 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
977 return ContainsElement(successors_, block, start_from);
978 }
979
980 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100981 return dominated_blocks_;
982 }
983
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100984 bool IsEntryBlock() const {
985 return graph_->GetEntryBlock() == this;
986 }
987
988 bool IsExitBlock() const {
989 return graph_->GetExitBlock() == this;
990 }
991
David Brazdil46e2a392015-03-16 17:31:52 +0000992 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200993 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700994 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000995 bool IsSingleTryBoundary() const;
996
997 // Returns true if this block emits nothing but a jump.
998 bool IsSingleJump() const {
999 HLoopInformation* loop_info = GetLoopInformation();
1000 return (IsSingleGoto() || IsSingleTryBoundary())
1001 // Back edges generate a suspend check.
1002 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1003 }
David Brazdil46e2a392015-03-16 17:31:52 +00001004
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001005 void AddBackEdge(HBasicBlock* back_edge) {
1006 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001007 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001008 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001009 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001010 loop_information_->AddBackEdge(back_edge);
1011 }
1012
Artem Serov7f4aff62017-06-21 17:02:18 +01001013 // Registers a back edge; if the block was not a loop header before the call associates a newly
1014 // created loop info with it.
1015 //
1016 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1017 // info for all blocks during back edges recalculation.
1018 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1019 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1020 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1021 }
1022 loop_information_->AddBackEdge(back_edge);
1023 }
1024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001025 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001028 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001029 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001030 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001032 HBasicBlock* GetDominator() const { return dominator_; }
1033 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001034 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1035
1036 void RemoveDominatedBlock(HBasicBlock* block) {
1037 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001038 }
Vladimir Marko60584552015-09-03 13:35:12 +00001039
1040 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1041 ReplaceElement(dominated_blocks_, existing, new_block);
1042 }
1043
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001044 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001045
1046 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001047 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001048 }
1049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001050 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1051 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001052 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001054 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1055 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001056
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001057 HInstruction* GetFirstInstructionDisregardMoves() const;
1058
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001060 successors_.push_back(block);
1061 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062 }
1063
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001064 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1065 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001066 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001067 new_block->predecessors_.push_back(this);
1068 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001069 }
1070
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1072 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001073 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001074 new_block->successors_.push_back(this);
1075 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001076 }
1077
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001078 // Insert `this` between `predecessor` and `successor. This method
1079 // preserves the indicies, and will update the first edge found between
1080 // `predecessor` and `successor`.
1081 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1082 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001083 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001084 successor->predecessors_[predecessor_index] = this;
1085 predecessor->successors_[successor_index] = this;
1086 successors_.push_back(successor);
1087 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001088 }
1089
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001090 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001091 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001092 }
1093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001095 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 }
1097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001099 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 }
1101
1102 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001103 predecessors_.push_back(block);
1104 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 }
1106
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001107 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001108 DCHECK_EQ(predecessors_.size(), 2u);
1109 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001110 }
1111
David Brazdil769c9e52015-04-27 13:54:09 +01001112 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001113 DCHECK_EQ(successors_.size(), 2u);
1114 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001115 }
1116
David Brazdilfc6a86a2015-06-26 10:33:45 +00001117 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001118 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001119 }
1120
David Brazdilfc6a86a2015-06-26 10:33:45 +00001121 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001122 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001123 }
1124
David Brazdilfc6a86a2015-06-26 10:33:45 +00001125 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001126 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001127 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001128 }
1129
1130 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001131 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001132 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001133 }
1134
1135 // Returns whether the first occurrence of `predecessor` in the list of
1136 // predecessors is at index `idx`.
1137 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001138 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001139 return GetPredecessorIndexOf(predecessor) == idx;
1140 }
1141
David Brazdild7558da2015-09-22 13:04:14 +01001142 // Create a new block between this block and its predecessors. The new block
1143 // is added to the graph, all predecessor edges are relinked to it and an edge
1144 // is created to `this`. Returns the new empty block. Reverse post order or
1145 // loop and try/catch information are not updated.
1146 HBasicBlock* CreateImmediateDominator();
1147
David Brazdilfc6a86a2015-06-26 10:33:45 +00001148 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001149 // created, latter block. Note that this method will add the block to the
1150 // graph, create a Goto at the end of the former block and will create an edge
1151 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001152 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001153 HBasicBlock* SplitBefore(HInstruction* cursor);
1154
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001155 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001156 // created block. Note that this method just updates raw block information,
1157 // like predecessors, successors, dominators, and instruction list. It does not
1158 // update the graph, reverse post order, loop information, nor make sure the
1159 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001160 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1161
1162 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1163 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001164
1165 // Merge `other` at the end of `this`. Successors and dominated blocks of
1166 // `other` are changed to be successors and dominated blocks of `this`. Note
1167 // that this method does not update the graph, reverse post order, loop
1168 // information, nor make sure the blocks are consistent (for example ending
1169 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001170 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001171
1172 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1173 // of `this` are moved to `other`.
1174 // Note that this method does not update the graph, reverse post order, loop
1175 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001176 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001177 void ReplaceWith(HBasicBlock* other);
1178
Aart Bik6b69e0a2017-01-11 10:20:43 -08001179 // Merges the instructions of `other` at the end of `this`.
1180 void MergeInstructionsWith(HBasicBlock* other);
1181
David Brazdil2d7352b2015-04-20 14:52:42 +01001182 // Merge `other` at the end of `this`. This method updates loops, reverse post
1183 // order, links to predecessors, successors, dominators and deletes the block
1184 // from the graph. The two blocks must be successive, i.e. `this` the only
1185 // predecessor of `other` and vice versa.
1186 void MergeWith(HBasicBlock* other);
1187
1188 // Disconnects `this` from all its predecessors, successors and dominator,
1189 // removes it from all loops it is included in and eventually from the graph.
1190 // The block must not dominate any other block. Predecessors and successors
1191 // are safely updated.
1192 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001193
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001194 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001195 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001196 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001197 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001198 // Replace phi `initial` with `replacement` within this block.
1199 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001200 // Replace instruction `initial` with `replacement` within this block.
1201 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1202 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001203 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001204 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001205 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1206 // instruction list. With 'ensure_safety' set to true, it verifies that the
1207 // instruction is not in use and removes it from the use lists of its inputs.
1208 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1209 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001210 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001211
1212 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001213 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214 }
1215
Roland Levillain6b879dd2014-09-22 17:13:44 +01001216 bool IsLoopPreHeaderFirstPredecessor() const {
1217 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001218 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001219 }
1220
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001221 bool IsFirstPredecessorBackEdge() const {
1222 DCHECK(IsLoopHeader());
1223 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1224 }
1225
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226 HLoopInformation* GetLoopInformation() const {
1227 return loop_information_;
1228 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001229
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 void SetInLoop(HLoopInformation* info) {
1234 if (IsLoopHeader()) {
1235 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001236 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001237 loop_information_ = info;
1238 } else if (loop_information_->Contains(*info->GetHeader())) {
1239 // Block is currently part of an outer loop. Make it part of this inner loop.
1240 // Note that a non loop header having a loop information means this loop information
1241 // has already been populated
1242 loop_information_ = info;
1243 } else {
1244 // Block is part of an inner loop. Do not update the loop information.
1245 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1246 // at this point, because this method is being called while populating `info`.
1247 }
1248 }
1249
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001250 // Raw update of the loop information.
1251 void SetLoopInformation(HLoopInformation* info) {
1252 loop_information_ = info;
1253 }
1254
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001255 bool IsInLoop() const { return loop_information_ != nullptr; }
1256
David Brazdilec16f792015-08-19 15:04:01 +01001257 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1258
1259 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1260 try_catch_information_ = try_catch_information;
1261 }
1262
1263 bool IsTryBlock() const {
1264 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1265 }
1266
1267 bool IsCatchBlock() const {
1268 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1269 }
David Brazdilffee3d32015-07-06 11:48:53 +01001270
1271 // Returns the try entry that this block's successors should have. They will
1272 // be in the same try, unless the block ends in a try boundary. In that case,
1273 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001274 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001275
Aart Bik75ff2c92018-04-21 01:28:11 +00001276 bool HasThrowingInstructions() const;
1277
David Brazdila4b8c212015-05-07 09:59:30 +01001278 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001279 bool Dominates(HBasicBlock* block) const;
1280
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001281 size_t GetLifetimeStart() const { return lifetime_start_; }
1282 size_t GetLifetimeEnd() const { return lifetime_end_; }
1283
1284 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1285 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1286
David Brazdil8d5b8b22015-03-24 10:51:52 +00001287 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001288 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001289 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001290 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001291 bool HasSinglePhi() const;
1292
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001293 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001294 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001295 ArenaVector<HBasicBlock*> predecessors_;
1296 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001297 HInstructionList instructions_;
1298 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001299 HLoopInformation* loop_information_;
1300 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001301 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001302 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001303 // The dex program counter of the first instruction of this block.
1304 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001305 size_t lifetime_start_;
1306 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001307 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001308
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001309 friend class HGraph;
1310 friend class HInstruction;
1311
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001312 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1313};
1314
David Brazdilb2bd1c52015-03-25 11:17:37 +00001315// Iterates over the LoopInformation of all loops which contain 'block'
1316// from the innermost to the outermost.
1317class HLoopInformationOutwardIterator : public ValueObject {
1318 public:
1319 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1320 : current_(block.GetLoopInformation()) {}
1321
1322 bool Done() const { return current_ == nullptr; }
1323
1324 void Advance() {
1325 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001326 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001327 }
1328
1329 HLoopInformation* Current() const {
1330 DCHECK(!Done());
1331 return current_;
1332 }
1333
1334 private:
1335 HLoopInformation* current_;
1336
1337 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1338};
1339
Alexandre Ramesef20f712015-06-09 10:29:30 +01001340#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001341 M(Above, Condition) \
1342 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001343 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001345 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(ArrayGet, Instruction) \
1347 M(ArrayLength, Instruction) \
1348 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001349 M(Below, Condition) \
1350 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001351 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001353 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001354 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001355 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001356 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001357 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001358 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001359 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001360 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001361 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001362 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001364 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001367 M(Exit, Instruction) \
1368 M(FloatConstant, Constant) \
1369 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(GreaterThan, Condition) \
1371 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001373 M(InstanceFieldGet, Instruction) \
1374 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001375 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001377 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001378 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001379 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001380 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001381 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001382 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001383 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001384 M(LessThan, Condition) \
1385 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001386 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001387 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001388 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001389 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001390 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001391 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001392 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001393 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001394 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001395 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001397 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001398 M(Neg, UnaryOperation) \
1399 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001400 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001401 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001402 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001403 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001404 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001405 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001406 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001407 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001408 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001409 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001410 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001411 M(Return, Instruction) \
1412 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001413 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001414 M(Shl, BinaryOperation) \
1415 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001416 M(StaticFieldGet, Instruction) \
1417 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001418 M(UnresolvedInstanceFieldGet, Instruction) \
1419 M(UnresolvedInstanceFieldSet, Instruction) \
1420 M(UnresolvedStaticFieldGet, Instruction) \
1421 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001422 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001424 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001425 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001426 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001427 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001428 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001429 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001430 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001431 M(VecExtractScalar, VecUnaryOperation) \
1432 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001433 M(VecCnv, VecUnaryOperation) \
1434 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001435 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001436 M(VecNot, VecUnaryOperation) \
1437 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001438 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001439 M(VecSub, VecBinaryOperation) \
1440 M(VecMul, VecBinaryOperation) \
1441 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001442 M(VecMin, VecBinaryOperation) \
1443 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001444 M(VecAnd, VecBinaryOperation) \
1445 M(VecAndNot, VecBinaryOperation) \
1446 M(VecOr, VecBinaryOperation) \
1447 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001448 M(VecSaturationAdd, VecBinaryOperation) \
1449 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001450 M(VecShl, VecBinaryOperation) \
1451 M(VecShr, VecBinaryOperation) \
1452 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001453 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001454 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001455 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001456 M(VecLoad, VecMemoryOperation) \
1457 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001458
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001459/*
1460 * Instructions, shared across several (not all) architectures.
1461 */
1462#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1463#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1464#else
1465#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001466 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001467 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001468 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001469 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001470#endif
1471
Alexandre Ramesef20f712015-06-09 10:29:30 +01001472#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1473
1474#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1475
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001476#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001477#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001478#else
1479#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1480 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001481 M(MipsPackedSwitch, Instruction) \
1482 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001483#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001484
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001485#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1486
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001487#ifndef ART_ENABLE_CODEGEN_x86
1488#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1489#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001490#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1491 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001492 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001493 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001494 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001495#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001496
1497#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1498
1499#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1500 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001501 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001502 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1503 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001504 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001505 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001506 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1507 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1508
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001509#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1510 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001511 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001512 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001513 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001514 M(Invoke, Instruction) \
1515 M(VecOperation, Instruction) \
1516 M(VecUnaryOperation, VecOperation) \
1517 M(VecBinaryOperation, VecOperation) \
1518 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001519
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001520#define FOR_EACH_INSTRUCTION(M) \
1521 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1522 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1523
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001524#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001525FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1526#undef FORWARD_DECLARATION
1527
Vladimir Marko372f10e2016-05-17 16:30:10 +01001528#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001529 private: \
1530 H##type& operator=(const H##type&) = delete; \
1531 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001532 const char* DebugName() const override { return #type; } \
1533 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001534 DCHECK(IsClonable()); \
1535 return new (arena) H##type(*this->As##type()); \
1536 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001537 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001538
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001539#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001540 private: \
1541 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001542 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001543
Artem Serovcced8ba2017-07-19 18:18:09 +01001544#define DEFAULT_COPY_CONSTRUCTOR(type) \
1545 explicit H##type(const H##type& other) = default;
1546
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001547template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001548class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1549 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001550 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001551 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001552 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001553 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001554 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001555 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001556 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001557
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001558 private:
David Brazdiled596192015-01-23 10:39:45 +00001559 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001560 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001561
1562 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001563 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001564
Vladimir Marko46817b82016-03-29 12:21:58 +01001565 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001566
1567 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1568};
1569
David Brazdiled596192015-01-23 10:39:45 +00001570template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001571using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001572
David Brazdil1abb4192015-02-17 18:33:36 +00001573// This class is used by HEnvironment and HInstruction classes to record the
1574// instructions they use and pointers to the corresponding HUseListNodes kept
1575// by the used instructions.
1576template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001577class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001578 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001579 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1580 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001581
Vladimir Marko46817b82016-03-29 12:21:58 +01001582 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1583 : HUserRecord(old_record.instruction_, before_use_node) {}
1584 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1585 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001586 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001587 }
1588
1589 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001590 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1591 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001592
1593 private:
1594 // Instruction used by the user.
1595 HInstruction* instruction_;
1596
Vladimir Marko46817b82016-03-29 12:21:58 +01001597 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1598 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001599};
1600
Vladimir Markoe9004912016-06-16 16:50:52 +01001601// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1602// This is used for HInstruction::GetInputs() to return a container wrapper providing
1603// HInstruction* values even though the underlying container has HUserRecord<>s.
1604struct HInputExtractor {
1605 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1606 return record.GetInstruction();
1607 }
1608 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1609 return record.GetInstruction();
1610 }
1611};
1612
1613using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1614using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1615
Aart Bik854a02b2015-07-14 16:07:00 -07001616/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001618 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001619 * For write/read dependences on fields/arrays, the dependence analysis uses
1620 * type disambiguation (e.g. a float field write cannot modify the value of an
1621 * integer field read) and the access type (e.g. a reference array write cannot
1622 * modify the value of a reference field read [although it may modify the
1623 * reference fetch prior to reading the field, which is represented by its own
1624 * write/read dependence]). The analysis makes conservative points-to
1625 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1626 * the same, and any reference read depends on any reference read without
1627 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001628 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001629 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1630 * alive across the point where garbage collection might happen.
1631 *
1632 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1633 *
1634 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1635 * those instructions from the compiler perspective (between this instruction and the next one
1636 * in the IR).
1637 *
1638 * Note: Instructions which can cause GC only on a fatal slow path do not need
1639 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1640 * one. However the execution may return to compiled code if there is a catch block in the
1641 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1642 * set.
1643 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001644 * The internal representation uses 38-bit and is described in the table below.
1645 * The first line indicates the side effect, and for field/array accesses the
1646 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001647 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001648 * The two numbered lines below indicate the bit position in the bitfield (read
1649 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001650 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001651 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1652 * +-------------+---------+---------+--------------+---------+---------+
1653 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1654 * | 3 |333333322|222222221| 1 |111111110|000000000|
1655 * | 7 |654321098|765432109| 8 |765432109|876543210|
1656 *
1657 * Note that, to ease the implementation, 'changes' bits are least significant
1658 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001659 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001660class SideEffects : public ValueObject {
1661 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001662 SideEffects() : flags_(0) {}
1663
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001664 static SideEffects None() {
1665 return SideEffects(0);
1666 }
1667
1668 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001669 return SideEffects(kAllChangeBits | kAllDependOnBits);
1670 }
1671
1672 static SideEffects AllChanges() {
1673 return SideEffects(kAllChangeBits);
1674 }
1675
1676 static SideEffects AllDependencies() {
1677 return SideEffects(kAllDependOnBits);
1678 }
1679
1680 static SideEffects AllExceptGCDependency() {
1681 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1682 }
1683
1684 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001685 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686 }
1687
Aart Bik34c3ba92015-07-20 14:08:59 -07001688 static SideEffects AllWrites() {
1689 return SideEffects(kAllWrites);
1690 }
1691
1692 static SideEffects AllReads() {
1693 return SideEffects(kAllReads);
1694 }
1695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001697 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001699 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001700 }
1701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001702 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001703 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001704 }
1705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001706 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001707 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001708 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001709 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001710 }
1711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001712 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001713 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001714 }
1715
Artem Serovd1aa7d02018-06-22 11:35:46 +01001716 // Returns whether GC might happen across this instruction from the compiler perspective so
1717 // the next instruction in the IR would see that.
1718 //
1719 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001720 static SideEffects CanTriggerGC() {
1721 return SideEffects(1ULL << kCanTriggerGCBit);
1722 }
1723
Artem Serovd1aa7d02018-06-22 11:35:46 +01001724 // Returns whether the instruction must not be alive across a GC point.
1725 //
1726 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001727 static SideEffects DependsOnGC() {
1728 return SideEffects(1ULL << kDependsOnGCBit);
1729 }
1730
Aart Bik854a02b2015-07-14 16:07:00 -07001731 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001732 SideEffects Union(SideEffects other) const {
1733 return SideEffects(flags_ | other.flags_);
1734 }
1735
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001736 SideEffects Exclusion(SideEffects other) const {
1737 return SideEffects(flags_ & ~other.flags_);
1738 }
1739
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001740 void Add(SideEffects other) {
1741 flags_ |= other.flags_;
1742 }
1743
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001744 bool Includes(SideEffects other) const {
1745 return (other.flags_ & flags_) == other.flags_;
1746 }
1747
1748 bool HasSideEffects() const {
1749 return (flags_ & kAllChangeBits);
1750 }
1751
1752 bool HasDependencies() const {
1753 return (flags_ & kAllDependOnBits);
1754 }
1755
1756 // Returns true if there are no side effects or dependencies.
1757 bool DoesNothing() const {
1758 return flags_ == 0;
1759 }
1760
Aart Bik854a02b2015-07-14 16:07:00 -07001761 // Returns true if something is written.
1762 bool DoesAnyWrite() const {
1763 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001764 }
1765
Aart Bik854a02b2015-07-14 16:07:00 -07001766 // Returns true if something is read.
1767 bool DoesAnyRead() const {
1768 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001769 }
1770
Aart Bik854a02b2015-07-14 16:07:00 -07001771 // Returns true if potentially everything is written and read
1772 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001773 bool DoesAllReadWrite() const {
1774 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1775 }
1776
Aart Bik854a02b2015-07-14 16:07:00 -07001777 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001778 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001779 }
1780
Roland Levillain0d5a2812015-11-13 10:07:31 +00001781 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001782 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001783 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1784 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001785 }
1786
1787 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001788 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001789 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001790 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 for (int s = kLastBit; s >= 0; s--) {
1792 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1793 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1794 // This is a bit for the GC side effect.
1795 if (current_bit_is_set) {
1796 flags += "GC";
1797 }
Aart Bik854a02b2015-07-14 16:07:00 -07001798 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001799 } else {
1800 // This is a bit for the array/field analysis.
1801 // The underscore character stands for the 'can trigger GC' bit.
1802 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1803 if (current_bit_is_set) {
1804 flags += kDebug[s];
1805 }
1806 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1807 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1808 flags += "|";
1809 }
1810 }
Aart Bik854a02b2015-07-14 16:07:00 -07001811 }
1812 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001813 }
1814
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001815 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001816
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001817 private:
1818 static constexpr int kFieldArrayAnalysisBits = 9;
1819
1820 static constexpr int kFieldWriteOffset = 0;
1821 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1822 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1823 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1824
1825 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1826
1827 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1828 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1829 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1830 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1831
1832 static constexpr int kLastBit = kDependsOnGCBit;
1833 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1834
1835 // Aliases.
1836
1837 static_assert(kChangeBits == kDependOnBits,
1838 "the 'change' bits should match the 'depend on' bits.");
1839
1840 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1841 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1842 static constexpr uint64_t kAllWrites =
1843 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1844 static constexpr uint64_t kAllReads =
1845 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001846
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001847 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001848 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001849 int shift;
1850 switch (type) {
1851 case DataType::Type::kReference: shift = 0; break;
1852 case DataType::Type::kBool: shift = 1; break;
1853 case DataType::Type::kInt8: shift = 2; break;
1854 case DataType::Type::kUint16: shift = 3; break;
1855 case DataType::Type::kInt16: shift = 4; break;
1856 case DataType::Type::kInt32: shift = 5; break;
1857 case DataType::Type::kInt64: shift = 6; break;
1858 case DataType::Type::kFloat32: shift = 7; break;
1859 case DataType::Type::kFloat64: shift = 8; break;
1860 default:
1861 LOG(FATAL) << "Unexpected data type " << type;
1862 UNREACHABLE();
1863 }
Aart Bik854a02b2015-07-14 16:07:00 -07001864 DCHECK_LE(kFieldWriteOffset, shift);
1865 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001866 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001867 }
1868
1869 // Private constructor on direct flags value.
1870 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1871
1872 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001873};
1874
David Brazdiled596192015-01-23 10:39:45 +00001875// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001876class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001877 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001878 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001879 size_t number_of_vregs,
1880 ArtMethod* method,
1881 uint32_t dex_pc,
1882 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001883 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1884 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001885 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001886 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001887 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001888 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001889 }
1890
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001891 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1892 const HEnvironment& to_copy,
1893 HInstruction* holder)
1894 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001895 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001896 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001897 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001898 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001899
Vladimir Markoec32f642017-06-02 10:51:55 +01001900 void AllocateLocations() {
1901 DCHECK(locations_.empty());
1902 locations_.resize(vregs_.size());
1903 }
1904
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001905 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001906 if (parent_ != nullptr) {
1907 parent_->SetAndCopyParentChain(allocator, parent);
1908 } else {
1909 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1910 parent_->CopyFrom(parent);
1911 if (parent->GetParent() != nullptr) {
1912 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1913 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001914 }
David Brazdiled596192015-01-23 10:39:45 +00001915 }
1916
Vladimir Marko69d310e2017-10-09 14:12:23 +01001917 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001918 void CopyFrom(HEnvironment* environment);
1919
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001920 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1921 // input to the loop phi instead. This is for inserting instructions that
1922 // require an environment (like HDeoptimization) in the loop pre-header.
1923 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001924
1925 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001926 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001927 }
1928
1929 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001930 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001931 }
1932
David Brazdil1abb4192015-02-17 18:33:36 +00001933 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001934
Artem Serovca210e32017-12-15 13:43:20 +00001935 // Replaces the input at the position 'index' with the replacement; the replacement and old
1936 // input instructions' env_uses_ lists are adjusted. The function works similar to
1937 // HInstruction::ReplaceInput.
1938 void ReplaceInput(HInstruction* replacement, size_t index);
1939
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001940 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001941
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001942 HEnvironment* GetParent() const { return parent_; }
1943
1944 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001945 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001946 }
1947
1948 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001949 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001950 }
1951
1952 uint32_t GetDexPc() const {
1953 return dex_pc_;
1954 }
1955
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001956 ArtMethod* GetMethod() const {
1957 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001958 }
1959
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001960 HInstruction* GetHolder() const {
1961 return holder_;
1962 }
1963
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001964
1965 bool IsFromInlinedInvoke() const {
1966 return GetParent() != nullptr;
1967 }
1968
David Brazdiled596192015-01-23 10:39:45 +00001969 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001970 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1971 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001972 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001973 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001974 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001975
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001976 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001977 HInstruction* const holder_;
1978
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001979 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001980
1981 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1982};
1983
Vladimir Markof9f64412015-09-02 14:05:49 +01001984class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001985 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301986#define DECLARE_KIND(type, super) k##type,
1987 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001988 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301989 kLastInstructionKind
1990 };
1991#undef DECLARE_KIND
1992
1993 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001994 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1995
1996 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001997 : previous_(nullptr),
1998 next_(nullptr),
1999 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002000 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002001 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002002 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002003 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002004 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002005 locations_(nullptr),
2006 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002007 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002008 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002009 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302010 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002011 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002012 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2013 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002014
Dave Allison20dfc792014-06-16 20:44:29 -07002015 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002016
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002017
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002018 HInstruction* GetNext() const { return next_; }
2019 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020
Calin Juravle77520bc2015-01-12 18:45:46 +00002021 HInstruction* GetNextDisregardingMoves() const;
2022 HInstruction* GetPreviousDisregardingMoves() const;
2023
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002024 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002025 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002026 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002027 bool IsInBlock() const { return block_ != nullptr; }
2028 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002029 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2030 bool IsIrreducibleLoopHeaderPhi() const {
2031 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2032 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002033
Vladimir Marko372f10e2016-05-17 16:30:10 +01002034 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2035
2036 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2037 // One virtual method is enough, just const_cast<> and then re-add the const.
2038 return ArrayRef<const HUserRecord<HInstruction*>>(
2039 const_cast<HInstruction*>(this)->GetInputRecords());
2040 }
2041
Vladimir Markoe9004912016-06-16 16:50:52 +01002042 HInputsRef GetInputs() {
2043 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002044 }
2045
Vladimir Markoe9004912016-06-16 16:50:52 +01002046 HConstInputsRef GetInputs() const {
2047 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002048 }
2049
2050 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002051 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002052
Aart Bik2767f4b2016-10-28 15:03:53 -07002053 bool HasInput(HInstruction* input) const {
2054 for (const HInstruction* i : GetInputs()) {
2055 if (i == input) {
2056 return true;
2057 }
2058 }
2059 return false;
2060 }
2061
Vladimir Marko372f10e2016-05-17 16:30:10 +01002062 void SetRawInputAt(size_t index, HInstruction* input) {
2063 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2064 }
2065
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002066 virtual void Accept(HGraphVisitor* visitor) = 0;
2067 virtual const char* DebugName() const = 0;
2068
Vladimir Markobd785672018-05-03 17:09:09 +01002069 DataType::Type GetType() const {
2070 return TypeField::Decode(GetPackedFields());
2071 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002072
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002073 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002074
2075 uint32_t GetDexPc() const { return dex_pc_; }
2076
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002077 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002078
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002079 // Can the instruction throw?
2080 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2081 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002082 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002083
2084 // Does the instruction always throw an exception unconditionally?
2085 virtual bool AlwaysThrows() const { return false; }
2086
David Brazdilec16f792015-08-19 15:04:01 +01002087 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002088
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002089 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002090 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002091
Calin Juravle10e244f2015-01-26 18:54:32 +00002092 // Does not apply for all instructions, but having this at top level greatly
2093 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002094 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002095 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002096 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002097 return true;
2098 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002099
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002100 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002101 return false;
2102 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002103
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002104 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2105 // with it. Otherwise return null.
2106 HNullCheck* GetImplicitNullCheck() const {
2107 // Find the first previous instruction which is not a move.
2108 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2109 if (first_prev_not_move != nullptr &&
2110 first_prev_not_move->IsNullCheck() &&
2111 first_prev_not_move->IsEmittedAtUseSite()) {
2112 return first_prev_not_move->AsNullCheck();
2113 }
2114 return nullptr;
2115 }
2116
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002117 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002118 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002119 }
2120
Calin Juravle2e768302015-07-28 14:41:11 +00002121 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002122
Calin Juravle61d544b2015-02-23 16:46:57 +00002123 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002125 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002126 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002127 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002128
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002129 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002130 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002131 // Note: fixup_end remains valid across push_front().
2132 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2133 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002134 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002135 uses_.push_front(*new_node);
2136 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002137 }
2138
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002139 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002140 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002141 // Note: env_fixup_end remains valid across push_front().
2142 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2143 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002144 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002145 env_uses_.push_front(*new_node);
2146 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002147 }
2148
David Brazdil1abb4192015-02-17 18:33:36 +00002149 void RemoveAsUserOfInput(size_t input) {
2150 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002151 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2152 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2153 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002154 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002155
Vladimir Marko372f10e2016-05-17 16:30:10 +01002156 void RemoveAsUserOfAllInputs() {
2157 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2158 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2159 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2160 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2161 }
2162 }
2163
David Brazdil1abb4192015-02-17 18:33:36 +00002164 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2165 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002166
Vladimir Marko46817b82016-03-29 12:21:58 +01002167 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2168 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2169 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002170 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002171 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002172 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002173
Aart Bikcc42be02016-10-20 16:14:16 -07002174 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002175 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002176 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002177 !CanThrow() &&
2178 !IsSuspendCheck() &&
2179 !IsControlFlow() &&
2180 !IsNativeDebugInfo() &&
2181 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002182 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002183 !IsMemoryBarrier() &&
2184 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002185 }
2186
Aart Bikcc42be02016-10-20 16:14:16 -07002187 bool IsDeadAndRemovable() const {
2188 return IsRemovable() && !HasUses();
2189 }
2190
Roland Levillain6c82d402014-10-13 16:10:27 +01002191 // Does this instruction strictly dominate `other_instruction`?
2192 // Returns false if this instruction and `other_instruction` are the same.
2193 // Aborts if this instruction and `other_instruction` are both phis.
2194 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002195
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002196 int GetId() const { return id_; }
2197 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002198
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002199 int GetSsaIndex() const { return ssa_index_; }
2200 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2201 bool HasSsaIndex() const { return ssa_index_ != -1; }
2202
2203 bool HasEnvironment() const { return environment_ != nullptr; }
2204 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002205 // Set the `environment_` field. Raw because this method does not
2206 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002207 void SetRawEnvironment(HEnvironment* environment) {
2208 DCHECK(environment_ == nullptr);
2209 DCHECK_EQ(environment->GetHolder(), this);
2210 environment_ = environment;
2211 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002212
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002213 void InsertRawEnvironment(HEnvironment* environment) {
2214 DCHECK(environment_ != nullptr);
2215 DCHECK_EQ(environment->GetHolder(), this);
2216 DCHECK(environment->GetParent() == nullptr);
2217 environment->parent_ = environment_;
2218 environment_ = environment;
2219 }
2220
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002221 void RemoveEnvironment();
2222
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002223 // Set the environment of this instruction, copying it from `environment`. While
2224 // copying, the uses lists are being updated.
2225 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002226 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002227 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002228 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002229 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002230 if (environment->GetParent() != nullptr) {
2231 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2232 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002233 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002234
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002235 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2236 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002237 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002238 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002239 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002240 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002241 if (environment->GetParent() != nullptr) {
2242 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2243 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002244 }
2245
Nicolas Geoffray39468442014-09-02 15:17:15 +01002246 // Returns the number of entries in the environment. Typically, that is the
2247 // number of dex registers in a method. It could be more in case of inlining.
2248 size_t EnvironmentSize() const;
2249
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002250 LocationSummary* GetLocations() const { return locations_; }
2251 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002252
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002253 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002254 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002255 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002256 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002257
Alexandre Rames188d4312015-04-09 18:30:21 +01002258 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2259 // uses of this instruction by `other` are *not* updated.
2260 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2261 ReplaceWith(other);
2262 other->ReplaceInput(this, use_index);
2263 }
2264
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002265 // Move `this` instruction before `cursor`
2266 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002267
Vladimir Markofb337ea2015-11-25 15:25:10 +00002268 // Move `this` before its first user and out of any loops. If there is no
2269 // out-of-loop user that dominates all other users, move the instruction
2270 // to the end of the out-of-loop common dominator of the user's blocks.
2271 //
2272 // This can be used only on non-throwing instructions with no side effects that
2273 // have at least one use but no environment uses.
2274 void MoveBeforeFirstUserAndOutOfLoops();
2275
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002276#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002277 bool Is##type() const;
2278
2279 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2280#undef INSTRUCTION_TYPE_CHECK
2281
2282#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002283 const H##type* As##type() const; \
2284 H##type* As##type();
2285
Vladimir Markoa90dd512018-05-04 15:04:45 +01002286 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2287#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002288
Artem Serovcced8ba2017-07-19 18:18:09 +01002289 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2290 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2291 // the instruction then the behaviour of this function is undefined.
2292 //
2293 // Note: It is semantically valid to create a clone of the instruction only until
2294 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2295 // copied.
2296 //
2297 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2298 // 'explicit HInstruction(const HInstruction& other)' for details.
2299 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2300 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2301 DebugName() << " " << GetId();
2302 UNREACHABLE();
2303 }
2304
2305 // Return whether instruction can be cloned (copied).
2306 virtual bool IsClonable() const { return false; }
2307
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002308 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002309 // TODO: this method is used by LICM and GVN with possibly different
2310 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002311 virtual bool CanBeMoved() const { return false; }
2312
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002313 // Returns whether any data encoded in the two instructions is equal.
2314 // This method does not look at the inputs. Both instructions must be
2315 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002316 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002317 return false;
2318 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002319
2320 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002321 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002322 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002323 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002324
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002325 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2326 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2327 // the virtual function because the __attribute__((__pure__)) doesn't really
2328 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302329 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002330
2331 virtual size_t ComputeHashCode() const {
2332 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002333 for (const HInstruction* input : GetInputs()) {
2334 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002335 }
2336 return result;
2337 }
2338
2339 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002340 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002341 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002342
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002343 size_t GetLifetimePosition() const { return lifetime_position_; }
2344 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2345 LiveInterval* GetLiveInterval() const { return live_interval_; }
2346 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2347 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2348
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002349 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2350
2351 // Returns whether the code generation of the instruction will require to have access
2352 // to the current method. Such instructions are:
2353 // (1): Instructions that require an environment, as calling the runtime requires
2354 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002355 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2356 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002357 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002358 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002359 }
2360
Vladimir Markodc151b22015-10-15 18:02:30 +01002361 // Returns whether the code generation of the instruction will require to have access
2362 // to the dex cache of the current method's declaring class via the current method.
2363 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002364
Mark Mendellc4701932015-04-10 13:18:51 -04002365 // Does this instruction have any use in an environment before
2366 // control flow hits 'other'?
2367 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2368
2369 // Remove all references to environment uses of this instruction.
2370 // The caller must ensure that this is safe to do.
2371 void RemoveEnvironmentUsers();
2372
Vladimir Markoa1de9182016-02-25 11:37:38 +00002373 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2374 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002375
David Brazdil1abb4192015-02-17 18:33:36 +00002376 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002377 // If set, the machine code for this instruction is assumed to be generated by
2378 // its users. Used by liveness analysis to compute use positions accordingly.
2379 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2380 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302381 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2382 static constexpr size_t kFieldInstructionKindSize =
2383 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002384 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302385 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002386 static constexpr size_t kFieldTypeSize =
2387 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2388 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002389 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2390
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302391 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2392 "Too many generic packed fields");
2393
Vladimir Markobd785672018-05-03 17:09:09 +01002394 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2395
Vladimir Marko372f10e2016-05-17 16:30:10 +01002396 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2397 return GetInputRecords()[i];
2398 }
2399
2400 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2401 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2402 input_records[index] = input;
2403 }
David Brazdil1abb4192015-02-17 18:33:36 +00002404
Vladimir Markoa1de9182016-02-25 11:37:38 +00002405 uint32_t GetPackedFields() const {
2406 return packed_fields_;
2407 }
2408
2409 template <size_t flag>
2410 bool GetPackedFlag() const {
2411 return (packed_fields_ & (1u << flag)) != 0u;
2412 }
2413
2414 template <size_t flag>
2415 void SetPackedFlag(bool value = true) {
2416 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2417 }
2418
2419 template <typename BitFieldType>
2420 typename BitFieldType::value_type GetPackedField() const {
2421 return BitFieldType::Decode(packed_fields_);
2422 }
2423
2424 template <typename BitFieldType>
2425 void SetPackedField(typename BitFieldType::value_type value) {
2426 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2427 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2428 }
2429
Artem Serovcced8ba2017-07-19 18:18:09 +01002430 // Copy construction for the instruction (used for Clone function).
2431 //
2432 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2433 // prepare_for_register_allocator are not copied (set to default values).
2434 //
2435 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2436 // fine for most of them. However for some of the instructions a custom copy constructor must be
2437 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2438 // for copying them).
2439 explicit HInstruction(const HInstruction& other)
2440 : previous_(nullptr),
2441 next_(nullptr),
2442 block_(nullptr),
2443 dex_pc_(other.dex_pc_),
2444 id_(-1),
2445 ssa_index_(-1),
2446 packed_fields_(other.packed_fields_),
2447 environment_(nullptr),
2448 locations_(nullptr),
2449 live_interval_(nullptr),
2450 lifetime_position_(kNoLifetime),
2451 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302452 reference_type_handle_(other.reference_type_handle_) {
2453 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002454
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002455 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302456 using InstructionKindField =
2457 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2458
Vladimir Marko46817b82016-03-29 12:21:58 +01002459 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2460 auto before_use_node = uses_.before_begin();
2461 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2462 HInstruction* user = use_node->GetUser();
2463 size_t input_index = use_node->GetIndex();
2464 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2465 before_use_node = use_node;
2466 }
2467 }
2468
2469 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2470 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2471 if (next != uses_.end()) {
2472 HInstruction* next_user = next->GetUser();
2473 size_t next_index = next->GetIndex();
2474 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2475 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2476 }
2477 }
2478
2479 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2480 auto before_env_use_node = env_uses_.before_begin();
2481 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2482 HEnvironment* user = env_use_node->GetUser();
2483 size_t input_index = env_use_node->GetIndex();
2484 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2485 before_env_use_node = env_use_node;
2486 }
2487 }
2488
2489 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2490 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2491 if (next != env_uses_.end()) {
2492 HEnvironment* next_user = next->GetUser();
2493 size_t next_index = next->GetIndex();
2494 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2495 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2496 }
2497 }
David Brazdil1abb4192015-02-17 18:33:36 +00002498
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002499 HInstruction* previous_;
2500 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002501 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002502 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002503
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002504 // An instruction gets an id when it is added to the graph.
2505 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002506 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002507 int id_;
2508
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002509 // When doing liveness analysis, instructions that have uses get an SSA index.
2510 int ssa_index_;
2511
Vladimir Markoa1de9182016-02-25 11:37:38 +00002512 // Packed fields.
2513 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002514
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002515 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002516 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002517
2518 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002519 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002520
Nicolas Geoffray39468442014-09-02 15:17:15 +01002521 // The environment associated with this instruction. Not null if the instruction
2522 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002523 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002524
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002525 // Set by the code generator.
2526 LocationSummary* locations_;
2527
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002528 // Set by the liveness analysis.
2529 LiveInterval* live_interval_;
2530
2531 // Set by the liveness analysis, this is the position in a linear
2532 // order of blocks where this instruction's live interval start.
2533 size_t lifetime_position_;
2534
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002535 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002536
Vladimir Markoa1de9182016-02-25 11:37:38 +00002537 // The reference handle part of the reference type info.
2538 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002539 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002540 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002541
David Brazdil1abb4192015-02-17 18:33:36 +00002542 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002543 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002544 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002545 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002546 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002547};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002548std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002549
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002550// Iterates over the instructions, while preserving the next instruction
2551// in case the current instruction gets removed from the list by the user
2552// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002553class HInstructionIterator : public ValueObject {
2554 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002555 explicit HInstructionIterator(const HInstructionList& instructions)
2556 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002557 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002558 }
2559
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002560 bool Done() const { return instruction_ == nullptr; }
2561 HInstruction* Current() const { return instruction_; }
2562 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002563 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002564 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002565 }
2566
2567 private:
2568 HInstruction* instruction_;
2569 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002570
2571 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002572};
2573
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002574// Iterates over the instructions without saving the next instruction,
2575// therefore handling changes in the graph potentially made by the user
2576// of this iterator.
2577class HInstructionIteratorHandleChanges : public ValueObject {
2578 public:
2579 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2580 : instruction_(instructions.first_instruction_) {
2581 }
2582
2583 bool Done() const { return instruction_ == nullptr; }
2584 HInstruction* Current() const { return instruction_; }
2585 void Advance() {
2586 instruction_ = instruction_->GetNext();
2587 }
2588
2589 private:
2590 HInstruction* instruction_;
2591
2592 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2593};
2594
2595
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002596class HBackwardInstructionIterator : public ValueObject {
2597 public:
2598 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2599 : instruction_(instructions.last_instruction_) {
2600 next_ = Done() ? nullptr : instruction_->GetPrevious();
2601 }
2602
2603 bool Done() const { return instruction_ == nullptr; }
2604 HInstruction* Current() const { return instruction_; }
2605 void Advance() {
2606 instruction_ = next_;
2607 next_ = Done() ? nullptr : instruction_->GetPrevious();
2608 }
2609
2610 private:
2611 HInstruction* instruction_;
2612 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002613
2614 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002615};
2616
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002617class HVariableInputSizeInstruction : public HInstruction {
2618 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002619 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002620 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002621 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2622 }
2623
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002624 void AddInput(HInstruction* input);
2625 void InsertInputAt(size_t index, HInstruction* input);
2626 void RemoveInputAt(size_t index);
2627
Igor Murashkind01745e2017-04-05 16:40:31 -07002628 // Removes all the inputs.
2629 // Also removes this instructions from each input's use list
2630 // (for non-environment uses only).
2631 void RemoveAllInputs();
2632
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002633 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302634 HVariableInputSizeInstruction(InstructionKind inst_kind,
2635 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002636 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002637 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002638 size_t number_of_inputs,
2639 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302640 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002641 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002642 HVariableInputSizeInstruction(InstructionKind inst_kind,
2643 DataType::Type type,
2644 SideEffects side_effects,
2645 uint32_t dex_pc,
2646 ArenaAllocator* allocator,
2647 size_t number_of_inputs,
2648 ArenaAllocKind kind)
2649 : HInstruction(inst_kind, type, side_effects, dex_pc),
2650 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002651
Artem Serovcced8ba2017-07-19 18:18:09 +01002652 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002653
Artem Serovcced8ba2017-07-19 18:18:09 +01002654 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002655};
2656
Vladimir Markof9f64412015-09-02 14:05:49 +01002657template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002658class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002660 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302661 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002662 HExpression<N>(InstructionKind kind,
2663 DataType::Type type,
2664 SideEffects side_effects,
2665 uint32_t dex_pc)
2666 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2667 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002668
Vladimir Marko372f10e2016-05-17 16:30:10 +01002669 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002670 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002671 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002672 }
2673
Artem Serovcced8ba2017-07-19 18:18:09 +01002674 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002675 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002676
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002677 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002678 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002679
2680 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002681};
2682
Vladimir Markobd785672018-05-03 17:09:09 +01002683// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002684template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002685class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002686 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002687 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002688
Vladimir Markobd785672018-05-03 17:09:09 +01002689 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002690
Vladimir Marko372f10e2016-05-17 16:30:10 +01002691 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002692 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002693 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002694 }
2695
Artem Serovcced8ba2017-07-19 18:18:09 +01002696 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002697 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002698
Vladimir Markof9f64412015-09-02 14:05:49 +01002699 private:
2700 friend class SsaBuilder;
2701};
2702
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002703// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2704// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002705class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002706 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002707 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002708 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302709 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002710
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002711 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002712
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002713 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002714
Artem Serovcced8ba2017-07-19 18:18:09 +01002715 protected:
2716 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002717};
2718
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002719// Represents dex's RETURN opcodes. A HReturn is a control flow
2720// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002721class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002722 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002723 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002724 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002725 SetRawInputAt(0, value);
2726 }
2727
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002728 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002729
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002730 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002731
Artem Serovcced8ba2017-07-19 18:18:09 +01002732 protected:
2733 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002734};
2735
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002736class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002737 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002738 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002739 uint32_t reg_number,
2740 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002742 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002743 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302744 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002745 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002746 SideEffects::None(),
2747 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002748 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002749 number_of_inputs,
2750 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002751 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002752 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002753 // Phis are constructed live and marked dead if conflicting or unused.
2754 // Individual steps of SsaBuilder should assume that if a phi has been
2755 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2756 SetPackedFlag<kFlagIsLive>(true);
2757 SetPackedFlag<kFlagCanBeNull>(true);
2758 }
2759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002760 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002761
David Brazdildee58d62016-04-07 09:54:26 +00002762 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002763 static DataType::Type ToPhiType(DataType::Type type) {
2764 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002765 }
2766
2767 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2768
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002769 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002770 // Make sure that only valid type changes occur. The following are allowed:
2771 // (1) int -> float/ref (primitive type propagation),
2772 // (2) long -> double (primitive type propagation).
2773 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002774 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2775 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2776 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002777 SetPackedField<TypeField>(new_type);
2778 }
2779
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002780 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002781 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2782
2783 uint32_t GetRegNumber() const { return reg_number_; }
2784
2785 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2786 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2787 bool IsDead() const { return !IsLive(); }
2788 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2789
Vladimir Markoe9004912016-06-16 16:50:52 +01002790 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002791 return other != nullptr
2792 && other->IsPhi()
2793 && other->AsPhi()->GetBlock() == GetBlock()
2794 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2795 }
2796
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002797 bool HasEquivalentPhi() const {
2798 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2799 return true;
2800 }
2801 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2802 return true;
2803 }
2804 return false;
2805 }
2806
David Brazdildee58d62016-04-07 09:54:26 +00002807 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2808 // An equivalent phi is a phi having the same dex register and type.
2809 // It assumes that phis with the same dex register are adjacent.
2810 HPhi* GetNextEquivalentPhiWithSameType() {
2811 HInstruction* next = GetNext();
2812 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2813 if (next->GetType() == GetType()) {
2814 return next->AsPhi();
2815 }
2816 next = next->GetNext();
2817 }
2818 return nullptr;
2819 }
2820
2821 DECLARE_INSTRUCTION(Phi);
2822
Artem Serovcced8ba2017-07-19 18:18:09 +01002823 protected:
2824 DEFAULT_COPY_CONSTRUCTOR(Phi);
2825
David Brazdildee58d62016-04-07 09:54:26 +00002826 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002827 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002828 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2829 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2830 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002831
David Brazdildee58d62016-04-07 09:54:26 +00002832 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002833};
2834
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002835// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002836// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002837// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002838class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002839 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302840 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002841 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302842 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002844 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002845
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002846 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002847
Artem Serovcced8ba2017-07-19 18:18:09 +01002848 protected:
2849 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002850};
2851
2852// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002853class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002854 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302855 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002856 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302857 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002859 bool IsClonable() const override { return true; }
2860 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002861
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002862 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002863 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002864 }
2865
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002866 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002867
Artem Serovcced8ba2017-07-19 18:18:09 +01002868 protected:
2869 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002870};
2871
Roland Levillain9867bc72015-08-05 10:21:34 +01002872class HConstant : public HExpression<0> {
2873 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302874 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2875 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2876 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002878 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002879
Roland Levillain1a653882016-03-18 18:05:57 +00002880 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002881 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002882 // Is this constant 0 in the arithmetic sense?
2883 virtual bool IsArithmeticZero() const { return false; }
2884 // Is this constant a 0-bit pattern?
2885 virtual bool IsZeroBitPattern() const { return false; }
2886 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002887 virtual bool IsOne() const { return false; }
2888
David Brazdil77a48ae2015-09-15 12:34:04 +00002889 virtual uint64_t GetValueAsUint64() const = 0;
2890
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002891 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002892
Artem Serovcced8ba2017-07-19 18:18:09 +01002893 protected:
2894 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002895};
2896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002897class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002898 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002899 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002900 return true;
2901 }
2902
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002903 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002905 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002906
Roland Levillain1a653882016-03-18 18:05:57 +00002907 // The null constant representation is a 0-bit pattern.
2908 virtual bool IsZeroBitPattern() const { return true; }
2909
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 DECLARE_INSTRUCTION(NullConstant);
2911
Artem Serovcced8ba2017-07-19 18:18:09 +01002912 protected:
2913 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2914
Roland Levillain9867bc72015-08-05 10:21:34 +01002915 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302917 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2918 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002919
2920 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002921};
2922
2923// Constants of the type int. Those can be from Dex instructions, or
2924// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002925class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002926 public:
2927 int32_t GetValue() const { return value_; }
2928
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002929 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002930 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2931 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002932
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002933 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002934 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002935 return other->AsIntConstant()->value_ == value_;
2936 }
2937
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002938 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002939
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002940 bool IsMinusOne() const override { return GetValue() == -1; }
2941 bool IsArithmeticZero() const override { return GetValue() == 0; }
2942 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2943 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002944
Roland Levillain1a653882016-03-18 18:05:57 +00002945 // Integer constants are used to encode Boolean values as well,
2946 // where 1 means true and 0 means false.
2947 bool IsTrue() const { return GetValue() == 1; }
2948 bool IsFalse() const { return GetValue() == 0; }
2949
Roland Levillain9867bc72015-08-05 10:21:34 +01002950 DECLARE_INSTRUCTION(IntConstant);
2951
Artem Serovcced8ba2017-07-19 18:18:09 +01002952 protected:
2953 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2954
Roland Levillain9867bc72015-08-05 10:21:34 +01002955 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002956 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302957 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2958 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002959 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302960 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2961 value_(value ? 1 : 0) {
2962 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002963
2964 const int32_t value_;
2965
2966 friend class HGraph;
2967 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2968 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002969};
2970
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002971class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002972 public:
2973 int64_t GetValue() const { return value_; }
2974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002975 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002977 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002978 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002979 return other->AsLongConstant()->value_ == value_;
2980 }
2981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002982 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002984 bool IsMinusOne() const override { return GetValue() == -1; }
2985 bool IsArithmeticZero() const override { return GetValue() == 0; }
2986 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2987 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002988
2989 DECLARE_INSTRUCTION(LongConstant);
2990
Artem Serovcced8ba2017-07-19 18:18:09 +01002991 protected:
2992 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2993
Roland Levillain9867bc72015-08-05 10:21:34 +01002994 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002995 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302996 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2997 value_(value) {
2998 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002999
3000 const int64_t value_;
3001
3002 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003003};
Dave Allison20dfc792014-06-16 20:44:29 -07003004
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003005class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003006 public:
3007 float GetValue() const { return value_; }
3008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003009 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003010 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3011 }
3012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003013 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003014 DCHECK(other->IsFloatConstant()) << other->DebugName();
3015 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3016 }
3017
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003018 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003020 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003021 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3022 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003023 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003024 return std::fpclassify(value_) == FP_ZERO;
3025 }
3026 bool IsArithmeticPositiveZero() const {
3027 return IsArithmeticZero() && !std::signbit(value_);
3028 }
3029 bool IsArithmeticNegativeZero() const {
3030 return IsArithmeticZero() && std::signbit(value_);
3031 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003032 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003033 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003034 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003035 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3037 }
3038 bool IsNaN() const {
3039 return std::isnan(value_);
3040 }
3041
3042 DECLARE_INSTRUCTION(FloatConstant);
3043
Artem Serovcced8ba2017-07-19 18:18:09 +01003044 protected:
3045 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3046
Roland Levillain31dd3d62016-02-16 12:21:02 +00003047 private:
3048 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303049 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3050 value_(value) {
3051 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003052 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303053 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3054 value_(bit_cast<float, int32_t>(value)) {
3055 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003056
3057 const float value_;
3058
3059 // Only the SsaBuilder and HGraph can create floating-point constants.
3060 friend class SsaBuilder;
3061 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062};
3063
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003064class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065 public:
3066 double GetValue() const { return value_; }
3067
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003068 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003070 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003071 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3072 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3073 }
3074
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003075 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003077 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3079 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003080 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003081 return std::fpclassify(value_) == FP_ZERO;
3082 }
3083 bool IsArithmeticPositiveZero() const {
3084 return IsArithmeticZero() && !std::signbit(value_);
3085 }
3086 bool IsArithmeticNegativeZero() const {
3087 return IsArithmeticZero() && std::signbit(value_);
3088 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003089 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003090 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003091 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003092 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003093 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3094 }
3095 bool IsNaN() const {
3096 return std::isnan(value_);
3097 }
3098
3099 DECLARE_INSTRUCTION(DoubleConstant);
3100
Artem Serovcced8ba2017-07-19 18:18:09 +01003101 protected:
3102 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3103
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 private:
3105 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303106 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3107 value_(value) {
3108 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303110 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3111 value_(bit_cast<double, int64_t>(value)) {
3112 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113
3114 const double value_;
3115
3116 // Only the SsaBuilder and HGraph can create floating-point constants.
3117 friend class SsaBuilder;
3118 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003119};
3120
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003121// Conditional branch. A block ending with an HIf instruction must have
3122// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003123class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003124 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003125 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003126 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003127 SetRawInputAt(0, input);
3128 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003130 bool IsClonable() const override { return true; }
3131 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003132
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003133 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003134 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003135 }
3136
3137 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003138 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003139 }
3140
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003141 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003142
Artem Serovcced8ba2017-07-19 18:18:09 +01003143 protected:
3144 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003145};
3146
David Brazdilfc6a86a2015-06-26 10:33:45 +00003147
3148// Abstract instruction which marks the beginning and/or end of a try block and
3149// links it to the respective exception handlers. Behaves the same as a Goto in
3150// non-exceptional control flow.
3151// Normal-flow successor is stored at index zero, exception handlers under
3152// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003153class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003154 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003155 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003156 kEntry,
3157 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003158 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003159 };
3160
Artem Serovd1aa7d02018-06-22 11:35:46 +01003161 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3162 // across the catch block entering edges as GC might happen during throwing an exception.
3163 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3164 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003165 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003166 : HExpression(kTryBoundary,
3167 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3168 : SideEffects::None(),
3169 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003170 SetPackedField<BoundaryKindField>(kind);
3171 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003173 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003174
3175 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003176 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003177
David Brazdild26a4112015-11-10 11:07:31 +00003178 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3179 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3180 }
3181
David Brazdilfc6a86a2015-06-26 10:33:45 +00003182 // Returns whether `handler` is among its exception handlers (non-zero index
3183 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003184 bool HasExceptionHandler(const HBasicBlock& handler) const {
3185 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003186 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003187 }
3188
3189 // If not present already, adds `handler` to its block's list of exception
3190 // handlers.
3191 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003192 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003193 GetBlock()->AddSuccessor(handler);
3194 }
3195 }
3196
Vladimir Markoa1de9182016-02-25 11:37:38 +00003197 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3198 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003199
David Brazdilffee3d32015-07-06 11:48:53 +01003200 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3201
David Brazdilfc6a86a2015-06-26 10:33:45 +00003202 DECLARE_INSTRUCTION(TryBoundary);
3203
Artem Serovcced8ba2017-07-19 18:18:09 +01003204 protected:
3205 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3206
David Brazdilfc6a86a2015-06-26 10:33:45 +00003207 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003208 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3209 static constexpr size_t kFieldBoundaryKindSize =
3210 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3211 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3212 kFieldBoundaryKind + kFieldBoundaryKindSize;
3213 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3214 "Too many packed fields.");
3215 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003216};
3217
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003218// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003219class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003220 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003221 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3222 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003223 HDeoptimize(ArenaAllocator* allocator,
3224 HInstruction* cond,
3225 DeoptimizationKind kind,
3226 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003227 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303228 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003229 SideEffects::All(),
3230 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003231 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003232 /* number_of_inputs */ 1,
3233 kArenaAllocMisc) {
3234 SetPackedFlag<kFieldCanBeMoved>(false);
3235 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003236 SetRawInputAt(0, cond);
3237 }
3238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003239 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003240
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003241 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3242 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3243 // instead of `guard`.
3244 // We set CanTriggerGC to prevent any intermediate address to be live
3245 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003246 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003247 HInstruction* cond,
3248 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003249 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003250 uint32_t dex_pc)
3251 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303252 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003253 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003254 SideEffects::CanTriggerGC(),
3255 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003256 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003257 /* number_of_inputs */ 2,
3258 kArenaAllocMisc) {
3259 SetPackedFlag<kFieldCanBeMoved>(true);
3260 SetPackedField<DeoptimizeKindField>(kind);
3261 SetRawInputAt(0, cond);
3262 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003263 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003264
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003265 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003267 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003268 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3269 }
3270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003271 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003272
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003273 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003274
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003275 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003276
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003277 bool GuardsAnInput() const {
3278 return InputCount() == 2;
3279 }
3280
3281 HInstruction* GuardedInput() const {
3282 DCHECK(GuardsAnInput());
3283 return InputAt(1);
3284 }
3285
3286 void RemoveGuard() {
3287 RemoveInputAt(1);
3288 }
3289
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003290 DECLARE_INSTRUCTION(Deoptimize);
3291
Artem Serovcced8ba2017-07-19 18:18:09 +01003292 protected:
3293 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3294
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003295 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003296 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3297 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3298 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003299 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003300 static constexpr size_t kNumberOfDeoptimizePackedBits =
3301 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3302 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3303 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003304 using DeoptimizeKindField =
3305 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003306};
3307
Mingyao Yang063fc772016-08-02 11:02:54 -07003308// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3309// The compiled code checks this flag value in a guard before devirtualized call and
3310// if it's true, starts to do deoptimization.
3311// It has a 4-byte slot on stack.
3312// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003313class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003314 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003315 // CHA guards are only optimized in a separate pass and it has no side effects
3316 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003317 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303318 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003319 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303320 SideEffects::None(),
3321 dex_pc,
3322 allocator,
3323 0,
3324 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003325 }
3326
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003327 // We do all CHA guard elimination/motion in a single pass, after which there is no
3328 // further guard elimination/motion since a guard might have been used for justification
3329 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3330 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003331 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003332
3333 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3334
Artem Serovcced8ba2017-07-19 18:18:09 +01003335 protected:
3336 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003337};
3338
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003339// Represents the ArtMethod that was passed as a first argument to
3340// the method. It is used by instructions that depend on it, like
3341// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003342class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003343 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003344 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303345 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3346 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003347
3348 DECLARE_INSTRUCTION(CurrentMethod);
3349
Artem Serovcced8ba2017-07-19 18:18:09 +01003350 protected:
3351 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003352};
3353
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003354// Fetches an ArtMethod from the virtual table or the interface method table
3355// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003356class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003357 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003358 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003359 kVTable,
3360 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003361 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003362 };
3363 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003364 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003365 TableKind kind,
3366 size_t index,
3367 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303368 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003369 index_(index) {
3370 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003371 SetRawInputAt(0, cls);
3372 }
3373
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003374 bool IsClonable() const override { return true; }
3375 bool CanBeMoved() const override { return true; }
3376 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003377 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003378 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003379 }
3380
Vladimir Markoa1de9182016-02-25 11:37:38 +00003381 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003382 size_t GetIndex() const { return index_; }
3383
3384 DECLARE_INSTRUCTION(ClassTableGet);
3385
Artem Serovcced8ba2017-07-19 18:18:09 +01003386 protected:
3387 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3388
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003389 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003390 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003391 static constexpr size_t kFieldTableKindSize =
3392 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3393 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3394 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3395 "Too many packed fields.");
3396 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3397
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003398 // The index of the ArtMethod in the table.
3399 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003400};
3401
Mark Mendellfe57faa2015-09-18 09:26:15 -04003402// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3403// have one successor for each entry in the switch table, and the final successor
3404// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003405class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003406 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003407 HPackedSwitch(int32_t start_value,
3408 uint32_t num_entries,
3409 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003410 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003411 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003412 start_value_(start_value),
3413 num_entries_(num_entries) {
3414 SetRawInputAt(0, input);
3415 }
3416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003417 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003418
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003419 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003420
3421 int32_t GetStartValue() const { return start_value_; }
3422
Vladimir Marko211c2112015-09-24 16:52:33 +01003423 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003424
3425 HBasicBlock* GetDefaultBlock() const {
3426 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003427 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003428 }
3429 DECLARE_INSTRUCTION(PackedSwitch);
3430
Artem Serovcced8ba2017-07-19 18:18:09 +01003431 protected:
3432 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3433
Mark Mendellfe57faa2015-09-18 09:26:15 -04003434 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003435 const int32_t start_value_;
3436 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003437};
3438
Roland Levillain88cb1752014-10-20 16:36:47 +01003439class HUnaryOperation : public HExpression<1> {
3440 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303441 HUnaryOperation(InstructionKind kind,
3442 DataType::Type result_type,
3443 HInstruction* input,
3444 uint32_t dex_pc = kNoDexPc)
3445 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003446 SetRawInputAt(0, input);
3447 }
3448
Artem Serovcced8ba2017-07-19 18:18:09 +01003449 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003450 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003451
Roland Levillain88cb1752014-10-20 16:36:47 +01003452 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003453 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003455 bool CanBeMoved() const override { return true; }
3456 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003457 return true;
3458 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003459
Roland Levillain31dd3d62016-02-16 12:21:02 +00003460 // Try to statically evaluate `this` and return a HConstant
3461 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003462 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003463 HConstant* TryStaticEvaluation() const;
3464
3465 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003466 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3467 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003468 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3469 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003470
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003471 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003472
Artem Serovcced8ba2017-07-19 18:18:09 +01003473 protected:
3474 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003475};
3476
Dave Allison20dfc792014-06-16 20:44:29 -07003477class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003478 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303479 HBinaryOperation(InstructionKind kind,
3480 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003481 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003482 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003483 SideEffects side_effects = SideEffects::None(),
3484 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303485 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003486 SetRawInputAt(0, left);
3487 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003488 }
3489
Artem Serovcced8ba2017-07-19 18:18:09 +01003490 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003491 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003492
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003493 HInstruction* GetLeft() const { return InputAt(0); }
3494 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003495 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003496
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003497 virtual bool IsCommutative() const { return false; }
3498
3499 // Put constant on the right.
3500 // Returns whether order is changed.
3501 bool OrderInputsWithConstantOnTheRight() {
3502 HInstruction* left = InputAt(0);
3503 HInstruction* right = InputAt(1);
3504 if (left->IsConstant() && !right->IsConstant()) {
3505 ReplaceInput(right, 0);
3506 ReplaceInput(left, 1);
3507 return true;
3508 }
3509 return false;
3510 }
3511
3512 // Order inputs by instruction id, but favor constant on the right side.
3513 // This helps GVN for commutative ops.
3514 void OrderInputs() {
3515 DCHECK(IsCommutative());
3516 HInstruction* left = InputAt(0);
3517 HInstruction* right = InputAt(1);
3518 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3519 return;
3520 }
3521 if (OrderInputsWithConstantOnTheRight()) {
3522 return;
3523 }
3524 // Order according to instruction id.
3525 if (left->GetId() > right->GetId()) {
3526 ReplaceInput(right, 0);
3527 ReplaceInput(left, 1);
3528 }
3529 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003530
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003531 bool CanBeMoved() const override { return true; }
3532 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003533 return true;
3534 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003535
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 // Try to statically evaluate `this` and return a HConstant
3537 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003538 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003539 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003540
3541 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003542 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3543 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003544 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3545 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003546 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003547 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3548 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003549 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3550 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003551 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3552 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003553 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3555 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003556
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003557 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003558 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003559 HConstant* GetConstantRight() const;
3560
3561 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003562 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003563 HInstruction* GetLeastConstantLeft() const;
3564
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003565 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003566
Artem Serovcced8ba2017-07-19 18:18:09 +01003567 protected:
3568 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003569};
3570
Mark Mendellc4701932015-04-10 13:18:51 -04003571// The comparison bias applies for floating point operations and indicates how NaN
3572// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003573enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003574 kNoBias, // bias is not applicable (i.e. for long operation)
3575 kGtBias, // return 1 for NaN comparisons
3576 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003577 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003578};
3579
Roland Levillain31dd3d62016-02-16 12:21:02 +00003580std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3581
Dave Allison20dfc792014-06-16 20:44:29 -07003582class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003583 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303584 HCondition(InstructionKind kind,
3585 HInstruction* first,
3586 HInstruction* second,
3587 uint32_t dex_pc = kNoDexPc)
3588 : HBinaryOperation(kind,
3589 DataType::Type::kBool,
3590 first,
3591 second,
3592 SideEffects::None(),
3593 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003594 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3595 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003596
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003597 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003598 // `instruction`, and disregard moves in between.
3599 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003600
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003601 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003602
3603 virtual IfCondition GetCondition() const = 0;
3604
Mark Mendellc4701932015-04-10 13:18:51 -04003605 virtual IfCondition GetOppositeCondition() const = 0;
3606
Vladimir Markoa1de9182016-02-25 11:37:38 +00003607 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003608 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3609
Vladimir Markoa1de9182016-02-25 11:37:38 +00003610 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3611 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003612
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003613 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003614 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003615 }
3616
Roland Levillain4fa13f62015-07-06 18:11:54 +01003617 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003618 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003619 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003620 if (if_cond == kCondNE) {
3621 return true;
3622 } else if (if_cond == kCondEQ) {
3623 return false;
3624 }
3625 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003626 }
3627
3628 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003629 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003630 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003631 if (if_cond == kCondEQ) {
3632 return true;
3633 } else if (if_cond == kCondNE) {
3634 return false;
3635 }
3636 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003637 }
3638
Roland Levillain31dd3d62016-02-16 12:21:02 +00003639 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003640 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003641 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003642 static constexpr size_t kFieldComparisonBiasSize =
3643 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3644 static constexpr size_t kNumberOfConditionPackedBits =
3645 kFieldComparisonBias + kFieldComparisonBiasSize;
3646 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3647 using ComparisonBiasField =
3648 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3649
Roland Levillain31dd3d62016-02-16 12:21:02 +00003650 template <typename T>
3651 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3652
3653 template <typename T>
3654 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003655 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003656 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3657 // Handle the bias.
3658 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3659 }
3660
3661 // Return an integer constant containing the result of a condition evaluated at compile time.
3662 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3663 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3664 }
3665
Artem Serovcced8ba2017-07-19 18:18:09 +01003666 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003667};
3668
3669// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003670class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003671 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003672 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303673 : HCondition(kEqual, first, second, dex_pc) {
3674 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003676 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003677
Vladimir Marko9e23df52015-11-10 17:14:35 +00003678 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003679 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003680 return MakeConstantCondition(true, GetDexPc());
3681 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003682 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3684 }
3685 // In the following Evaluate methods, a HCompare instruction has
3686 // been merged into this HEqual instruction; evaluate it as
3687 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003688 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003689 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3690 GetDexPc());
3691 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003692 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003693 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3694 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003695 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003696 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003697 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003698
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003699 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003700
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003701 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003702 return kCondEQ;
3703 }
3704
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003705 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003706 return kCondNE;
3707 }
3708
Artem Serovcced8ba2017-07-19 18:18:09 +01003709 protected:
3710 DEFAULT_COPY_CONSTRUCTOR(Equal);
3711
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003712 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003713 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003714};
3715
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003716class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003717 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303718 HNotEqual(HInstruction* first, HInstruction* second,
3719 uint32_t dex_pc = kNoDexPc)
3720 : HCondition(kNotEqual, first, second, dex_pc) {
3721 }
Dave Allison20dfc792014-06-16 20:44:29 -07003722
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003723 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003724
Vladimir Marko9e23df52015-11-10 17:14:35 +00003725 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003726 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003727 return MakeConstantCondition(false, GetDexPc());
3728 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003729 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003730 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3731 }
3732 // In the following Evaluate methods, a HCompare instruction has
3733 // been merged into this HNotEqual instruction; evaluate it as
3734 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003735 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003736 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3737 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003738 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003739 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3740 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003741 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003742 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003743 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003744
Dave Allison20dfc792014-06-16 20:44:29 -07003745 DECLARE_INSTRUCTION(NotEqual);
3746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003747 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003748 return kCondNE;
3749 }
3750
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003751 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003752 return kCondEQ;
3753 }
3754
Artem Serovcced8ba2017-07-19 18:18:09 +01003755 protected:
3756 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3757
Dave Allison20dfc792014-06-16 20:44:29 -07003758 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003759 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003760};
3761
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003762class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003763 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303764 HLessThan(HInstruction* first, HInstruction* second,
3765 uint32_t dex_pc = kNoDexPc)
3766 : HCondition(kLessThan, first, second, dex_pc) {
3767 }
Dave Allison20dfc792014-06-16 20:44:29 -07003768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003769 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003770 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003771 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003772 // In the following Evaluate methods, a HCompare instruction has
3773 // been merged into this HLessThan instruction; evaluate it as
3774 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003775 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003776 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3777 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003778 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003779 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3780 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003781 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003782 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003783 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003784
Dave Allison20dfc792014-06-16 20:44:29 -07003785 DECLARE_INSTRUCTION(LessThan);
3786
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003787 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003788 return kCondLT;
3789 }
3790
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003792 return kCondGE;
3793 }
3794
Artem Serovcced8ba2017-07-19 18:18:09 +01003795 protected:
3796 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3797
Dave Allison20dfc792014-06-16 20:44:29 -07003798 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003799 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003800};
3801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003802class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003803 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303804 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3805 uint32_t dex_pc = kNoDexPc)
3806 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3807 }
Dave Allison20dfc792014-06-16 20:44:29 -07003808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003809 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003810 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003811 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003812 // In the following Evaluate methods, a HCompare instruction has
3813 // been merged into this HLessThanOrEqual instruction; evaluate it as
3814 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003816 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3817 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003818 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003819 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3820 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003821 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003822 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003823 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003824
Dave Allison20dfc792014-06-16 20:44:29 -07003825 DECLARE_INSTRUCTION(LessThanOrEqual);
3826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003827 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003828 return kCondLE;
3829 }
3830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003831 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003832 return kCondGT;
3833 }
3834
Artem Serovcced8ba2017-07-19 18:18:09 +01003835 protected:
3836 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3837
Dave Allison20dfc792014-06-16 20:44:29 -07003838 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003839 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003840};
3841
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003842class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003843 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003844 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303845 : HCondition(kGreaterThan, first, second, dex_pc) {
3846 }
Dave Allison20dfc792014-06-16 20:44:29 -07003847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003848 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003849 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003850 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003851 // In the following Evaluate methods, a HCompare instruction has
3852 // been merged into this HGreaterThan instruction; evaluate it as
3853 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003854 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003855 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3856 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003857 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003858 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3859 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003862 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003863
Dave Allison20dfc792014-06-16 20:44:29 -07003864 DECLARE_INSTRUCTION(GreaterThan);
3865
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003866 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003867 return kCondGT;
3868 }
3869
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003870 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003871 return kCondLE;
3872 }
3873
Artem Serovcced8ba2017-07-19 18:18:09 +01003874 protected:
3875 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3876
Dave Allison20dfc792014-06-16 20:44:29 -07003877 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003878 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003879};
3880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003881class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003882 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003883 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303884 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3885 }
Dave Allison20dfc792014-06-16 20:44:29 -07003886
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003887 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003888 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003889 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003890 // In the following Evaluate methods, a HCompare instruction has
3891 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3892 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003893 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003894 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3895 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003896 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003897 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3898 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003899 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003900 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003901 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003902
Dave Allison20dfc792014-06-16 20:44:29 -07003903 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003905 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003906 return kCondGE;
3907 }
3908
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003909 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003910 return kCondLT;
3911 }
3912
Artem Serovcced8ba2017-07-19 18:18:09 +01003913 protected:
3914 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3915
Dave Allison20dfc792014-06-16 20:44:29 -07003916 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003917 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003918};
3919
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003920class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003921 public:
3922 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303923 : HCondition(kBelow, first, second, dex_pc) {
3924 }
Aart Bike9f37602015-10-09 11:15:55 -07003925
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003926 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003927 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003928 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003929 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003930 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3931 }
3932 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003933 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003934 LOG(FATAL) << DebugName() << " is not defined for float values";
3935 UNREACHABLE();
3936 }
3937 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003938 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003939 LOG(FATAL) << DebugName() << " is not defined for double values";
3940 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003941 }
3942
3943 DECLARE_INSTRUCTION(Below);
3944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003945 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003946 return kCondB;
3947 }
3948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003949 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003950 return kCondAE;
3951 }
3952
Artem Serovcced8ba2017-07-19 18:18:09 +01003953 protected:
3954 DEFAULT_COPY_CONSTRUCTOR(Below);
3955
Aart Bike9f37602015-10-09 11:15:55 -07003956 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003957 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003958 return MakeUnsigned(x) < MakeUnsigned(y);
3959 }
Aart Bike9f37602015-10-09 11:15:55 -07003960};
3961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003962class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003963 public:
3964 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303965 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3966 }
Aart Bike9f37602015-10-09 11:15:55 -07003967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003968 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003969 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003970 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003971 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003972 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3973 }
3974 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003975 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003976 LOG(FATAL) << DebugName() << " is not defined for float values";
3977 UNREACHABLE();
3978 }
3979 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003980 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003981 LOG(FATAL) << DebugName() << " is not defined for double values";
3982 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003983 }
3984
3985 DECLARE_INSTRUCTION(BelowOrEqual);
3986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003987 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003988 return kCondBE;
3989 }
3990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003991 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003992 return kCondA;
3993 }
3994
Artem Serovcced8ba2017-07-19 18:18:09 +01003995 protected:
3996 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3997
Aart Bike9f37602015-10-09 11:15:55 -07003998 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003999 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004000 return MakeUnsigned(x) <= MakeUnsigned(y);
4001 }
Aart Bike9f37602015-10-09 11:15:55 -07004002};
4003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004004class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004005 public:
4006 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304007 : HCondition(kAbove, first, second, dex_pc) {
4008 }
Aart Bike9f37602015-10-09 11:15:55 -07004009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004010 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004011 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004012 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004013 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004014 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4015 }
4016 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004017 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004018 LOG(FATAL) << DebugName() << " is not defined for float values";
4019 UNREACHABLE();
4020 }
4021 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004022 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004023 LOG(FATAL) << DebugName() << " is not defined for double values";
4024 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004025 }
4026
4027 DECLARE_INSTRUCTION(Above);
4028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004029 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004030 return kCondA;
4031 }
4032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004033 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004034 return kCondBE;
4035 }
4036
Artem Serovcced8ba2017-07-19 18:18:09 +01004037 protected:
4038 DEFAULT_COPY_CONSTRUCTOR(Above);
4039
Aart Bike9f37602015-10-09 11:15:55 -07004040 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004041 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004042 return MakeUnsigned(x) > MakeUnsigned(y);
4043 }
Aart Bike9f37602015-10-09 11:15:55 -07004044};
4045
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004046class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004047 public:
4048 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304049 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4050 }
Aart Bike9f37602015-10-09 11:15:55 -07004051
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004052 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004053 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004054 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004055 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004056 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4057 }
4058 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004059 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004060 LOG(FATAL) << DebugName() << " is not defined for float values";
4061 UNREACHABLE();
4062 }
4063 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004064 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004065 LOG(FATAL) << DebugName() << " is not defined for double values";
4066 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004067 }
4068
4069 DECLARE_INSTRUCTION(AboveOrEqual);
4070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004071 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004072 return kCondAE;
4073 }
4074
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004075 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004076 return kCondB;
4077 }
4078
Artem Serovcced8ba2017-07-19 18:18:09 +01004079 protected:
4080 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4081
Aart Bike9f37602015-10-09 11:15:55 -07004082 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004083 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004084 return MakeUnsigned(x) >= MakeUnsigned(y);
4085 }
Aart Bike9f37602015-10-09 11:15:55 -07004086};
Dave Allison20dfc792014-06-16 20:44:29 -07004087
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004088// Instruction to check how two inputs compare to each other.
4089// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004090class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004091 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004092 // Note that `comparison_type` is the type of comparison performed
4093 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004094 // HCompare instruction (which is always DataType::Type::kInt).
4095 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004096 HInstruction* first,
4097 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004098 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004099 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304100 : HBinaryOperation(kCompare,
4101 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004102 first,
4103 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004104 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004105 dex_pc) {
4106 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004107 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4108 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004109 }
4110
Roland Levillain9867bc72015-08-05 10:21:34 +01004111 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004112 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4113
4114 template <typename T>
4115 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004116 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004117 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4118 // Handle the bias.
4119 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4120 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004122 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004123 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4124 // reach this code path when processing a freshly built HIR
4125 // graph. However HCompare integer instructions can be synthesized
4126 // by the instruction simplifier to implement IntegerCompare and
4127 // IntegerSignum intrinsics, so we have to handle this case.
4128 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004129 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004130 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004131 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4132 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004133 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004134 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4135 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004136 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004137 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004138 }
4139
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004140 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004141 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004142 }
4143
Vladimir Markoa1de9182016-02-25 11:37:38 +00004144 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004145
Roland Levillain31dd3d62016-02-16 12:21:02 +00004146 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004147 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004148 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004150 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004151 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004153 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004154 // Comparisons do not require a runtime call in any back end.
4155 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004156 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004157
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004158 DECLARE_INSTRUCTION(Compare);
4159
Roland Levillain31dd3d62016-02-16 12:21:02 +00004160 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004161 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004162 static constexpr size_t kFieldComparisonBiasSize =
4163 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4164 static constexpr size_t kNumberOfComparePackedBits =
4165 kFieldComparisonBias + kFieldComparisonBiasSize;
4166 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4167 using ComparisonBiasField =
4168 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4169
Roland Levillain31dd3d62016-02-16 12:21:02 +00004170 // Return an integer constant containing the result of a comparison evaluated at compile time.
4171 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4172 DCHECK(value == -1 || value == 0 || value == 1) << value;
4173 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4174 }
4175
Artem Serovcced8ba2017-07-19 18:18:09 +01004176 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004177};
4178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004179class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004180 public:
4181 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004182 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004183 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004184 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004185 bool finalizable,
4186 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304187 : HExpression(kNewInstance,
4188 DataType::Type::kReference,
4189 SideEffects::CanTriggerGC(),
4190 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004191 type_index_(type_index),
4192 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004193 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004194 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004195 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004196 }
4197
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004198 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004199
Andreas Gampea5b09a62016-11-17 15:21:22 -08004200 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004201 const DexFile& GetDexFile() const { return dex_file_; }
4202
4203 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004204 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004205
Mingyao Yang062157f2016-03-02 10:15:36 -08004206 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004207 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004208
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004209 bool NeedsChecks() const {
4210 return entrypoint_ == kQuickAllocObjectWithChecks;
4211 }
David Brazdil6de19382016-01-08 17:37:10 +00004212
Vladimir Markoa1de9182016-02-25 11:37:38 +00004213 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004214
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004215 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004216
4217 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4218
4219 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4220 entrypoint_ = entrypoint;
4221 }
4222
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004223 HLoadClass* GetLoadClass() const {
4224 HInstruction* input = InputAt(0);
4225 if (input->IsClinitCheck()) {
4226 input = input->InputAt(0);
4227 }
4228 DCHECK(input->IsLoadClass());
4229 return input->AsLoadClass();
4230 }
4231
David Brazdil6de19382016-01-08 17:37:10 +00004232 bool IsStringAlloc() const;
4233
4234 DECLARE_INSTRUCTION(NewInstance);
4235
Artem Serovcced8ba2017-07-19 18:18:09 +01004236 protected:
4237 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4238
David Brazdil6de19382016-01-08 17:37:10 +00004239 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004240 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004241 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4242 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4243 "Too many packed fields.");
4244
Andreas Gampea5b09a62016-11-17 15:21:22 -08004245 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004246 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004247 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004248};
4249
Agi Csaki05f20562015-08-19 14:58:14 -07004250enum IntrinsicNeedsEnvironmentOrCache {
4251 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4252 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004253};
4254
Aart Bik5d75afe2015-12-14 11:57:01 -08004255enum IntrinsicSideEffects {
4256 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4257 kReadSideEffects, // Intrinsic may read heap memory.
4258 kWriteSideEffects, // Intrinsic may write heap memory.
4259 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4260};
4261
4262enum IntrinsicExceptions {
4263 kNoThrow, // Intrinsic does not throw any exceptions.
4264 kCanThrow // Intrinsic may throw exceptions.
4265};
4266
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004267class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004268 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004269 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004270
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004271 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004272 SetRawInputAt(index, argument);
4273 }
4274
Roland Levillain3e3d7332015-04-28 11:00:54 +01004275 // Return the number of arguments. This number can be lower than
4276 // the number of inputs returned by InputCount(), as some invoke
4277 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4278 // inputs at the end of their list of inputs.
4279 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4280
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004281 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4282
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004283 InvokeType GetInvokeType() const {
4284 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004285 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004286
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004287 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004288 return intrinsic_;
4289 }
4290
Aart Bik5d75afe2015-12-14 11:57:01 -08004291 void SetIntrinsic(Intrinsics intrinsic,
4292 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4293 IntrinsicSideEffects side_effects,
4294 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004295
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004296 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004297 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004298 }
4299
Aart Bikff7d89c2016-11-07 08:49:28 -08004300 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4301
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004302 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004303
Aart Bika8b8e9b2018-01-09 11:01:02 -08004304 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4305
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004306 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004307
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004308 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004309
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004310 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004311 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4312 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004313
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004314 uint32_t* GetIntrinsicOptimizations() {
4315 return &intrinsic_optimizations_;
4316 }
4317
4318 const uint32_t* GetIntrinsicOptimizations() const {
4319 return &intrinsic_optimizations_;
4320 }
4321
4322 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4323
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004324 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004325 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004326
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004327 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004328
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004329 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004330 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4331 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004332 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004333 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004334 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4335 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004336 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004337 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004338
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304339 HInvoke(InstructionKind kind,
4340 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004341 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004342 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004343 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004344 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004345 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004346 ArtMethod* resolved_method,
4347 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004348 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304349 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004350 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004351 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4352 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004353 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004354 number_of_arguments + number_of_other_inputs,
4355 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004356 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004357 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004358 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004359 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004360 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004361 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004362 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004363 }
4364
Artem Serovcced8ba2017-07-19 18:18:09 +01004365 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4366
Roland Levillain3e3d7332015-04-28 11:00:54 +01004367 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004368 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004369 const uint32_t dex_method_index_;
4370 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004371
4372 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4373 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004374};
4375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004376class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004377 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004378 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004379 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004380 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004381 uint32_t dex_pc,
4382 uint32_t dex_method_index,
4383 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304384 : HInvoke(kInvokeUnresolved,
4385 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004386 number_of_arguments,
4387 0u /* number_of_other_inputs */,
4388 return_type,
4389 dex_pc,
4390 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004391 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004392 invoke_type) {
4393 }
4394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004395 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004396
Calin Juravle175dc732015-08-25 15:42:32 +01004397 DECLARE_INSTRUCTION(InvokeUnresolved);
4398
Artem Serovcced8ba2017-07-19 18:18:09 +01004399 protected:
4400 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004401};
4402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004403class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004404 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004405 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004406 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004407 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004408 uint32_t dex_pc,
4409 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304410 : HInvoke(kInvokePolymorphic,
4411 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004412 number_of_arguments,
4413 0u /* number_of_other_inputs */,
4414 return_type,
4415 dex_pc,
4416 dex_method_index,
4417 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304418 kVirtual) {
4419 }
Orion Hodsonac141392017-01-13 11:53:47 +00004420
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004421 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004422
Orion Hodsonac141392017-01-13 11:53:47 +00004423 DECLARE_INSTRUCTION(InvokePolymorphic);
4424
Artem Serovcced8ba2017-07-19 18:18:09 +01004425 protected:
4426 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004427};
4428
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004429class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004430 public:
4431 HInvokeCustom(ArenaAllocator* allocator,
4432 uint32_t number_of_arguments,
4433 uint32_t call_site_index,
4434 DataType::Type return_type,
4435 uint32_t dex_pc)
4436 : HInvoke(kInvokeCustom,
4437 allocator,
4438 number_of_arguments,
4439 /* number_of_other_inputs */ 0u,
4440 return_type,
4441 dex_pc,
4442 /* dex_method_index */ dex::kDexNoIndex,
4443 /* resolved_method */ nullptr,
4444 kStatic),
4445 call_site_index_(call_site_index) {
4446 }
4447
4448 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4449
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004450 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004451
4452 DECLARE_INSTRUCTION(InvokeCustom);
4453
4454 protected:
4455 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4456
4457 private:
4458 uint32_t call_site_index_;
4459};
4460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004461class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004462 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004463 // Requirements of this method call regarding the class
4464 // initialization (clinit) check of its declaring class.
4465 enum class ClinitCheckRequirement {
4466 kNone, // Class already initialized.
4467 kExplicit, // Static call having explicit clinit check as last input.
4468 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004469 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004470 };
4471
Vladimir Marko58155012015-08-19 12:49:41 +00004472 // Determines how to load the target ArtMethod*.
4473 enum class MethodLoadKind {
4474 // Use a String init ArtMethod* loaded from Thread entrypoints.
4475 kStringInit,
4476
4477 // Use the method's own ArtMethod* loaded by the register allocator.
4478 kRecursive,
4479
Vladimir Marko65979462017-05-19 17:25:12 +01004480 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4481 // Used for boot image methods referenced by boot image code.
4482 kBootImageLinkTimePcRelative,
4483
Vladimir Markob066d432018-01-03 13:14:37 +00004484 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4485 // Used for app->boot calls with relocatable image.
4486 kBootImageRelRo,
4487
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004488 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004489 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004490 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004491
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004492 // Use ArtMethod* at a known address, embed the direct address in the code.
4493 // Used for for JIT-compiled calls.
4494 kJitDirectAddress,
4495
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004496 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4497 // used when other kinds are unimplemented on a particular architecture.
4498 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004499 };
4500
4501 // Determines the location of the code pointer.
4502 enum class CodePtrLocation {
4503 // Recursive call, use local PC-relative call instruction.
4504 kCallSelf,
4505
Vladimir Marko58155012015-08-19 12:49:41 +00004506 // Use code pointer from the ArtMethod*.
4507 // Used when we don't know the target code. This is also the last-resort-kind used when
4508 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4509 kCallArtMethod,
4510 };
4511
4512 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004513 MethodLoadKind method_load_kind;
4514 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004515 // The method load data holds
4516 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4517 // Note that there are multiple string init methods, each having its own offset.
4518 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004519 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004520 };
4521
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004522 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004523 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004524 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004525 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004526 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004527 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004528 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004529 InvokeType invoke_type,
4530 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004531 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304532 : HInvoke(kInvokeStaticOrDirect,
4533 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004534 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004535 // There is potentially one extra argument for the HCurrentMethod node, and
4536 // potentially one other if the clinit check is explicit, and potentially
4537 // one other if the method is a string factory.
4538 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004539 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004540 return_type,
4541 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004542 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004543 resolved_method,
4544 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004545 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004546 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004547 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4548 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004549
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004550 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004551
Vladimir Markodc151b22015-10-15 18:02:30 +01004552 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004553 bool had_current_method_input = HasCurrentMethodInput();
4554 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4555
4556 // Using the current method is the default and once we find a better
4557 // method load kind, we should not go back to using the current method.
4558 DCHECK(had_current_method_input || !needs_current_method_input);
4559
4560 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004561 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4562 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004563 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004564 dispatch_info_ = dispatch_info;
4565 }
4566
Aart Bik6daebeb2017-04-03 14:35:41 -07004567 DispatchInfo GetDispatchInfo() const {
4568 return dispatch_info_;
4569 }
4570
Vladimir Markoc53c0792015-11-19 15:48:33 +00004571 void AddSpecialInput(HInstruction* input) {
4572 // We allow only one special input.
4573 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4574 DCHECK(InputCount() == GetSpecialInputIndex() ||
4575 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4576 InsertInputAt(GetSpecialInputIndex(), input);
4577 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004578
Vladimir Marko372f10e2016-05-17 16:30:10 +01004579 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004580 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004581 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4582 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4583 DCHECK(!input_records.empty());
4584 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4585 HInstruction* last_input = input_records.back().GetInstruction();
4586 // Note: `last_input` may be null during arguments setup.
4587 if (last_input != nullptr) {
4588 // `last_input` is the last input of a static invoke marked as having
4589 // an explicit clinit check. It must either be:
4590 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4591 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4592 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4593 }
4594 }
4595 return input_records;
4596 }
4597
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004598 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004599 // We access the method via the dex cache so we can't do an implicit null check.
4600 // TODO: for intrinsics we can generate implicit null checks.
4601 return false;
4602 }
4603
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004604 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004605 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004606 }
4607
Vladimir Markoc53c0792015-11-19 15:48:33 +00004608 // Get the index of the special input, if any.
4609 //
David Brazdil6de19382016-01-08 17:37:10 +00004610 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4611 // method pointer; otherwise there may be one platform-specific special input,
4612 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004613 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004614 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004615
Vladimir Marko58155012015-08-19 12:49:41 +00004616 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4617 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4618 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004619 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004620 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004621 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004622 bool HasPcRelativeMethodLoadKind() const {
4623 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004624 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004625 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004626 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004627 bool HasCurrentMethodInput() const {
4628 // This function can be called only after the invoke has been fully initialized by the builder.
4629 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004630 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004631 return true;
4632 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004633 DCHECK(InputCount() == GetSpecialInputIndex() ||
4634 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004635 return false;
4636 }
4637 }
Vladimir Marko58155012015-08-19 12:49:41 +00004638
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004639 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004640 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004641 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004642 }
4643
4644 uint64_t GetMethodAddress() const {
4645 DCHECK(HasMethodAddress());
4646 return dispatch_info_.method_load_data;
4647 }
4648
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004649 const DexFile& GetDexFileForPcRelativeDexCache() const;
4650
Vladimir Markoa1de9182016-02-25 11:37:38 +00004651 ClinitCheckRequirement GetClinitCheckRequirement() const {
4652 return GetPackedField<ClinitCheckRequirementField>();
4653 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004654
Roland Levillain4c0eb422015-04-24 16:43:49 +01004655 // Is this instruction a call to a static method?
4656 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004657 return GetInvokeType() == kStatic;
4658 }
4659
4660 MethodReference GetTargetMethod() const {
4661 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004662 }
4663
Vladimir Markofbb184a2015-11-13 14:47:00 +00004664 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4665 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4666 // instruction; only relevant for static calls with explicit clinit check.
4667 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004668 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004669 size_t last_input_index = inputs_.size() - 1u;
4670 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004671 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004672 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004673 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004674 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004675 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004676 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004677 }
4678
4679 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004680 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004681 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004682 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004683 }
4684
4685 // Is this a call to a static method whose declaring class has an
4686 // implicit intialization check requirement?
4687 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004688 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004689 }
4690
Vladimir Markob554b5a2015-11-06 12:57:55 +00004691 // Does this method load kind need the current method as an input?
4692 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004693 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004694 }
4695
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004696 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004697
Artem Serovcced8ba2017-07-19 18:18:09 +01004698 protected:
4699 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4700
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004701 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004702 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004703 static constexpr size_t kFieldClinitCheckRequirementSize =
4704 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4705 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4706 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4707 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4708 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004709 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4710 kFieldClinitCheckRequirement,
4711 kFieldClinitCheckRequirementSize>;
4712
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004713 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004714 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004715 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004716};
Vladimir Markof64242a2015-12-01 14:58:23 +00004717std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004718std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004719
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004720class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004721 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004722 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004723 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004724 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004725 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004726 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004727 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004728 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304729 : HInvoke(kInvokeVirtual,
4730 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004731 number_of_arguments,
4732 0u,
4733 return_type,
4734 dex_pc,
4735 dex_method_index,
4736 resolved_method,
4737 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304738 vtable_index_(vtable_index) {
4739 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004740
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004741 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004742
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004743 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004744 switch (GetIntrinsic()) {
4745 case Intrinsics::kThreadCurrentThread:
4746 case Intrinsics::kStringBufferAppend:
4747 case Intrinsics::kStringBufferToString:
4748 case Intrinsics::kStringBuilderAppend:
4749 case Intrinsics::kStringBuilderToString:
4750 return false;
4751 default:
4752 return HInvoke::CanBeNull();
4753 }
4754 }
4755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004756 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004757 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004758 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004759 }
4760
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004761 uint32_t GetVTableIndex() const { return vtable_index_; }
4762
4763 DECLARE_INSTRUCTION(InvokeVirtual);
4764
Artem Serovcced8ba2017-07-19 18:18:09 +01004765 protected:
4766 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4767
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004768 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004769 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004770 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004771};
4772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004773class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004774 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004775 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004776 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004777 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004778 uint32_t dex_pc,
4779 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004780 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004781 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304782 : HInvoke(kInvokeInterface,
4783 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004784 number_of_arguments,
4785 0u,
4786 return_type,
4787 dex_pc,
4788 dex_method_index,
4789 resolved_method,
4790 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304791 imt_index_(imt_index) {
4792 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004793
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004794 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004795
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004796 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004797 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004798 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004799 }
4800
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004801 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004802 // The assembly stub currently needs it.
4803 return true;
4804 }
4805
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004806 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004807
4808 DECLARE_INSTRUCTION(InvokeInterface);
4809
Artem Serovcced8ba2017-07-19 18:18:09 +01004810 protected:
4811 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4812
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004813 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004814 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004815 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004816};
4817
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004818class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004819 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004820 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304821 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004822 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004823 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004824
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004825 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004827 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004828 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004829 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004830 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004831 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004832 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004833 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004834 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4835 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004836 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004837 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4838 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004839
Roland Levillain88cb1752014-10-20 16:36:47 +01004840 DECLARE_INSTRUCTION(Neg);
4841
Artem Serovcced8ba2017-07-19 18:18:09 +01004842 protected:
4843 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004844};
4845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004846class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004847 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004848 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304849 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004850 SetRawInputAt(0, cls);
4851 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004852 }
4853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004854 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004855
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004856 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004857 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004858
Mingyao Yang0c365e62015-03-31 15:09:29 -07004859 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004860 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004862 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004863
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004864 HLoadClass* GetLoadClass() const {
4865 DCHECK(InputAt(0)->IsLoadClass());
4866 return InputAt(0)->AsLoadClass();
4867 }
4868
4869 HInstruction* GetLength() const {
4870 return InputAt(1);
4871 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004872
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004873 DECLARE_INSTRUCTION(NewArray);
4874
Artem Serovcced8ba2017-07-19 18:18:09 +01004875 protected:
4876 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004877};
4878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004879class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004880 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004881 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004882 HInstruction* left,
4883 HInstruction* right,
4884 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304885 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4886 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004888 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004889
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004890 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004892 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004893 return GetBlock()->GetGraph()->GetIntConstant(
4894 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004895 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004896 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004897 return GetBlock()->GetGraph()->GetLongConstant(
4898 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004899 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004900 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004901 return GetBlock()->GetGraph()->GetFloatConstant(
4902 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4903 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004904 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004905 return GetBlock()->GetGraph()->GetDoubleConstant(
4906 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4907 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004908
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004909 DECLARE_INSTRUCTION(Add);
4910
Artem Serovcced8ba2017-07-19 18:18:09 +01004911 protected:
4912 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004913};
4914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004915class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004916 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004917 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004918 HInstruction* left,
4919 HInstruction* right,
4920 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304921 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4922 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004923
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004924 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004925
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004926 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004927 return GetBlock()->GetGraph()->GetIntConstant(
4928 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004929 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004930 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 return GetBlock()->GetGraph()->GetLongConstant(
4932 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004933 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004934 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004935 return GetBlock()->GetGraph()->GetFloatConstant(
4936 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4937 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004938 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004939 return GetBlock()->GetGraph()->GetDoubleConstant(
4940 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4941 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004942
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004943 DECLARE_INSTRUCTION(Sub);
4944
Artem Serovcced8ba2017-07-19 18:18:09 +01004945 protected:
4946 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004947};
4948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004949class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004950 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004951 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004952 HInstruction* left,
4953 HInstruction* right,
4954 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304955 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4956 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004958 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004959
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004960 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004962 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004963 return GetBlock()->GetGraph()->GetIntConstant(
4964 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004965 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004966 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004967 return GetBlock()->GetGraph()->GetLongConstant(
4968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004971 return GetBlock()->GetGraph()->GetFloatConstant(
4972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4973 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004974 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004975 return GetBlock()->GetGraph()->GetDoubleConstant(
4976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4977 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004978
4979 DECLARE_INSTRUCTION(Mul);
4980
Artem Serovcced8ba2017-07-19 18:18:09 +01004981 protected:
4982 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004983};
4984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004985class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004986 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004987 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004988 HInstruction* left,
4989 HInstruction* right,
4990 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304991 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4992 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004993
Roland Levillain9867bc72015-08-05 10:21:34 +01004994 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004995 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004996 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004997 // Our graph structure ensures we never have 0 for `y` during
4998 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004999 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005000 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005001 return (y == -1) ? -x : x / y;
5002 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005003
Roland Levillain31dd3d62016-02-16 12:21:02 +00005004 template <typename T>
5005 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005006 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005007 return x / y;
5008 }
5009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005010 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005011 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005012 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005013 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005014 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005015 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005016 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5017 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005018 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005019 return GetBlock()->GetGraph()->GetFloatConstant(
5020 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5021 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005022 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005023 return GetBlock()->GetGraph()->GetDoubleConstant(
5024 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005025 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005026
5027 DECLARE_INSTRUCTION(Div);
5028
Artem Serovcced8ba2017-07-19 18:18:09 +01005029 protected:
5030 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005031};
5032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005033class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005034 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005035 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005036 HInstruction* left,
5037 HInstruction* right,
5038 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305039 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5040 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005041
Roland Levillain9867bc72015-08-05 10:21:34 +01005042 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005043 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005044 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005045 // Our graph structure ensures we never have 0 for `y` during
5046 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005047 DCHECK_NE(y, 0);
5048 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5049 return (y == -1) ? 0 : x % y;
5050 }
5051
Roland Levillain31dd3d62016-02-16 12:21:02 +00005052 template <typename T>
5053 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005054 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005055 return std::fmod(x, y);
5056 }
5057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005058 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005059 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005060 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005061 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005062 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005064 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005065 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005066 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005067 return GetBlock()->GetGraph()->GetFloatConstant(
5068 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5069 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005070 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005071 return GetBlock()->GetGraph()->GetDoubleConstant(
5072 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5073 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005074
5075 DECLARE_INSTRUCTION(Rem);
5076
Artem Serovcced8ba2017-07-19 18:18:09 +01005077 protected:
5078 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005079};
5080
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005081class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005082 public:
5083 HMin(DataType::Type result_type,
5084 HInstruction* left,
5085 HInstruction* right,
5086 uint32_t dex_pc)
5087 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5088
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005089 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005090
5091 // Evaluation for integral values.
5092 template <typename T> static T ComputeIntegral(T x, T y) {
5093 return (x <= y) ? x : y;
5094 }
5095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005096 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005097 return GetBlock()->GetGraph()->GetIntConstant(
5098 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5099 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005100 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005101 return GetBlock()->GetGraph()->GetLongConstant(
5102 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5103 }
5104 // TODO: Evaluation for floating-point values.
5105 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005106 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005107 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005108 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005109
5110 DECLARE_INSTRUCTION(Min);
5111
5112 protected:
5113 DEFAULT_COPY_CONSTRUCTOR(Min);
5114};
5115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005116class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005117 public:
5118 HMax(DataType::Type result_type,
5119 HInstruction* left,
5120 HInstruction* right,
5121 uint32_t dex_pc)
5122 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005124 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005125
5126 // Evaluation for integral values.
5127 template <typename T> static T ComputeIntegral(T x, T y) {
5128 return (x >= y) ? x : y;
5129 }
5130
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005131 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005132 return GetBlock()->GetGraph()->GetIntConstant(
5133 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5134 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005135 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005136 return GetBlock()->GetGraph()->GetLongConstant(
5137 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5138 }
5139 // TODO: Evaluation for floating-point values.
5140 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005141 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005142 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005143 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005144
5145 DECLARE_INSTRUCTION(Max);
5146
5147 protected:
5148 DEFAULT_COPY_CONSTRUCTOR(Max);
5149};
5150
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005151class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005152 public:
5153 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5154 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5155
5156 // Evaluation for integral values.
5157 template <typename T> static T ComputeIntegral(T x) {
5158 return x < 0 ? -x : x;
5159 }
5160
5161 // Evaluation for floating-point values.
5162 // Note, as a "quality of implementation", rather than pure "spec compliance",
5163 // we require that Math.abs() clears the sign bit (but changes nothing else)
5164 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5165 // http://b/30758343
5166 template <typename T, typename S> static T ComputeFP(T x) {
5167 S bits = bit_cast<S, T>(x);
5168 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5169 }
5170
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005171 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005172 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5173 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005174 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005175 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5176 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005177 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005178 return GetBlock()->GetGraph()->GetFloatConstant(
5179 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5180 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005181 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005182 return GetBlock()->GetGraph()->GetDoubleConstant(
5183 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5184 }
5185
5186 DECLARE_INSTRUCTION(Abs);
5187
5188 protected:
5189 DEFAULT_COPY_CONSTRUCTOR(Abs);
5190};
5191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005192class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005193 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005194 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005195 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5196 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005197 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005198 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005199 SetRawInputAt(0, value);
5200 }
5201
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005202 bool IsClonable() const override { return true; }
5203 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005204
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005205 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005206 return true;
5207 }
5208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 bool NeedsEnvironment() const override { return true; }
5210 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005211
Calin Juravled0d48522014-11-04 16:40:20 +00005212 DECLARE_INSTRUCTION(DivZeroCheck);
5213
Artem Serovcced8ba2017-07-19 18:18:09 +01005214 protected:
5215 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005216};
5217
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005218class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005219 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005220 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005221 HInstruction* value,
5222 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005223 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305224 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005225 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5226 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005227 }
5228
Roland Levillain5b5b9312016-03-22 14:57:31 +00005229 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005230 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005231 return value << (distance & max_shift_distance);
5232 }
5233
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005234 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005235 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005236 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005237 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005238 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005239 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005240 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005241 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005242 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005243 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005244 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5245 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005246 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005247 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005248 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005249 LOG(FATAL) << DebugName() << " is not defined for float values";
5250 UNREACHABLE();
5251 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005252 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005253 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005254 LOG(FATAL) << DebugName() << " is not defined for double values";
5255 UNREACHABLE();
5256 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005257
5258 DECLARE_INSTRUCTION(Shl);
5259
Artem Serovcced8ba2017-07-19 18:18:09 +01005260 protected:
5261 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005262};
5263
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005264class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005265 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005266 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005267 HInstruction* value,
5268 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005269 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305270 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005271 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5272 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005273 }
5274
Roland Levillain5b5b9312016-03-22 14:57:31 +00005275 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005276 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005277 return value >> (distance & max_shift_distance);
5278 }
5279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005280 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005281 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005282 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005283 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005285 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005286 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005287 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005288 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005289 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005290 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5291 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005292 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005293 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005294 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005295 LOG(FATAL) << DebugName() << " is not defined for float values";
5296 UNREACHABLE();
5297 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005298 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005299 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005300 LOG(FATAL) << DebugName() << " is not defined for double values";
5301 UNREACHABLE();
5302 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005303
5304 DECLARE_INSTRUCTION(Shr);
5305
Artem Serovcced8ba2017-07-19 18:18:09 +01005306 protected:
5307 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005308};
5309
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005310class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005311 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005312 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005313 HInstruction* value,
5314 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005315 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305316 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005317 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5318 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005319 }
5320
Roland Levillain5b5b9312016-03-22 14:57:31 +00005321 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005322 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005323 typedef typename std::make_unsigned<T>::type V;
5324 V ux = static_cast<V>(value);
5325 return static_cast<T>(ux >> (distance & max_shift_distance));
5326 }
5327
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005328 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005329 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005330 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005331 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005332 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005333 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005334 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005335 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005336 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005337 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005338 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5339 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005340 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005341 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005342 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005343 LOG(FATAL) << DebugName() << " is not defined for float values";
5344 UNREACHABLE();
5345 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005346 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005347 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005348 LOG(FATAL) << DebugName() << " is not defined for double values";
5349 UNREACHABLE();
5350 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005351
5352 DECLARE_INSTRUCTION(UShr);
5353
Artem Serovcced8ba2017-07-19 18:18:09 +01005354 protected:
5355 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005356};
5357
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005358class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005359 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005360 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005361 HInstruction* left,
5362 HInstruction* right,
5363 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305364 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5365 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005366
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005367 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005368
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005369 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005370
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005371 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005372 return GetBlock()->GetGraph()->GetIntConstant(
5373 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005374 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005375 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005376 return GetBlock()->GetGraph()->GetLongConstant(
5377 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005378 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005379 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005380 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005381 LOG(FATAL) << DebugName() << " is not defined for float values";
5382 UNREACHABLE();
5383 }
5384 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005385 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005386 LOG(FATAL) << DebugName() << " is not defined for double values";
5387 UNREACHABLE();
5388 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005389
5390 DECLARE_INSTRUCTION(And);
5391
Artem Serovcced8ba2017-07-19 18:18:09 +01005392 protected:
5393 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005394};
5395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005396class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005397 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005398 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005399 HInstruction* left,
5400 HInstruction* right,
5401 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305402 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5403 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005405 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005406
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005407 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005408
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005409 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005410 return GetBlock()->GetGraph()->GetIntConstant(
5411 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005412 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005413 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005414 return GetBlock()->GetGraph()->GetLongConstant(
5415 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005416 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005417 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005418 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005419 LOG(FATAL) << DebugName() << " is not defined for float values";
5420 UNREACHABLE();
5421 }
5422 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005423 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005424 LOG(FATAL) << DebugName() << " is not defined for double values";
5425 UNREACHABLE();
5426 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005427
5428 DECLARE_INSTRUCTION(Or);
5429
Artem Serovcced8ba2017-07-19 18:18:09 +01005430 protected:
5431 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005432};
5433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005434class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005435 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005436 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005437 HInstruction* left,
5438 HInstruction* right,
5439 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305440 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5441 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005442
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005443 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005444
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005445 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005446
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005447 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005448 return GetBlock()->GetGraph()->GetIntConstant(
5449 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005450 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005451 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005452 return GetBlock()->GetGraph()->GetLongConstant(
5453 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005454 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005455 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005456 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005457 LOG(FATAL) << DebugName() << " is not defined for float values";
5458 UNREACHABLE();
5459 }
5460 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005461 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005462 LOG(FATAL) << DebugName() << " is not defined for double values";
5463 UNREACHABLE();
5464 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005465
5466 DECLARE_INSTRUCTION(Xor);
5467
Artem Serovcced8ba2017-07-19 18:18:09 +01005468 protected:
5469 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005470};
5471
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005472class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005473 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005474 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305475 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005476 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5477 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005478 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005479
Roland Levillain5b5b9312016-03-22 14:57:31 +00005480 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005481 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005482 typedef typename std::make_unsigned<T>::type V;
5483 V ux = static_cast<V>(value);
5484 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005485 return static_cast<T>(ux);
5486 } else {
5487 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005488 return static_cast<T>(ux >> (distance & max_shift_value)) |
5489 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005490 }
5491 }
5492
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005493 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005494 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005495 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005496 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005497 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005498 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005499 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005500 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005501 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005502 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005503 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5504 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005505 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005506 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005507 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005508 LOG(FATAL) << DebugName() << " is not defined for float values";
5509 UNREACHABLE();
5510 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005511 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005512 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005513 LOG(FATAL) << DebugName() << " is not defined for double values";
5514 UNREACHABLE();
5515 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005516
5517 DECLARE_INSTRUCTION(Ror);
5518
Artem Serovcced8ba2017-07-19 18:18:09 +01005519 protected:
5520 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005521};
5522
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005523// The value of a parameter in this method. Its location depends on
5524// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005525class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005526 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005527 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005528 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005529 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005531 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305532 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005533 dex_file_(dex_file),
5534 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005535 index_(index) {
5536 SetPackedFlag<kFlagIsThis>(is_this);
5537 SetPackedFlag<kFlagCanBeNull>(!is_this);
5538 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005539
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005540 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005541 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005542 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005543 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005544
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005545 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005546 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005547
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005548 DECLARE_INSTRUCTION(ParameterValue);
5549
Artem Serovcced8ba2017-07-19 18:18:09 +01005550 protected:
5551 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5552
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005553 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005554 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005555 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005556 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5557 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5558 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5559 "Too many packed fields.");
5560
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005561 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005562 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005563 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005564 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005565 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005566};
5567
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005568class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005569 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005570 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305571 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5572 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005574 bool CanBeMoved() const override { return true; }
5575 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005576 return true;
5577 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005578
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005579 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005580
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005581 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005582 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005583 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005584 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005585 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005586 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005587 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005588 LOG(FATAL) << DebugName() << " is not defined for float values";
5589 UNREACHABLE();
5590 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005591 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005592 LOG(FATAL) << DebugName() << " is not defined for double values";
5593 UNREACHABLE();
5594 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005595
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005596 DECLARE_INSTRUCTION(Not);
5597
Artem Serovcced8ba2017-07-19 18:18:09 +01005598 protected:
5599 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005600};
5601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005602class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005603 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005604 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305605 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5606 }
David Brazdil66d126e2015-04-03 16:02:44 +01005607
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005608 bool CanBeMoved() const override { return true; }
5609 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005610 return true;
5611 }
5612
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005613 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005614 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005615 return !x;
5616 }
5617
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005618 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005619 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005620 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005621 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005622 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005623 UNREACHABLE();
5624 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005625 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005626 LOG(FATAL) << DebugName() << " is not defined for float values";
5627 UNREACHABLE();
5628 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005629 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005630 LOG(FATAL) << DebugName() << " is not defined for double values";
5631 UNREACHABLE();
5632 }
David Brazdil66d126e2015-04-03 16:02:44 +01005633
5634 DECLARE_INSTRUCTION(BooleanNot);
5635
Artem Serovcced8ba2017-07-19 18:18:09 +01005636 protected:
5637 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005638};
5639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005640class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005641 public:
5642 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005643 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305644 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005645 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005646 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005647 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005648 }
5649
5650 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005651 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5652 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005653
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005654 bool IsClonable() const override { return true; }
5655 bool CanBeMoved() const override { return true; }
5656 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005657 return true;
5658 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005659
Mark Mendelle82549b2015-05-06 10:55:34 -04005660 // Try to statically evaluate the conversion and return a HConstant
5661 // containing the result. If the input cannot be converted, return nullptr.
5662 HConstant* TryStaticEvaluation() const;
5663
Roland Levillaindff1f282014-11-05 14:15:05 +00005664 DECLARE_INSTRUCTION(TypeConversion);
5665
Artem Serovcced8ba2017-07-19 18:18:09 +01005666 protected:
5667 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005668};
5669
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005670static constexpr uint32_t kNoRegNumber = -1;
5671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005672class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005673 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005674 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005675 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5676 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005677 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005678 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005679 SetRawInputAt(0, value);
5680 }
5681
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005682 bool IsClonable() const override { return true; }
5683 bool CanBeMoved() const override { return true; }
5684 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005685 return true;
5686 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005687
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005688 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005689
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005690 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005691
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005692 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005693
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005694 DECLARE_INSTRUCTION(NullCheck);
5695
Artem Serovcced8ba2017-07-19 18:18:09 +01005696 protected:
5697 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005698};
5699
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005700// Embeds an ArtField and all the information required by the compiler. We cache
5701// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005702class FieldInfo : public ValueObject {
5703 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005704 FieldInfo(ArtField* field,
5705 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005706 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005707 bool is_volatile,
5708 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005709 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005710 const DexFile& dex_file)
5711 : field_(field),
5712 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005713 field_type_(field_type),
5714 is_volatile_(is_volatile),
5715 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005716 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005717 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005718
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005719 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005720 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005721 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005722 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005723 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005724 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005725 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005726
5727 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005728 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005729 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005730 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005731 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005732 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005733 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005734 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005735};
5736
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005737class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005738 public:
5739 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005740 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005741 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005742 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005743 bool is_volatile,
5744 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005745 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005746 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005747 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305748 : HExpression(kInstanceFieldGet,
5749 field_type,
5750 SideEffects::FieldReadOfType(field_type, is_volatile),
5751 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005752 field_info_(field,
5753 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005754 field_type,
5755 is_volatile,
5756 field_idx,
5757 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005758 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005759 SetRawInputAt(0, value);
5760 }
5761
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005762 bool IsClonable() const override { return true; }
5763 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005764
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005765 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005766 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005767 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005768 }
5769
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005770 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005771 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005772 }
5773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005774 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005775 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5776 }
5777
Calin Juravle52c48962014-12-16 17:02:57 +00005778 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005779 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005780 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005781 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005782
Vladimir Marko61b92282017-10-11 13:23:17 +01005783 void SetType(DataType::Type new_type) {
5784 DCHECK(DataType::IsIntegralType(GetType()));
5785 DCHECK(DataType::IsIntegralType(new_type));
5786 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5787 SetPackedField<TypeField>(new_type);
5788 }
5789
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005790 DECLARE_INSTRUCTION(InstanceFieldGet);
5791
Artem Serovcced8ba2017-07-19 18:18:09 +01005792 protected:
5793 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5794
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005795 private:
5796 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005797};
5798
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005799class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005800 public:
5801 HInstanceFieldSet(HInstruction* object,
5802 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005803 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005804 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005805 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005806 bool is_volatile,
5807 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005808 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005809 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005810 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005811 : HExpression(kInstanceFieldSet,
5812 SideEffects::FieldWriteOfType(field_type, is_volatile),
5813 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005814 field_info_(field,
5815 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005816 field_type,
5817 is_volatile,
5818 field_idx,
5819 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005820 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005821 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005822 SetRawInputAt(0, object);
5823 SetRawInputAt(1, value);
5824 }
5825
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005826 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005828 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005829 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005830 }
5831
Calin Juravle52c48962014-12-16 17:02:57 +00005832 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005833 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005834 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005835 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005836 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005837 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5838 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005839
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005840 DECLARE_INSTRUCTION(InstanceFieldSet);
5841
Artem Serovcced8ba2017-07-19 18:18:09 +01005842 protected:
5843 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5844
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005845 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005846 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5847 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5848 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5849 "Too many packed fields.");
5850
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005851 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005852};
5853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005854class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005855 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005856 HArrayGet(HInstruction* array,
5857 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005858 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005859 uint32_t dex_pc)
5860 : HArrayGet(array,
5861 index,
5862 type,
5863 SideEffects::ArrayReadOfType(type),
5864 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305865 /* is_string_char_at */ false) {
5866 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005867
5868 HArrayGet(HInstruction* array,
5869 HInstruction* index,
5870 DataType::Type type,
5871 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005872 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005873 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305874 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005875 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005876 SetRawInputAt(0, array);
5877 SetRawInputAt(1, index);
5878 }
5879
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005880 bool IsClonable() const override { return true; }
5881 bool CanBeMoved() const override { return true; }
5882 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005883 return true;
5884 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005885 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005886 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005887 // Currently, unless the array is the result of NewArray, the array access is always
5888 // preceded by some form of null NullCheck necessary for the bounds check, usually
5889 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5890 // dynamic BCE. There are cases when these could be removed to produce better code.
5891 // If we ever add optimizations to do so we should allow an implicit check here
5892 // (as long as the address falls in the first page).
5893 //
5894 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5895 // a = cond ? new int[1] : null;
5896 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005897 return false;
5898 }
5899
David Brazdil4833f5a2015-12-16 10:37:39 +00005900 bool IsEquivalentOf(HArrayGet* other) const {
5901 bool result = (GetDexPc() == other->GetDexPc());
5902 if (kIsDebugBuild && result) {
5903 DCHECK_EQ(GetBlock(), other->GetBlock());
5904 DCHECK_EQ(GetArray(), other->GetArray());
5905 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005906 if (DataType::IsIntOrLongType(GetType())) {
5907 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005908 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005909 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5910 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005911 }
5912 }
5913 return result;
5914 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005915
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005916 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5917
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005918 HInstruction* GetArray() const { return InputAt(0); }
5919 HInstruction* GetIndex() const { return InputAt(1); }
5920
Vladimir Marko61b92282017-10-11 13:23:17 +01005921 void SetType(DataType::Type new_type) {
5922 DCHECK(DataType::IsIntegralType(GetType()));
5923 DCHECK(DataType::IsIntegralType(new_type));
5924 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5925 SetPackedField<TypeField>(new_type);
5926 }
5927
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005928 DECLARE_INSTRUCTION(ArrayGet);
5929
Artem Serovcced8ba2017-07-19 18:18:09 +01005930 protected:
5931 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5932
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005933 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005934 // We treat a String as an array, creating the HArrayGet from String.charAt()
5935 // intrinsic in the instruction simplifier. We can always determine whether
5936 // a particular HArrayGet is actually a String.charAt() by looking at the type
5937 // of the input but that requires holding the mutator lock, so we prefer to use
5938 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005939 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005940 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5941 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5942 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005943};
5944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005945class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005946 public:
5947 HArraySet(HInstruction* array,
5948 HInstruction* index,
5949 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005950 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005951 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005952 : HArraySet(array,
5953 index,
5954 value,
5955 expected_component_type,
5956 // Make a best guess for side effects now, may be refined during SSA building.
5957 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305958 dex_pc) {
5959 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005960
5961 HArraySet(HInstruction* array,
5962 HInstruction* index,
5963 HInstruction* value,
5964 DataType::Type expected_component_type,
5965 SideEffects side_effects,
5966 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005967 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005968 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005969 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005970 SetPackedFlag<kFlagValueCanBeNull>(true);
5971 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005972 SetRawInputAt(0, array);
5973 SetRawInputAt(1, index);
5974 SetRawInputAt(2, value);
5975 }
5976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005977 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005979 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005980 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005981 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005982 }
5983
Mingyao Yang81014cb2015-06-02 03:16:27 -07005984 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005985 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005987 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005988 // TODO: Same as for ArrayGet.
5989 return false;
5990 }
5991
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005992 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005993 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005994 }
5995
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005996 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005997 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005998 }
5999
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006000 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006001 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006002 }
6003
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6005 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6006 bool StaticTypeOfArrayIsObjectArray() const {
6007 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6008 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006009
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006010 HInstruction* GetArray() const { return InputAt(0); }
6011 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006012 HInstruction* GetValue() const { return InputAt(2); }
6013
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006014 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006015 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6016 }
6017
6018 static DataType::Type GetComponentType(DataType::Type value_type,
6019 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006020 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006021 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006022 // be correct, we also check what is the value type. If it is a floating
6023 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006024 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006025 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006026 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006027 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006028
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006029 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006030 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006031 }
6032
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006033 static SideEffects ComputeSideEffects(DataType::Type type) {
6034 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006035 }
6036
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006037 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6038 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6039 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006040 }
6041
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006042 DECLARE_INSTRUCTION(ArraySet);
6043
Artem Serovcced8ba2017-07-19 18:18:09 +01006044 protected:
6045 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6046
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006047 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006048 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6049 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006050 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006051 static constexpr size_t kFlagNeedsTypeCheck =
6052 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6053 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006054 // Cached information for the reference_type_info_ so that codegen
6055 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006056 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6057 static constexpr size_t kNumberOfArraySetPackedBits =
6058 kFlagStaticTypeOfArrayIsObjectArray + 1;
6059 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6060 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006061 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006062};
6063
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006064class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006065 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006066 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306067 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006068 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006069 // Note that arrays do not change length, so the instruction does not
6070 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006071 SetRawInputAt(0, array);
6072 }
6073
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006074 bool IsClonable() const override { return true; }
6075 bool CanBeMoved() const override { return true; }
6076 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006077 return true;
6078 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006079 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006080 return obj == InputAt(0);
6081 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006082
Vladimir Markodce016e2016-04-28 13:10:02 +01006083 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6084
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006085 DECLARE_INSTRUCTION(ArrayLength);
6086
Artem Serovcced8ba2017-07-19 18:18:09 +01006087 protected:
6088 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6089
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006090 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006091 // We treat a String as an array, creating the HArrayLength from String.length()
6092 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6093 // determine whether a particular HArrayLength is actually a String.length() by
6094 // looking at the type of the input but that requires holding the mutator lock, so
6095 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006096 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006097 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6098 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6099 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006100};
6101
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006102class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006103 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006104 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006105 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6106 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006107 HBoundsCheck(HInstruction* index,
6108 HInstruction* length,
6109 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006110 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006111 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006112 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006113 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006114 SetRawInputAt(0, index);
6115 SetRawInputAt(1, length);
6116 }
6117
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006118 bool IsClonable() const override { return true; }
6119 bool CanBeMoved() const override { return true; }
6120 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006121 return true;
6122 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006124 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006126 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006127
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006128 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006129
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006130 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006131
6132 DECLARE_INSTRUCTION(BoundsCheck);
6133
Artem Serovcced8ba2017-07-19 18:18:09 +01006134 protected:
6135 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6136
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006137 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006138 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006139};
6140
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006141class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006142 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006143 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006144 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306145 slow_path_(nullptr) {
6146 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006148 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006149
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006150 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006151 return true;
6152 }
6153
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006154 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6155 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006156
6157 DECLARE_INSTRUCTION(SuspendCheck);
6158
Artem Serovcced8ba2017-07-19 18:18:09 +01006159 protected:
6160 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6161
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006162 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006163 // Only used for code generation, in order to share the same slow path between back edges
6164 // of a same loop.
6165 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006166};
6167
David Srbecky0cf44932015-12-09 14:09:59 +00006168// Pseudo-instruction which provides the native debugger with mapping information.
6169// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006170class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006171 public:
6172 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006173 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306174 }
David Srbecky0cf44932015-12-09 14:09:59 +00006175
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006176 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006177 return true;
6178 }
6179
6180 DECLARE_INSTRUCTION(NativeDebugInfo);
6181
Artem Serovcced8ba2017-07-19 18:18:09 +01006182 protected:
6183 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006184};
6185
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006186/**
6187 * Instruction to load a Class object.
6188 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006189class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006190 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006191 // Determines how to load the Class.
6192 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006193 // We cannot load this class. See HSharpening::SharpenLoadClass.
6194 kInvalid = -1,
6195
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006196 // Use the Class* from the method's own ArtMethod*.
6197 kReferrersClass,
6198
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006199 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006200 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006201 kBootImageLinkTimePcRelative,
6202
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006203 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006204 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006205 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006206
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006207 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006208 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006209 kBssEntry,
6210
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006211 // Use a known boot image Class* address, embedded in the code by the codegen.
6212 // Used for boot image classes referenced by apps in JIT-compiled code.
6213 kJitBootImageAddress,
6214
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006215 // Load from the root table associated with the JIT compiled method.
6216 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006217
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006218 // Load using a simple runtime call. This is the fall-back load kind when
6219 // the codegen is unable to use another appropriate kind.
6220 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006221
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006222 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006223 };
6224
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006225 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006226 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006227 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006228 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006229 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006230 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006231 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006232 : HInstruction(kLoadClass,
6233 DataType::Type::kReference,
6234 SideEffectsForArchRuntimeCalls(),
6235 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006236 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006237 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006238 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006239 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006240 // Referrers class should not need access check. We never inline unverified
6241 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006242 DCHECK(!is_referrers_class || !needs_access_check);
6243
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006244 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006245 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006246 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006247 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006248 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006249 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006250 }
6251
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006252 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006253
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006254 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006255
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006256 LoadKind GetLoadKind() const {
6257 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006258 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006259
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006260 bool HasPcRelativeLoadKind() const {
6261 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6262 GetLoadKind() == LoadKind::kBootImageRelRo ||
6263 GetLoadKind() == LoadKind::kBssEntry;
6264 }
6265
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006266 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006267
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006268 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006269
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006270 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006271
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006272 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006274 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006275 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006276 }
6277
Calin Juravle0ba218d2015-05-19 18:46:01 +01006278 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006279 // The entrypoint the code generator is going to call does not do
6280 // clinit of the class.
6281 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006282 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006283 }
6284
6285 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006286 return NeedsAccessCheck() ||
6287 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006288 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006289 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006290 }
6291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006292 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006293 return NeedsAccessCheck() ||
6294 MustGenerateClinitCheck() ||
6295 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006296 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006297 GetLoadKind() == LoadKind::kBssEntry) &&
6298 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006299 }
6300
Calin Juravleacf735c2015-02-12 15:25:22 +00006301 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006302 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6303 // Note: The is_exact flag from the return value should not be used.
6304 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6305 } else {
6306 return ReferenceTypeInfo::CreateInvalid();
6307 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006308 }
6309
Vladimir Marko175e7862018-03-27 09:03:13 +00006310 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6311 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6312 DCHECK(klass_ != nullptr);
6313 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006314 }
6315
Andreas Gampea5b09a62016-11-17 15:21:22 -08006316 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006317 const DexFile& GetDexFile() const { return dex_file_; }
6318
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006319 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006320 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006321 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006322
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006323 static SideEffects SideEffectsForArchRuntimeCalls() {
6324 return SideEffects::CanTriggerGC();
6325 }
6326
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006327 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006328 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006329 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006330 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006331
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006332 bool MustResolveTypeOnSlowPath() const {
6333 // Check that this instruction has a slow path.
6334 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6335 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6336 return GetLoadKind() == LoadKind::kBssEntry;
6337 }
6338
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006339 void MarkInBootImage() {
6340 SetPackedFlag<kFlagIsInBootImage>(true);
6341 }
6342
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006343 void AddSpecialInput(HInstruction* special_input);
6344
6345 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006346 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006347 return ArrayRef<HUserRecord<HInstruction*>>(
6348 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6349 }
6350
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006351 Handle<mirror::Class> GetClass() const {
6352 return klass_;
6353 }
6354
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006355 DECLARE_INSTRUCTION(LoadClass);
6356
Artem Serovcced8ba2017-07-19 18:18:09 +01006357 protected:
6358 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6359
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006360 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006361 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006362 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006363 // Whether this instruction must generate the initialization check.
6364 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006365 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006366 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6367 static constexpr size_t kFieldLoadKindSize =
6368 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006369 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6370 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006371 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006372 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6373
6374 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006375 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006376 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006377 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006378 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006379 }
6380
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006381 void SetLoadKindInternal(LoadKind load_kind);
6382
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006383 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006384 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006385 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006386 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006387
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006388 // A type index and dex file where the class can be accessed. The dex file can be:
6389 // - The compiling method's dex file if the class is defined there too.
6390 // - The compiling method's dex file if the class is referenced there.
6391 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006392 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006393 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006394 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006395
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006396 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006397};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006398std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6399
6400// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006401inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6402 // The load kind should be determined before inserting the instruction to the graph.
6403 DCHECK(GetBlock() == nullptr);
6404 DCHECK(GetEnvironment() == nullptr);
6405 SetPackedField<LoadKindField>(load_kind);
6406 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6407 special_input_ = HUserRecord<HInstruction*>(nullptr);
6408 }
6409 if (!NeedsEnvironment()) {
6410 SetSideEffects(SideEffects::None());
6411 }
6412}
6413
6414// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006415inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006416 // The special input is used for PC-relative loads on some architectures,
6417 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006418 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006419 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006420 GetLoadKind() == LoadKind::kBssEntry ||
6421 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006422 DCHECK(special_input_.GetInstruction() == nullptr);
6423 special_input_ = HUserRecord<HInstruction*>(special_input);
6424 special_input->AddUseAt(this, 0);
6425}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006426
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006427class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006428 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006429 // Determines how to load the String.
6430 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006431 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006432 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006433 kBootImageLinkTimePcRelative,
6434
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006435 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006436 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006437 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006438
Vladimir Markoaad75c62016-10-03 08:46:48 +00006439 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006440 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006441 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006442
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006443 // Use a known boot image String* address, embedded in the code by the codegen.
6444 // Used for boot image strings referenced by apps in JIT-compiled code.
6445 kJitBootImageAddress,
6446
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006447 // Load from the root table associated with the JIT compiled method.
6448 kJitTableAddress,
6449
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006450 // Load using a simple runtime call. This is the fall-back load kind when
6451 // the codegen is unable to use another appropriate kind.
6452 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006453
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006454 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006455 };
6456
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006457 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006458 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006459 const DexFile& dex_file,
6460 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006461 : HInstruction(kLoadString,
6462 DataType::Type::kReference,
6463 SideEffectsForArchRuntimeCalls(),
6464 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006465 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006466 string_index_(string_index),
6467 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006468 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006469 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006470
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006471 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006472
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006473 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006474
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006475 LoadKind GetLoadKind() const {
6476 return GetPackedField<LoadKindField>();
6477 }
6478
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006479 bool HasPcRelativeLoadKind() const {
6480 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6481 GetLoadKind() == LoadKind::kBootImageRelRo ||
6482 GetLoadKind() == LoadKind::kBssEntry;
6483 }
6484
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006485 const DexFile& GetDexFile() const {
6486 return dex_file_;
6487 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006488
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006489 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006490 return string_index_;
6491 }
6492
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006493 Handle<mirror::String> GetString() const {
6494 return string_;
6495 }
6496
6497 void SetString(Handle<mirror::String> str) {
6498 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006499 }
6500
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006501 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006502
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006503 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006504
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006505 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006506
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006507 // Will call the runtime if we need to load the string through
6508 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006509 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006510 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006511 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006512 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006513 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006514 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006515 return false;
6516 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006517 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006518 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006520 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006521 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006522 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006523
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006524 bool CanBeNull() const override { return false; }
6525 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006526
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006527 static SideEffects SideEffectsForArchRuntimeCalls() {
6528 return SideEffects::CanTriggerGC();
6529 }
6530
Vladimir Marko372f10e2016-05-17 16:30:10 +01006531 void AddSpecialInput(HInstruction* special_input);
6532
6533 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006534 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006535 return ArrayRef<HUserRecord<HInstruction*>>(
6536 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006537 }
6538
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006539 DECLARE_INSTRUCTION(LoadString);
6540
Artem Serovcced8ba2017-07-19 18:18:09 +01006541 protected:
6542 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6543
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006544 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006545 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006546 static constexpr size_t kFieldLoadKindSize =
6547 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6548 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006549 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006550 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006551
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006552 void SetLoadKindInternal(LoadKind load_kind);
6553
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006554 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006555 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006556 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006557 HUserRecord<HInstruction*> special_input_;
6558
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006559 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006560 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006561
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006562 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006563};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006564std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6565
6566// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006567inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6568 // The load kind should be determined before inserting the instruction to the graph.
6569 DCHECK(GetBlock() == nullptr);
6570 DCHECK(GetEnvironment() == nullptr);
6571 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6572 SetPackedField<LoadKindField>(load_kind);
6573 if (load_kind != LoadKind::kRuntimeCall) {
6574 special_input_ = HUserRecord<HInstruction*>(nullptr);
6575 }
6576 if (!NeedsEnvironment()) {
6577 SetSideEffects(SideEffects::None());
6578 }
6579}
6580
6581// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006582inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006583 // The special input is used for PC-relative loads on some architectures,
6584 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006585 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006586 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006587 GetLoadKind() == LoadKind::kBssEntry ||
6588 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006589 // HLoadString::GetInputRecords() returns an empty array at this point,
6590 // so use the GetInputRecords() from the base class to set the input record.
6591 DCHECK(special_input_.GetInstruction() == nullptr);
6592 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006593 special_input->AddUseAt(this, 0);
6594}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006595
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006596class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006597 public:
6598 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006599 uint16_t method_handle_idx,
6600 const DexFile& dex_file,
6601 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006602 : HInstruction(kLoadMethodHandle,
6603 DataType::Type::kReference,
6604 SideEffectsForArchRuntimeCalls(),
6605 dex_pc),
6606 special_input_(HUserRecord<HInstruction*>(current_method)),
6607 method_handle_idx_(method_handle_idx),
6608 dex_file_(dex_file) {
6609 }
6610
6611 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006612 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006613 return ArrayRef<HUserRecord<HInstruction*>>(
6614 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6615 }
6616
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006617 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006618
6619 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6620
6621 const DexFile& GetDexFile() const { return dex_file_; }
6622
6623 static SideEffects SideEffectsForArchRuntimeCalls() {
6624 return SideEffects::CanTriggerGC();
6625 }
6626
6627 DECLARE_INSTRUCTION(LoadMethodHandle);
6628
6629 protected:
6630 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6631
6632 private:
6633 // The special input is the HCurrentMethod for kRuntimeCall.
6634 HUserRecord<HInstruction*> special_input_;
6635
6636 const uint16_t method_handle_idx_;
6637 const DexFile& dex_file_;
6638};
6639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006640class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006641 public:
6642 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006643 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006644 const DexFile& dex_file,
6645 uint32_t dex_pc)
6646 : HInstruction(kLoadMethodType,
6647 DataType::Type::kReference,
6648 SideEffectsForArchRuntimeCalls(),
6649 dex_pc),
6650 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006651 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006652 dex_file_(dex_file) {
6653 }
6654
6655 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006656 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006657 return ArrayRef<HUserRecord<HInstruction*>>(
6658 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6659 }
6660
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006661 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006662
Orion Hodson06d10a72018-05-14 08:53:38 +01006663 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006664
6665 const DexFile& GetDexFile() const { return dex_file_; }
6666
6667 static SideEffects SideEffectsForArchRuntimeCalls() {
6668 return SideEffects::CanTriggerGC();
6669 }
6670
6671 DECLARE_INSTRUCTION(LoadMethodType);
6672
6673 protected:
6674 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6675
6676 private:
6677 // The special input is the HCurrentMethod for kRuntimeCall.
6678 HUserRecord<HInstruction*> special_input_;
6679
Orion Hodson06d10a72018-05-14 08:53:38 +01006680 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006681 const DexFile& dex_file_;
6682};
6683
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006684/**
6685 * Performs an initialization check on its Class object input.
6686 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006687class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006688 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006689 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006690 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306691 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006692 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006693 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006694 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006695 SetRawInputAt(0, constant);
6696 }
Artem Serova6e26142018-06-19 14:55:17 +01006697 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006698 bool CanBeMoved() const override { return true; }
6699 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006700 return true;
6701 }
6702
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006703 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006704 // May call runtime to initialize the class.
6705 return true;
6706 }
6707
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006708 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006709
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006710 HLoadClass* GetLoadClass() const {
6711 DCHECK(InputAt(0)->IsLoadClass());
6712 return InputAt(0)->AsLoadClass();
6713 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006714
6715 DECLARE_INSTRUCTION(ClinitCheck);
6716
Artem Serovcced8ba2017-07-19 18:18:09 +01006717
6718 protected:
6719 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006720};
6721
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006722class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006723 public:
6724 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006725 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006726 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006727 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006728 bool is_volatile,
6729 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006730 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006731 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006732 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306733 : HExpression(kStaticFieldGet,
6734 field_type,
6735 SideEffects::FieldReadOfType(field_type, is_volatile),
6736 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006737 field_info_(field,
6738 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006739 field_type,
6740 is_volatile,
6741 field_idx,
6742 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006743 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006744 SetRawInputAt(0, cls);
6745 }
6746
Calin Juravle52c48962014-12-16 17:02:57 +00006747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006748 bool IsClonable() const override { return true; }
6749 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006750
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006751 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006752 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006753 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006754 }
6755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006756 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006757 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6758 }
6759
Calin Juravle52c48962014-12-16 17:02:57 +00006760 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006761 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006762 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006763 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006764
Vladimir Marko61b92282017-10-11 13:23:17 +01006765 void SetType(DataType::Type new_type) {
6766 DCHECK(DataType::IsIntegralType(GetType()));
6767 DCHECK(DataType::IsIntegralType(new_type));
6768 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6769 SetPackedField<TypeField>(new_type);
6770 }
6771
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006772 DECLARE_INSTRUCTION(StaticFieldGet);
6773
Artem Serovcced8ba2017-07-19 18:18:09 +01006774 protected:
6775 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6776
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006777 private:
6778 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006779};
6780
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006781class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006782 public:
6783 HStaticFieldSet(HInstruction* cls,
6784 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006785 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006786 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006787 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006788 bool is_volatile,
6789 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006790 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006791 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006792 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006793 : HExpression(kStaticFieldSet,
6794 SideEffects::FieldWriteOfType(field_type, is_volatile),
6795 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006796 field_info_(field,
6797 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006798 field_type,
6799 is_volatile,
6800 field_idx,
6801 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006802 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006803 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006804 SetRawInputAt(0, cls);
6805 SetRawInputAt(1, value);
6806 }
6807
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006808 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006809 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006810 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006811 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006812 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006813
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006814 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006815 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6816 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006817
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006818 DECLARE_INSTRUCTION(StaticFieldSet);
6819
Artem Serovcced8ba2017-07-19 18:18:09 +01006820 protected:
6821 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6822
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006823 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006824 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6825 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6826 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6827 "Too many packed fields.");
6828
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006829 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006830};
6831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006832class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006833 public:
6834 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006835 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006836 uint32_t field_index,
6837 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306838 : HExpression(kUnresolvedInstanceFieldGet,
6839 field_type,
6840 SideEffects::AllExceptGCDependency(),
6841 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006842 field_index_(field_index) {
6843 SetRawInputAt(0, obj);
6844 }
6845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006846 bool IsClonable() const override { return true; }
6847 bool NeedsEnvironment() const override { return true; }
6848 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006849
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006850 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006851 uint32_t GetFieldIndex() const { return field_index_; }
6852
6853 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6854
Artem Serovcced8ba2017-07-19 18:18:09 +01006855 protected:
6856 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6857
Calin Juravlee460d1d2015-09-29 04:52:17 +01006858 private:
6859 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006860};
6861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006862class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006863 public:
6864 HUnresolvedInstanceFieldSet(HInstruction* obj,
6865 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006866 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006867 uint32_t field_index,
6868 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006869 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006870 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006871 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006872 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006873 SetRawInputAt(0, obj);
6874 SetRawInputAt(1, value);
6875 }
6876
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006877 bool IsClonable() const override { return true; }
6878 bool NeedsEnvironment() const override { return true; }
6879 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006881 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006882 uint32_t GetFieldIndex() const { return field_index_; }
6883
6884 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6885
Artem Serovcced8ba2017-07-19 18:18:09 +01006886 protected:
6887 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6888
Calin Juravlee460d1d2015-09-29 04:52:17 +01006889 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006890 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6891 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006892 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006893 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6894 kFieldFieldType + kFieldFieldTypeSize;
6895 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6896 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006897 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006898
Calin Juravlee460d1d2015-09-29 04:52:17 +01006899 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006900};
6901
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006902class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006903 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006904 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006905 uint32_t field_index,
6906 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306907 : HExpression(kUnresolvedStaticFieldGet,
6908 field_type,
6909 SideEffects::AllExceptGCDependency(),
6910 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006911 field_index_(field_index) {
6912 }
6913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006914 bool IsClonable() const override { return true; }
6915 bool NeedsEnvironment() const override { return true; }
6916 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006917
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006918 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006919 uint32_t GetFieldIndex() const { return field_index_; }
6920
6921 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6922
Artem Serovcced8ba2017-07-19 18:18:09 +01006923 protected:
6924 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6925
Calin Juravlee460d1d2015-09-29 04:52:17 +01006926 private:
6927 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006928};
6929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006930class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006931 public:
6932 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006933 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006934 uint32_t field_index,
6935 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006936 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006937 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006938 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006939 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006940 SetRawInputAt(0, value);
6941 }
6942
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006943 bool IsClonable() const override { return true; }
6944 bool NeedsEnvironment() const override { return true; }
6945 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006946
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006947 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006948 uint32_t GetFieldIndex() const { return field_index_; }
6949
6950 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6951
Artem Serovcced8ba2017-07-19 18:18:09 +01006952 protected:
6953 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6954
Calin Juravlee460d1d2015-09-29 04:52:17 +01006955 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006956 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6957 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006958 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006959 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6960 kFieldFieldType + kFieldFieldTypeSize;
6961 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6962 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006963 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006964
Calin Juravlee460d1d2015-09-29 04:52:17 +01006965 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006966};
6967
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006968// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006969class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006970 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006971 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306972 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6973 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006975 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01006976
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006977 DECLARE_INSTRUCTION(LoadException);
6978
Artem Serovcced8ba2017-07-19 18:18:09 +01006979 protected:
6980 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006981};
6982
David Brazdilcb1c0552015-08-04 16:22:25 +01006983// Implicit part of move-exception which clears thread-local exception storage.
6984// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006985class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006986 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006987 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006988 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306989 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006990
6991 DECLARE_INSTRUCTION(ClearException);
6992
Artem Serovcced8ba2017-07-19 18:18:09 +01006993 protected:
6994 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006995};
6996
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006997class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006998 public:
6999 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007000 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007001 SetRawInputAt(0, exception);
7002 }
7003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007004 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007006 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007008 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007010 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007011
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007012 DECLARE_INSTRUCTION(Throw);
7013
Artem Serovcced8ba2017-07-19 18:18:09 +01007014 protected:
7015 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007016};
7017
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007018/**
7019 * Implementation strategies for the code generator of a HInstanceOf
7020 * or `HCheckCast`.
7021 */
7022enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007023 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007024 kExactCheck, // Can do a single class compare.
7025 kClassHierarchyCheck, // Can just walk the super class chain.
7026 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7027 kInterfaceCheck, // No optimization yet when checking against an interface.
7028 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007029 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007030 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007031 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007032};
7033
Roland Levillain86503782016-02-11 19:07:30 +00007034std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7035
Vladimir Marko175e7862018-03-27 09:03:13 +00007036// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7037// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7038class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007039 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007040 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007041 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007042 HInstruction* object,
7043 HInstruction* target_class_or_null,
7044 TypeCheckKind check_kind,
7045 Handle<mirror::Class> klass,
7046 uint32_t dex_pc,
7047 ArenaAllocator* allocator,
7048 HIntConstant* bitstring_path_to_root,
7049 HIntConstant* bitstring_mask,
7050 SideEffects side_effects)
7051 : HVariableInputSizeInstruction(
7052 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007053 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007054 side_effects,
7055 dex_pc,
7056 allocator,
7057 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
7058 kArenaAllocTypeCheckInputs),
7059 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007060 SetPackedField<TypeCheckKindField>(check_kind);
7061 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007062 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007063 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007064 SetRawInputAt(1, target_class_or_null);
7065 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7066 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7067 if (check_kind == TypeCheckKind::kBitstringCheck) {
7068 DCHECK(target_class_or_null->IsNullConstant());
7069 SetRawInputAt(2, bitstring_path_to_root);
7070 SetRawInputAt(3, bitstring_mask);
7071 } else {
7072 DCHECK(target_class_or_null->IsLoadClass());
7073 }
Vladimir Marko87584542017-12-12 17:47:52 +00007074 }
7075
7076 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007077 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007078 HInstruction* load_class = InputAt(1);
7079 DCHECK(load_class->IsLoadClass());
7080 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007081 }
7082
Vladimir Marko175e7862018-03-27 09:03:13 +00007083 uint32_t GetBitstringPathToRoot() const {
7084 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7085 HInstruction* path_to_root = InputAt(2);
7086 DCHECK(path_to_root->IsIntConstant());
7087 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7088 }
7089
7090 uint32_t GetBitstringMask() const {
7091 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7092 HInstruction* mask = InputAt(3);
7093 DCHECK(mask->IsIntConstant());
7094 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7095 }
7096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007097 bool IsClonable() const override { return true; }
7098 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007100 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007101 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7102 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007103 }
7104
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007105 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7106 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7107 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7108 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7109
Vladimir Marko175e7862018-03-27 09:03:13 +00007110 ReferenceTypeInfo GetTargetClassRTI() {
7111 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7112 // Note: The is_exact flag from the return value should not be used.
7113 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7114 } else {
7115 return ReferenceTypeInfo::CreateInvalid();
7116 }
7117 }
7118
7119 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7120 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7121 DCHECK(klass_ != nullptr);
7122 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7123 }
7124
7125 Handle<mirror::Class> GetClass() const {
7126 return klass_;
7127 }
7128
7129 protected:
7130 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7131
7132 private:
7133 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7134 static constexpr size_t kFieldTypeCheckKindSize =
7135 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7136 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7137 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7138 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7139 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7140 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7141
7142 Handle<mirror::Class> klass_;
7143};
7144
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007145class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007146 public:
7147 HInstanceOf(HInstruction* object,
7148 HInstruction* target_class_or_null,
7149 TypeCheckKind check_kind,
7150 Handle<mirror::Class> klass,
7151 uint32_t dex_pc,
7152 ArenaAllocator* allocator,
7153 HIntConstant* bitstring_path_to_root,
7154 HIntConstant* bitstring_mask)
7155 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007156 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007157 object,
7158 target_class_or_null,
7159 check_kind,
7160 klass,
7161 dex_pc,
7162 allocator,
7163 bitstring_path_to_root,
7164 bitstring_mask,
7165 SideEffectsForArchRuntimeCalls(check_kind)) {}
7166
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007167 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007168
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007169 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007170 return CanCallRuntime(GetTypeCheckKind());
7171 }
7172
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007173 static bool CanCallRuntime(TypeCheckKind check_kind) {
7174 // Mips currently does runtime calls for any other checks.
7175 return check_kind != TypeCheckKind::kExactCheck;
7176 }
7177
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007178 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007179 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007180 }
7181
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007182 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007183
Artem Serovcced8ba2017-07-19 18:18:09 +01007184 protected:
7185 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007186};
7187
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007188class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007189 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007190 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307191 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007192 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7193 SetPackedFlag<kFlagUpperCanBeNull>(true);
7194 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007195 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007196 SetRawInputAt(0, input);
7197 }
7198
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007199 bool InstructionDataEquals(const HInstruction* other) const override;
7200 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007201
David Brazdilf5552582015-12-27 13:36:12 +00007202 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007203 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007204 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007205 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007206
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007207 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007208 DCHECK(GetUpperCanBeNull() || !can_be_null);
7209 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007210 }
7211
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007212 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007213
Calin Juravleb1498f62015-02-16 13:13:29 +00007214 DECLARE_INSTRUCTION(BoundType);
7215
Artem Serovcced8ba2017-07-19 18:18:09 +01007216 protected:
7217 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7218
Calin Juravleb1498f62015-02-16 13:13:29 +00007219 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007220 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7221 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007222 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007223 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7224 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7225 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7226
Calin Juravleb1498f62015-02-16 13:13:29 +00007227 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007228 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7229 // It is used to bound the type in cases like:
7230 // if (x instanceof ClassX) {
7231 // // uper_bound_ will be ClassX
7232 // }
David Brazdilf5552582015-12-27 13:36:12 +00007233 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007234};
7235
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007236class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007237 public:
7238 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007239 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007240 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007241 Handle<mirror::Class> klass,
7242 uint32_t dex_pc,
7243 ArenaAllocator* allocator,
7244 HIntConstant* bitstring_path_to_root,
7245 HIntConstant* bitstring_mask)
7246 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007247 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007248 object,
7249 target_class_or_null,
7250 check_kind,
7251 klass,
7252 dex_pc,
7253 allocator,
7254 bitstring_path_to_root,
7255 bitstring_mask,
7256 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007257
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007258 bool IsClonable() const override { return true; }
7259 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007260 // Instruction may throw a CheckCastError.
7261 return true;
7262 }
7263
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007264 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007265
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007266 DECLARE_INSTRUCTION(CheckCast);
7267
Artem Serovcced8ba2017-07-19 18:18:09 +01007268 protected:
7269 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007270};
7271
Andreas Gampe26de38b2016-07-27 17:53:11 -07007272/**
7273 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7274 * @details We define the combined barrier types that are actually required
7275 * by the Java Memory Model, rather than using exactly the terminology from
7276 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7277 * primitives. Note that the JSR-133 cookbook generally does not deal with
7278 * store atomicity issues, and the recipes there are not always entirely sufficient.
7279 * The current recipe is as follows:
7280 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7281 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7282 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7283 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7284 * class has final fields.
7285 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7286 * store-to-memory instructions. Only generated together with non-temporal stores.
7287 */
7288enum MemBarrierKind {
7289 kAnyStore,
7290 kLoadAny,
7291 kStoreStore,
7292 kAnyAny,
7293 kNTStoreStore,
7294 kLastBarrierKind = kNTStoreStore
7295};
7296std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7297
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007298class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007299 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007300 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007301 : HExpression(kMemoryBarrier,
7302 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7303 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007304 SetPackedField<BarrierKindField>(barrier_kind);
7305 }
Calin Juravle27df7582015-04-17 19:12:31 +01007306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007307 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007308
Vladimir Markoa1de9182016-02-25 11:37:38 +00007309 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007310
7311 DECLARE_INSTRUCTION(MemoryBarrier);
7312
Artem Serovcced8ba2017-07-19 18:18:09 +01007313 protected:
7314 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7315
Calin Juravle27df7582015-04-17 19:12:31 +01007316 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007317 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7318 static constexpr size_t kFieldBarrierKindSize =
7319 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7320 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7321 kFieldBarrierKind + kFieldBarrierKindSize;
7322 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7323 "Too many packed fields.");
7324 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007325};
7326
Igor Murashkind01745e2017-04-05 16:40:31 -07007327// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7328// the specified object(s), with respect to any subsequent store that might "publish"
7329// (i.e. make visible) the specified object to another thread.
7330//
7331// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7332// for all final fields (that were set) at the end of the invoked constructor.
7333//
7334// The constructor fence models the freeze actions for the final fields of an object
7335// being constructed (semantically at the end of the constructor). Constructor fences
7336// have a per-object affinity; two separate objects being constructed get two separate
7337// constructor fences.
7338//
7339// (Note: that if calling a super-constructor or forwarding to another constructor,
7340// the freezes would happen at the end of *that* constructor being invoked).
7341//
7342// The memory model guarantees that when the object being constructed is "published" after
7343// constructor completion (i.e. escapes the current thread via a store), then any final field
7344// writes must be observable on other threads (once they observe that publication).
7345//
7346// Further, anything written before the freeze, and read by dereferencing through the final field,
7347// must also be visible (so final object field could itself have an object with non-final fields;
7348// yet the freeze must also extend to them).
7349//
7350// Constructor example:
7351//
7352// class HasFinal {
7353// final int field; Optimizing IR for <init>()V:
7354// HasFinal() {
7355// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7356// // freeze(this.field); HConstructorFence(this)
7357// } HReturn
7358// }
7359//
7360// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7361// already-initialized classes; in that case the allocation must act as a "default-initializer"
7362// of the object which effectively writes the class pointer "final field".
7363//
7364// For example, we can model default-initialiation as roughly the equivalent of the following:
7365//
7366// class Object {
7367// private final Class header;
7368// }
7369//
7370// Java code: Optimizing IR:
7371//
7372// T new_instance<T>() {
7373// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7374// obj.header = T.class; // header write is done by above call.
7375// // freeze(obj.header) HConstructorFence(obj)
7376// return (T)obj;
7377// }
7378//
7379// See also:
7380// * CompilerDriver::RequiresConstructorBarrier
7381// * QuasiAtomic::ThreadFenceForConstructor
7382//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007383class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007384 // A fence has variable inputs because the inputs can be removed
7385 // after prepare_for_register_allocation phase.
7386 // (TODO: In the future a fence could freeze multiple objects
7387 // after merging two fences together.)
7388 public:
7389 // `fence_object` is the reference that needs to be protected for correct publication.
7390 //
7391 // It makes sense in the following situations:
7392 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7393 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7394 //
7395 // After construction the `fence_object` becomes the 0th input.
7396 // This is not an input in a real sense, but just a convenient place to stash the information
7397 // about the associated object.
7398 HConstructorFence(HInstruction* fence_object,
7399 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007400 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007401 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7402 // constraints described in the class header. We claim that these SideEffects constraints
7403 // enforce a superset of the real constraints.
7404 //
7405 // The ordering described above is conservatively modeled with SideEffects as follows:
7406 //
7407 // * To prevent reordering of the publication stores:
7408 // ----> "Reads of objects" is the initial SideEffect.
7409 // * For every primitive final field store in the constructor:
7410 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7411 // * If there are any stores to reference final fields in the constructor:
7412 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7413 // that are reachable from `fence_object` also need to be prevented for reordering
7414 // (and we do not want to do alias analysis to figure out what those stores are).
7415 //
7416 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7417 // even more conservative approach than the one described above, and prevents all of the
7418 // above reordering without analyzing any of the instructions in the constructor.
7419 //
7420 // If in a later phase we discover that there are no writes to reference final fields,
7421 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307422 : HVariableInputSizeInstruction(kConstructorFence,
7423 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007424 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007425 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007426 /* number_of_inputs */ 1,
7427 kArenaAllocConstructorFenceInputs) {
7428 DCHECK(fence_object != nullptr);
7429 SetRawInputAt(0, fence_object);
7430 }
7431
7432 // The object associated with this constructor fence.
7433 //
7434 // (Note: This will be null after the prepare_for_register_allocation phase,
7435 // as all constructor fence inputs are removed there).
7436 HInstruction* GetFenceObject() const {
7437 return InputAt(0);
7438 }
7439
7440 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7441 // - Delete `fence_use` from `this`'s use list.
7442 // - Delete `this` from `fence_use`'s inputs list.
7443 // - If the `fence_use` is dead, remove it from the graph.
7444 //
7445 // A fence is considered dead once it no longer has any uses
7446 // and all of the inputs are dead.
7447 //
7448 // This must *not* be called during/after prepare_for_register_allocation,
7449 // because that removes all the inputs to the fences but the fence is actually
7450 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007451 //
7452 // Returns how many HConstructorFence instructions were removed from graph.
7453 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007454
Igor Murashkindd018df2017-08-09 10:38:31 -07007455 // Combine all inputs of `this` and `other` instruction and remove
7456 // `other` from the graph.
7457 //
7458 // Inputs are unique after the merge.
7459 //
7460 // Requirement: `this` must not be the same as `other.
7461 void Merge(HConstructorFence* other);
7462
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007463 // Check if this constructor fence is protecting
7464 // an HNewInstance or HNewArray that is also the immediate
7465 // predecessor of `this`.
7466 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007467 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7468 // to be one of the inputs of `this`.
7469 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007470 // Returns the associated HNewArray or HNewInstance,
7471 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007472 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007473
Igor Murashkind01745e2017-04-05 16:40:31 -07007474 DECLARE_INSTRUCTION(ConstructorFence);
7475
Artem Serovcced8ba2017-07-19 18:18:09 +01007476 protected:
7477 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007478};
7479
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007480class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007481 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007482 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007483 kEnter,
7484 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007485 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007486 };
7487
7488 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007489 : HExpression(kMonitorOperation,
7490 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7491 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007492 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007493 SetRawInputAt(0, object);
7494 }
7495
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007496 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007497 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007498
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007499 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007500 // Verifier guarantees that monitor-exit cannot throw.
7501 // This is important because it allows the HGraphBuilder to remove
7502 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7503 return IsEnter();
7504 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007505
Vladimir Markoa1de9182016-02-25 11:37:38 +00007506 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7507 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007508
7509 DECLARE_INSTRUCTION(MonitorOperation);
7510
Artem Serovcced8ba2017-07-19 18:18:09 +01007511 protected:
7512 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7513
Calin Juravle52c48962014-12-16 17:02:57 +00007514 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007515 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7516 static constexpr size_t kFieldOperationKindSize =
7517 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7518 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7519 kFieldOperationKind + kFieldOperationKindSize;
7520 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7521 "Too many packed fields.");
7522 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007523};
7524
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007525class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007526 public:
7527 HSelect(HInstruction* condition,
7528 HInstruction* true_value,
7529 HInstruction* false_value,
7530 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307531 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007532 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7533
7534 // First input must be `true_value` or `false_value` to allow codegens to
7535 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7536 // that architectures which implement HSelect as a conditional move also
7537 // will not need to invert the condition.
7538 SetRawInputAt(0, false_value);
7539 SetRawInputAt(1, true_value);
7540 SetRawInputAt(2, condition);
7541 }
7542
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007543 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007544 HInstruction* GetFalseValue() const { return InputAt(0); }
7545 HInstruction* GetTrueValue() const { return InputAt(1); }
7546 HInstruction* GetCondition() const { return InputAt(2); }
7547
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007548 bool CanBeMoved() const override { return true; }
7549 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007550 return true;
7551 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007552
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007553 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007554 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7555 }
7556
7557 DECLARE_INSTRUCTION(Select);
7558
Artem Serovcced8ba2017-07-19 18:18:09 +01007559 protected:
7560 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007561};
7562
Vladimir Markof9f64412015-09-02 14:05:49 +01007563class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007564 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007565 MoveOperands(Location source,
7566 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007567 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007568 HInstruction* instruction)
7569 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007570
7571 Location GetSource() const { return source_; }
7572 Location GetDestination() const { return destination_; }
7573
7574 void SetSource(Location value) { source_ = value; }
7575 void SetDestination(Location value) { destination_ = value; }
7576
7577 // The parallel move resolver marks moves as "in-progress" by clearing the
7578 // destination (but not the source).
7579 Location MarkPending() {
7580 DCHECK(!IsPending());
7581 Location dest = destination_;
7582 destination_ = Location::NoLocation();
7583 return dest;
7584 }
7585
7586 void ClearPending(Location dest) {
7587 DCHECK(IsPending());
7588 destination_ = dest;
7589 }
7590
7591 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007592 DCHECK(source_.IsValid() || destination_.IsInvalid());
7593 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007594 }
7595
7596 // True if this blocks a move from the given location.
7597 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007598 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007599 }
7600
7601 // A move is redundant if it's been eliminated, if its source and
7602 // destination are the same, or if its destination is unneeded.
7603 bool IsRedundant() const {
7604 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7605 }
7606
7607 // We clear both operands to indicate move that's been eliminated.
7608 void Eliminate() {
7609 source_ = destination_ = Location::NoLocation();
7610 }
7611
7612 bool IsEliminated() const {
7613 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7614 return source_.IsInvalid();
7615 }
7616
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007617 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007618
Nicolas Geoffray90218252015-04-15 11:56:51 +01007619 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007620 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007621 }
7622
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007623 HInstruction* GetInstruction() const { return instruction_; }
7624
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007625 private:
7626 Location source_;
7627 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007628 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007629 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007630 // The instruction this move is assocatied with. Null when this move is
7631 // for moving an input in the expected locations of user (including a phi user).
7632 // This is only used in debug mode, to ensure we do not connect interval siblings
7633 // in the same parallel move.
7634 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007635};
7636
Roland Levillainc9285912015-12-18 10:38:42 +00007637std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7638
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007639static constexpr size_t kDefaultNumberOfMoves = 4;
7640
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007641class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007642 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007643 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007644 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007645 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007646 moves_.reserve(kDefaultNumberOfMoves);
7647 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007648
Nicolas Geoffray90218252015-04-15 11:56:51 +01007649 void AddMove(Location source,
7650 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007651 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007652 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007653 DCHECK(source.IsValid());
7654 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007655 if (kIsDebugBuild) {
7656 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007657 for (const MoveOperands& move : moves_) {
7658 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007659 // Special case the situation where the move is for the spill slot
7660 // of the instruction.
7661 if ((GetPrevious() == instruction)
7662 || ((GetPrevious() == nullptr)
7663 && instruction->IsPhi()
7664 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007665 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007666 << "Doing parallel moves for the same instruction.";
7667 } else {
7668 DCHECK(false) << "Doing parallel moves for the same instruction.";
7669 }
7670 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007671 }
7672 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007673 for (const MoveOperands& move : moves_) {
7674 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007675 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007676 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007677 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007678 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007679 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007680 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007681 }
7682
Vladimir Marko225b6462015-09-28 12:17:40 +01007683 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007684 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007685 }
7686
Vladimir Marko225b6462015-09-28 12:17:40 +01007687 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007688
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007689 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007690
Artem Serovcced8ba2017-07-19 18:18:09 +01007691 protected:
7692 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7693
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007694 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007695 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007696};
7697
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007698// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7699// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7700// never used across anything that can trigger GC.
7701// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7702// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007703class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007704 public:
7705 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307706 : HExpression(kIntermediateAddress,
7707 DataType::Type::kInt32,
7708 SideEffects::DependsOnGC(),
7709 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007710 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7711 DataType::Size(DataType::Type::kReference))
7712 << "kPrimInt and kPrimNot have different sizes.";
7713 SetRawInputAt(0, base_address);
7714 SetRawInputAt(1, offset);
7715 }
7716
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007717 bool IsClonable() const override { return true; }
7718 bool CanBeMoved() const override { return true; }
7719 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007720 return true;
7721 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007722 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007723
7724 HInstruction* GetBaseAddress() const { return InputAt(0); }
7725 HInstruction* GetOffset() const { return InputAt(1); }
7726
7727 DECLARE_INSTRUCTION(IntermediateAddress);
7728
Artem Serovcced8ba2017-07-19 18:18:09 +01007729 protected:
7730 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007731};
7732
7733
Mark Mendell0616ae02015-04-17 12:49:27 -04007734} // namespace art
7735
Aart Bikf8f5a162017-02-06 15:35:29 -08007736#include "nodes_vector.h"
7737
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007738#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7739#include "nodes_shared.h"
7740#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007741#ifdef ART_ENABLE_CODEGEN_mips
7742#include "nodes_mips.h"
7743#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007744#ifdef ART_ENABLE_CODEGEN_x86
7745#include "nodes_x86.h"
7746#endif
7747
7748namespace art {
7749
Igor Murashkin6ef45672017-08-08 13:59:55 -07007750class OptimizingCompilerStats;
7751
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007752class HGraphVisitor : public ValueObject {
7753 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007754 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7755 : stats_(stats),
7756 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007757 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007758
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007759 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007760 virtual void VisitBasicBlock(HBasicBlock* block);
7761
Roland Levillain633021e2014-10-01 14:12:25 +01007762 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007763 void VisitInsertionOrder();
7764
Roland Levillain633021e2014-10-01 14:12:25 +01007765 // Visit the graph following dominator tree reverse post-order.
7766 void VisitReversePostOrder();
7767
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007768 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007769
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007770 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007771#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007772 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7773
7774 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7775
7776#undef DECLARE_VISIT_INSTRUCTION
7777
Igor Murashkin6ef45672017-08-08 13:59:55 -07007778 protected:
7779 OptimizingCompilerStats* stats_;
7780
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007781 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007782 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007783
7784 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7785};
7786
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007787class HGraphDelegateVisitor : public HGraphVisitor {
7788 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007789 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7790 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007791 virtual ~HGraphDelegateVisitor() {}
7792
7793 // Visit functions that delegate to to super class.
7794#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007795 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007796
7797 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7798
7799#undef DECLARE_VISIT_INSTRUCTION
7800
7801 private:
7802 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7803};
7804
Artem Serovcced8ba2017-07-19 18:18:09 +01007805// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7806// and remove the old instruction.
7807HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7808
7809// Create a clone for each clonable instructions/phis and replace the original with the clone.
7810//
7811// Used for testing individual instruction cloner.
7812class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7813 public:
7814 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007815 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007816
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007817 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007818 if (instruction->IsClonable()) {
7819 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007820 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007821 }
7822 }
7823
Artem Serov7f4aff62017-06-21 17:02:18 +01007824 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007825
7826 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007827 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007828
7829 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7830};
7831
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007832// Iterator over the blocks that art part of the loop. Includes blocks part
7833// of an inner loop. The order in which the blocks are iterated is on their
7834// block id.
7835class HBlocksInLoopIterator : public ValueObject {
7836 public:
7837 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7838 : blocks_in_loop_(info.GetBlocks()),
7839 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7840 index_(0) {
7841 if (!blocks_in_loop_.IsBitSet(index_)) {
7842 Advance();
7843 }
7844 }
7845
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007846 bool Done() const { return index_ == blocks_.size(); }
7847 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007848 void Advance() {
7849 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007850 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007851 if (blocks_in_loop_.IsBitSet(index_)) {
7852 break;
7853 }
7854 }
7855 }
7856
7857 private:
7858 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007859 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007860 size_t index_;
7861
7862 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7863};
7864
Mingyao Yang3584bce2015-05-19 16:01:59 -07007865// Iterator over the blocks that art part of the loop. Includes blocks part
7866// of an inner loop. The order in which the blocks are iterated is reverse
7867// post order.
7868class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7869 public:
7870 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7871 : blocks_in_loop_(info.GetBlocks()),
7872 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7873 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007874 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007875 Advance();
7876 }
7877 }
7878
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007879 bool Done() const { return index_ == blocks_.size(); }
7880 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007881 void Advance() {
7882 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007883 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7884 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007885 break;
7886 }
7887 }
7888 }
7889
7890 private:
7891 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007892 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007893 size_t index_;
7894
7895 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7896};
7897
Aart Bikf3e61ee2017-04-12 17:09:20 -07007898// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007899inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007900 if (constant->IsIntConstant()) {
7901 return constant->AsIntConstant()->GetValue();
7902 } else if (constant->IsLongConstant()) {
7903 return constant->AsLongConstant()->GetValue();
7904 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007905 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007906 return 0;
7907 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007908}
7909
Aart Bikf3e61ee2017-04-12 17:09:20 -07007910// Returns true iff instruction is an integral constant (and sets value on success).
7911inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7912 if (instruction->IsIntConstant()) {
7913 *value = instruction->AsIntConstant()->GetValue();
7914 return true;
7915 } else if (instruction->IsLongConstant()) {
7916 *value = instruction->AsLongConstant()->GetValue();
7917 return true;
7918 } else if (instruction->IsNullConstant()) {
7919 *value = 0;
7920 return true;
7921 }
7922 return false;
7923}
7924
Aart Bik0148de42017-09-05 09:25:01 -07007925// Returns true iff instruction is the given integral constant.
7926inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7927 int64_t val = 0;
7928 return IsInt64AndGet(instruction, &val) && val == value;
7929}
7930
7931// Returns true iff instruction is a zero bit pattern.
7932inline bool IsZeroBitPattern(HInstruction* instruction) {
7933 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7934}
7935
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007936// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007937#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007938 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7939 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7940#undef INSTRUCTION_TYPE_CHECK
7941
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007942// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007943#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7944 std::is_base_of<BaseType, H##type>::value,
7945#define INSTRUCTION_TYPE_CHECK(type, super) \
7946 inline bool HInstruction::Is##type() const { \
7947 DCHECK_LT(GetKind(), kLastInstructionKind); \
7948 using BaseType = H##type; \
7949 static constexpr bool results[] = { \
7950 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7951 }; \
7952 return results[static_cast<size_t>(GetKind())]; \
7953 }
7954
7955 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7956#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007957#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007958
7959#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007960 inline const H##type* HInstruction::As##type() const { \
7961 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7962 } \
7963 inline H##type* HInstruction::As##type() { \
7964 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7965 }
7966
Vladimir Markoa90dd512018-05-04 15:04:45 +01007967 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7968#undef INSTRUCTION_TYPE_CAST
7969
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007970
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007971// Create space in `blocks` for adding `number_of_new_blocks` entries
7972// starting at location `at`. Blocks after `at` are moved accordingly.
7973inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7974 size_t number_of_new_blocks,
7975 size_t after) {
7976 DCHECK_LT(after, blocks->size());
7977 size_t old_size = blocks->size();
7978 size_t new_size = old_size + number_of_new_blocks;
7979 blocks->resize(new_size);
7980 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7981}
7982
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007983/*
7984 * Hunt "under the hood" of array lengths (leading to array references),
7985 * null checks (also leading to array references), and new arrays
7986 * (leading to the actual length). This makes it more likely related
7987 * instructions become actually comparable.
7988 */
7989inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7990 while (instruction->IsArrayLength() ||
7991 instruction->IsNullCheck() ||
7992 instruction->IsNewArray()) {
7993 instruction = instruction->IsNewArray()
7994 ? instruction->AsNewArray()->GetLength()
7995 : instruction->InputAt(0);
7996 }
7997 return instruction;
7998}
7999
Artem Serov21c7e6f2017-07-27 16:04:42 +01008000void RemoveEnvironmentUses(HInstruction* instruction);
8001bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8002void ResetEnvironmentInputRecords(HInstruction* instruction);
8003
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008004} // namespace art
8005
8006#endif // ART_COMPILER_OPTIMIZING_NODES_H_