blob: 76887f9a5b19df74ad2ee7990664c4f715b7ad51 [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) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001456 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001457 M(VecLoad, VecMemoryOperation) \
1458 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001459
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001460/*
1461 * Instructions, shared across several (not all) architectures.
1462 */
1463#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1464#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1465#else
1466#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001467 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001468 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001469 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001470 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001471#endif
1472
Alexandre Ramesef20f712015-06-09 10:29:30 +01001473#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1474
1475#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1476
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001477#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001478#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001479#else
1480#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1481 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001482 M(MipsPackedSwitch, Instruction) \
1483 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001484#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001485
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001486#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1487
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001488#ifndef ART_ENABLE_CODEGEN_x86
1489#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1490#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001491#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1492 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001493 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001494 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001495 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001496#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001497
1498#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1499
1500#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1501 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001502 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001503 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1504 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001505 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001506 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001507 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1508 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1509
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001510#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1511 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001512 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001513 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001514 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001515 M(Invoke, Instruction) \
1516 M(VecOperation, Instruction) \
1517 M(VecUnaryOperation, VecOperation) \
1518 M(VecBinaryOperation, VecOperation) \
1519 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001520
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001521#define FOR_EACH_INSTRUCTION(M) \
1522 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1523 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1524
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001525#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001526FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1527#undef FORWARD_DECLARATION
1528
Vladimir Marko372f10e2016-05-17 16:30:10 +01001529#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001530 private: \
1531 H##type& operator=(const H##type&) = delete; \
1532 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001533 const char* DebugName() const override { return #type; } \
1534 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001535 DCHECK(IsClonable()); \
1536 return new (arena) H##type(*this->As##type()); \
1537 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001538 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001539
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001540#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001541 private: \
1542 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001543 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001544
Artem Serovcced8ba2017-07-19 18:18:09 +01001545#define DEFAULT_COPY_CONSTRUCTOR(type) \
1546 explicit H##type(const H##type& other) = default;
1547
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001548template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001549class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1550 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001551 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001552 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001553 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001554 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001555 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001556 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001557 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001558
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001559 private:
David Brazdiled596192015-01-23 10:39:45 +00001560 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001561 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001562
1563 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001564 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001565
Vladimir Marko46817b82016-03-29 12:21:58 +01001566 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001567
1568 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1569};
1570
David Brazdiled596192015-01-23 10:39:45 +00001571template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001572using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001573
David Brazdil1abb4192015-02-17 18:33:36 +00001574// This class is used by HEnvironment and HInstruction classes to record the
1575// instructions they use and pointers to the corresponding HUseListNodes kept
1576// by the used instructions.
1577template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001578class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001579 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001580 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1581 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001582
Vladimir Marko46817b82016-03-29 12:21:58 +01001583 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1584 : HUserRecord(old_record.instruction_, before_use_node) {}
1585 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1586 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001587 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001588 }
1589
1590 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001591 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1592 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001593
1594 private:
1595 // Instruction used by the user.
1596 HInstruction* instruction_;
1597
Vladimir Marko46817b82016-03-29 12:21:58 +01001598 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1599 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001600};
1601
Vladimir Markoe9004912016-06-16 16:50:52 +01001602// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1603// This is used for HInstruction::GetInputs() to return a container wrapper providing
1604// HInstruction* values even though the underlying container has HUserRecord<>s.
1605struct HInputExtractor {
1606 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1607 return record.GetInstruction();
1608 }
1609 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1610 return record.GetInstruction();
1611 }
1612};
1613
1614using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1615using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1616
Aart Bik854a02b2015-07-14 16:07:00 -07001617/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001619 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001620 * For write/read dependences on fields/arrays, the dependence analysis uses
1621 * type disambiguation (e.g. a float field write cannot modify the value of an
1622 * integer field read) and the access type (e.g. a reference array write cannot
1623 * modify the value of a reference field read [although it may modify the
1624 * reference fetch prior to reading the field, which is represented by its own
1625 * write/read dependence]). The analysis makes conservative points-to
1626 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1627 * the same, and any reference read depends on any reference read without
1628 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001629 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001630 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1631 * alive across the point where garbage collection might happen.
1632 *
1633 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1634 *
1635 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1636 * those instructions from the compiler perspective (between this instruction and the next one
1637 * in the IR).
1638 *
1639 * Note: Instructions which can cause GC only on a fatal slow path do not need
1640 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1641 * one. However the execution may return to compiled code if there is a catch block in the
1642 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1643 * set.
1644 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001645 * The internal representation uses 38-bit and is described in the table below.
1646 * The first line indicates the side effect, and for field/array accesses the
1647 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001648 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001649 * The two numbered lines below indicate the bit position in the bitfield (read
1650 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001651 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001652 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1653 * +-------------+---------+---------+--------------+---------+---------+
1654 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1655 * | 3 |333333322|222222221| 1 |111111110|000000000|
1656 * | 7 |654321098|765432109| 8 |765432109|876543210|
1657 *
1658 * Note that, to ease the implementation, 'changes' bits are least significant
1659 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001660 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001661class SideEffects : public ValueObject {
1662 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001663 SideEffects() : flags_(0) {}
1664
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001665 static SideEffects None() {
1666 return SideEffects(0);
1667 }
1668
1669 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001670 return SideEffects(kAllChangeBits | kAllDependOnBits);
1671 }
1672
1673 static SideEffects AllChanges() {
1674 return SideEffects(kAllChangeBits);
1675 }
1676
1677 static SideEffects AllDependencies() {
1678 return SideEffects(kAllDependOnBits);
1679 }
1680
1681 static SideEffects AllExceptGCDependency() {
1682 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1683 }
1684
1685 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001686 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001687 }
1688
Aart Bik34c3ba92015-07-20 14:08:59 -07001689 static SideEffects AllWrites() {
1690 return SideEffects(kAllWrites);
1691 }
1692
1693 static SideEffects AllReads() {
1694 return SideEffects(kAllReads);
1695 }
1696
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001697 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001698 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001699 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001700 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001701 }
1702
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001703 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001704 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001705 }
1706
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001707 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001708 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001709 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001710 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001711 }
1712
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001713 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001714 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001715 }
1716
Artem Serovd1aa7d02018-06-22 11:35:46 +01001717 // Returns whether GC might happen across this instruction from the compiler perspective so
1718 // the next instruction in the IR would see that.
1719 //
1720 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 static SideEffects CanTriggerGC() {
1722 return SideEffects(1ULL << kCanTriggerGCBit);
1723 }
1724
Artem Serovd1aa7d02018-06-22 11:35:46 +01001725 // Returns whether the instruction must not be alive across a GC point.
1726 //
1727 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001728 static SideEffects DependsOnGC() {
1729 return SideEffects(1ULL << kDependsOnGCBit);
1730 }
1731
Aart Bik854a02b2015-07-14 16:07:00 -07001732 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001733 SideEffects Union(SideEffects other) const {
1734 return SideEffects(flags_ | other.flags_);
1735 }
1736
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001737 SideEffects Exclusion(SideEffects other) const {
1738 return SideEffects(flags_ & ~other.flags_);
1739 }
1740
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001741 void Add(SideEffects other) {
1742 flags_ |= other.flags_;
1743 }
1744
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001745 bool Includes(SideEffects other) const {
1746 return (other.flags_ & flags_) == other.flags_;
1747 }
1748
1749 bool HasSideEffects() const {
1750 return (flags_ & kAllChangeBits);
1751 }
1752
1753 bool HasDependencies() const {
1754 return (flags_ & kAllDependOnBits);
1755 }
1756
1757 // Returns true if there are no side effects or dependencies.
1758 bool DoesNothing() const {
1759 return flags_ == 0;
1760 }
1761
Aart Bik854a02b2015-07-14 16:07:00 -07001762 // Returns true if something is written.
1763 bool DoesAnyWrite() const {
1764 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001765 }
1766
Aart Bik854a02b2015-07-14 16:07:00 -07001767 // Returns true if something is read.
1768 bool DoesAnyRead() const {
1769 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001770 }
1771
Aart Bik854a02b2015-07-14 16:07:00 -07001772 // Returns true if potentially everything is written and read
1773 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001774 bool DoesAllReadWrite() const {
1775 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1776 }
1777
Aart Bik854a02b2015-07-14 16:07:00 -07001778 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001779 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001780 }
1781
Roland Levillain0d5a2812015-11-13 10:07:31 +00001782 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001783 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001784 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1785 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001786 }
1787
1788 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001789 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001790 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001791 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001792 for (int s = kLastBit; s >= 0; s--) {
1793 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1794 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1795 // This is a bit for the GC side effect.
1796 if (current_bit_is_set) {
1797 flags += "GC";
1798 }
Aart Bik854a02b2015-07-14 16:07:00 -07001799 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001800 } else {
1801 // This is a bit for the array/field analysis.
1802 // The underscore character stands for the 'can trigger GC' bit.
1803 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1804 if (current_bit_is_set) {
1805 flags += kDebug[s];
1806 }
1807 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1808 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1809 flags += "|";
1810 }
1811 }
Aart Bik854a02b2015-07-14 16:07:00 -07001812 }
1813 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001814 }
1815
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001816 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001817
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001818 private:
1819 static constexpr int kFieldArrayAnalysisBits = 9;
1820
1821 static constexpr int kFieldWriteOffset = 0;
1822 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1823 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1824 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1825
1826 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1827
1828 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1829 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1830 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1831 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1832
1833 static constexpr int kLastBit = kDependsOnGCBit;
1834 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1835
1836 // Aliases.
1837
1838 static_assert(kChangeBits == kDependOnBits,
1839 "the 'change' bits should match the 'depend on' bits.");
1840
1841 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1842 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1843 static constexpr uint64_t kAllWrites =
1844 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1845 static constexpr uint64_t kAllReads =
1846 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001847
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001848 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001850 int shift;
1851 switch (type) {
1852 case DataType::Type::kReference: shift = 0; break;
1853 case DataType::Type::kBool: shift = 1; break;
1854 case DataType::Type::kInt8: shift = 2; break;
1855 case DataType::Type::kUint16: shift = 3; break;
1856 case DataType::Type::kInt16: shift = 4; break;
1857 case DataType::Type::kInt32: shift = 5; break;
1858 case DataType::Type::kInt64: shift = 6; break;
1859 case DataType::Type::kFloat32: shift = 7; break;
1860 case DataType::Type::kFloat64: shift = 8; break;
1861 default:
1862 LOG(FATAL) << "Unexpected data type " << type;
1863 UNREACHABLE();
1864 }
Aart Bik854a02b2015-07-14 16:07:00 -07001865 DCHECK_LE(kFieldWriteOffset, shift);
1866 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001867 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001868 }
1869
1870 // Private constructor on direct flags value.
1871 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1872
1873 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001874};
1875
David Brazdiled596192015-01-23 10:39:45 +00001876// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001877class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001878 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001879 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001880 size_t number_of_vregs,
1881 ArtMethod* method,
1882 uint32_t dex_pc,
1883 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001884 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1885 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001886 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001887 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001888 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001889 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001890 }
1891
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001892 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1893 const HEnvironment& to_copy,
1894 HInstruction* holder)
1895 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001896 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001897 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001898 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001899 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001900
Vladimir Markoec32f642017-06-02 10:51:55 +01001901 void AllocateLocations() {
1902 DCHECK(locations_.empty());
1903 locations_.resize(vregs_.size());
1904 }
1905
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001906 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001907 if (parent_ != nullptr) {
1908 parent_->SetAndCopyParentChain(allocator, parent);
1909 } else {
1910 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1911 parent_->CopyFrom(parent);
1912 if (parent->GetParent() != nullptr) {
1913 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1914 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001915 }
David Brazdiled596192015-01-23 10:39:45 +00001916 }
1917
Vladimir Marko69d310e2017-10-09 14:12:23 +01001918 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001919 void CopyFrom(HEnvironment* environment);
1920
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001921 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1922 // input to the loop phi instead. This is for inserting instructions that
1923 // require an environment (like HDeoptimization) in the loop pre-header.
1924 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001925
1926 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001927 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001928 }
1929
1930 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001931 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001932 }
1933
David Brazdil1abb4192015-02-17 18:33:36 +00001934 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001935
Artem Serovca210e32017-12-15 13:43:20 +00001936 // Replaces the input at the position 'index' with the replacement; the replacement and old
1937 // input instructions' env_uses_ lists are adjusted. The function works similar to
1938 // HInstruction::ReplaceInput.
1939 void ReplaceInput(HInstruction* replacement, size_t index);
1940
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001941 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001942
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001943 HEnvironment* GetParent() const { return parent_; }
1944
1945 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001946 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001947 }
1948
1949 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001950 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001951 }
1952
1953 uint32_t GetDexPc() const {
1954 return dex_pc_;
1955 }
1956
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001957 ArtMethod* GetMethod() const {
1958 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001959 }
1960
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001961 HInstruction* GetHolder() const {
1962 return holder_;
1963 }
1964
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001965
1966 bool IsFromInlinedInvoke() const {
1967 return GetParent() != nullptr;
1968 }
1969
David Brazdiled596192015-01-23 10:39:45 +00001970 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001971 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1972 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001973 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001974 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001975 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001976
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001977 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001978 HInstruction* const holder_;
1979
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001980 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001981
1982 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1983};
1984
Vladimir Markof9f64412015-09-02 14:05:49 +01001985class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001986 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301987#define DECLARE_KIND(type, super) k##type,
1988 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001989 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301990 kLastInstructionKind
1991 };
1992#undef DECLARE_KIND
1993
1994 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001995 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1996
1997 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001998 : previous_(nullptr),
1999 next_(nullptr),
2000 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002001 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002002 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002003 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002004 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002006 locations_(nullptr),
2007 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002008 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002009 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002010 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302011 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002012 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002013 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2014 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002015
Dave Allison20dfc792014-06-16 20:44:29 -07002016 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002017
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002018
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002019 HInstruction* GetNext() const { return next_; }
2020 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002021
Calin Juravle77520bc2015-01-12 18:45:46 +00002022 HInstruction* GetNextDisregardingMoves() const;
2023 HInstruction* GetPreviousDisregardingMoves() const;
2024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002025 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002026 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002027 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002028 bool IsInBlock() const { return block_ != nullptr; }
2029 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002030 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2031 bool IsIrreducibleLoopHeaderPhi() const {
2032 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2033 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002034
Vladimir Marko372f10e2016-05-17 16:30:10 +01002035 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2036
2037 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2038 // One virtual method is enough, just const_cast<> and then re-add the const.
2039 return ArrayRef<const HUserRecord<HInstruction*>>(
2040 const_cast<HInstruction*>(this)->GetInputRecords());
2041 }
2042
Vladimir Markoe9004912016-06-16 16:50:52 +01002043 HInputsRef GetInputs() {
2044 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002045 }
2046
Vladimir Markoe9004912016-06-16 16:50:52 +01002047 HConstInputsRef GetInputs() const {
2048 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002049 }
2050
2051 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002052 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002053
Aart Bik2767f4b2016-10-28 15:03:53 -07002054 bool HasInput(HInstruction* input) const {
2055 for (const HInstruction* i : GetInputs()) {
2056 if (i == input) {
2057 return true;
2058 }
2059 }
2060 return false;
2061 }
2062
Vladimir Marko372f10e2016-05-17 16:30:10 +01002063 void SetRawInputAt(size_t index, HInstruction* input) {
2064 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2065 }
2066
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002067 virtual void Accept(HGraphVisitor* visitor) = 0;
2068 virtual const char* DebugName() const = 0;
2069
Vladimir Markobd785672018-05-03 17:09:09 +01002070 DataType::Type GetType() const {
2071 return TypeField::Decode(GetPackedFields());
2072 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002073
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002074 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002075
2076 uint32_t GetDexPc() const { return dex_pc_; }
2077
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002078 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002079
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002080 // Can the instruction throw?
2081 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2082 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002083 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002084
2085 // Does the instruction always throw an exception unconditionally?
2086 virtual bool AlwaysThrows() const { return false; }
2087
David Brazdilec16f792015-08-19 15:04:01 +01002088 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002089
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002090 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002091 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002092
Calin Juravle10e244f2015-01-26 18:54:32 +00002093 // Does not apply for all instructions, but having this at top level greatly
2094 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002095 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002096 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002097 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002098 return true;
2099 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002100
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002101 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002102 return false;
2103 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002104
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002105 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2106 // with it. Otherwise return null.
2107 HNullCheck* GetImplicitNullCheck() const {
2108 // Find the first previous instruction which is not a move.
2109 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2110 if (first_prev_not_move != nullptr &&
2111 first_prev_not_move->IsNullCheck() &&
2112 first_prev_not_move->IsEmittedAtUseSite()) {
2113 return first_prev_not_move->AsNullCheck();
2114 }
2115 return nullptr;
2116 }
2117
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002118 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002120 }
2121
Calin Juravle2e768302015-07-28 14:41:11 +00002122 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002123
Calin Juravle61d544b2015-02-23 16:46:57 +00002124 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002125 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002126 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002127 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002128 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002129
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002130 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002131 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002132 // Note: fixup_end remains valid across push_front().
2133 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2134 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002135 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002136 uses_.push_front(*new_node);
2137 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002138 }
2139
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002140 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002141 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002142 // Note: env_fixup_end remains valid across push_front().
2143 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2144 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002145 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002146 env_uses_.push_front(*new_node);
2147 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002148 }
2149
David Brazdil1abb4192015-02-17 18:33:36 +00002150 void RemoveAsUserOfInput(size_t input) {
2151 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002152 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2153 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2154 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002155 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002156
Vladimir Marko372f10e2016-05-17 16:30:10 +01002157 void RemoveAsUserOfAllInputs() {
2158 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2159 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2160 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2161 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2162 }
2163 }
2164
David Brazdil1abb4192015-02-17 18:33:36 +00002165 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2166 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002167
Vladimir Marko46817b82016-03-29 12:21:58 +01002168 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2169 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2170 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002171 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002172 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002173 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002174
Aart Bikcc42be02016-10-20 16:14:16 -07002175 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002176 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002177 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002178 !CanThrow() &&
2179 !IsSuspendCheck() &&
2180 !IsControlFlow() &&
2181 !IsNativeDebugInfo() &&
2182 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002183 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002184 !IsMemoryBarrier() &&
2185 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002186 }
2187
Aart Bikcc42be02016-10-20 16:14:16 -07002188 bool IsDeadAndRemovable() const {
2189 return IsRemovable() && !HasUses();
2190 }
2191
Roland Levillain6c82d402014-10-13 16:10:27 +01002192 // Does this instruction strictly dominate `other_instruction`?
2193 // Returns false if this instruction and `other_instruction` are the same.
2194 // Aborts if this instruction and `other_instruction` are both phis.
2195 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002196
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002197 int GetId() const { return id_; }
2198 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002199
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002200 int GetSsaIndex() const { return ssa_index_; }
2201 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2202 bool HasSsaIndex() const { return ssa_index_ != -1; }
2203
2204 bool HasEnvironment() const { return environment_ != nullptr; }
2205 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002206 // Set the `environment_` field. Raw because this method does not
2207 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002208 void SetRawEnvironment(HEnvironment* environment) {
2209 DCHECK(environment_ == nullptr);
2210 DCHECK_EQ(environment->GetHolder(), this);
2211 environment_ = environment;
2212 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002213
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002214 void InsertRawEnvironment(HEnvironment* environment) {
2215 DCHECK(environment_ != nullptr);
2216 DCHECK_EQ(environment->GetHolder(), this);
2217 DCHECK(environment->GetParent() == nullptr);
2218 environment->parent_ = environment_;
2219 environment_ = environment;
2220 }
2221
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002222 void RemoveEnvironment();
2223
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002224 // Set the environment of this instruction, copying it from `environment`. While
2225 // copying, the uses lists are being updated.
2226 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002227 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002228 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002229 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002230 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002231 if (environment->GetParent() != nullptr) {
2232 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2233 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002234 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002235
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002236 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2237 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002238 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002239 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002240 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002241 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002242 if (environment->GetParent() != nullptr) {
2243 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2244 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002245 }
2246
Nicolas Geoffray39468442014-09-02 15:17:15 +01002247 // Returns the number of entries in the environment. Typically, that is the
2248 // number of dex registers in a method. It could be more in case of inlining.
2249 size_t EnvironmentSize() const;
2250
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002251 LocationSummary* GetLocations() const { return locations_; }
2252 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002253
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002254 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002255 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002256 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002257 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002258
Alexandre Rames188d4312015-04-09 18:30:21 +01002259 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2260 // uses of this instruction by `other` are *not* updated.
2261 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2262 ReplaceWith(other);
2263 other->ReplaceInput(this, use_index);
2264 }
2265
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002266 // Move `this` instruction before `cursor`
2267 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002268
Vladimir Markofb337ea2015-11-25 15:25:10 +00002269 // Move `this` before its first user and out of any loops. If there is no
2270 // out-of-loop user that dominates all other users, move the instruction
2271 // to the end of the out-of-loop common dominator of the user's blocks.
2272 //
2273 // This can be used only on non-throwing instructions with no side effects that
2274 // have at least one use but no environment uses.
2275 void MoveBeforeFirstUserAndOutOfLoops();
2276
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002277#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002278 bool Is##type() const;
2279
2280 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2281#undef INSTRUCTION_TYPE_CHECK
2282
2283#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002284 const H##type* As##type() const; \
2285 H##type* As##type();
2286
Vladimir Markoa90dd512018-05-04 15:04:45 +01002287 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2288#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002289
Artem Serovcced8ba2017-07-19 18:18:09 +01002290 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2291 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2292 // the instruction then the behaviour of this function is undefined.
2293 //
2294 // Note: It is semantically valid to create a clone of the instruction only until
2295 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2296 // copied.
2297 //
2298 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2299 // 'explicit HInstruction(const HInstruction& other)' for details.
2300 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2301 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2302 DebugName() << " " << GetId();
2303 UNREACHABLE();
2304 }
2305
2306 // Return whether instruction can be cloned (copied).
2307 virtual bool IsClonable() const { return false; }
2308
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002309 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002310 // TODO: this method is used by LICM and GVN with possibly different
2311 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002312 virtual bool CanBeMoved() const { return false; }
2313
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002314 // Returns whether any data encoded in the two instructions is equal.
2315 // This method does not look at the inputs. Both instructions must be
2316 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002317 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002318 return false;
2319 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002320
2321 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002322 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002323 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002324 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002325
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002326 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2327 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2328 // the virtual function because the __attribute__((__pure__)) doesn't really
2329 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302330 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002331
2332 virtual size_t ComputeHashCode() const {
2333 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002334 for (const HInstruction* input : GetInputs()) {
2335 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002336 }
2337 return result;
2338 }
2339
2340 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002341 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002342 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002343
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002344 size_t GetLifetimePosition() const { return lifetime_position_; }
2345 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2346 LiveInterval* GetLiveInterval() const { return live_interval_; }
2347 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2348 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2349
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002350 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2351
2352 // Returns whether the code generation of the instruction will require to have access
2353 // to the current method. Such instructions are:
2354 // (1): Instructions that require an environment, as calling the runtime requires
2355 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002356 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2357 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002358 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002359 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002360 }
2361
Vladimir Markodc151b22015-10-15 18:02:30 +01002362 // Returns whether the code generation of the instruction will require to have access
2363 // to the dex cache of the current method's declaring class via the current method.
2364 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002365
Mark Mendellc4701932015-04-10 13:18:51 -04002366 // Does this instruction have any use in an environment before
2367 // control flow hits 'other'?
2368 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2369
2370 // Remove all references to environment uses of this instruction.
2371 // The caller must ensure that this is safe to do.
2372 void RemoveEnvironmentUsers();
2373
Vladimir Markoa1de9182016-02-25 11:37:38 +00002374 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2375 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002376
David Brazdil1abb4192015-02-17 18:33:36 +00002377 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002378 // If set, the machine code for this instruction is assumed to be generated by
2379 // its users. Used by liveness analysis to compute use positions accordingly.
2380 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2381 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302382 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2383 static constexpr size_t kFieldInstructionKindSize =
2384 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002385 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302386 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002387 static constexpr size_t kFieldTypeSize =
2388 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2389 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002390 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2391
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302392 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2393 "Too many generic packed fields");
2394
Vladimir Markobd785672018-05-03 17:09:09 +01002395 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2396
Vladimir Marko372f10e2016-05-17 16:30:10 +01002397 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2398 return GetInputRecords()[i];
2399 }
2400
2401 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2402 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2403 input_records[index] = input;
2404 }
David Brazdil1abb4192015-02-17 18:33:36 +00002405
Vladimir Markoa1de9182016-02-25 11:37:38 +00002406 uint32_t GetPackedFields() const {
2407 return packed_fields_;
2408 }
2409
2410 template <size_t flag>
2411 bool GetPackedFlag() const {
2412 return (packed_fields_ & (1u << flag)) != 0u;
2413 }
2414
2415 template <size_t flag>
2416 void SetPackedFlag(bool value = true) {
2417 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2418 }
2419
2420 template <typename BitFieldType>
2421 typename BitFieldType::value_type GetPackedField() const {
2422 return BitFieldType::Decode(packed_fields_);
2423 }
2424
2425 template <typename BitFieldType>
2426 void SetPackedField(typename BitFieldType::value_type value) {
2427 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2428 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2429 }
2430
Artem Serovcced8ba2017-07-19 18:18:09 +01002431 // Copy construction for the instruction (used for Clone function).
2432 //
2433 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2434 // prepare_for_register_allocator are not copied (set to default values).
2435 //
2436 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2437 // fine for most of them. However for some of the instructions a custom copy constructor must be
2438 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2439 // for copying them).
2440 explicit HInstruction(const HInstruction& other)
2441 : previous_(nullptr),
2442 next_(nullptr),
2443 block_(nullptr),
2444 dex_pc_(other.dex_pc_),
2445 id_(-1),
2446 ssa_index_(-1),
2447 packed_fields_(other.packed_fields_),
2448 environment_(nullptr),
2449 locations_(nullptr),
2450 live_interval_(nullptr),
2451 lifetime_position_(kNoLifetime),
2452 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302453 reference_type_handle_(other.reference_type_handle_) {
2454 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002455
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002456 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302457 using InstructionKindField =
2458 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2459
Vladimir Marko46817b82016-03-29 12:21:58 +01002460 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2461 auto before_use_node = uses_.before_begin();
2462 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2463 HInstruction* user = use_node->GetUser();
2464 size_t input_index = use_node->GetIndex();
2465 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2466 before_use_node = use_node;
2467 }
2468 }
2469
2470 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2471 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2472 if (next != uses_.end()) {
2473 HInstruction* next_user = next->GetUser();
2474 size_t next_index = next->GetIndex();
2475 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2476 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2477 }
2478 }
2479
2480 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2481 auto before_env_use_node = env_uses_.before_begin();
2482 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2483 HEnvironment* user = env_use_node->GetUser();
2484 size_t input_index = env_use_node->GetIndex();
2485 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2486 before_env_use_node = env_use_node;
2487 }
2488 }
2489
2490 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2491 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2492 if (next != env_uses_.end()) {
2493 HEnvironment* next_user = next->GetUser();
2494 size_t next_index = next->GetIndex();
2495 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2496 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2497 }
2498 }
David Brazdil1abb4192015-02-17 18:33:36 +00002499
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002500 HInstruction* previous_;
2501 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002502 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002503 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002504
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002505 // An instruction gets an id when it is added to the graph.
2506 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002507 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002508 int id_;
2509
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002510 // When doing liveness analysis, instructions that have uses get an SSA index.
2511 int ssa_index_;
2512
Vladimir Markoa1de9182016-02-25 11:37:38 +00002513 // Packed fields.
2514 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002515
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002516 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002517 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002518
2519 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002520 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002521
Nicolas Geoffray39468442014-09-02 15:17:15 +01002522 // The environment associated with this instruction. Not null if the instruction
2523 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002524 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002525
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002526 // Set by the code generator.
2527 LocationSummary* locations_;
2528
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002529 // Set by the liveness analysis.
2530 LiveInterval* live_interval_;
2531
2532 // Set by the liveness analysis, this is the position in a linear
2533 // order of blocks where this instruction's live interval start.
2534 size_t lifetime_position_;
2535
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002536 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002537
Vladimir Markoa1de9182016-02-25 11:37:38 +00002538 // The reference handle part of the reference type info.
2539 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002540 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002541 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002542
David Brazdil1abb4192015-02-17 18:33:36 +00002543 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002544 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002545 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002546 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002547 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002548};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002549std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002550
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002551// Iterates over the instructions, while preserving the next instruction
2552// in case the current instruction gets removed from the list by the user
2553// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002554class HInstructionIterator : public ValueObject {
2555 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002556 explicit HInstructionIterator(const HInstructionList& instructions)
2557 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002558 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002559 }
2560
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002561 bool Done() const { return instruction_ == nullptr; }
2562 HInstruction* Current() const { return instruction_; }
2563 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002564 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002565 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002566 }
2567
2568 private:
2569 HInstruction* instruction_;
2570 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002571
2572 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002573};
2574
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002575// Iterates over the instructions without saving the next instruction,
2576// therefore handling changes in the graph potentially made by the user
2577// of this iterator.
2578class HInstructionIteratorHandleChanges : public ValueObject {
2579 public:
2580 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2581 : instruction_(instructions.first_instruction_) {
2582 }
2583
2584 bool Done() const { return instruction_ == nullptr; }
2585 HInstruction* Current() const { return instruction_; }
2586 void Advance() {
2587 instruction_ = instruction_->GetNext();
2588 }
2589
2590 private:
2591 HInstruction* instruction_;
2592
2593 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2594};
2595
2596
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002597class HBackwardInstructionIterator : public ValueObject {
2598 public:
2599 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2600 : instruction_(instructions.last_instruction_) {
2601 next_ = Done() ? nullptr : instruction_->GetPrevious();
2602 }
2603
2604 bool Done() const { return instruction_ == nullptr; }
2605 HInstruction* Current() const { return instruction_; }
2606 void Advance() {
2607 instruction_ = next_;
2608 next_ = Done() ? nullptr : instruction_->GetPrevious();
2609 }
2610
2611 private:
2612 HInstruction* instruction_;
2613 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002614
2615 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002616};
2617
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002618class HVariableInputSizeInstruction : public HInstruction {
2619 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002620 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002621 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002622 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2623 }
2624
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002625 void AddInput(HInstruction* input);
2626 void InsertInputAt(size_t index, HInstruction* input);
2627 void RemoveInputAt(size_t index);
2628
Igor Murashkind01745e2017-04-05 16:40:31 -07002629 // Removes all the inputs.
2630 // Also removes this instructions from each input's use list
2631 // (for non-environment uses only).
2632 void RemoveAllInputs();
2633
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002634 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302635 HVariableInputSizeInstruction(InstructionKind inst_kind,
2636 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002637 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002638 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002639 size_t number_of_inputs,
2640 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302641 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002642 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002643 HVariableInputSizeInstruction(InstructionKind inst_kind,
2644 DataType::Type type,
2645 SideEffects side_effects,
2646 uint32_t dex_pc,
2647 ArenaAllocator* allocator,
2648 size_t number_of_inputs,
2649 ArenaAllocKind kind)
2650 : HInstruction(inst_kind, type, side_effects, dex_pc),
2651 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002652
Artem Serovcced8ba2017-07-19 18:18:09 +01002653 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002654
Artem Serovcced8ba2017-07-19 18:18:09 +01002655 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002656};
2657
Vladimir Markof9f64412015-09-02 14:05:49 +01002658template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002659class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002660 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002661 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302662 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002663 HExpression<N>(InstructionKind kind,
2664 DataType::Type type,
2665 SideEffects side_effects,
2666 uint32_t dex_pc)
2667 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2668 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002669
Vladimir Marko372f10e2016-05-17 16:30:10 +01002670 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002671 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002672 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002673 }
2674
Artem Serovcced8ba2017-07-19 18:18:09 +01002675 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002676 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002677
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002678 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002679 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002680
2681 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002682};
2683
Vladimir Markobd785672018-05-03 17:09:09 +01002684// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002685template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002686class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002687 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002688 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002689
Vladimir Markobd785672018-05-03 17:09:09 +01002690 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002691
Vladimir Marko372f10e2016-05-17 16:30:10 +01002692 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002693 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002694 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002695 }
2696
Artem Serovcced8ba2017-07-19 18:18:09 +01002697 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002698 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002699
Vladimir Markof9f64412015-09-02 14:05:49 +01002700 private:
2701 friend class SsaBuilder;
2702};
2703
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002704// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2705// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002706class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002707 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002708 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002709 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302710 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002712 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002713
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002714 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002715
Artem Serovcced8ba2017-07-19 18:18:09 +01002716 protected:
2717 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002718};
2719
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002720// Represents dex's RETURN opcodes. A HReturn is a control flow
2721// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002722class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002723 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002724 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002725 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002726 SetRawInputAt(0, value);
2727 }
2728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002729 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002730
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002731 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002732
Artem Serovcced8ba2017-07-19 18:18:09 +01002733 protected:
2734 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002735};
2736
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002737class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002738 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002739 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002740 uint32_t reg_number,
2741 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002742 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002743 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002744 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302745 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002746 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002747 SideEffects::None(),
2748 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002749 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002750 number_of_inputs,
2751 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002752 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002754 // Phis are constructed live and marked dead if conflicting or unused.
2755 // Individual steps of SsaBuilder should assume that if a phi has been
2756 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2757 SetPackedFlag<kFlagIsLive>(true);
2758 SetPackedFlag<kFlagCanBeNull>(true);
2759 }
2760
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002761 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002762
David Brazdildee58d62016-04-07 09:54:26 +00002763 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 static DataType::Type ToPhiType(DataType::Type type) {
2765 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002766 }
2767
2768 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002770 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002771 // Make sure that only valid type changes occur. The following are allowed:
2772 // (1) int -> float/ref (primitive type propagation),
2773 // (2) long -> double (primitive type propagation).
2774 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002775 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2776 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2777 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002778 SetPackedField<TypeField>(new_type);
2779 }
2780
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002781 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002782 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2783
2784 uint32_t GetRegNumber() const { return reg_number_; }
2785
2786 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2787 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2788 bool IsDead() const { return !IsLive(); }
2789 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2790
Vladimir Markoe9004912016-06-16 16:50:52 +01002791 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002792 return other != nullptr
2793 && other->IsPhi()
2794 && other->AsPhi()->GetBlock() == GetBlock()
2795 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2796 }
2797
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002798 bool HasEquivalentPhi() const {
2799 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2800 return true;
2801 }
2802 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2803 return true;
2804 }
2805 return false;
2806 }
2807
David Brazdildee58d62016-04-07 09:54:26 +00002808 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2809 // An equivalent phi is a phi having the same dex register and type.
2810 // It assumes that phis with the same dex register are adjacent.
2811 HPhi* GetNextEquivalentPhiWithSameType() {
2812 HInstruction* next = GetNext();
2813 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2814 if (next->GetType() == GetType()) {
2815 return next->AsPhi();
2816 }
2817 next = next->GetNext();
2818 }
2819 return nullptr;
2820 }
2821
2822 DECLARE_INSTRUCTION(Phi);
2823
Artem Serovcced8ba2017-07-19 18:18:09 +01002824 protected:
2825 DEFAULT_COPY_CONSTRUCTOR(Phi);
2826
David Brazdildee58d62016-04-07 09:54:26 +00002827 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002828 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002829 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2830 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2831 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002832
David Brazdildee58d62016-04-07 09:54:26 +00002833 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002834};
2835
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002836// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002837// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002838// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002839class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002840 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302841 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002842 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302843 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002845 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002846
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002847 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002848
Artem Serovcced8ba2017-07-19 18:18:09 +01002849 protected:
2850 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002851};
2852
2853// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002854class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002855 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302856 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002857 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302858 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002860 bool IsClonable() const override { return true; }
2861 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002862
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002863 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002864 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002865 }
2866
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002867 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002868
Artem Serovcced8ba2017-07-19 18:18:09 +01002869 protected:
2870 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002871};
2872
Roland Levillain9867bc72015-08-05 10:21:34 +01002873class HConstant : public HExpression<0> {
2874 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302875 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2876 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2877 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002879 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002880
Roland Levillain1a653882016-03-18 18:05:57 +00002881 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002882 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002883 // Is this constant 0 in the arithmetic sense?
2884 virtual bool IsArithmeticZero() const { return false; }
2885 // Is this constant a 0-bit pattern?
2886 virtual bool IsZeroBitPattern() const { return false; }
2887 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002888 virtual bool IsOne() const { return false; }
2889
David Brazdil77a48ae2015-09-15 12:34:04 +00002890 virtual uint64_t GetValueAsUint64() const = 0;
2891
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002892 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002893
Artem Serovcced8ba2017-07-19 18:18:09 +01002894 protected:
2895 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002896};
2897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002898class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002899 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002900 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002901 return true;
2902 }
2903
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002904 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002906 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002907
Roland Levillain1a653882016-03-18 18:05:57 +00002908 // The null constant representation is a 0-bit pattern.
2909 virtual bool IsZeroBitPattern() const { return true; }
2910
Roland Levillain9867bc72015-08-05 10:21:34 +01002911 DECLARE_INSTRUCTION(NullConstant);
2912
Artem Serovcced8ba2017-07-19 18:18:09 +01002913 protected:
2914 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2915
Roland Levillain9867bc72015-08-05 10:21:34 +01002916 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302918 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2919 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002920
2921 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002922};
2923
2924// Constants of the type int. Those can be from Dex instructions, or
2925// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002926class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002927 public:
2928 int32_t GetValue() const { return value_; }
2929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002930 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002931 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2932 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002933
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002934 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002935 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002936 return other->AsIntConstant()->value_ == value_;
2937 }
2938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002939 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002941 bool IsMinusOne() const override { return GetValue() == -1; }
2942 bool IsArithmeticZero() const override { return GetValue() == 0; }
2943 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2944 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002945
Roland Levillain1a653882016-03-18 18:05:57 +00002946 // Integer constants are used to encode Boolean values as well,
2947 // where 1 means true and 0 means false.
2948 bool IsTrue() const { return GetValue() == 1; }
2949 bool IsFalse() const { return GetValue() == 0; }
2950
Roland Levillain9867bc72015-08-05 10:21:34 +01002951 DECLARE_INSTRUCTION(IntConstant);
2952
Artem Serovcced8ba2017-07-19 18:18:09 +01002953 protected:
2954 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2955
Roland Levillain9867bc72015-08-05 10:21:34 +01002956 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002957 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302958 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2959 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002960 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302961 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2962 value_(value ? 1 : 0) {
2963 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002964
2965 const int32_t value_;
2966
2967 friend class HGraph;
2968 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2969 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002970};
2971
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002972class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002973 public:
2974 int64_t GetValue() const { return value_; }
2975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002976 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002977
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002978 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002979 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002980 return other->AsLongConstant()->value_ == value_;
2981 }
2982
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002983 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002985 bool IsMinusOne() const override { return GetValue() == -1; }
2986 bool IsArithmeticZero() const override { return GetValue() == 0; }
2987 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2988 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002989
2990 DECLARE_INSTRUCTION(LongConstant);
2991
Artem Serovcced8ba2017-07-19 18:18:09 +01002992 protected:
2993 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2994
Roland Levillain9867bc72015-08-05 10:21:34 +01002995 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002996 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302997 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2998 value_(value) {
2999 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003000
3001 const int64_t value_;
3002
3003 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003004};
Dave Allison20dfc792014-06-16 20:44:29 -07003005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003006class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003007 public:
3008 float GetValue() const { return value_; }
3009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003010 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003011 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3012 }
3013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003014 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003015 DCHECK(other->IsFloatConstant()) << other->DebugName();
3016 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3017 }
3018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003019 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003020
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003021 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3023 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003024 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003025 return std::fpclassify(value_) == FP_ZERO;
3026 }
3027 bool IsArithmeticPositiveZero() const {
3028 return IsArithmeticZero() && !std::signbit(value_);
3029 }
3030 bool IsArithmeticNegativeZero() const {
3031 return IsArithmeticZero() && std::signbit(value_);
3032 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003033 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003034 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003035 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003036 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003037 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3038 }
3039 bool IsNaN() const {
3040 return std::isnan(value_);
3041 }
3042
3043 DECLARE_INSTRUCTION(FloatConstant);
3044
Artem Serovcced8ba2017-07-19 18:18:09 +01003045 protected:
3046 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3047
Roland Levillain31dd3d62016-02-16 12:21:02 +00003048 private:
3049 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303050 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3051 value_(value) {
3052 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003053 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303054 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3055 value_(bit_cast<float, int32_t>(value)) {
3056 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003057
3058 const float value_;
3059
3060 // Only the SsaBuilder and HGraph can create floating-point constants.
3061 friend class SsaBuilder;
3062 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063};
3064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003065class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066 public:
3067 double GetValue() const { return value_; }
3068
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003069 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003071 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003072 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3073 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3074 }
3075
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003076 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003078 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3080 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003081 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003082 return std::fpclassify(value_) == FP_ZERO;
3083 }
3084 bool IsArithmeticPositiveZero() const {
3085 return IsArithmeticZero() && !std::signbit(value_);
3086 }
3087 bool IsArithmeticNegativeZero() const {
3088 return IsArithmeticZero() && std::signbit(value_);
3089 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003090 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003091 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003092 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003093 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3095 }
3096 bool IsNaN() const {
3097 return std::isnan(value_);
3098 }
3099
3100 DECLARE_INSTRUCTION(DoubleConstant);
3101
Artem Serovcced8ba2017-07-19 18:18:09 +01003102 protected:
3103 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3104
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105 private:
3106 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303107 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3108 value_(value) {
3109 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303111 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3112 value_(bit_cast<double, int64_t>(value)) {
3113 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003114
3115 const double value_;
3116
3117 // Only the SsaBuilder and HGraph can create floating-point constants.
3118 friend class SsaBuilder;
3119 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120};
3121
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003122// Conditional branch. A block ending with an HIf instruction must have
3123// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003124class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003125 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003126 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003127 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003128 SetRawInputAt(0, input);
3129 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003130
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003131 bool IsClonable() const override { return true; }
3132 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003133
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003134 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003135 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003136 }
3137
3138 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003139 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003140 }
3141
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003142 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003143
Artem Serovcced8ba2017-07-19 18:18:09 +01003144 protected:
3145 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003146};
3147
David Brazdilfc6a86a2015-06-26 10:33:45 +00003148
3149// Abstract instruction which marks the beginning and/or end of a try block and
3150// links it to the respective exception handlers. Behaves the same as a Goto in
3151// non-exceptional control flow.
3152// Normal-flow successor is stored at index zero, exception handlers under
3153// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003154class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003155 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003156 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003157 kEntry,
3158 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003159 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003160 };
3161
Artem Serovd1aa7d02018-06-22 11:35:46 +01003162 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3163 // across the catch block entering edges as GC might happen during throwing an exception.
3164 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3165 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003166 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003167 : HExpression(kTryBoundary,
3168 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3169 : SideEffects::None(),
3170 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003171 SetPackedField<BoundaryKindField>(kind);
3172 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003173
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003174 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003175
3176 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003177 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003178
David Brazdild26a4112015-11-10 11:07:31 +00003179 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3180 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3181 }
3182
David Brazdilfc6a86a2015-06-26 10:33:45 +00003183 // Returns whether `handler` is among its exception handlers (non-zero index
3184 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003185 bool HasExceptionHandler(const HBasicBlock& handler) const {
3186 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003187 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003188 }
3189
3190 // If not present already, adds `handler` to its block's list of exception
3191 // handlers.
3192 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003193 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003194 GetBlock()->AddSuccessor(handler);
3195 }
3196 }
3197
Vladimir Markoa1de9182016-02-25 11:37:38 +00003198 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3199 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003200
David Brazdilffee3d32015-07-06 11:48:53 +01003201 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3202
David Brazdilfc6a86a2015-06-26 10:33:45 +00003203 DECLARE_INSTRUCTION(TryBoundary);
3204
Artem Serovcced8ba2017-07-19 18:18:09 +01003205 protected:
3206 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3207
David Brazdilfc6a86a2015-06-26 10:33:45 +00003208 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003209 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3210 static constexpr size_t kFieldBoundaryKindSize =
3211 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3212 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3213 kFieldBoundaryKind + kFieldBoundaryKindSize;
3214 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3215 "Too many packed fields.");
3216 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003217};
3218
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003219// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003220class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003221 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003222 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3223 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003224 HDeoptimize(ArenaAllocator* allocator,
3225 HInstruction* cond,
3226 DeoptimizationKind kind,
3227 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003228 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303229 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003230 SideEffects::All(),
3231 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003232 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003233 /* number_of_inputs */ 1,
3234 kArenaAllocMisc) {
3235 SetPackedFlag<kFieldCanBeMoved>(false);
3236 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003237 SetRawInputAt(0, cond);
3238 }
3239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003240 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003241
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003242 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3243 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3244 // instead of `guard`.
3245 // We set CanTriggerGC to prevent any intermediate address to be live
3246 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003247 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003248 HInstruction* cond,
3249 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003250 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003251 uint32_t dex_pc)
3252 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303253 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003254 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003255 SideEffects::CanTriggerGC(),
3256 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003257 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003258 /* number_of_inputs */ 2,
3259 kArenaAllocMisc) {
3260 SetPackedFlag<kFieldCanBeMoved>(true);
3261 SetPackedField<DeoptimizeKindField>(kind);
3262 SetRawInputAt(0, cond);
3263 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003264 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003265
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003266 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003268 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003269 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3270 }
3271
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003272 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003274 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003275
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003276 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003277
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003278 bool GuardsAnInput() const {
3279 return InputCount() == 2;
3280 }
3281
3282 HInstruction* GuardedInput() const {
3283 DCHECK(GuardsAnInput());
3284 return InputAt(1);
3285 }
3286
3287 void RemoveGuard() {
3288 RemoveInputAt(1);
3289 }
3290
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003291 DECLARE_INSTRUCTION(Deoptimize);
3292
Artem Serovcced8ba2017-07-19 18:18:09 +01003293 protected:
3294 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3295
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003296 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003297 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3298 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3299 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003300 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003301 static constexpr size_t kNumberOfDeoptimizePackedBits =
3302 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3303 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3304 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003305 using DeoptimizeKindField =
3306 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003307};
3308
Mingyao Yang063fc772016-08-02 11:02:54 -07003309// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3310// The compiled code checks this flag value in a guard before devirtualized call and
3311// if it's true, starts to do deoptimization.
3312// It has a 4-byte slot on stack.
3313// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003314class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003315 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003316 // CHA guards are only optimized in a separate pass and it has no side effects
3317 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003318 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303319 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003320 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303321 SideEffects::None(),
3322 dex_pc,
3323 allocator,
3324 0,
3325 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003326 }
3327
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003328 // We do all CHA guard elimination/motion in a single pass, after which there is no
3329 // further guard elimination/motion since a guard might have been used for justification
3330 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3331 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003332 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003333
3334 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3335
Artem Serovcced8ba2017-07-19 18:18:09 +01003336 protected:
3337 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003338};
3339
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003340// Represents the ArtMethod that was passed as a first argument to
3341// the method. It is used by instructions that depend on it, like
3342// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003343class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003344 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003345 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303346 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3347 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003348
3349 DECLARE_INSTRUCTION(CurrentMethod);
3350
Artem Serovcced8ba2017-07-19 18:18:09 +01003351 protected:
3352 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003353};
3354
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003355// Fetches an ArtMethod from the virtual table or the interface method table
3356// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003357class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003358 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003359 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003360 kVTable,
3361 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003362 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003363 };
3364 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003365 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003366 TableKind kind,
3367 size_t index,
3368 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303369 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003370 index_(index) {
3371 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003372 SetRawInputAt(0, cls);
3373 }
3374
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003375 bool IsClonable() const override { return true; }
3376 bool CanBeMoved() const override { return true; }
3377 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003378 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003379 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003380 }
3381
Vladimir Markoa1de9182016-02-25 11:37:38 +00003382 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003383 size_t GetIndex() const { return index_; }
3384
3385 DECLARE_INSTRUCTION(ClassTableGet);
3386
Artem Serovcced8ba2017-07-19 18:18:09 +01003387 protected:
3388 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3389
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003390 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003391 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003392 static constexpr size_t kFieldTableKindSize =
3393 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3394 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3395 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3396 "Too many packed fields.");
3397 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3398
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003399 // The index of the ArtMethod in the table.
3400 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003401};
3402
Mark Mendellfe57faa2015-09-18 09:26:15 -04003403// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3404// have one successor for each entry in the switch table, and the final successor
3405// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003406class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003407 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003408 HPackedSwitch(int32_t start_value,
3409 uint32_t num_entries,
3410 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003411 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003412 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003413 start_value_(start_value),
3414 num_entries_(num_entries) {
3415 SetRawInputAt(0, input);
3416 }
3417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003418 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003420 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003421
3422 int32_t GetStartValue() const { return start_value_; }
3423
Vladimir Marko211c2112015-09-24 16:52:33 +01003424 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003425
3426 HBasicBlock* GetDefaultBlock() const {
3427 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003428 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003429 }
3430 DECLARE_INSTRUCTION(PackedSwitch);
3431
Artem Serovcced8ba2017-07-19 18:18:09 +01003432 protected:
3433 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3434
Mark Mendellfe57faa2015-09-18 09:26:15 -04003435 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003436 const int32_t start_value_;
3437 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003438};
3439
Roland Levillain88cb1752014-10-20 16:36:47 +01003440class HUnaryOperation : public HExpression<1> {
3441 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303442 HUnaryOperation(InstructionKind kind,
3443 DataType::Type result_type,
3444 HInstruction* input,
3445 uint32_t dex_pc = kNoDexPc)
3446 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003447 SetRawInputAt(0, input);
3448 }
3449
Artem Serovcced8ba2017-07-19 18:18:09 +01003450 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003451 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003452
Roland Levillain88cb1752014-10-20 16:36:47 +01003453 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003454 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003455
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003456 bool CanBeMoved() const override { return true; }
3457 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003458 return true;
3459 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003460
Roland Levillain31dd3d62016-02-16 12:21:02 +00003461 // Try to statically evaluate `this` and return a HConstant
3462 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003463 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003464 HConstant* TryStaticEvaluation() const;
3465
3466 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003467 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3468 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003469 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3470 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003471
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003472 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003473
Artem Serovcced8ba2017-07-19 18:18:09 +01003474 protected:
3475 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003476};
3477
Dave Allison20dfc792014-06-16 20:44:29 -07003478class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003479 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303480 HBinaryOperation(InstructionKind kind,
3481 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003482 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003483 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003484 SideEffects side_effects = SideEffects::None(),
3485 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303486 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003487 SetRawInputAt(0, left);
3488 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003489 }
3490
Artem Serovcced8ba2017-07-19 18:18:09 +01003491 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003492 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003493
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003494 HInstruction* GetLeft() const { return InputAt(0); }
3495 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003496 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003497
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003498 virtual bool IsCommutative() const { return false; }
3499
3500 // Put constant on the right.
3501 // Returns whether order is changed.
3502 bool OrderInputsWithConstantOnTheRight() {
3503 HInstruction* left = InputAt(0);
3504 HInstruction* right = InputAt(1);
3505 if (left->IsConstant() && !right->IsConstant()) {
3506 ReplaceInput(right, 0);
3507 ReplaceInput(left, 1);
3508 return true;
3509 }
3510 return false;
3511 }
3512
3513 // Order inputs by instruction id, but favor constant on the right side.
3514 // This helps GVN for commutative ops.
3515 void OrderInputs() {
3516 DCHECK(IsCommutative());
3517 HInstruction* left = InputAt(0);
3518 HInstruction* right = InputAt(1);
3519 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3520 return;
3521 }
3522 if (OrderInputsWithConstantOnTheRight()) {
3523 return;
3524 }
3525 // Order according to instruction id.
3526 if (left->GetId() > right->GetId()) {
3527 ReplaceInput(right, 0);
3528 ReplaceInput(left, 1);
3529 }
3530 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003531
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003532 bool CanBeMoved() const override { return true; }
3533 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003534 return true;
3535 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003536
Roland Levillain31dd3d62016-02-16 12:21:02 +00003537 // Try to statically evaluate `this` and return a HConstant
3538 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003539 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003540 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003541
3542 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003543 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3544 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003545 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3546 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003547 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003548 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3549 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003550 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3551 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003552 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3553 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003554 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003555 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3556 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003557
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003558 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003559 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003560 HConstant* GetConstantRight() const;
3561
3562 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003563 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003564 HInstruction* GetLeastConstantLeft() const;
3565
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003566 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003567
Artem Serovcced8ba2017-07-19 18:18:09 +01003568 protected:
3569 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003570};
3571
Mark Mendellc4701932015-04-10 13:18:51 -04003572// The comparison bias applies for floating point operations and indicates how NaN
3573// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003574enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003575 kNoBias, // bias is not applicable (i.e. for long operation)
3576 kGtBias, // return 1 for NaN comparisons
3577 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003578 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003579};
3580
Roland Levillain31dd3d62016-02-16 12:21:02 +00003581std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3582
Dave Allison20dfc792014-06-16 20:44:29 -07003583class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003584 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303585 HCondition(InstructionKind kind,
3586 HInstruction* first,
3587 HInstruction* second,
3588 uint32_t dex_pc = kNoDexPc)
3589 : HBinaryOperation(kind,
3590 DataType::Type::kBool,
3591 first,
3592 second,
3593 SideEffects::None(),
3594 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003595 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3596 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003597
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003598 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003599 // `instruction`, and disregard moves in between.
3600 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003601
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003602 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003603
3604 virtual IfCondition GetCondition() const = 0;
3605
Mark Mendellc4701932015-04-10 13:18:51 -04003606 virtual IfCondition GetOppositeCondition() const = 0;
3607
Vladimir Markoa1de9182016-02-25 11:37:38 +00003608 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003609 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3610
Vladimir Markoa1de9182016-02-25 11:37:38 +00003611 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3612 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003613
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003614 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003615 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003616 }
3617
Roland Levillain4fa13f62015-07-06 18:11:54 +01003618 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003619 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003620 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003621 if (if_cond == kCondNE) {
3622 return true;
3623 } else if (if_cond == kCondEQ) {
3624 return false;
3625 }
3626 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003627 }
3628
3629 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003630 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003631 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003632 if (if_cond == kCondEQ) {
3633 return true;
3634 } else if (if_cond == kCondNE) {
3635 return false;
3636 }
3637 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003638 }
3639
Roland Levillain31dd3d62016-02-16 12:21:02 +00003640 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003641 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003642 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003643 static constexpr size_t kFieldComparisonBiasSize =
3644 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3645 static constexpr size_t kNumberOfConditionPackedBits =
3646 kFieldComparisonBias + kFieldComparisonBiasSize;
3647 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3648 using ComparisonBiasField =
3649 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3650
Roland Levillain31dd3d62016-02-16 12:21:02 +00003651 template <typename T>
3652 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3653
3654 template <typename T>
3655 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003656 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003657 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3658 // Handle the bias.
3659 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3660 }
3661
3662 // Return an integer constant containing the result of a condition evaluated at compile time.
3663 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3664 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3665 }
3666
Artem Serovcced8ba2017-07-19 18:18:09 +01003667 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003668};
3669
3670// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003671class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003672 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003673 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303674 : HCondition(kEqual, first, second, dex_pc) {
3675 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003676
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003677 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003678
Vladimir Marko9e23df52015-11-10 17:14:35 +00003679 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003680 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003681 return MakeConstantCondition(true, GetDexPc());
3682 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003683 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003684 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3685 }
3686 // In the following Evaluate methods, a HCompare instruction has
3687 // been merged into this HEqual instruction; evaluate it as
3688 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003689 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003690 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3691 GetDexPc());
3692 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003693 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003694 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3695 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003696 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003697 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003698 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003699
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003700 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003701
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003702 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003703 return kCondEQ;
3704 }
3705
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003706 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003707 return kCondNE;
3708 }
3709
Artem Serovcced8ba2017-07-19 18:18:09 +01003710 protected:
3711 DEFAULT_COPY_CONSTRUCTOR(Equal);
3712
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003713 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003714 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003715};
3716
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003717class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003718 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303719 HNotEqual(HInstruction* first, HInstruction* second,
3720 uint32_t dex_pc = kNoDexPc)
3721 : HCondition(kNotEqual, first, second, dex_pc) {
3722 }
Dave Allison20dfc792014-06-16 20:44:29 -07003723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003724 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003725
Vladimir Marko9e23df52015-11-10 17:14:35 +00003726 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003727 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 return MakeConstantCondition(false, GetDexPc());
3729 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003730 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003731 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3732 }
3733 // In the following Evaluate methods, a HCompare instruction has
3734 // been merged into this HNotEqual instruction; evaluate it as
3735 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003736 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003737 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3738 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003739 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3741 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003742 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003744 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003745
Dave Allison20dfc792014-06-16 20:44:29 -07003746 DECLARE_INSTRUCTION(NotEqual);
3747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003748 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003749 return kCondNE;
3750 }
3751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003752 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003753 return kCondEQ;
3754 }
3755
Artem Serovcced8ba2017-07-19 18:18:09 +01003756 protected:
3757 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3758
Dave Allison20dfc792014-06-16 20:44:29 -07003759 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003760 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003761};
3762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003763class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003764 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303765 HLessThan(HInstruction* first, HInstruction* second,
3766 uint32_t dex_pc = kNoDexPc)
3767 : HCondition(kLessThan, first, second, dex_pc) {
3768 }
Dave Allison20dfc792014-06-16 20:44:29 -07003769
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003770 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003771 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003772 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003773 // In the following Evaluate methods, a HCompare instruction has
3774 // been merged into this HLessThan instruction; evaluate it as
3775 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003776 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003777 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3778 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003779 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003780 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3781 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003782 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003783 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003784 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003785
Dave Allison20dfc792014-06-16 20:44:29 -07003786 DECLARE_INSTRUCTION(LessThan);
3787
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003788 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003789 return kCondLT;
3790 }
3791
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003792 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003793 return kCondGE;
3794 }
3795
Artem Serovcced8ba2017-07-19 18:18:09 +01003796 protected:
3797 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3798
Dave Allison20dfc792014-06-16 20:44:29 -07003799 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003800 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003801};
3802
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003803class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003804 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303805 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3806 uint32_t dex_pc = kNoDexPc)
3807 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3808 }
Dave Allison20dfc792014-06-16 20:44:29 -07003809
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003810 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003811 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003812 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003813 // In the following Evaluate methods, a HCompare instruction has
3814 // been merged into this HLessThanOrEqual instruction; evaluate it as
3815 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003816 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003817 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3818 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003819 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003820 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3821 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003822 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003823 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003824 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003825
Dave Allison20dfc792014-06-16 20:44:29 -07003826 DECLARE_INSTRUCTION(LessThanOrEqual);
3827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003828 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003829 return kCondLE;
3830 }
3831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003832 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003833 return kCondGT;
3834 }
3835
Artem Serovcced8ba2017-07-19 18:18:09 +01003836 protected:
3837 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3838
Dave Allison20dfc792014-06-16 20:44:29 -07003839 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003840 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003841};
3842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003843class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003844 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003845 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303846 : HCondition(kGreaterThan, first, second, dex_pc) {
3847 }
Dave Allison20dfc792014-06-16 20:44:29 -07003848
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003849 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003850 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003851 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003852 // In the following Evaluate methods, a HCompare instruction has
3853 // been merged into this HGreaterThan instruction; evaluate it as
3854 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003855 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003856 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3857 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003858 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003859 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3860 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003861 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003862 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003863 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003864
Dave Allison20dfc792014-06-16 20:44:29 -07003865 DECLARE_INSTRUCTION(GreaterThan);
3866
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003867 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003868 return kCondGT;
3869 }
3870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003871 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003872 return kCondLE;
3873 }
3874
Artem Serovcced8ba2017-07-19 18:18:09 +01003875 protected:
3876 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3877
Dave Allison20dfc792014-06-16 20:44:29 -07003878 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003879 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003880};
3881
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003882class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003883 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003884 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303885 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3886 }
Dave Allison20dfc792014-06-16 20:44:29 -07003887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003888 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003889 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003890 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003891 // In the following Evaluate methods, a HCompare instruction has
3892 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3893 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003894 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003895 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3896 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003897 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003898 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3899 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003900 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003901 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003902 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003903
Dave Allison20dfc792014-06-16 20:44:29 -07003904 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003906 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003907 return kCondGE;
3908 }
3909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003910 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003911 return kCondLT;
3912 }
3913
Artem Serovcced8ba2017-07-19 18:18:09 +01003914 protected:
3915 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3916
Dave Allison20dfc792014-06-16 20:44:29 -07003917 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003918 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003919};
3920
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003921class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003922 public:
3923 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303924 : HCondition(kBelow, first, second, dex_pc) {
3925 }
Aart Bike9f37602015-10-09 11:15:55 -07003926
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003927 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003928 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003929 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003930 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003931 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3932 }
3933 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003934 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003935 LOG(FATAL) << DebugName() << " is not defined for float values";
3936 UNREACHABLE();
3937 }
3938 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003939 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003940 LOG(FATAL) << DebugName() << " is not defined for double values";
3941 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003942 }
3943
3944 DECLARE_INSTRUCTION(Below);
3945
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003946 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003947 return kCondB;
3948 }
3949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003950 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003951 return kCondAE;
3952 }
3953
Artem Serovcced8ba2017-07-19 18:18:09 +01003954 protected:
3955 DEFAULT_COPY_CONSTRUCTOR(Below);
3956
Aart Bike9f37602015-10-09 11:15:55 -07003957 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003958 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003959 return MakeUnsigned(x) < MakeUnsigned(y);
3960 }
Aart Bike9f37602015-10-09 11:15:55 -07003961};
3962
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003963class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003964 public:
3965 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303966 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3967 }
Aart Bike9f37602015-10-09 11:15:55 -07003968
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003969 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003970 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003971 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003972 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003973 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3974 }
3975 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003976 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003977 LOG(FATAL) << DebugName() << " is not defined for float values";
3978 UNREACHABLE();
3979 }
3980 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003981 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003982 LOG(FATAL) << DebugName() << " is not defined for double values";
3983 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003984 }
3985
3986 DECLARE_INSTRUCTION(BelowOrEqual);
3987
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003988 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003989 return kCondBE;
3990 }
3991
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003992 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003993 return kCondA;
3994 }
3995
Artem Serovcced8ba2017-07-19 18:18:09 +01003996 protected:
3997 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3998
Aart Bike9f37602015-10-09 11:15:55 -07003999 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004000 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004001 return MakeUnsigned(x) <= MakeUnsigned(y);
4002 }
Aart Bike9f37602015-10-09 11:15:55 -07004003};
4004
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004005class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004006 public:
4007 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304008 : HCondition(kAbove, first, second, dex_pc) {
4009 }
Aart Bike9f37602015-10-09 11:15:55 -07004010
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004011 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004012 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004013 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004014 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004015 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4016 }
4017 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004018 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004019 LOG(FATAL) << DebugName() << " is not defined for float values";
4020 UNREACHABLE();
4021 }
4022 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004023 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004024 LOG(FATAL) << DebugName() << " is not defined for double values";
4025 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004026 }
4027
4028 DECLARE_INSTRUCTION(Above);
4029
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004030 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004031 return kCondA;
4032 }
4033
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004034 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004035 return kCondBE;
4036 }
4037
Artem Serovcced8ba2017-07-19 18:18:09 +01004038 protected:
4039 DEFAULT_COPY_CONSTRUCTOR(Above);
4040
Aart Bike9f37602015-10-09 11:15:55 -07004041 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004042 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004043 return MakeUnsigned(x) > MakeUnsigned(y);
4044 }
Aart Bike9f37602015-10-09 11:15:55 -07004045};
4046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004047class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004048 public:
4049 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304050 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4051 }
Aart Bike9f37602015-10-09 11:15:55 -07004052
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004053 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004054 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004055 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004056 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004057 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4058 }
4059 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004060 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004061 LOG(FATAL) << DebugName() << " is not defined for float values";
4062 UNREACHABLE();
4063 }
4064 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004065 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004066 LOG(FATAL) << DebugName() << " is not defined for double values";
4067 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004068 }
4069
4070 DECLARE_INSTRUCTION(AboveOrEqual);
4071
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004072 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004073 return kCondAE;
4074 }
4075
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004076 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004077 return kCondB;
4078 }
4079
Artem Serovcced8ba2017-07-19 18:18:09 +01004080 protected:
4081 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4082
Aart Bike9f37602015-10-09 11:15:55 -07004083 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004084 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004085 return MakeUnsigned(x) >= MakeUnsigned(y);
4086 }
Aart Bike9f37602015-10-09 11:15:55 -07004087};
Dave Allison20dfc792014-06-16 20:44:29 -07004088
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004089// Instruction to check how two inputs compare to each other.
4090// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004091class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004092 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004093 // Note that `comparison_type` is the type of comparison performed
4094 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004095 // HCompare instruction (which is always DataType::Type::kInt).
4096 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004097 HInstruction* first,
4098 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004099 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004100 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304101 : HBinaryOperation(kCompare,
4102 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004103 first,
4104 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004105 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004106 dex_pc) {
4107 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004108 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4109 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004110 }
4111
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004113 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4114
4115 template <typename T>
4116 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004117 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004118 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4119 // Handle the bias.
4120 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4121 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004122
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004123 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004124 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4125 // reach this code path when processing a freshly built HIR
4126 // graph. However HCompare integer instructions can be synthesized
4127 // by the instruction simplifier to implement IntegerCompare and
4128 // IntegerSignum intrinsics, so we have to handle this case.
4129 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004130 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004131 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004132 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4133 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004134 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004135 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4136 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004137 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004138 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004139 }
4140
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004141 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004142 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004143 }
4144
Vladimir Markoa1de9182016-02-25 11:37:38 +00004145 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004146
Roland Levillain31dd3d62016-02-16 12:21:02 +00004147 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004148 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004149 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004150 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004151 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004152 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004154 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004155 // Comparisons do not require a runtime call in any back end.
4156 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004157 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004158
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004159 DECLARE_INSTRUCTION(Compare);
4160
Roland Levillain31dd3d62016-02-16 12:21:02 +00004161 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004162 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004163 static constexpr size_t kFieldComparisonBiasSize =
4164 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4165 static constexpr size_t kNumberOfComparePackedBits =
4166 kFieldComparisonBias + kFieldComparisonBiasSize;
4167 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4168 using ComparisonBiasField =
4169 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4170
Roland Levillain31dd3d62016-02-16 12:21:02 +00004171 // Return an integer constant containing the result of a comparison evaluated at compile time.
4172 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4173 DCHECK(value == -1 || value == 0 || value == 1) << value;
4174 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4175 }
4176
Artem Serovcced8ba2017-07-19 18:18:09 +01004177 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004178};
4179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004180class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004181 public:
4182 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004183 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004184 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004185 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004186 bool finalizable,
4187 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304188 : HExpression(kNewInstance,
4189 DataType::Type::kReference,
4190 SideEffects::CanTriggerGC(),
4191 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004192 type_index_(type_index),
4193 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004194 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004195 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004196 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004197 }
4198
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004199 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004200
Andreas Gampea5b09a62016-11-17 15:21:22 -08004201 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004202 const DexFile& GetDexFile() const { return dex_file_; }
4203
4204 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004205 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004206
Mingyao Yang062157f2016-03-02 10:15:36 -08004207 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004208 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004209
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004210 bool NeedsChecks() const {
4211 return entrypoint_ == kQuickAllocObjectWithChecks;
4212 }
David Brazdil6de19382016-01-08 17:37:10 +00004213
Vladimir Markoa1de9182016-02-25 11:37:38 +00004214 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004215
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004216 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004217
4218 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4219
4220 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4221 entrypoint_ = entrypoint;
4222 }
4223
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004224 HLoadClass* GetLoadClass() const {
4225 HInstruction* input = InputAt(0);
4226 if (input->IsClinitCheck()) {
4227 input = input->InputAt(0);
4228 }
4229 DCHECK(input->IsLoadClass());
4230 return input->AsLoadClass();
4231 }
4232
David Brazdil6de19382016-01-08 17:37:10 +00004233 bool IsStringAlloc() const;
4234
4235 DECLARE_INSTRUCTION(NewInstance);
4236
Artem Serovcced8ba2017-07-19 18:18:09 +01004237 protected:
4238 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4239
David Brazdil6de19382016-01-08 17:37:10 +00004240 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004241 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004242 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4243 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4244 "Too many packed fields.");
4245
Andreas Gampea5b09a62016-11-17 15:21:22 -08004246 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004247 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004248 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004249};
4250
Agi Csaki05f20562015-08-19 14:58:14 -07004251enum IntrinsicNeedsEnvironmentOrCache {
4252 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4253 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004254};
4255
Aart Bik5d75afe2015-12-14 11:57:01 -08004256enum IntrinsicSideEffects {
4257 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4258 kReadSideEffects, // Intrinsic may read heap memory.
4259 kWriteSideEffects, // Intrinsic may write heap memory.
4260 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4261};
4262
4263enum IntrinsicExceptions {
4264 kNoThrow, // Intrinsic does not throw any exceptions.
4265 kCanThrow // Intrinsic may throw exceptions.
4266};
4267
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004268class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004269 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004270 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004271
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004272 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004273 SetRawInputAt(index, argument);
4274 }
4275
Roland Levillain3e3d7332015-04-28 11:00:54 +01004276 // Return the number of arguments. This number can be lower than
4277 // the number of inputs returned by InputCount(), as some invoke
4278 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4279 // inputs at the end of their list of inputs.
4280 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4281
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004282 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4283
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004284 InvokeType GetInvokeType() const {
4285 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004286 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004287
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004288 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004289 return intrinsic_;
4290 }
4291
Aart Bik5d75afe2015-12-14 11:57:01 -08004292 void SetIntrinsic(Intrinsics intrinsic,
4293 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4294 IntrinsicSideEffects side_effects,
4295 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004296
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004297 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004298 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004299 }
4300
Aart Bikff7d89c2016-11-07 08:49:28 -08004301 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4302
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004303 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004304
Aart Bika8b8e9b2018-01-09 11:01:02 -08004305 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004307 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004309 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004311 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004312 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4313 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004314
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004315 uint32_t* GetIntrinsicOptimizations() {
4316 return &intrinsic_optimizations_;
4317 }
4318
4319 const uint32_t* GetIntrinsicOptimizations() const {
4320 return &intrinsic_optimizations_;
4321 }
4322
4323 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4324
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004325 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004326 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004327
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004328 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004329
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004330 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004331 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4332 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004333 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004334 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004335 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4336 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004337 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004338 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004339
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304340 HInvoke(InstructionKind kind,
4341 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004342 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004343 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004344 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004345 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004346 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004347 ArtMethod* resolved_method,
4348 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004349 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304350 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004351 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004352 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4353 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004354 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004355 number_of_arguments + number_of_other_inputs,
4356 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004357 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004358 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004359 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004360 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004361 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004362 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004363 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004364 }
4365
Artem Serovcced8ba2017-07-19 18:18:09 +01004366 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4367
Roland Levillain3e3d7332015-04-28 11:00:54 +01004368 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004369 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004370 const uint32_t dex_method_index_;
4371 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004372
4373 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4374 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004375};
4376
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004377class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004378 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004379 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004380 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004381 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004382 uint32_t dex_pc,
4383 uint32_t dex_method_index,
4384 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304385 : HInvoke(kInvokeUnresolved,
4386 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004387 number_of_arguments,
4388 0u /* number_of_other_inputs */,
4389 return_type,
4390 dex_pc,
4391 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004392 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004393 invoke_type) {
4394 }
4395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004396 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004397
Calin Juravle175dc732015-08-25 15:42:32 +01004398 DECLARE_INSTRUCTION(InvokeUnresolved);
4399
Artem Serovcced8ba2017-07-19 18:18:09 +01004400 protected:
4401 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004402};
4403
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004404class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004405 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004406 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004407 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004408 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004409 uint32_t dex_pc,
4410 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304411 : HInvoke(kInvokePolymorphic,
4412 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004413 number_of_arguments,
4414 0u /* number_of_other_inputs */,
4415 return_type,
4416 dex_pc,
4417 dex_method_index,
4418 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304419 kVirtual) {
4420 }
Orion Hodsonac141392017-01-13 11:53:47 +00004421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004422 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004423
Orion Hodsonac141392017-01-13 11:53:47 +00004424 DECLARE_INSTRUCTION(InvokePolymorphic);
4425
Artem Serovcced8ba2017-07-19 18:18:09 +01004426 protected:
4427 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004428};
4429
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004430class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004431 public:
4432 HInvokeCustom(ArenaAllocator* allocator,
4433 uint32_t number_of_arguments,
4434 uint32_t call_site_index,
4435 DataType::Type return_type,
4436 uint32_t dex_pc)
4437 : HInvoke(kInvokeCustom,
4438 allocator,
4439 number_of_arguments,
4440 /* number_of_other_inputs */ 0u,
4441 return_type,
4442 dex_pc,
4443 /* dex_method_index */ dex::kDexNoIndex,
4444 /* resolved_method */ nullptr,
4445 kStatic),
4446 call_site_index_(call_site_index) {
4447 }
4448
4449 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4450
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004451 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004452
4453 DECLARE_INSTRUCTION(InvokeCustom);
4454
4455 protected:
4456 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4457
4458 private:
4459 uint32_t call_site_index_;
4460};
4461
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004462class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004463 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004464 // Requirements of this method call regarding the class
4465 // initialization (clinit) check of its declaring class.
4466 enum class ClinitCheckRequirement {
4467 kNone, // Class already initialized.
4468 kExplicit, // Static call having explicit clinit check as last input.
4469 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004470 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004471 };
4472
Vladimir Marko58155012015-08-19 12:49:41 +00004473 // Determines how to load the target ArtMethod*.
4474 enum class MethodLoadKind {
4475 // Use a String init ArtMethod* loaded from Thread entrypoints.
4476 kStringInit,
4477
4478 // Use the method's own ArtMethod* loaded by the register allocator.
4479 kRecursive,
4480
Vladimir Marko65979462017-05-19 17:25:12 +01004481 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4482 // Used for boot image methods referenced by boot image code.
4483 kBootImageLinkTimePcRelative,
4484
Vladimir Markob066d432018-01-03 13:14:37 +00004485 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4486 // Used for app->boot calls with relocatable image.
4487 kBootImageRelRo,
4488
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004489 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004490 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004491 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004492
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004493 // Use ArtMethod* at a known address, embed the direct address in the code.
4494 // Used for for JIT-compiled calls.
4495 kJitDirectAddress,
4496
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004497 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4498 // used when other kinds are unimplemented on a particular architecture.
4499 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004500 };
4501
4502 // Determines the location of the code pointer.
4503 enum class CodePtrLocation {
4504 // Recursive call, use local PC-relative call instruction.
4505 kCallSelf,
4506
Vladimir Marko58155012015-08-19 12:49:41 +00004507 // Use code pointer from the ArtMethod*.
4508 // Used when we don't know the target code. This is also the last-resort-kind used when
4509 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4510 kCallArtMethod,
4511 };
4512
4513 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004514 MethodLoadKind method_load_kind;
4515 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004516 // The method load data holds
4517 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4518 // Note that there are multiple string init methods, each having its own offset.
4519 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004520 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004521 };
4522
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004523 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004524 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004525 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004526 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004527 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004528 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004529 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004530 InvokeType invoke_type,
4531 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004532 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304533 : HInvoke(kInvokeStaticOrDirect,
4534 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004535 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004536 // There is potentially one extra argument for the HCurrentMethod node, and
4537 // potentially one other if the clinit check is explicit, and potentially
4538 // one other if the method is a string factory.
4539 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004540 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004541 return_type,
4542 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004543 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004544 resolved_method,
4545 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004546 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004547 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004548 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4549 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004550
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004551 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004552
Vladimir Markodc151b22015-10-15 18:02:30 +01004553 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004554 bool had_current_method_input = HasCurrentMethodInput();
4555 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4556
4557 // Using the current method is the default and once we find a better
4558 // method load kind, we should not go back to using the current method.
4559 DCHECK(had_current_method_input || !needs_current_method_input);
4560
4561 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004562 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4563 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004564 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004565 dispatch_info_ = dispatch_info;
4566 }
4567
Aart Bik6daebeb2017-04-03 14:35:41 -07004568 DispatchInfo GetDispatchInfo() const {
4569 return dispatch_info_;
4570 }
4571
Vladimir Markoc53c0792015-11-19 15:48:33 +00004572 void AddSpecialInput(HInstruction* input) {
4573 // We allow only one special input.
4574 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4575 DCHECK(InputCount() == GetSpecialInputIndex() ||
4576 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4577 InsertInputAt(GetSpecialInputIndex(), input);
4578 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004579
Vladimir Marko372f10e2016-05-17 16:30:10 +01004580 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004581 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004582 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4583 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4584 DCHECK(!input_records.empty());
4585 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4586 HInstruction* last_input = input_records.back().GetInstruction();
4587 // Note: `last_input` may be null during arguments setup.
4588 if (last_input != nullptr) {
4589 // `last_input` is the last input of a static invoke marked as having
4590 // an explicit clinit check. It must either be:
4591 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4592 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4593 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4594 }
4595 }
4596 return input_records;
4597 }
4598
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004599 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004600 // We access the method via the dex cache so we can't do an implicit null check.
4601 // TODO: for intrinsics we can generate implicit null checks.
4602 return false;
4603 }
4604
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004605 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004606 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004607 }
4608
Vladimir Markoc53c0792015-11-19 15:48:33 +00004609 // Get the index of the special input, if any.
4610 //
David Brazdil6de19382016-01-08 17:37:10 +00004611 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4612 // method pointer; otherwise there may be one platform-specific special input,
4613 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004614 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004615 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004616
Vladimir Marko58155012015-08-19 12:49:41 +00004617 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4618 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4619 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004620 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004621 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004622 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004623 bool HasPcRelativeMethodLoadKind() const {
4624 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004625 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004626 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004627 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004628 bool HasCurrentMethodInput() const {
4629 // This function can be called only after the invoke has been fully initialized by the builder.
4630 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004631 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004632 return true;
4633 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004634 DCHECK(InputCount() == GetSpecialInputIndex() ||
4635 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004636 return false;
4637 }
4638 }
Vladimir Marko58155012015-08-19 12:49:41 +00004639
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004640 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004641 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004642 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004643 }
4644
4645 uint64_t GetMethodAddress() const {
4646 DCHECK(HasMethodAddress());
4647 return dispatch_info_.method_load_data;
4648 }
4649
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004650 const DexFile& GetDexFileForPcRelativeDexCache() const;
4651
Vladimir Markoa1de9182016-02-25 11:37:38 +00004652 ClinitCheckRequirement GetClinitCheckRequirement() const {
4653 return GetPackedField<ClinitCheckRequirementField>();
4654 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004655
Roland Levillain4c0eb422015-04-24 16:43:49 +01004656 // Is this instruction a call to a static method?
4657 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004658 return GetInvokeType() == kStatic;
4659 }
4660
4661 MethodReference GetTargetMethod() const {
4662 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004663 }
4664
Vladimir Markofbb184a2015-11-13 14:47:00 +00004665 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4666 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4667 // instruction; only relevant for static calls with explicit clinit check.
4668 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004669 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004670 size_t last_input_index = inputs_.size() - 1u;
4671 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004672 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004673 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004674 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004675 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004676 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004677 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004678 }
4679
4680 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004681 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004682 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004683 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004684 }
4685
4686 // Is this a call to a static method whose declaring class has an
4687 // implicit intialization check requirement?
4688 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004689 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004690 }
4691
Vladimir Markob554b5a2015-11-06 12:57:55 +00004692 // Does this method load kind need the current method as an input?
4693 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004694 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004695 }
4696
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004697 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004698
Artem Serovcced8ba2017-07-19 18:18:09 +01004699 protected:
4700 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4701
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004702 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004703 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004704 static constexpr size_t kFieldClinitCheckRequirementSize =
4705 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4706 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4707 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4708 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4709 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004710 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4711 kFieldClinitCheckRequirement,
4712 kFieldClinitCheckRequirementSize>;
4713
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004714 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004715 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004716 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004717};
Vladimir Markof64242a2015-12-01 14:58:23 +00004718std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004719std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004720
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004721class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004722 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004723 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004724 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004725 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004726 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004727 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004728 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004729 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304730 : HInvoke(kInvokeVirtual,
4731 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004732 number_of_arguments,
4733 0u,
4734 return_type,
4735 dex_pc,
4736 dex_method_index,
4737 resolved_method,
4738 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304739 vtable_index_(vtable_index) {
4740 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004741
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004742 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004743
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004744 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004745 switch (GetIntrinsic()) {
4746 case Intrinsics::kThreadCurrentThread:
4747 case Intrinsics::kStringBufferAppend:
4748 case Intrinsics::kStringBufferToString:
4749 case Intrinsics::kStringBuilderAppend:
4750 case Intrinsics::kStringBuilderToString:
4751 return false;
4752 default:
4753 return HInvoke::CanBeNull();
4754 }
4755 }
4756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004757 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004758 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004759 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004760 }
4761
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004762 uint32_t GetVTableIndex() const { return vtable_index_; }
4763
4764 DECLARE_INSTRUCTION(InvokeVirtual);
4765
Artem Serovcced8ba2017-07-19 18:18:09 +01004766 protected:
4767 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4768
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004769 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004770 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004771 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004772};
4773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004774class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004775 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004776 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004777 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004778 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004779 uint32_t dex_pc,
4780 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004781 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004782 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304783 : HInvoke(kInvokeInterface,
4784 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004785 number_of_arguments,
4786 0u,
4787 return_type,
4788 dex_pc,
4789 dex_method_index,
4790 resolved_method,
4791 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304792 imt_index_(imt_index) {
4793 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004794
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004795 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004796
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004797 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004798 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004799 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004800 }
4801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004802 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004803 // The assembly stub currently needs it.
4804 return true;
4805 }
4806
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004807 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004808
4809 DECLARE_INSTRUCTION(InvokeInterface);
4810
Artem Serovcced8ba2017-07-19 18:18:09 +01004811 protected:
4812 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4813
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004814 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004815 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004816 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004817};
4818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004819class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004820 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004821 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304822 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004823 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004824 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004825
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004826 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004828 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004829 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004830 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004831 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004834 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004835 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4836 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004837 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004838 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4839 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004840
Roland Levillain88cb1752014-10-20 16:36:47 +01004841 DECLARE_INSTRUCTION(Neg);
4842
Artem Serovcced8ba2017-07-19 18:18:09 +01004843 protected:
4844 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004845};
4846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004847class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004848 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004849 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304850 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004851 SetRawInputAt(0, cls);
4852 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004853 }
4854
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004855 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004856
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004857 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004858 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004859
Mingyao Yang0c365e62015-03-31 15:09:29 -07004860 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004861 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004862
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004863 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004864
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004865 HLoadClass* GetLoadClass() const {
4866 DCHECK(InputAt(0)->IsLoadClass());
4867 return InputAt(0)->AsLoadClass();
4868 }
4869
4870 HInstruction* GetLength() const {
4871 return InputAt(1);
4872 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004873
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004874 DECLARE_INSTRUCTION(NewArray);
4875
Artem Serovcced8ba2017-07-19 18:18:09 +01004876 protected:
4877 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004878};
4879
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004880class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004881 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004882 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004883 HInstruction* left,
4884 HInstruction* right,
4885 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304886 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4887 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004889 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004890
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004891 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004892
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004893 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004894 return GetBlock()->GetGraph()->GetIntConstant(
4895 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004896 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004897 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004898 return GetBlock()->GetGraph()->GetLongConstant(
4899 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004900 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004901 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004902 return GetBlock()->GetGraph()->GetFloatConstant(
4903 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4904 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004905 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004906 return GetBlock()->GetGraph()->GetDoubleConstant(
4907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4908 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004909
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004910 DECLARE_INSTRUCTION(Add);
4911
Artem Serovcced8ba2017-07-19 18:18:09 +01004912 protected:
4913 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004914};
4915
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004916class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004917 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004918 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004919 HInstruction* left,
4920 HInstruction* right,
4921 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304922 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4923 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004924
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004925 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004926
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004927 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004928 return GetBlock()->GetGraph()->GetIntConstant(
4929 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004930 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004931 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004932 return GetBlock()->GetGraph()->GetLongConstant(
4933 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004934 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004935 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004936 return GetBlock()->GetGraph()->GetFloatConstant(
4937 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4938 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004939 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004940 return GetBlock()->GetGraph()->GetDoubleConstant(
4941 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4942 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004943
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004944 DECLARE_INSTRUCTION(Sub);
4945
Artem Serovcced8ba2017-07-19 18:18:09 +01004946 protected:
4947 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004948};
4949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004950class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004951 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004952 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004953 HInstruction* left,
4954 HInstruction* right,
4955 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304956 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4957 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004958
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004959 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004960
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004961 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004962
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004963 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004964 return GetBlock()->GetGraph()->GetIntConstant(
4965 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004966 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004967 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004968 return GetBlock()->GetGraph()->GetLongConstant(
4969 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004970 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004971 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004972 return GetBlock()->GetGraph()->GetFloatConstant(
4973 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4974 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004975 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004976 return GetBlock()->GetGraph()->GetDoubleConstant(
4977 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4978 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004979
4980 DECLARE_INSTRUCTION(Mul);
4981
Artem Serovcced8ba2017-07-19 18:18:09 +01004982 protected:
4983 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004984};
4985
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004986class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004987 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004988 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004989 HInstruction* left,
4990 HInstruction* right,
4991 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304992 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4993 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004994
Roland Levillain9867bc72015-08-05 10:21:34 +01004995 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004996 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004997 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004998 // Our graph structure ensures we never have 0 for `y` during
4999 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005000 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005001 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005002 return (y == -1) ? -x : x / y;
5003 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005004
Roland Levillain31dd3d62016-02-16 12:21:02 +00005005 template <typename T>
5006 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005007 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005008 return x / y;
5009 }
5010
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005011 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005012 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005013 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005014 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005015 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005016 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005017 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5018 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005019 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005020 return GetBlock()->GetGraph()->GetFloatConstant(
5021 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5022 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005023 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005024 return GetBlock()->GetGraph()->GetDoubleConstant(
5025 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005026 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005027
5028 DECLARE_INSTRUCTION(Div);
5029
Artem Serovcced8ba2017-07-19 18:18:09 +01005030 protected:
5031 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005032};
5033
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005034class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005035 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005036 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005037 HInstruction* left,
5038 HInstruction* right,
5039 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305040 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5041 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005042
Roland Levillain9867bc72015-08-05 10:21:34 +01005043 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005044 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005045 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005046 // Our graph structure ensures we never have 0 for `y` during
5047 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005048 DCHECK_NE(y, 0);
5049 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5050 return (y == -1) ? 0 : x % y;
5051 }
5052
Roland Levillain31dd3d62016-02-16 12:21:02 +00005053 template <typename T>
5054 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005055 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005056 return std::fmod(x, y);
5057 }
5058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005059 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005060 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005061 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005062 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005063 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005064 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005065 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005066 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005067 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005068 return GetBlock()->GetGraph()->GetFloatConstant(
5069 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5070 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005071 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005072 return GetBlock()->GetGraph()->GetDoubleConstant(
5073 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5074 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005075
5076 DECLARE_INSTRUCTION(Rem);
5077
Artem Serovcced8ba2017-07-19 18:18:09 +01005078 protected:
5079 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005080};
5081
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005082class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005083 public:
5084 HMin(DataType::Type result_type,
5085 HInstruction* left,
5086 HInstruction* right,
5087 uint32_t dex_pc)
5088 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5089
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005090 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005091
5092 // Evaluation for integral values.
5093 template <typename T> static T ComputeIntegral(T x, T y) {
5094 return (x <= y) ? x : y;
5095 }
5096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005097 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005098 return GetBlock()->GetGraph()->GetIntConstant(
5099 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5100 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005101 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005102 return GetBlock()->GetGraph()->GetLongConstant(
5103 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5104 }
5105 // TODO: Evaluation for floating-point values.
5106 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005107 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005108 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005109 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005110
5111 DECLARE_INSTRUCTION(Min);
5112
5113 protected:
5114 DEFAULT_COPY_CONSTRUCTOR(Min);
5115};
5116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005117class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005118 public:
5119 HMax(DataType::Type result_type,
5120 HInstruction* left,
5121 HInstruction* right,
5122 uint32_t dex_pc)
5123 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005125 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005126
5127 // Evaluation for integral values.
5128 template <typename T> static T ComputeIntegral(T x, T y) {
5129 return (x >= y) ? x : y;
5130 }
5131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005132 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005133 return GetBlock()->GetGraph()->GetIntConstant(
5134 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5135 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005136 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005137 return GetBlock()->GetGraph()->GetLongConstant(
5138 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5139 }
5140 // TODO: Evaluation for floating-point values.
5141 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005142 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005143 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005144 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005145
5146 DECLARE_INSTRUCTION(Max);
5147
5148 protected:
5149 DEFAULT_COPY_CONSTRUCTOR(Max);
5150};
5151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005152class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005153 public:
5154 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5155 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5156
5157 // Evaluation for integral values.
5158 template <typename T> static T ComputeIntegral(T x) {
5159 return x < 0 ? -x : x;
5160 }
5161
5162 // Evaluation for floating-point values.
5163 // Note, as a "quality of implementation", rather than pure "spec compliance",
5164 // we require that Math.abs() clears the sign bit (but changes nothing else)
5165 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5166 // http://b/30758343
5167 template <typename T, typename S> static T ComputeFP(T x) {
5168 S bits = bit_cast<S, T>(x);
5169 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5170 }
5171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005172 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005173 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5174 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005175 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005176 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5177 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005178 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005179 return GetBlock()->GetGraph()->GetFloatConstant(
5180 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5181 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005182 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005183 return GetBlock()->GetGraph()->GetDoubleConstant(
5184 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5185 }
5186
5187 DECLARE_INSTRUCTION(Abs);
5188
5189 protected:
5190 DEFAULT_COPY_CONSTRUCTOR(Abs);
5191};
5192
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005193class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005194 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005195 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005196 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5197 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005198 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005199 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005200 SetRawInputAt(0, value);
5201 }
5202
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005203 bool IsClonable() const override { return true; }
5204 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005206 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005207 return true;
5208 }
5209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005210 bool NeedsEnvironment() const override { return true; }
5211 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005212
Calin Juravled0d48522014-11-04 16:40:20 +00005213 DECLARE_INSTRUCTION(DivZeroCheck);
5214
Artem Serovcced8ba2017-07-19 18:18:09 +01005215 protected:
5216 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005217};
5218
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005219class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005220 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005221 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005222 HInstruction* value,
5223 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005224 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305225 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005226 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5227 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005228 }
5229
Roland Levillain5b5b9312016-03-22 14:57:31 +00005230 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005231 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005232 return value << (distance & max_shift_distance);
5233 }
5234
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005235 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005236 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005237 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005238 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005239 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005240 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005241 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005242 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005243 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005244 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005245 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5246 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005247 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005248 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005249 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005250 LOG(FATAL) << DebugName() << " is not defined for float values";
5251 UNREACHABLE();
5252 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005253 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005254 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005255 LOG(FATAL) << DebugName() << " is not defined for double values";
5256 UNREACHABLE();
5257 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005258
5259 DECLARE_INSTRUCTION(Shl);
5260
Artem Serovcced8ba2017-07-19 18:18:09 +01005261 protected:
5262 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005263};
5264
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005265class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005266 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005267 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005268 HInstruction* value,
5269 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005270 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305271 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005272 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5273 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005274 }
5275
Roland Levillain5b5b9312016-03-22 14:57:31 +00005276 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005277 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005278 return value >> (distance & max_shift_distance);
5279 }
5280
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005281 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005282 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005283 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005284 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005285 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005286 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005287 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005288 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005289 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005290 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005291 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5292 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005293 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005294 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005295 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005296 LOG(FATAL) << DebugName() << " is not defined for float values";
5297 UNREACHABLE();
5298 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005299 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005300 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005301 LOG(FATAL) << DebugName() << " is not defined for double values";
5302 UNREACHABLE();
5303 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005304
5305 DECLARE_INSTRUCTION(Shr);
5306
Artem Serovcced8ba2017-07-19 18:18:09 +01005307 protected:
5308 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005309};
5310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005311class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005312 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005313 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005314 HInstruction* value,
5315 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005316 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305317 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005318 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5319 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005320 }
5321
Roland Levillain5b5b9312016-03-22 14:57:31 +00005322 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005323 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005324 typedef typename std::make_unsigned<T>::type V;
5325 V ux = static_cast<V>(value);
5326 return static_cast<T>(ux >> (distance & max_shift_distance));
5327 }
5328
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005329 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005330 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005331 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005332 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005333 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005334 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005335 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005336 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005337 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005338 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005339 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5340 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005341 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005342 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005343 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005344 LOG(FATAL) << DebugName() << " is not defined for float values";
5345 UNREACHABLE();
5346 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005347 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005348 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005349 LOG(FATAL) << DebugName() << " is not defined for double values";
5350 UNREACHABLE();
5351 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005352
5353 DECLARE_INSTRUCTION(UShr);
5354
Artem Serovcced8ba2017-07-19 18:18:09 +01005355 protected:
5356 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005357};
5358
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005359class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005360 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005361 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005362 HInstruction* left,
5363 HInstruction* right,
5364 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305365 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5366 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005367
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005368 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005369
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005370 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005371
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005372 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005373 return GetBlock()->GetGraph()->GetIntConstant(
5374 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005375 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005376 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005377 return GetBlock()->GetGraph()->GetLongConstant(
5378 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005379 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005380 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005381 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005382 LOG(FATAL) << DebugName() << " is not defined for float values";
5383 UNREACHABLE();
5384 }
5385 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005386 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005387 LOG(FATAL) << DebugName() << " is not defined for double values";
5388 UNREACHABLE();
5389 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005390
5391 DECLARE_INSTRUCTION(And);
5392
Artem Serovcced8ba2017-07-19 18:18:09 +01005393 protected:
5394 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005395};
5396
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005397class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005398 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005399 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005400 HInstruction* left,
5401 HInstruction* right,
5402 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305403 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5404 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005405
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005406 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005407
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005408 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005409
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005410 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005411 return GetBlock()->GetGraph()->GetIntConstant(
5412 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005413 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005414 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005415 return GetBlock()->GetGraph()->GetLongConstant(
5416 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005417 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005418 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005419 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005420 LOG(FATAL) << DebugName() << " is not defined for float values";
5421 UNREACHABLE();
5422 }
5423 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005424 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005425 LOG(FATAL) << DebugName() << " is not defined for double values";
5426 UNREACHABLE();
5427 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005428
5429 DECLARE_INSTRUCTION(Or);
5430
Artem Serovcced8ba2017-07-19 18:18:09 +01005431 protected:
5432 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005433};
5434
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005435class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005436 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005437 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005438 HInstruction* left,
5439 HInstruction* right,
5440 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305441 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5442 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005443
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005444 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005445
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005446 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005448 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005449 return GetBlock()->GetGraph()->GetIntConstant(
5450 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005451 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005452 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005453 return GetBlock()->GetGraph()->GetLongConstant(
5454 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005455 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005456 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005457 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005458 LOG(FATAL) << DebugName() << " is not defined for float values";
5459 UNREACHABLE();
5460 }
5461 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005463 LOG(FATAL) << DebugName() << " is not defined for double values";
5464 UNREACHABLE();
5465 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005466
5467 DECLARE_INSTRUCTION(Xor);
5468
Artem Serovcced8ba2017-07-19 18:18:09 +01005469 protected:
5470 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005471};
5472
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005473class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005474 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005475 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305476 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005477 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5478 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005479 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005480
Roland Levillain5b5b9312016-03-22 14:57:31 +00005481 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005482 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005483 typedef typename std::make_unsigned<T>::type V;
5484 V ux = static_cast<V>(value);
5485 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005486 return static_cast<T>(ux);
5487 } else {
5488 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005489 return static_cast<T>(ux >> (distance & max_shift_value)) |
5490 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005491 }
5492 }
5493
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005494 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005495 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005496 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005497 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005498 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005499 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005500 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005501 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005502 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005503 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005504 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5505 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005506 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005507 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005508 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005509 LOG(FATAL) << DebugName() << " is not defined for float values";
5510 UNREACHABLE();
5511 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005512 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005513 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005514 LOG(FATAL) << DebugName() << " is not defined for double values";
5515 UNREACHABLE();
5516 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005517
5518 DECLARE_INSTRUCTION(Ror);
5519
Artem Serovcced8ba2017-07-19 18:18:09 +01005520 protected:
5521 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005522};
5523
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005524// The value of a parameter in this method. Its location depends on
5525// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005526class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005527 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005528 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005529 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005530 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005531 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005532 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305533 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005534 dex_file_(dex_file),
5535 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005536 index_(index) {
5537 SetPackedFlag<kFlagIsThis>(is_this);
5538 SetPackedFlag<kFlagCanBeNull>(!is_this);
5539 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005540
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005541 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005542 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005543 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005544 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005545
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005546 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005547 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005548
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005549 DECLARE_INSTRUCTION(ParameterValue);
5550
Artem Serovcced8ba2017-07-19 18:18:09 +01005551 protected:
5552 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5553
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005554 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005555 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005556 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005557 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5558 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5559 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5560 "Too many packed fields.");
5561
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005562 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005563 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005564 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005565 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005566 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005567};
5568
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005569class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005570 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005571 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305572 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5573 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005574
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005575 bool CanBeMoved() const override { return true; }
5576 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005577 return true;
5578 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005579
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005580 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005582 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005583 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005584 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005585 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005586 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005587 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005588 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005589 LOG(FATAL) << DebugName() << " is not defined for float values";
5590 UNREACHABLE();
5591 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005592 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005593 LOG(FATAL) << DebugName() << " is not defined for double values";
5594 UNREACHABLE();
5595 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005596
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005597 DECLARE_INSTRUCTION(Not);
5598
Artem Serovcced8ba2017-07-19 18:18:09 +01005599 protected:
5600 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005601};
5602
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005603class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005604 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005605 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305606 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5607 }
David Brazdil66d126e2015-04-03 16:02:44 +01005608
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005609 bool CanBeMoved() const override { return true; }
5610 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005611 return true;
5612 }
5613
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005614 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005615 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005616 return !x;
5617 }
5618
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005619 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005620 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005621 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005622 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005623 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005624 UNREACHABLE();
5625 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005626 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005627 LOG(FATAL) << DebugName() << " is not defined for float values";
5628 UNREACHABLE();
5629 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005630 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005631 LOG(FATAL) << DebugName() << " is not defined for double values";
5632 UNREACHABLE();
5633 }
David Brazdil66d126e2015-04-03 16:02:44 +01005634
5635 DECLARE_INSTRUCTION(BooleanNot);
5636
Artem Serovcced8ba2017-07-19 18:18:09 +01005637 protected:
5638 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005639};
5640
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005641class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005642 public:
5643 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005644 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305645 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005646 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005647 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005648 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005649 }
5650
5651 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5653 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005655 bool IsClonable() const override { return true; }
5656 bool CanBeMoved() const override { return true; }
5657 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005658 return true;
5659 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005660
Mark Mendelle82549b2015-05-06 10:55:34 -04005661 // Try to statically evaluate the conversion and return a HConstant
5662 // containing the result. If the input cannot be converted, return nullptr.
5663 HConstant* TryStaticEvaluation() const;
5664
Roland Levillaindff1f282014-11-05 14:15:05 +00005665 DECLARE_INSTRUCTION(TypeConversion);
5666
Artem Serovcced8ba2017-07-19 18:18:09 +01005667 protected:
5668 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005669};
5670
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005671static constexpr uint32_t kNoRegNumber = -1;
5672
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005673class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005674 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005675 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005676 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5677 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005678 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005679 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005680 SetRawInputAt(0, value);
5681 }
5682
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005683 bool IsClonable() const override { return true; }
5684 bool CanBeMoved() const override { return true; }
5685 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005686 return true;
5687 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005688
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005689 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005690
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005691 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005692
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005693 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005694
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005695 DECLARE_INSTRUCTION(NullCheck);
5696
Artem Serovcced8ba2017-07-19 18:18:09 +01005697 protected:
5698 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005699};
5700
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005701// Embeds an ArtField and all the information required by the compiler. We cache
5702// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005703class FieldInfo : public ValueObject {
5704 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005705 FieldInfo(ArtField* field,
5706 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005707 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005708 bool is_volatile,
5709 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005710 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005711 const DexFile& dex_file)
5712 : field_(field),
5713 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005714 field_type_(field_type),
5715 is_volatile_(is_volatile),
5716 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005717 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005718 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005719
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005720 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005721 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005722 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005723 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005724 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005725 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005726 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005727
5728 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005729 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005730 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005731 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005732 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005733 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005734 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005735 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005736};
5737
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005738class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005739 public:
5740 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005741 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005742 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005743 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005744 bool is_volatile,
5745 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005746 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005747 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005748 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305749 : HExpression(kInstanceFieldGet,
5750 field_type,
5751 SideEffects::FieldReadOfType(field_type, is_volatile),
5752 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005753 field_info_(field,
5754 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005755 field_type,
5756 is_volatile,
5757 field_idx,
5758 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005759 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005760 SetRawInputAt(0, value);
5761 }
5762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005763 bool IsClonable() const override { return true; }
5764 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005765
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005766 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005767 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005768 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005769 }
5770
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005771 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005772 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005773 }
5774
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005775 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005776 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5777 }
5778
Calin Juravle52c48962014-12-16 17:02:57 +00005779 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005780 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005782 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005783
Vladimir Marko61b92282017-10-11 13:23:17 +01005784 void SetType(DataType::Type new_type) {
5785 DCHECK(DataType::IsIntegralType(GetType()));
5786 DCHECK(DataType::IsIntegralType(new_type));
5787 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5788 SetPackedField<TypeField>(new_type);
5789 }
5790
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005791 DECLARE_INSTRUCTION(InstanceFieldGet);
5792
Artem Serovcced8ba2017-07-19 18:18:09 +01005793 protected:
5794 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5795
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005796 private:
5797 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005798};
5799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005800class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005801 public:
5802 HInstanceFieldSet(HInstruction* object,
5803 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005804 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005805 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005806 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005807 bool is_volatile,
5808 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005809 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005810 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005811 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005812 : HExpression(kInstanceFieldSet,
5813 SideEffects::FieldWriteOfType(field_type, is_volatile),
5814 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005815 field_info_(field,
5816 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005817 field_type,
5818 is_volatile,
5819 field_idx,
5820 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005821 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005822 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005823 SetRawInputAt(0, object);
5824 SetRawInputAt(1, value);
5825 }
5826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005827 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005829 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005830 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005831 }
5832
Calin Juravle52c48962014-12-16 17:02:57 +00005833 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005834 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005835 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005836 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005837 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005838 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5839 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005840
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005841 DECLARE_INSTRUCTION(InstanceFieldSet);
5842
Artem Serovcced8ba2017-07-19 18:18:09 +01005843 protected:
5844 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5845
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005846 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005847 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5848 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5849 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5850 "Too many packed fields.");
5851
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005852 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005853};
5854
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005855class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005856 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005857 HArrayGet(HInstruction* array,
5858 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005859 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005860 uint32_t dex_pc)
5861 : HArrayGet(array,
5862 index,
5863 type,
5864 SideEffects::ArrayReadOfType(type),
5865 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305866 /* is_string_char_at */ false) {
5867 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005868
5869 HArrayGet(HInstruction* array,
5870 HInstruction* index,
5871 DataType::Type type,
5872 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005873 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005874 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305875 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005876 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005877 SetRawInputAt(0, array);
5878 SetRawInputAt(1, index);
5879 }
5880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005881 bool IsClonable() const override { return true; }
5882 bool CanBeMoved() const override { return true; }
5883 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005884 return true;
5885 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005886 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005887 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005888 // Currently, unless the array is the result of NewArray, the array access is always
5889 // preceded by some form of null NullCheck necessary for the bounds check, usually
5890 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5891 // dynamic BCE. There are cases when these could be removed to produce better code.
5892 // If we ever add optimizations to do so we should allow an implicit check here
5893 // (as long as the address falls in the first page).
5894 //
5895 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5896 // a = cond ? new int[1] : null;
5897 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005898 return false;
5899 }
5900
David Brazdil4833f5a2015-12-16 10:37:39 +00005901 bool IsEquivalentOf(HArrayGet* other) const {
5902 bool result = (GetDexPc() == other->GetDexPc());
5903 if (kIsDebugBuild && result) {
5904 DCHECK_EQ(GetBlock(), other->GetBlock());
5905 DCHECK_EQ(GetArray(), other->GetArray());
5906 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005907 if (DataType::IsIntOrLongType(GetType())) {
5908 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005909 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005910 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5911 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005912 }
5913 }
5914 return result;
5915 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005916
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005917 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5918
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005919 HInstruction* GetArray() const { return InputAt(0); }
5920 HInstruction* GetIndex() const { return InputAt(1); }
5921
Vladimir Marko61b92282017-10-11 13:23:17 +01005922 void SetType(DataType::Type new_type) {
5923 DCHECK(DataType::IsIntegralType(GetType()));
5924 DCHECK(DataType::IsIntegralType(new_type));
5925 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5926 SetPackedField<TypeField>(new_type);
5927 }
5928
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005929 DECLARE_INSTRUCTION(ArrayGet);
5930
Artem Serovcced8ba2017-07-19 18:18:09 +01005931 protected:
5932 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5933
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005934 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005935 // We treat a String as an array, creating the HArrayGet from String.charAt()
5936 // intrinsic in the instruction simplifier. We can always determine whether
5937 // a particular HArrayGet is actually a String.charAt() by looking at the type
5938 // of the input but that requires holding the mutator lock, so we prefer to use
5939 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005940 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005941 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5942 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5943 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005944};
5945
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005946class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005947 public:
5948 HArraySet(HInstruction* array,
5949 HInstruction* index,
5950 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005951 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005952 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005953 : HArraySet(array,
5954 index,
5955 value,
5956 expected_component_type,
5957 // Make a best guess for side effects now, may be refined during SSA building.
5958 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305959 dex_pc) {
5960 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005961
5962 HArraySet(HInstruction* array,
5963 HInstruction* index,
5964 HInstruction* value,
5965 DataType::Type expected_component_type,
5966 SideEffects side_effects,
5967 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005968 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005970 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005971 SetPackedFlag<kFlagValueCanBeNull>(true);
5972 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005973 SetRawInputAt(0, array);
5974 SetRawInputAt(1, index);
5975 SetRawInputAt(2, value);
5976 }
5977
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005978 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005979
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005980 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005981 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005982 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005983 }
5984
Mingyao Yang81014cb2015-06-02 03:16:27 -07005985 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005986 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005987
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005988 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005989 // TODO: Same as for ArrayGet.
5990 return false;
5991 }
5992
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005993 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005994 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005995 }
5996
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005997 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005998 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005999 }
6000
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006001 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006002 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006003 }
6004
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6006 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6007 bool StaticTypeOfArrayIsObjectArray() const {
6008 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6009 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006010
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006011 HInstruction* GetArray() const { return InputAt(0); }
6012 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006013 HInstruction* GetValue() const { return InputAt(2); }
6014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006015 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006016 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6017 }
6018
6019 static DataType::Type GetComponentType(DataType::Type value_type,
6020 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006021 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006022 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006023 // be correct, we also check what is the value type. If it is a floating
6024 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006025 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006026 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006027 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006028 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006030 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006031 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006032 }
6033
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006034 static SideEffects ComputeSideEffects(DataType::Type type) {
6035 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006036 }
6037
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006038 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6039 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6040 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006041 }
6042
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006043 DECLARE_INSTRUCTION(ArraySet);
6044
Artem Serovcced8ba2017-07-19 18:18:09 +01006045 protected:
6046 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6047
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006048 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6050 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006051 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006052 static constexpr size_t kFlagNeedsTypeCheck =
6053 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6054 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006055 // Cached information for the reference_type_info_ so that codegen
6056 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006057 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6058 static constexpr size_t kNumberOfArraySetPackedBits =
6059 kFlagStaticTypeOfArrayIsObjectArray + 1;
6060 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6061 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006062 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006063};
6064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006065class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006066 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006067 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306068 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006069 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006070 // Note that arrays do not change length, so the instruction does not
6071 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006072 SetRawInputAt(0, array);
6073 }
6074
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006075 bool IsClonable() const override { return true; }
6076 bool CanBeMoved() const override { return true; }
6077 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006078 return true;
6079 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006080 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006081 return obj == InputAt(0);
6082 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006083
Vladimir Markodce016e2016-04-28 13:10:02 +01006084 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6085
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006086 DECLARE_INSTRUCTION(ArrayLength);
6087
Artem Serovcced8ba2017-07-19 18:18:09 +01006088 protected:
6089 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6090
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006091 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006092 // We treat a String as an array, creating the HArrayLength from String.length()
6093 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6094 // determine whether a particular HArrayLength is actually a String.length() by
6095 // looking at the type of the input but that requires holding the mutator lock, so
6096 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006097 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006098 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6099 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6100 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006101};
6102
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006103class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006104 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006105 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006106 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6107 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006108 HBoundsCheck(HInstruction* index,
6109 HInstruction* length,
6110 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006111 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006112 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006113 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006114 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006115 SetRawInputAt(0, index);
6116 SetRawInputAt(1, length);
6117 }
6118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006119 bool IsClonable() const override { return true; }
6120 bool CanBeMoved() const override { return true; }
6121 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006122 return true;
6123 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006125 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006126
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006127 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006128
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006129 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006130
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006131 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006132
6133 DECLARE_INSTRUCTION(BoundsCheck);
6134
Artem Serovcced8ba2017-07-19 18:18:09 +01006135 protected:
6136 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6137
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006138 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006139 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006140};
6141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006142class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006143 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006144 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006145 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306146 slow_path_(nullptr) {
6147 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006149 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006150
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006151 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006152 return true;
6153 }
6154
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006155 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6156 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006157
6158 DECLARE_INSTRUCTION(SuspendCheck);
6159
Artem Serovcced8ba2017-07-19 18:18:09 +01006160 protected:
6161 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6162
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006163 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006164 // Only used for code generation, in order to share the same slow path between back edges
6165 // of a same loop.
6166 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006167};
6168
David Srbecky0cf44932015-12-09 14:09:59 +00006169// Pseudo-instruction which provides the native debugger with mapping information.
6170// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006171class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006172 public:
6173 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006174 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306175 }
David Srbecky0cf44932015-12-09 14:09:59 +00006176
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006177 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006178 return true;
6179 }
6180
6181 DECLARE_INSTRUCTION(NativeDebugInfo);
6182
Artem Serovcced8ba2017-07-19 18:18:09 +01006183 protected:
6184 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006185};
6186
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006187/**
6188 * Instruction to load a Class object.
6189 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006190class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006191 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006192 // Determines how to load the Class.
6193 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006194 // We cannot load this class. See HSharpening::SharpenLoadClass.
6195 kInvalid = -1,
6196
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006197 // Use the Class* from the method's own ArtMethod*.
6198 kReferrersClass,
6199
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006200 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006201 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006202 kBootImageLinkTimePcRelative,
6203
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006204 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006205 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006206 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006207
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006208 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006209 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006210 kBssEntry,
6211
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006212 // Use a known boot image Class* address, embedded in the code by the codegen.
6213 // Used for boot image classes referenced by apps in JIT-compiled code.
6214 kJitBootImageAddress,
6215
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006216 // Load from the root table associated with the JIT compiled method.
6217 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006218
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006219 // Load using a simple runtime call. This is the fall-back load kind when
6220 // the codegen is unable to use another appropriate kind.
6221 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006222
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006223 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006224 };
6225
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006226 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006227 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006228 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006229 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006230 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006231 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006232 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006233 : HInstruction(kLoadClass,
6234 DataType::Type::kReference,
6235 SideEffectsForArchRuntimeCalls(),
6236 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006237 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006238 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006239 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006240 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006241 // Referrers class should not need access check. We never inline unverified
6242 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006243 DCHECK(!is_referrers_class || !needs_access_check);
6244
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006245 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006246 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006247 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006248 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006249 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006250 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006251 }
6252
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006253 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006254
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006255 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006256
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006257 LoadKind GetLoadKind() const {
6258 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006259 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006260
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006261 bool HasPcRelativeLoadKind() const {
6262 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6263 GetLoadKind() == LoadKind::kBootImageRelRo ||
6264 GetLoadKind() == LoadKind::kBssEntry;
6265 }
6266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006267 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006268
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006269 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006271 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006272
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006273 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006275 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006276 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006277 }
6278
Calin Juravle0ba218d2015-05-19 18:46:01 +01006279 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006280 // The entrypoint the code generator is going to call does not do
6281 // clinit of the class.
6282 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006283 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006284 }
6285
6286 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006287 return NeedsAccessCheck() ||
6288 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006289 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006290 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006291 }
6292
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006293 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006294 return NeedsAccessCheck() ||
6295 MustGenerateClinitCheck() ||
6296 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006297 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006298 GetLoadKind() == LoadKind::kBssEntry) &&
6299 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006300 }
6301
Calin Juravleacf735c2015-02-12 15:25:22 +00006302 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006303 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6304 // Note: The is_exact flag from the return value should not be used.
6305 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6306 } else {
6307 return ReferenceTypeInfo::CreateInvalid();
6308 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006309 }
6310
Vladimir Marko175e7862018-03-27 09:03:13 +00006311 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6312 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6313 DCHECK(klass_ != nullptr);
6314 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006315 }
6316
Andreas Gampea5b09a62016-11-17 15:21:22 -08006317 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006318 const DexFile& GetDexFile() const { return dex_file_; }
6319
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006320 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006321 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006322 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006323
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006324 static SideEffects SideEffectsForArchRuntimeCalls() {
6325 return SideEffects::CanTriggerGC();
6326 }
6327
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006328 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006329 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006330 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006331 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006332
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006333 bool MustResolveTypeOnSlowPath() const {
6334 // Check that this instruction has a slow path.
6335 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6336 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6337 return GetLoadKind() == LoadKind::kBssEntry;
6338 }
6339
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006340 void MarkInBootImage() {
6341 SetPackedFlag<kFlagIsInBootImage>(true);
6342 }
6343
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006344 void AddSpecialInput(HInstruction* special_input);
6345
6346 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006347 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006348 return ArrayRef<HUserRecord<HInstruction*>>(
6349 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6350 }
6351
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006352 Handle<mirror::Class> GetClass() const {
6353 return klass_;
6354 }
6355
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006356 DECLARE_INSTRUCTION(LoadClass);
6357
Artem Serovcced8ba2017-07-19 18:18:09 +01006358 protected:
6359 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6360
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006361 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006362 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006363 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006364 // Whether this instruction must generate the initialization check.
6365 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006366 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006367 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6368 static constexpr size_t kFieldLoadKindSize =
6369 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006370 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6371 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006372 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006373 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6374
6375 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006376 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006377 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006378 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006379 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006380 }
6381
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006382 void SetLoadKindInternal(LoadKind load_kind);
6383
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006384 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006385 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006386 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006387 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006388
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006389 // A type index and dex file where the class can be accessed. The dex file can be:
6390 // - The compiling method's dex file if the class is defined there too.
6391 // - The compiling method's dex file if the class is referenced there.
6392 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006393 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006394 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006395 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006396
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006397 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006398};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006399std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6400
6401// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006402inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6403 // The load kind should be determined before inserting the instruction to the graph.
6404 DCHECK(GetBlock() == nullptr);
6405 DCHECK(GetEnvironment() == nullptr);
6406 SetPackedField<LoadKindField>(load_kind);
6407 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6408 special_input_ = HUserRecord<HInstruction*>(nullptr);
6409 }
6410 if (!NeedsEnvironment()) {
6411 SetSideEffects(SideEffects::None());
6412 }
6413}
6414
6415// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006416inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006417 // The special input is used for PC-relative loads on some architectures,
6418 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006419 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006420 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006421 GetLoadKind() == LoadKind::kBssEntry ||
6422 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006423 DCHECK(special_input_.GetInstruction() == nullptr);
6424 special_input_ = HUserRecord<HInstruction*>(special_input);
6425 special_input->AddUseAt(this, 0);
6426}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006427
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006428class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006429 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006430 // Determines how to load the String.
6431 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006432 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006433 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006434 kBootImageLinkTimePcRelative,
6435
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006436 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006437 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006438 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006439
Vladimir Markoaad75c62016-10-03 08:46:48 +00006440 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006441 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006442 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006443
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006444 // Use a known boot image String* address, embedded in the code by the codegen.
6445 // Used for boot image strings referenced by apps in JIT-compiled code.
6446 kJitBootImageAddress,
6447
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006448 // Load from the root table associated with the JIT compiled method.
6449 kJitTableAddress,
6450
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006451 // Load using a simple runtime call. This is the fall-back load kind when
6452 // the codegen is unable to use another appropriate kind.
6453 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006454
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006455 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006456 };
6457
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006458 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006459 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006460 const DexFile& dex_file,
6461 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006462 : HInstruction(kLoadString,
6463 DataType::Type::kReference,
6464 SideEffectsForArchRuntimeCalls(),
6465 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006466 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006467 string_index_(string_index),
6468 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006469 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006470 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006471
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006472 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006473
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006474 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006475
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006476 LoadKind GetLoadKind() const {
6477 return GetPackedField<LoadKindField>();
6478 }
6479
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006480 bool HasPcRelativeLoadKind() const {
6481 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6482 GetLoadKind() == LoadKind::kBootImageRelRo ||
6483 GetLoadKind() == LoadKind::kBssEntry;
6484 }
6485
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006486 const DexFile& GetDexFile() const {
6487 return dex_file_;
6488 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006489
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006490 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006491 return string_index_;
6492 }
6493
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006494 Handle<mirror::String> GetString() const {
6495 return string_;
6496 }
6497
6498 void SetString(Handle<mirror::String> str) {
6499 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006500 }
6501
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006502 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006503
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006504 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006506 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006507
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006508 // Will call the runtime if we need to load the string through
6509 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006510 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006511 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006512 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006513 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006514 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006515 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006516 return false;
6517 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006518 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006519 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006520
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006521 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006522 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006523 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006524
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006525 bool CanBeNull() const override { return false; }
6526 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006527
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006528 static SideEffects SideEffectsForArchRuntimeCalls() {
6529 return SideEffects::CanTriggerGC();
6530 }
6531
Vladimir Marko372f10e2016-05-17 16:30:10 +01006532 void AddSpecialInput(HInstruction* special_input);
6533
6534 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006535 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006536 return ArrayRef<HUserRecord<HInstruction*>>(
6537 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006538 }
6539
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006540 DECLARE_INSTRUCTION(LoadString);
6541
Artem Serovcced8ba2017-07-19 18:18:09 +01006542 protected:
6543 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6544
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006545 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006546 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006547 static constexpr size_t kFieldLoadKindSize =
6548 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6549 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006550 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006551 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006552
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006553 void SetLoadKindInternal(LoadKind load_kind);
6554
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006555 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006556 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006557 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006558 HUserRecord<HInstruction*> special_input_;
6559
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006560 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006561 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006562
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006563 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006564};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006565std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6566
6567// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006568inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6569 // The load kind should be determined before inserting the instruction to the graph.
6570 DCHECK(GetBlock() == nullptr);
6571 DCHECK(GetEnvironment() == nullptr);
6572 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6573 SetPackedField<LoadKindField>(load_kind);
6574 if (load_kind != LoadKind::kRuntimeCall) {
6575 special_input_ = HUserRecord<HInstruction*>(nullptr);
6576 }
6577 if (!NeedsEnvironment()) {
6578 SetSideEffects(SideEffects::None());
6579 }
6580}
6581
6582// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006583inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006584 // The special input is used for PC-relative loads on some architectures,
6585 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006586 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006587 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006588 GetLoadKind() == LoadKind::kBssEntry ||
6589 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006590 // HLoadString::GetInputRecords() returns an empty array at this point,
6591 // so use the GetInputRecords() from the base class to set the input record.
6592 DCHECK(special_input_.GetInstruction() == nullptr);
6593 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006594 special_input->AddUseAt(this, 0);
6595}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006596
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006597class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006598 public:
6599 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006600 uint16_t method_handle_idx,
6601 const DexFile& dex_file,
6602 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006603 : HInstruction(kLoadMethodHandle,
6604 DataType::Type::kReference,
6605 SideEffectsForArchRuntimeCalls(),
6606 dex_pc),
6607 special_input_(HUserRecord<HInstruction*>(current_method)),
6608 method_handle_idx_(method_handle_idx),
6609 dex_file_(dex_file) {
6610 }
6611
6612 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006613 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006614 return ArrayRef<HUserRecord<HInstruction*>>(
6615 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6616 }
6617
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006618 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006619
6620 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6621
6622 const DexFile& GetDexFile() const { return dex_file_; }
6623
6624 static SideEffects SideEffectsForArchRuntimeCalls() {
6625 return SideEffects::CanTriggerGC();
6626 }
6627
6628 DECLARE_INSTRUCTION(LoadMethodHandle);
6629
6630 protected:
6631 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6632
6633 private:
6634 // The special input is the HCurrentMethod for kRuntimeCall.
6635 HUserRecord<HInstruction*> special_input_;
6636
6637 const uint16_t method_handle_idx_;
6638 const DexFile& dex_file_;
6639};
6640
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006641class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006642 public:
6643 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006644 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006645 const DexFile& dex_file,
6646 uint32_t dex_pc)
6647 : HInstruction(kLoadMethodType,
6648 DataType::Type::kReference,
6649 SideEffectsForArchRuntimeCalls(),
6650 dex_pc),
6651 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006652 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006653 dex_file_(dex_file) {
6654 }
6655
6656 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006657 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006658 return ArrayRef<HUserRecord<HInstruction*>>(
6659 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6660 }
6661
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006662 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006663
Orion Hodson06d10a72018-05-14 08:53:38 +01006664 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006665
6666 const DexFile& GetDexFile() const { return dex_file_; }
6667
6668 static SideEffects SideEffectsForArchRuntimeCalls() {
6669 return SideEffects::CanTriggerGC();
6670 }
6671
6672 DECLARE_INSTRUCTION(LoadMethodType);
6673
6674 protected:
6675 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6676
6677 private:
6678 // The special input is the HCurrentMethod for kRuntimeCall.
6679 HUserRecord<HInstruction*> special_input_;
6680
Orion Hodson06d10a72018-05-14 08:53:38 +01006681 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006682 const DexFile& dex_file_;
6683};
6684
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006685/**
6686 * Performs an initialization check on its Class object input.
6687 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006688class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006689 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006690 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006691 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306692 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006693 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006694 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006695 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006696 SetRawInputAt(0, constant);
6697 }
Artem Serova6e26142018-06-19 14:55:17 +01006698 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006699 bool CanBeMoved() const override { return true; }
6700 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006701 return true;
6702 }
6703
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006704 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006705 // May call runtime to initialize the class.
6706 return true;
6707 }
6708
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006709 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006710
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006711 HLoadClass* GetLoadClass() const {
6712 DCHECK(InputAt(0)->IsLoadClass());
6713 return InputAt(0)->AsLoadClass();
6714 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006715
6716 DECLARE_INSTRUCTION(ClinitCheck);
6717
Artem Serovcced8ba2017-07-19 18:18:09 +01006718
6719 protected:
6720 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006721};
6722
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006723class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006724 public:
6725 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006726 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006728 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006729 bool is_volatile,
6730 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006731 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006732 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006733 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306734 : HExpression(kStaticFieldGet,
6735 field_type,
6736 SideEffects::FieldReadOfType(field_type, is_volatile),
6737 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006738 field_info_(field,
6739 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006740 field_type,
6741 is_volatile,
6742 field_idx,
6743 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006744 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006745 SetRawInputAt(0, cls);
6746 }
6747
Calin Juravle52c48962014-12-16 17:02:57 +00006748
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006749 bool IsClonable() const override { return true; }
6750 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006752 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006753 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006754 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006755 }
6756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006757 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006758 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6759 }
6760
Calin Juravle52c48962014-12-16 17:02:57 +00006761 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006762 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006763 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006764 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006765
Vladimir Marko61b92282017-10-11 13:23:17 +01006766 void SetType(DataType::Type new_type) {
6767 DCHECK(DataType::IsIntegralType(GetType()));
6768 DCHECK(DataType::IsIntegralType(new_type));
6769 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6770 SetPackedField<TypeField>(new_type);
6771 }
6772
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006773 DECLARE_INSTRUCTION(StaticFieldGet);
6774
Artem Serovcced8ba2017-07-19 18:18:09 +01006775 protected:
6776 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6777
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006778 private:
6779 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006780};
6781
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006782class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006783 public:
6784 HStaticFieldSet(HInstruction* cls,
6785 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006786 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006787 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006788 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006789 bool is_volatile,
6790 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006791 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006792 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006793 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006794 : HExpression(kStaticFieldSet,
6795 SideEffects::FieldWriteOfType(field_type, is_volatile),
6796 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006797 field_info_(field,
6798 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006799 field_type,
6800 is_volatile,
6801 field_idx,
6802 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006803 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006804 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006805 SetRawInputAt(0, cls);
6806 SetRawInputAt(1, value);
6807 }
6808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006809 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006810 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006811 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006812 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006813 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006814
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006815 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006816 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6817 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006818
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006819 DECLARE_INSTRUCTION(StaticFieldSet);
6820
Artem Serovcced8ba2017-07-19 18:18:09 +01006821 protected:
6822 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6823
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006824 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006825 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6826 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6827 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6828 "Too many packed fields.");
6829
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006830 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006831};
6832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006833class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006834 public:
6835 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006836 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006837 uint32_t field_index,
6838 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306839 : HExpression(kUnresolvedInstanceFieldGet,
6840 field_type,
6841 SideEffects::AllExceptGCDependency(),
6842 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006843 field_index_(field_index) {
6844 SetRawInputAt(0, obj);
6845 }
6846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006847 bool IsClonable() const override { return true; }
6848 bool NeedsEnvironment() const override { return true; }
6849 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006851 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006852 uint32_t GetFieldIndex() const { return field_index_; }
6853
6854 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6855
Artem Serovcced8ba2017-07-19 18:18:09 +01006856 protected:
6857 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6858
Calin Juravlee460d1d2015-09-29 04:52:17 +01006859 private:
6860 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006861};
6862
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006863class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006864 public:
6865 HUnresolvedInstanceFieldSet(HInstruction* obj,
6866 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006867 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006868 uint32_t field_index,
6869 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006870 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006871 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006872 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006873 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006874 SetRawInputAt(0, obj);
6875 SetRawInputAt(1, value);
6876 }
6877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006878 bool IsClonable() const override { return true; }
6879 bool NeedsEnvironment() const override { return true; }
6880 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006882 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006883 uint32_t GetFieldIndex() const { return field_index_; }
6884
6885 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6886
Artem Serovcced8ba2017-07-19 18:18:09 +01006887 protected:
6888 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6889
Calin Juravlee460d1d2015-09-29 04:52:17 +01006890 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006891 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6892 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006893 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006894 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6895 kFieldFieldType + kFieldFieldTypeSize;
6896 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6897 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006898 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006899
Calin Juravlee460d1d2015-09-29 04:52:17 +01006900 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006901};
6902
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006903class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006904 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006905 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006906 uint32_t field_index,
6907 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306908 : HExpression(kUnresolvedStaticFieldGet,
6909 field_type,
6910 SideEffects::AllExceptGCDependency(),
6911 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006912 field_index_(field_index) {
6913 }
6914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006915 bool IsClonable() const override { return true; }
6916 bool NeedsEnvironment() const override { return true; }
6917 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006919 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006920 uint32_t GetFieldIndex() const { return field_index_; }
6921
6922 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6923
Artem Serovcced8ba2017-07-19 18:18:09 +01006924 protected:
6925 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6926
Calin Juravlee460d1d2015-09-29 04:52:17 +01006927 private:
6928 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006929};
6930
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006931class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006932 public:
6933 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006934 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006935 uint32_t field_index,
6936 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006937 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006938 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006939 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006940 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006941 SetRawInputAt(0, value);
6942 }
6943
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006944 bool IsClonable() const override { return true; }
6945 bool NeedsEnvironment() const override { return true; }
6946 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006947
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006948 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006949 uint32_t GetFieldIndex() const { return field_index_; }
6950
6951 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6952
Artem Serovcced8ba2017-07-19 18:18:09 +01006953 protected:
6954 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6955
Calin Juravlee460d1d2015-09-29 04:52:17 +01006956 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006957 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6958 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006959 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006960 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6961 kFieldFieldType + kFieldFieldTypeSize;
6962 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6963 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006964 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006965
Calin Juravlee460d1d2015-09-29 04:52:17 +01006966 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006967};
6968
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006969// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006970class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006971 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006972 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306973 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6974 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006976 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01006977
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006978 DECLARE_INSTRUCTION(LoadException);
6979
Artem Serovcced8ba2017-07-19 18:18:09 +01006980 protected:
6981 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006982};
6983
David Brazdilcb1c0552015-08-04 16:22:25 +01006984// Implicit part of move-exception which clears thread-local exception storage.
6985// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006986class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006987 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006988 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006989 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306990 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006991
6992 DECLARE_INSTRUCTION(ClearException);
6993
Artem Serovcced8ba2017-07-19 18:18:09 +01006994 protected:
6995 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006996};
6997
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006998class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006999 public:
7000 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007001 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007002 SetRawInputAt(0, exception);
7003 }
7004
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007005 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007007 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007009 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007010
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007011 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007012
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007013 DECLARE_INSTRUCTION(Throw);
7014
Artem Serovcced8ba2017-07-19 18:18:09 +01007015 protected:
7016 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007017};
7018
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007019/**
7020 * Implementation strategies for the code generator of a HInstanceOf
7021 * or `HCheckCast`.
7022 */
7023enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007024 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007025 kExactCheck, // Can do a single class compare.
7026 kClassHierarchyCheck, // Can just walk the super class chain.
7027 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7028 kInterfaceCheck, // No optimization yet when checking against an interface.
7029 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007030 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007031 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007032 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007033};
7034
Roland Levillain86503782016-02-11 19:07:30 +00007035std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7036
Vladimir Marko175e7862018-03-27 09:03:13 +00007037// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7038// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7039class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007040 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007041 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007042 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007043 HInstruction* object,
7044 HInstruction* target_class_or_null,
7045 TypeCheckKind check_kind,
7046 Handle<mirror::Class> klass,
7047 uint32_t dex_pc,
7048 ArenaAllocator* allocator,
7049 HIntConstant* bitstring_path_to_root,
7050 HIntConstant* bitstring_mask,
7051 SideEffects side_effects)
7052 : HVariableInputSizeInstruction(
7053 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007054 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007055 side_effects,
7056 dex_pc,
7057 allocator,
7058 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
7059 kArenaAllocTypeCheckInputs),
7060 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007061 SetPackedField<TypeCheckKindField>(check_kind);
7062 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007063 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007064 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007065 SetRawInputAt(1, target_class_or_null);
7066 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7067 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7068 if (check_kind == TypeCheckKind::kBitstringCheck) {
7069 DCHECK(target_class_or_null->IsNullConstant());
7070 SetRawInputAt(2, bitstring_path_to_root);
7071 SetRawInputAt(3, bitstring_mask);
7072 } else {
7073 DCHECK(target_class_or_null->IsLoadClass());
7074 }
Vladimir Marko87584542017-12-12 17:47:52 +00007075 }
7076
7077 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007078 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007079 HInstruction* load_class = InputAt(1);
7080 DCHECK(load_class->IsLoadClass());
7081 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007082 }
7083
Vladimir Marko175e7862018-03-27 09:03:13 +00007084 uint32_t GetBitstringPathToRoot() const {
7085 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7086 HInstruction* path_to_root = InputAt(2);
7087 DCHECK(path_to_root->IsIntConstant());
7088 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7089 }
7090
7091 uint32_t GetBitstringMask() const {
7092 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7093 HInstruction* mask = InputAt(3);
7094 DCHECK(mask->IsIntConstant());
7095 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7096 }
7097
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007098 bool IsClonable() const override { return true; }
7099 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007100
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007101 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007102 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7103 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007104 }
7105
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007106 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7107 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7108 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7109 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7110
Vladimir Marko175e7862018-03-27 09:03:13 +00007111 ReferenceTypeInfo GetTargetClassRTI() {
7112 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7113 // Note: The is_exact flag from the return value should not be used.
7114 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7115 } else {
7116 return ReferenceTypeInfo::CreateInvalid();
7117 }
7118 }
7119
7120 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7121 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7122 DCHECK(klass_ != nullptr);
7123 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7124 }
7125
7126 Handle<mirror::Class> GetClass() const {
7127 return klass_;
7128 }
7129
7130 protected:
7131 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7132
7133 private:
7134 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7135 static constexpr size_t kFieldTypeCheckKindSize =
7136 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7137 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7138 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7139 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7140 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7141 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7142
7143 Handle<mirror::Class> klass_;
7144};
7145
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007146class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007147 public:
7148 HInstanceOf(HInstruction* object,
7149 HInstruction* target_class_or_null,
7150 TypeCheckKind check_kind,
7151 Handle<mirror::Class> klass,
7152 uint32_t dex_pc,
7153 ArenaAllocator* allocator,
7154 HIntConstant* bitstring_path_to_root,
7155 HIntConstant* bitstring_mask)
7156 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007157 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007158 object,
7159 target_class_or_null,
7160 check_kind,
7161 klass,
7162 dex_pc,
7163 allocator,
7164 bitstring_path_to_root,
7165 bitstring_mask,
7166 SideEffectsForArchRuntimeCalls(check_kind)) {}
7167
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007168 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007169
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007170 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007171 return CanCallRuntime(GetTypeCheckKind());
7172 }
7173
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007174 static bool CanCallRuntime(TypeCheckKind check_kind) {
7175 // Mips currently does runtime calls for any other checks.
7176 return check_kind != TypeCheckKind::kExactCheck;
7177 }
7178
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007179 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007180 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007181 }
7182
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007183 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007184
Artem Serovcced8ba2017-07-19 18:18:09 +01007185 protected:
7186 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007187};
7188
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007189class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007190 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007191 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307192 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007193 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7194 SetPackedFlag<kFlagUpperCanBeNull>(true);
7195 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007196 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007197 SetRawInputAt(0, input);
7198 }
7199
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007200 bool InstructionDataEquals(const HInstruction* other) const override;
7201 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007202
David Brazdilf5552582015-12-27 13:36:12 +00007203 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007204 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007205 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007206 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007207
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007208 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007209 DCHECK(GetUpperCanBeNull() || !can_be_null);
7210 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007211 }
7212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007213 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007214
Calin Juravleb1498f62015-02-16 13:13:29 +00007215 DECLARE_INSTRUCTION(BoundType);
7216
Artem Serovcced8ba2017-07-19 18:18:09 +01007217 protected:
7218 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7219
Calin Juravleb1498f62015-02-16 13:13:29 +00007220 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007221 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7222 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007223 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007224 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7225 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7226 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7227
Calin Juravleb1498f62015-02-16 13:13:29 +00007228 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007229 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7230 // It is used to bound the type in cases like:
7231 // if (x instanceof ClassX) {
7232 // // uper_bound_ will be ClassX
7233 // }
David Brazdilf5552582015-12-27 13:36:12 +00007234 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007235};
7236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007237class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007238 public:
7239 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007240 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007241 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007242 Handle<mirror::Class> klass,
7243 uint32_t dex_pc,
7244 ArenaAllocator* allocator,
7245 HIntConstant* bitstring_path_to_root,
7246 HIntConstant* bitstring_mask)
7247 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007248 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007249 object,
7250 target_class_or_null,
7251 check_kind,
7252 klass,
7253 dex_pc,
7254 allocator,
7255 bitstring_path_to_root,
7256 bitstring_mask,
7257 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007258
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007259 bool IsClonable() const override { return true; }
7260 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007261 // Instruction may throw a CheckCastError.
7262 return true;
7263 }
7264
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007265 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007266
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007267 DECLARE_INSTRUCTION(CheckCast);
7268
Artem Serovcced8ba2017-07-19 18:18:09 +01007269 protected:
7270 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007271};
7272
Andreas Gampe26de38b2016-07-27 17:53:11 -07007273/**
7274 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7275 * @details We define the combined barrier types that are actually required
7276 * by the Java Memory Model, rather than using exactly the terminology from
7277 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7278 * primitives. Note that the JSR-133 cookbook generally does not deal with
7279 * store atomicity issues, and the recipes there are not always entirely sufficient.
7280 * The current recipe is as follows:
7281 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7282 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7283 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7284 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7285 * class has final fields.
7286 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7287 * store-to-memory instructions. Only generated together with non-temporal stores.
7288 */
7289enum MemBarrierKind {
7290 kAnyStore,
7291 kLoadAny,
7292 kStoreStore,
7293 kAnyAny,
7294 kNTStoreStore,
7295 kLastBarrierKind = kNTStoreStore
7296};
7297std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007299class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007300 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007301 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007302 : HExpression(kMemoryBarrier,
7303 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7304 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007305 SetPackedField<BarrierKindField>(barrier_kind);
7306 }
Calin Juravle27df7582015-04-17 19:12:31 +01007307
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007308 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007309
Vladimir Markoa1de9182016-02-25 11:37:38 +00007310 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007311
7312 DECLARE_INSTRUCTION(MemoryBarrier);
7313
Artem Serovcced8ba2017-07-19 18:18:09 +01007314 protected:
7315 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7316
Calin Juravle27df7582015-04-17 19:12:31 +01007317 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007318 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7319 static constexpr size_t kFieldBarrierKindSize =
7320 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7321 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7322 kFieldBarrierKind + kFieldBarrierKindSize;
7323 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7324 "Too many packed fields.");
7325 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007326};
7327
Igor Murashkind01745e2017-04-05 16:40:31 -07007328// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7329// the specified object(s), with respect to any subsequent store that might "publish"
7330// (i.e. make visible) the specified object to another thread.
7331//
7332// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7333// for all final fields (that were set) at the end of the invoked constructor.
7334//
7335// The constructor fence models the freeze actions for the final fields of an object
7336// being constructed (semantically at the end of the constructor). Constructor fences
7337// have a per-object affinity; two separate objects being constructed get two separate
7338// constructor fences.
7339//
7340// (Note: that if calling a super-constructor or forwarding to another constructor,
7341// the freezes would happen at the end of *that* constructor being invoked).
7342//
7343// The memory model guarantees that when the object being constructed is "published" after
7344// constructor completion (i.e. escapes the current thread via a store), then any final field
7345// writes must be observable on other threads (once they observe that publication).
7346//
7347// Further, anything written before the freeze, and read by dereferencing through the final field,
7348// must also be visible (so final object field could itself have an object with non-final fields;
7349// yet the freeze must also extend to them).
7350//
7351// Constructor example:
7352//
7353// class HasFinal {
7354// final int field; Optimizing IR for <init>()V:
7355// HasFinal() {
7356// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7357// // freeze(this.field); HConstructorFence(this)
7358// } HReturn
7359// }
7360//
7361// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7362// already-initialized classes; in that case the allocation must act as a "default-initializer"
7363// of the object which effectively writes the class pointer "final field".
7364//
7365// For example, we can model default-initialiation as roughly the equivalent of the following:
7366//
7367// class Object {
7368// private final Class header;
7369// }
7370//
7371// Java code: Optimizing IR:
7372//
7373// T new_instance<T>() {
7374// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7375// obj.header = T.class; // header write is done by above call.
7376// // freeze(obj.header) HConstructorFence(obj)
7377// return (T)obj;
7378// }
7379//
7380// See also:
7381// * CompilerDriver::RequiresConstructorBarrier
7382// * QuasiAtomic::ThreadFenceForConstructor
7383//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007384class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007385 // A fence has variable inputs because the inputs can be removed
7386 // after prepare_for_register_allocation phase.
7387 // (TODO: In the future a fence could freeze multiple objects
7388 // after merging two fences together.)
7389 public:
7390 // `fence_object` is the reference that needs to be protected for correct publication.
7391 //
7392 // It makes sense in the following situations:
7393 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7394 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7395 //
7396 // After construction the `fence_object` becomes the 0th input.
7397 // This is not an input in a real sense, but just a convenient place to stash the information
7398 // about the associated object.
7399 HConstructorFence(HInstruction* fence_object,
7400 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007401 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007402 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7403 // constraints described in the class header. We claim that these SideEffects constraints
7404 // enforce a superset of the real constraints.
7405 //
7406 // The ordering described above is conservatively modeled with SideEffects as follows:
7407 //
7408 // * To prevent reordering of the publication stores:
7409 // ----> "Reads of objects" is the initial SideEffect.
7410 // * For every primitive final field store in the constructor:
7411 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7412 // * If there are any stores to reference final fields in the constructor:
7413 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7414 // that are reachable from `fence_object` also need to be prevented for reordering
7415 // (and we do not want to do alias analysis to figure out what those stores are).
7416 //
7417 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7418 // even more conservative approach than the one described above, and prevents all of the
7419 // above reordering without analyzing any of the instructions in the constructor.
7420 //
7421 // If in a later phase we discover that there are no writes to reference final fields,
7422 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307423 : HVariableInputSizeInstruction(kConstructorFence,
7424 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007425 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007426 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007427 /* number_of_inputs */ 1,
7428 kArenaAllocConstructorFenceInputs) {
7429 DCHECK(fence_object != nullptr);
7430 SetRawInputAt(0, fence_object);
7431 }
7432
7433 // The object associated with this constructor fence.
7434 //
7435 // (Note: This will be null after the prepare_for_register_allocation phase,
7436 // as all constructor fence inputs are removed there).
7437 HInstruction* GetFenceObject() const {
7438 return InputAt(0);
7439 }
7440
7441 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7442 // - Delete `fence_use` from `this`'s use list.
7443 // - Delete `this` from `fence_use`'s inputs list.
7444 // - If the `fence_use` is dead, remove it from the graph.
7445 //
7446 // A fence is considered dead once it no longer has any uses
7447 // and all of the inputs are dead.
7448 //
7449 // This must *not* be called during/after prepare_for_register_allocation,
7450 // because that removes all the inputs to the fences but the fence is actually
7451 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007452 //
7453 // Returns how many HConstructorFence instructions were removed from graph.
7454 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007455
Igor Murashkindd018df2017-08-09 10:38:31 -07007456 // Combine all inputs of `this` and `other` instruction and remove
7457 // `other` from the graph.
7458 //
7459 // Inputs are unique after the merge.
7460 //
7461 // Requirement: `this` must not be the same as `other.
7462 void Merge(HConstructorFence* other);
7463
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007464 // Check if this constructor fence is protecting
7465 // an HNewInstance or HNewArray that is also the immediate
7466 // predecessor of `this`.
7467 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007468 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7469 // to be one of the inputs of `this`.
7470 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007471 // Returns the associated HNewArray or HNewInstance,
7472 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007473 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007474
Igor Murashkind01745e2017-04-05 16:40:31 -07007475 DECLARE_INSTRUCTION(ConstructorFence);
7476
Artem Serovcced8ba2017-07-19 18:18:09 +01007477 protected:
7478 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007479};
7480
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007481class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007482 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007483 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007484 kEnter,
7485 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007486 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007487 };
7488
7489 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007490 : HExpression(kMonitorOperation,
7491 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7492 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007493 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007494 SetRawInputAt(0, object);
7495 }
7496
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007497 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007498 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007499
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007500 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007501 // Verifier guarantees that monitor-exit cannot throw.
7502 // This is important because it allows the HGraphBuilder to remove
7503 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7504 return IsEnter();
7505 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007506
Vladimir Markoa1de9182016-02-25 11:37:38 +00007507 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7508 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007509
7510 DECLARE_INSTRUCTION(MonitorOperation);
7511
Artem Serovcced8ba2017-07-19 18:18:09 +01007512 protected:
7513 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7514
Calin Juravle52c48962014-12-16 17:02:57 +00007515 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007516 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7517 static constexpr size_t kFieldOperationKindSize =
7518 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7519 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7520 kFieldOperationKind + kFieldOperationKindSize;
7521 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7522 "Too many packed fields.");
7523 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007524};
7525
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007526class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007527 public:
7528 HSelect(HInstruction* condition,
7529 HInstruction* true_value,
7530 HInstruction* false_value,
7531 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307532 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007533 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7534
7535 // First input must be `true_value` or `false_value` to allow codegens to
7536 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7537 // that architectures which implement HSelect as a conditional move also
7538 // will not need to invert the condition.
7539 SetRawInputAt(0, false_value);
7540 SetRawInputAt(1, true_value);
7541 SetRawInputAt(2, condition);
7542 }
7543
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007544 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007545 HInstruction* GetFalseValue() const { return InputAt(0); }
7546 HInstruction* GetTrueValue() const { return InputAt(1); }
7547 HInstruction* GetCondition() const { return InputAt(2); }
7548
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007549 bool CanBeMoved() const override { return true; }
7550 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007551 return true;
7552 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007554 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007555 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7556 }
7557
7558 DECLARE_INSTRUCTION(Select);
7559
Artem Serovcced8ba2017-07-19 18:18:09 +01007560 protected:
7561 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007562};
7563
Vladimir Markof9f64412015-09-02 14:05:49 +01007564class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007565 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007566 MoveOperands(Location source,
7567 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007568 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007569 HInstruction* instruction)
7570 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007571
7572 Location GetSource() const { return source_; }
7573 Location GetDestination() const { return destination_; }
7574
7575 void SetSource(Location value) { source_ = value; }
7576 void SetDestination(Location value) { destination_ = value; }
7577
7578 // The parallel move resolver marks moves as "in-progress" by clearing the
7579 // destination (but not the source).
7580 Location MarkPending() {
7581 DCHECK(!IsPending());
7582 Location dest = destination_;
7583 destination_ = Location::NoLocation();
7584 return dest;
7585 }
7586
7587 void ClearPending(Location dest) {
7588 DCHECK(IsPending());
7589 destination_ = dest;
7590 }
7591
7592 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007593 DCHECK(source_.IsValid() || destination_.IsInvalid());
7594 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007595 }
7596
7597 // True if this blocks a move from the given location.
7598 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007599 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007600 }
7601
7602 // A move is redundant if it's been eliminated, if its source and
7603 // destination are the same, or if its destination is unneeded.
7604 bool IsRedundant() const {
7605 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7606 }
7607
7608 // We clear both operands to indicate move that's been eliminated.
7609 void Eliminate() {
7610 source_ = destination_ = Location::NoLocation();
7611 }
7612
7613 bool IsEliminated() const {
7614 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7615 return source_.IsInvalid();
7616 }
7617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007618 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007619
Nicolas Geoffray90218252015-04-15 11:56:51 +01007620 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007621 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007622 }
7623
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007624 HInstruction* GetInstruction() const { return instruction_; }
7625
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007626 private:
7627 Location source_;
7628 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007629 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007630 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007631 // The instruction this move is assocatied with. Null when this move is
7632 // for moving an input in the expected locations of user (including a phi user).
7633 // This is only used in debug mode, to ensure we do not connect interval siblings
7634 // in the same parallel move.
7635 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007636};
7637
Roland Levillainc9285912015-12-18 10:38:42 +00007638std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7639
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007640static constexpr size_t kDefaultNumberOfMoves = 4;
7641
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007642class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007643 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007644 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007645 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007646 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007647 moves_.reserve(kDefaultNumberOfMoves);
7648 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007649
Nicolas Geoffray90218252015-04-15 11:56:51 +01007650 void AddMove(Location source,
7651 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007652 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007653 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007654 DCHECK(source.IsValid());
7655 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007656 if (kIsDebugBuild) {
7657 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007658 for (const MoveOperands& move : moves_) {
7659 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007660 // Special case the situation where the move is for the spill slot
7661 // of the instruction.
7662 if ((GetPrevious() == instruction)
7663 || ((GetPrevious() == nullptr)
7664 && instruction->IsPhi()
7665 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007666 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007667 << "Doing parallel moves for the same instruction.";
7668 } else {
7669 DCHECK(false) << "Doing parallel moves for the same instruction.";
7670 }
7671 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007672 }
7673 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007674 for (const MoveOperands& move : moves_) {
7675 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007676 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007677 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007678 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007679 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007680 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007681 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007682 }
7683
Vladimir Marko225b6462015-09-28 12:17:40 +01007684 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007685 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007686 }
7687
Vladimir Marko225b6462015-09-28 12:17:40 +01007688 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007689
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007690 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007691
Artem Serovcced8ba2017-07-19 18:18:09 +01007692 protected:
7693 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7694
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007695 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007696 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007697};
7698
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007699// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7700// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7701// never used across anything that can trigger GC.
7702// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7703// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007704class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007705 public:
7706 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307707 : HExpression(kIntermediateAddress,
7708 DataType::Type::kInt32,
7709 SideEffects::DependsOnGC(),
7710 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007711 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7712 DataType::Size(DataType::Type::kReference))
7713 << "kPrimInt and kPrimNot have different sizes.";
7714 SetRawInputAt(0, base_address);
7715 SetRawInputAt(1, offset);
7716 }
7717
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007718 bool IsClonable() const override { return true; }
7719 bool CanBeMoved() const override { return true; }
7720 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007721 return true;
7722 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007723 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007724
7725 HInstruction* GetBaseAddress() const { return InputAt(0); }
7726 HInstruction* GetOffset() const { return InputAt(1); }
7727
7728 DECLARE_INSTRUCTION(IntermediateAddress);
7729
Artem Serovcced8ba2017-07-19 18:18:09 +01007730 protected:
7731 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007732};
7733
7734
Mark Mendell0616ae02015-04-17 12:49:27 -04007735} // namespace art
7736
Aart Bikf8f5a162017-02-06 15:35:29 -08007737#include "nodes_vector.h"
7738
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007739#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7740#include "nodes_shared.h"
7741#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007742#ifdef ART_ENABLE_CODEGEN_mips
7743#include "nodes_mips.h"
7744#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007745#ifdef ART_ENABLE_CODEGEN_x86
7746#include "nodes_x86.h"
7747#endif
7748
7749namespace art {
7750
Igor Murashkin6ef45672017-08-08 13:59:55 -07007751class OptimizingCompilerStats;
7752
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007753class HGraphVisitor : public ValueObject {
7754 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007755 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7756 : stats_(stats),
7757 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007758 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007759
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007760 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007761 virtual void VisitBasicBlock(HBasicBlock* block);
7762
Roland Levillain633021e2014-10-01 14:12:25 +01007763 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007764 void VisitInsertionOrder();
7765
Roland Levillain633021e2014-10-01 14:12:25 +01007766 // Visit the graph following dominator tree reverse post-order.
7767 void VisitReversePostOrder();
7768
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007769 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007770
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007771 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007772#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007773 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7774
7775 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7776
7777#undef DECLARE_VISIT_INSTRUCTION
7778
Igor Murashkin6ef45672017-08-08 13:59:55 -07007779 protected:
7780 OptimizingCompilerStats* stats_;
7781
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007782 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007783 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007784
7785 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7786};
7787
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007788class HGraphDelegateVisitor : public HGraphVisitor {
7789 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007790 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7791 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007792 virtual ~HGraphDelegateVisitor() {}
7793
7794 // Visit functions that delegate to to super class.
7795#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007796 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007797
7798 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7799
7800#undef DECLARE_VISIT_INSTRUCTION
7801
7802 private:
7803 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7804};
7805
Artem Serovcced8ba2017-07-19 18:18:09 +01007806// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7807// and remove the old instruction.
7808HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7809
7810// Create a clone for each clonable instructions/phis and replace the original with the clone.
7811//
7812// Used for testing individual instruction cloner.
7813class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7814 public:
7815 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007816 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007817
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007818 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007819 if (instruction->IsClonable()) {
7820 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007821 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007822 }
7823 }
7824
Artem Serov7f4aff62017-06-21 17:02:18 +01007825 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007826
7827 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007828 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007829
7830 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7831};
7832
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007833// Iterator over the blocks that art part of the loop. Includes blocks part
7834// of an inner loop. The order in which the blocks are iterated is on their
7835// block id.
7836class HBlocksInLoopIterator : public ValueObject {
7837 public:
7838 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7839 : blocks_in_loop_(info.GetBlocks()),
7840 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7841 index_(0) {
7842 if (!blocks_in_loop_.IsBitSet(index_)) {
7843 Advance();
7844 }
7845 }
7846
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007847 bool Done() const { return index_ == blocks_.size(); }
7848 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007849 void Advance() {
7850 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007851 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007852 if (blocks_in_loop_.IsBitSet(index_)) {
7853 break;
7854 }
7855 }
7856 }
7857
7858 private:
7859 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007860 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007861 size_t index_;
7862
7863 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7864};
7865
Mingyao Yang3584bce2015-05-19 16:01:59 -07007866// Iterator over the blocks that art part of the loop. Includes blocks part
7867// of an inner loop. The order in which the blocks are iterated is reverse
7868// post order.
7869class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7870 public:
7871 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7872 : blocks_in_loop_(info.GetBlocks()),
7873 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7874 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007875 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007876 Advance();
7877 }
7878 }
7879
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007880 bool Done() const { return index_ == blocks_.size(); }
7881 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007882 void Advance() {
7883 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007884 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7885 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007886 break;
7887 }
7888 }
7889 }
7890
7891 private:
7892 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007893 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007894 size_t index_;
7895
7896 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7897};
7898
Aart Bikf3e61ee2017-04-12 17:09:20 -07007899// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007900inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007901 if (constant->IsIntConstant()) {
7902 return constant->AsIntConstant()->GetValue();
7903 } else if (constant->IsLongConstant()) {
7904 return constant->AsLongConstant()->GetValue();
7905 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007906 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007907 return 0;
7908 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007909}
7910
Aart Bikf3e61ee2017-04-12 17:09:20 -07007911// Returns true iff instruction is an integral constant (and sets value on success).
7912inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7913 if (instruction->IsIntConstant()) {
7914 *value = instruction->AsIntConstant()->GetValue();
7915 return true;
7916 } else if (instruction->IsLongConstant()) {
7917 *value = instruction->AsLongConstant()->GetValue();
7918 return true;
7919 } else if (instruction->IsNullConstant()) {
7920 *value = 0;
7921 return true;
7922 }
7923 return false;
7924}
7925
Aart Bik0148de42017-09-05 09:25:01 -07007926// Returns true iff instruction is the given integral constant.
7927inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7928 int64_t val = 0;
7929 return IsInt64AndGet(instruction, &val) && val == value;
7930}
7931
7932// Returns true iff instruction is a zero bit pattern.
7933inline bool IsZeroBitPattern(HInstruction* instruction) {
7934 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7935}
7936
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007937// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007938#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007939 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7940 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7941#undef INSTRUCTION_TYPE_CHECK
7942
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007943// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007944#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7945 std::is_base_of<BaseType, H##type>::value,
7946#define INSTRUCTION_TYPE_CHECK(type, super) \
7947 inline bool HInstruction::Is##type() const { \
7948 DCHECK_LT(GetKind(), kLastInstructionKind); \
7949 using BaseType = H##type; \
7950 static constexpr bool results[] = { \
7951 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7952 }; \
7953 return results[static_cast<size_t>(GetKind())]; \
7954 }
7955
7956 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7957#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007958#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007959
7960#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007961 inline const H##type* HInstruction::As##type() const { \
7962 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7963 } \
7964 inline H##type* HInstruction::As##type() { \
7965 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7966 }
7967
Vladimir Markoa90dd512018-05-04 15:04:45 +01007968 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7969#undef INSTRUCTION_TYPE_CAST
7970
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007971
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007972// Create space in `blocks` for adding `number_of_new_blocks` entries
7973// starting at location `at`. Blocks after `at` are moved accordingly.
7974inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7975 size_t number_of_new_blocks,
7976 size_t after) {
7977 DCHECK_LT(after, blocks->size());
7978 size_t old_size = blocks->size();
7979 size_t new_size = old_size + number_of_new_blocks;
7980 blocks->resize(new_size);
7981 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7982}
7983
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007984/*
7985 * Hunt "under the hood" of array lengths (leading to array references),
7986 * null checks (also leading to array references), and new arrays
7987 * (leading to the actual length). This makes it more likely related
7988 * instructions become actually comparable.
7989 */
7990inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7991 while (instruction->IsArrayLength() ||
7992 instruction->IsNullCheck() ||
7993 instruction->IsNewArray()) {
7994 instruction = instruction->IsNewArray()
7995 ? instruction->AsNewArray()->GetLength()
7996 : instruction->InputAt(0);
7997 }
7998 return instruction;
7999}
8000
Artem Serov21c7e6f2017-07-27 16:04:42 +01008001void RemoveEnvironmentUses(HInstruction* instruction);
8002bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8003void ResetEnvironmentInputRecords(HInstruction* instruction);
8004
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008005} // namespace art
8006
8007#endif // ART_COMPILER_OPTIMIZING_NODES_H_