blob: 16a741730144131579b45dc72087f960ce3fa9a5 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000030#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010031#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010032#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file.h"
35#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080036#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070037#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000038#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000039#include "handle.h"
40#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010041#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010042#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000043#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010044#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010045#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010046#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047
48namespace art {
49
Vladimir Markoca6fff82017-10-03 14:49:14 +010050class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000051class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070053class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010054class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000058class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000061class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000062class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000064class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070065class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010067class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010068class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010069class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000070class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010071class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000072class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Mathieu Chartier736b5602015-09-02 14:54:11 -070074namespace mirror {
75class DexCache;
76} // namespace mirror
77
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078static const int kDefaultNumberOfBlocks = 8;
79static const int kDefaultNumberOfSuccessors = 2;
80static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010081static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010082static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084
Roland Levillain5b5b9312016-03-22 14:57:31 +000085// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
86static constexpr int32_t kMaxIntShiftDistance = 0x1f;
87// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
88static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000089
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070091static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010092
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010093static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
94
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060095static constexpr uint32_t kNoDexPc = -1;
96
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010097inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
98 // For the purposes of the compiler, the dex files must actually be the same object
99 // if we want to safely treat them as the same. This is especially important for JIT
100 // as custom class loaders can open the same underlying file (or memory) multiple
101 // times and provide different class resolution but no two class loaders should ever
102 // use the same DexFile object - doing so is an unsupported hack that can lead to
103 // all sorts of weird failures.
104 return &lhs == &rhs;
105}
106
Dave Allison20dfc792014-06-16 20:44:29 -0700107enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700108 // All types.
109 kCondEQ, // ==
110 kCondNE, // !=
111 // Signed integers and floating-point numbers.
112 kCondLT, // <
113 kCondLE, // <=
114 kCondGT, // >
115 kCondGE, // >=
116 // Unsigned integers.
117 kCondB, // <
118 kCondBE, // <=
119 kCondA, // >
120 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100121 // First and last aliases.
122 kCondFirst = kCondEQ,
123 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700124};
125
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000127 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000128 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000129 kAnalysisFailThrowCatchLoop,
130 kAnalysisFailAmbiguousArrayOp,
131 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000132};
133
Andreas Gampe9186ced2016-12-12 14:28:21 -0800134template <typename T>
135static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
136 return static_cast<typename std::make_unsigned<T>::type>(x);
137}
138
Vladimir Markof9f64412015-09-02 14:05:49 +0100139class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 public:
141 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
142
143 void AddInstruction(HInstruction* instruction);
144 void RemoveInstruction(HInstruction* instruction);
145
David Brazdilc3d743f2015-04-22 13:40:50 +0100146 // Insert `instruction` before/after an existing instruction `cursor`.
147 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
148 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
149
Roland Levillain6b469232014-09-25 10:10:38 +0100150 // Return true if this list contains `instruction`.
151 bool Contains(HInstruction* instruction) const;
152
Roland Levillainccc07a92014-09-16 14:48:16 +0100153 // Return true if `instruction1` is found before `instruction2` in
154 // this instruction list and false otherwise. Abort if none
155 // of these instructions is found.
156 bool FoundBefore(const HInstruction* instruction1,
157 const HInstruction* instruction2) const;
158
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000159 bool IsEmpty() const { return first_instruction_ == nullptr; }
160 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
161
162 // Update the block of all instructions to be `block`.
163 void SetBlockOfInstructions(HBasicBlock* block) const;
164
165 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000166 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000167 void Add(const HInstructionList& instruction_list);
168
David Brazdil2d7352b2015-04-20 14:52:42 +0100169 // Return the number of instructions in the list. This is an expensive operation.
170 size_t CountSize() const;
171
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100172 private:
173 HInstruction* first_instruction_;
174 HInstruction* last_instruction_;
175
176 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 friend class HGraph;
178 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000180 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100181 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182
183 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
184};
185
David Brazdil4833f5a2015-12-16 10:37:39 +0000186class ReferenceTypeInfo : ValueObject {
187 public:
188 typedef Handle<mirror::Class> TypeHandle;
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
191
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100193 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
194 }
195
Vladimir Markoa1de9182016-02-25 11:37:38 +0000196 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return ReferenceTypeInfo(type_handle, is_exact);
198 }
199
200 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
201
Vladimir Markof39745e2016-01-26 12:16:55 +0000202 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000203 return handle.GetReference() != nullptr;
204 }
205
Vladimir Marko456307a2016-04-19 14:12:13 +0000206 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 return IsValidHandle(type_handle_);
208 }
209
210 bool IsExact() const { return is_exact_; }
211
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000213 DCHECK(IsValid());
214 return GetTypeHandle()->IsObjectClass();
215 }
216
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000218 DCHECK(IsValid());
219 return GetTypeHandle()->IsStringClass();
220 }
221
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000223 DCHECK(IsValid());
224 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
225 }
226
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000228 DCHECK(IsValid());
229 return GetTypeHandle()->IsInterface();
230 }
231
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000233 DCHECK(IsValid());
234 return GetTypeHandle()->IsArrayClass();
235 }
236
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000238 DCHECK(IsValid());
239 return GetTypeHandle()->IsPrimitiveArray();
240 }
241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 DCHECK(IsValid());
244 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
245 }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 if (!IsExact()) return false;
250 if (!IsArrayClass()) return false;
251 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
252 }
253
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000255 DCHECK(IsValid());
256 if (!IsExact()) return false;
257 if (!IsArrayClass()) return false;
258 if (!rti.IsArrayClass()) return false;
259 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
260 rti.GetTypeHandle()->GetComponentType());
261 }
262
263 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
269 }
270
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700271 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000272 DCHECK(IsValid());
273 DCHECK(rti.IsValid());
274 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
275 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
276 }
277
278 // Returns true if the type information provide the same amount of details.
279 // Note that it does not mean that the instructions have the same actual type
280 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700281 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000282 if (!IsValid() && !rti.IsValid()) {
283 // Invalid types are equal.
284 return true;
285 }
286 if (!IsValid() || !rti.IsValid()) {
287 // One is valid, the other not.
288 return false;
289 }
290 return IsExact() == rti.IsExact()
291 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
292 }
293
294 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000295 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
296 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
297 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000298
299 // The class of the object.
300 TypeHandle type_handle_;
301 // Whether or not the type is exact or a superclass of the actual type.
302 // Whether or not we have any information about this type.
303 bool is_exact_;
304};
305
306std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
307
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100309class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100311 HGraph(ArenaAllocator* allocator,
312 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 const DexFile& dex_file,
314 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100316 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000318 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100320 : allocator_(allocator),
321 arena_stack_(arena_stack),
322 blocks_(allocator->Adapter(kArenaAllocBlockList)),
323 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
324 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 entry_block_(nullptr),
326 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100327 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100328 number_of_vregs_(0),
329 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000330 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400331 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000332 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700333 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800334 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000335 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000336 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000337 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100338 dex_file_(dex_file),
339 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100340 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800342 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000344 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100345 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
347 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
348 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000349 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100350 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000351 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700352 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100353 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100354 blocks_.reserve(kDefaultNumberOfBlocks);
355 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
David Brazdilbadd8262016-02-02 16:28:56 +0000357 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700358 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000359
Vladimir Markoca6fff82017-10-03 14:49:14 +0100360 ArenaAllocator* GetAllocator() const { return allocator_; }
361 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100362 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
363
David Brazdil69ba7b72015-06-23 18:27:30 +0100364 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000365 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100366
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000367 HBasicBlock* GetEntryBlock() const { return entry_block_; }
368 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100369 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000370
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
372 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000373
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000374 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100376 void ComputeDominanceInformation();
377 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000378 void ClearLoopInformation();
379 void FindBackEdges(ArenaBitVector* visited);
380 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100381 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100382 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // Analyze all natural loops in this graph. Returns a code specifying that it
385 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000386 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000387 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388
David Brazdilffee3d32015-07-06 11:48:53 +0100389 // Iterate over blocks to compute try block membership. Needs reverse post
390 // order and loop information.
391 void ComputeTryBlockInformation();
392
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000393 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000394 // Returns the instruction to replace the invoke expression or null if the
395 // invoke is for a void method. Note that the caller is responsible for replacing
396 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000397 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000398
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000399 // Update the loop and try membership of `block`, which was spawned from `reference`.
400 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
401 // should be the new back edge.
402 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
403 HBasicBlock* reference,
404 bool replace_if_back_edge);
405
Mingyao Yang3584bce2015-05-19 16:01:59 -0700406 // Need to add a couple of blocks to test if the loop body is entered and
407 // put deoptimization instructions, etc.
408 void TransformLoopHeaderForBCE(HBasicBlock* header);
409
Aart Bikf8f5a162017-02-06 15:35:29 -0800410 // Adds a new loop directly after the loop with the given header and exit.
411 // Returns the new preheader.
412 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
413 HBasicBlock* body,
414 HBasicBlock* exit);
415
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000416 // Removes `block` from the graph. Assumes `block` has been disconnected from
417 // other blocks and has no instructions or phis.
418 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000419
David Brazdilfc6a86a2015-06-26 10:33:45 +0000420 // Splits the edge between `block` and `successor` while preserving the
421 // indices in the predecessor/successor lists. If there are multiple edges
422 // between the blocks, the lowest indices are used.
423 // Returns the new block which is empty and has the same dex pc as `successor`.
424 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
425
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100427 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000428
429 // Transform a loop into a format with a single preheader.
430 //
431 // Each phi in the header should be split: original one in the header should only hold
432 // inputs reachable from the back edges and a single input from the preheader. The newly created
433 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
434 //
435 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
436 // that no longer have this property.
437 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
438
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100439 void SimplifyLoop(HBasicBlock* header);
440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000441 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100442 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000443 return current_instruction_id_++;
444 }
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t GetCurrentInstructionId() const {
447 return current_instruction_id_;
448 }
449
450 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100451 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000452 current_instruction_id_ = id;
453 }
454
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100455 uint16_t GetMaximumNumberOfOutVRegs() const {
456 return maximum_number_of_out_vregs_;
457 }
458
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
460 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100461 }
462
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100463 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
464 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
465 }
466
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000467 void UpdateTemporariesVRegSlots(size_t slots) {
468 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100469 }
470
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100472 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000473 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 }
475
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100476 void SetNumberOfVRegs(uint16_t number_of_vregs) {
477 number_of_vregs_ = number_of_vregs;
478 }
479
480 uint16_t GetNumberOfVRegs() const {
481 return number_of_vregs_;
482 }
483
484 void SetNumberOfInVRegs(uint16_t value) {
485 number_of_in_vregs_ = value;
486 }
487
David Brazdildee58d62016-04-07 09:54:26 +0000488 uint16_t GetNumberOfInVRegs() const {
489 return number_of_in_vregs_;
490 }
491
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100492 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100493 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100494 return number_of_vregs_ - number_of_in_vregs_;
495 }
496
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100497 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100499 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100500
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100501 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
502 DCHECK(GetReversePostOrder()[0] == entry_block_);
503 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
504 }
505
506 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
507 return ReverseRange(GetReversePostOrder());
508 }
509
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100511 return linear_order_;
512 }
513
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100514 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
515 return ReverseRange(GetLinearOrder());
516 }
517
Mark Mendell1152c922015-04-24 17:06:35 -0400518 bool HasBoundsChecks() const {
519 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800520 }
521
Mark Mendell1152c922015-04-24 17:06:35 -0400522 void SetHasBoundsChecks(bool value) {
523 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800524 }
525
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000526 bool IsDebuggable() const { return debuggable_; }
527
David Brazdil8d5b8b22015-03-24 10:51:52 +0000528 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 // already, it is created and inserted into the graph. This method is only for
530 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100531 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000532
533 // TODO: This is problematic for the consistency of reference type propagation
534 // because it can be created anytime after the pass and thus it will be left
535 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600536 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000537
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600538 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
539 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000540 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600541 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
542 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000543 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600544 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
545 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000546 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600547 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
548 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000549 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000550
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100551 HCurrentMethod* GetCurrentMethod();
552
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100553 const DexFile& GetDexFile() const {
554 return dex_file_;
555 }
556
557 uint32_t GetMethodIdx() const {
558 return method_idx_;
559 }
560
Igor Murashkind01745e2017-04-05 16:40:31 -0700561 // Get the method name (without the signature), e.g. "<init>"
562 const char* GetMethodName() const;
563
564 // Get the pretty method name (class + name + optionally signature).
565 std::string PrettyMethod(bool with_signature = true) const;
566
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100567 InvokeType GetInvokeType() const {
568 return invoke_type_;
569 }
570
Mark Mendellc4701932015-04-10 13:18:51 -0400571 InstructionSet GetInstructionSet() const {
572 return instruction_set_;
573 }
574
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000575 bool IsCompilingOsr() const { return osr_; }
576
Mingyao Yang063fc772016-08-02 11:02:54 -0700577 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
578 return cha_single_implementation_list_;
579 }
580
581 void AddCHASingleImplementationDependency(ArtMethod* method) {
582 cha_single_implementation_list_.insert(method);
583 }
584
585 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800586 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700587 }
588
David Brazdil77a48ae2015-09-15 12:34:04 +0000589 bool HasTryCatch() const { return has_try_catch_; }
590 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100591
Aart Bikb13c65b2017-03-21 20:14:07 -0700592 bool HasSIMD() const { return has_simd_; }
593 void SetHasSIMD(bool value) { has_simd_ = value; }
594
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800595 bool HasLoops() const { return has_loops_; }
596 void SetHasLoops(bool value) { has_loops_ = value; }
597
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000598 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
599 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
600
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100601 ArtMethod* GetArtMethod() const { return art_method_; }
602 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
603
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100604 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500605 // The instruction has been inserted into the graph, either as a constant, or
606 // before cursor.
607 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
608
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000609 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
610
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800611 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
612 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
613 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
614
David Brazdil2d7352b2015-04-20 14:52:42 +0100615 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000616 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100617 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 template <class InstructionType, typename ValueType>
620 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600621 ArenaSafeMap<ValueType, InstructionType*>* cache,
622 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000623 // Try to find an existing constant of the given value.
624 InstructionType* constant = nullptr;
625 auto cached_constant = cache->find(value);
626 if (cached_constant != cache->end()) {
627 constant = cached_constant->second;
628 }
629
630 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100631 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100633 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000634 cache->Overwrite(value, constant);
635 InsertConstant(constant);
636 }
637 return constant;
638 }
639
David Brazdil8d5b8b22015-03-24 10:51:52 +0000640 void InsertConstant(HConstant* instruction);
641
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 // Cache a float constant into the graph. This method should only be
643 // called by the SsaBuilder when creating "equivalent" instructions.
644 void CacheFloatConstant(HFloatConstant* constant);
645
646 // See CacheFloatConstant comment.
647 void CacheDoubleConstant(HDoubleConstant* constant);
648
Vladimir Markoca6fff82017-10-03 14:49:14 +0100649 ArenaAllocator* const allocator_;
650 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000651
652 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657
Aart Bik281c6812016-08-26 11:31:48 -0700658 // List of blocks to perform a linear order tree traversal. Unlike the reverse
659 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100660 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100661
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000662 HBasicBlock* entry_block_;
663 HBasicBlock* exit_block_;
664
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100665 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100666 uint16_t maximum_number_of_out_vregs_;
667
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100668 // The number of virtual registers in this method. Contains the parameters.
669 uint16_t number_of_vregs_;
670
671 // The number of virtual registers used by parameters of this method.
672 uint16_t number_of_in_vregs_;
673
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000674 // Number of vreg size slots that the temporaries use (used in baseline compiler).
675 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100676
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800677 // Flag whether there are bounds checks in the graph. We can skip
678 // BCE if it's false. It's only best effort to keep it up to date in
679 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400680 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800681
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800682 // Flag whether there are try/catch blocks in the graph. We will skip
683 // try/catch-related passes if it's false. It's only best effort to keep
684 // it up to date in the presence of code elimination so there might be
685 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000686 bool has_try_catch_;
687
Aart Bikb13c65b2017-03-21 20:14:07 -0700688 // Flag whether SIMD instructions appear in the graph. If true, the
689 // code generators may have to be more careful spilling the wider
690 // contents of SIMD registers.
691 bool has_simd_;
692
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800693 // Flag whether there are any loops in the graph. We can skip loop
694 // optimization if it's false. It's only best effort to keep it up
695 // to date in the presence of code elimination so there might be false
696 // positives.
697 bool has_loops_;
698
699 // Flag whether there are any irreducible loops in the graph. It's only
700 // best effort to keep it up to date in the presence of code elimination
701 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000702 bool has_irreducible_loops_;
703
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000704 // Indicates whether the graph should be compiled in a way that
705 // ensures full debuggability. If false, we can apply more
706 // aggressive optimizations that may limit the level of debugging.
707 const bool debuggable_;
708
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000710 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000711
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100712 // The dex file from which the method is from.
713 const DexFile& dex_file_;
714
715 // The method index in the dex file.
716 const uint32_t method_idx_;
717
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100718 // If inlined, this encodes how the callee is being invoked.
719 const InvokeType invoke_type_;
720
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100721 // Whether the graph has been transformed to SSA form. Only used
722 // in debug mode to ensure we are not using properties only valid
723 // for non-SSA form (like the number of temporaries).
724 bool in_ssa_form_;
725
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800726 // Number of CHA guards in the graph. Used to short-circuit the
727 // CHA guard optimization pass when there is no CHA guard left.
728 uint32_t number_of_cha_guards_;
729
Mathieu Chartiere401d142015-04-22 13:56:20 -0700730 const InstructionSet instruction_set_;
731
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000732 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000733 HNullConstant* cached_null_constant_;
734 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000736 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000737 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000738
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100739 HCurrentMethod* cached_current_method_;
740
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100741 // The ArtMethod this graph is for. Note that for AOT, it may be null,
742 // for example for methods whose declaring class could not be resolved
743 // (such as when the superclass could not be found).
744 ArtMethod* art_method_;
745
David Brazdil4833f5a2015-12-16 10:37:39 +0000746 // Keep the RTI of inexact Object to avoid having to pass stack handle
747 // collection pointer to passes which may create NullConstant.
748 ReferenceTypeInfo inexact_object_rti_;
749
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000750 // Whether we are compiling this graph for on stack replacement: this will
751 // make all loops seen as irreducible and emit special stack maps to mark
752 // compiled code entries which the interpreter can directly jump to.
753 const bool osr_;
754
Mingyao Yang063fc772016-08-02 11:02:54 -0700755 // List of methods that are assumed to have single implementation.
756 ArenaSet<ArtMethod*> cha_single_implementation_list_;
757
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000758 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100759 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000760 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000761 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762 DISALLOW_COPY_AND_ASSIGN(HGraph);
763};
764
Vladimir Markof9f64412015-09-02 14:05:49 +0100765class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000766 public:
767 HLoopInformation(HBasicBlock* header, HGraph* graph)
768 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100769 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000770 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100771 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100772 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100773 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100774 blocks_(graph->GetAllocator(),
775 graph->GetBlocks().size(),
776 true,
777 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778 back_edges_.reserve(kDefaultNumberOfBackEdges);
779 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100782 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000783
784 void Dump(std::ostream& os);
785
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 HBasicBlock* GetHeader() const {
787 return header_;
788 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000789
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000790 void SetHeader(HBasicBlock* block) {
791 header_ = block;
792 }
793
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100794 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
795 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
796 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
797
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100799 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000800 }
801
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 }
805
David Brazdil46e2a392015-03-16 17:31:52 +0000806 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100807 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 }
809
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000810 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100811 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000812 }
813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100814 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100816 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100818 }
819
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100820 // Returns the lifetime position of the back edge that has the
821 // greatest lifetime position.
822 size_t GetLifetimeEnd() const;
823
824 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100825 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100826 }
827
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000828 // Finds blocks that are part of this loop.
829 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100830
Artem Serov7f4aff62017-06-21 17:02:18 +0100831 // Updates blocks population of the loop and all of its outer' ones recursively after the
832 // population of the inner loop is updated.
833 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
834
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100835 // Returns whether this loop information contains `block`.
836 // Note that this loop information *must* be populated before entering this function.
837 bool Contains(const HBasicBlock& block) const;
838
839 // Returns whether this loop information is an inner loop of `other`.
840 // Note that `other` *must* be populated before entering this function.
841 bool IsIn(const HLoopInformation& other) const;
842
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800843 // Returns true if instruction is not defined within this loop.
844 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700845
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100846 const ArenaBitVector& GetBlocks() const { return blocks_; }
847
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000849 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000851 void ClearAllBlocks() {
852 blocks_.ClearAllBits();
853 }
854
David Brazdil3f4a5222016-05-06 12:46:21 +0100855 bool HasBackEdgeNotDominatedByHeader() const;
856
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100857 bool IsPopulated() const {
858 return blocks_.GetHighestBitSet() != -1;
859 }
860
Anton Shaminf89381f2016-05-16 16:44:13 +0600861 bool DominatesAllBackEdges(HBasicBlock* block);
862
David Sehrc757dec2016-11-04 15:48:34 -0700863 bool HasExitEdge() const;
864
Artem Serov7f4aff62017-06-21 17:02:18 +0100865 // Resets back edge and blocks-in-loop data.
866 void ResetBasicBlockData() {
867 back_edges_.clear();
868 ClearAllBlocks();
869 }
870
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872 // Internal recursive implementation of `Populate`.
873 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100874 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100875
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000876 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100877 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000878 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100879 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100880 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100881 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000882
883 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
884};
885
David Brazdilec16f792015-08-19 15:04:01 +0100886// Stores try/catch information for basic blocks.
887// Note that HGraph is constructed so that catch blocks cannot simultaneously
888// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100889class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100890 public:
891 // Try block information constructor.
892 explicit TryCatchInformation(const HTryBoundary& try_entry)
893 : try_entry_(&try_entry),
894 catch_dex_file_(nullptr),
895 catch_type_index_(DexFile::kDexNoIndex16) {
896 DCHECK(try_entry_ != nullptr);
897 }
898
899 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800900 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100901 : try_entry_(nullptr),
902 catch_dex_file_(&dex_file),
903 catch_type_index_(catch_type_index) {}
904
905 bool IsTryBlock() const { return try_entry_ != nullptr; }
906
907 const HTryBoundary& GetTryEntry() const {
908 DCHECK(IsTryBlock());
909 return *try_entry_;
910 }
911
912 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
913
914 bool IsCatchAllTypeIndex() const {
915 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800916 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100917 }
918
Andreas Gampea5b09a62016-11-17 15:21:22 -0800919 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100920 DCHECK(IsCatchBlock());
921 return catch_type_index_;
922 }
923
924 const DexFile& GetCatchDexFile() const {
925 DCHECK(IsCatchBlock());
926 return *catch_dex_file_;
927 }
928
929 private:
930 // One of possibly several TryBoundary instructions entering the block's try.
931 // Only set for try blocks.
932 const HTryBoundary* try_entry_;
933
934 // Exception type information. Only set for catch blocks.
935 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800936 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100937};
938
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100940static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100941
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942// A block in a method. Contains the list of instructions represented
943// as a double linked list. Each block knows its predecessors and
944// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100945
Vladimir Markof9f64412015-09-02 14:05:49 +0100946class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700948 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100950 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
951 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000952 loop_information_(nullptr),
953 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100954 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100955 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100956 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100957 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000958 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000959 try_catch_information_(nullptr) {
960 predecessors_.reserve(kDefaultNumberOfPredecessors);
961 successors_.reserve(kDefaultNumberOfSuccessors);
962 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
963 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964
Vladimir Marko60584552015-09-03 13:35:12 +0000965 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 }
968
Vladimir Marko60584552015-09-03 13:35:12 +0000969 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100970 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971 }
972
David Brazdild26a4112015-11-10 11:07:31 +0000973 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
974 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
975
Vladimir Marko60584552015-09-03 13:35:12 +0000976 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
977 return ContainsElement(successors_, block, start_from);
978 }
979
980 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100981 return dominated_blocks_;
982 }
983
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100984 bool IsEntryBlock() const {
985 return graph_->GetEntryBlock() == this;
986 }
987
988 bool IsExitBlock() const {
989 return graph_->GetExitBlock() == this;
990 }
991
David Brazdil46e2a392015-03-16 17:31:52 +0000992 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200993 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700994 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000995 bool IsSingleTryBoundary() const;
996
997 // Returns true if this block emits nothing but a jump.
998 bool IsSingleJump() const {
999 HLoopInformation* loop_info = GetLoopInformation();
1000 return (IsSingleGoto() || IsSingleTryBoundary())
1001 // Back edges generate a suspend check.
1002 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1003 }
David Brazdil46e2a392015-03-16 17:31:52 +00001004
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001005 void AddBackEdge(HBasicBlock* back_edge) {
1006 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001007 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001008 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001009 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001010 loop_information_->AddBackEdge(back_edge);
1011 }
1012
Artem Serov7f4aff62017-06-21 17:02:18 +01001013 // Registers a back edge; if the block was not a loop header before the call associates a newly
1014 // created loop info with it.
1015 //
1016 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1017 // info for all blocks during back edges recalculation.
1018 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1019 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1020 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1021 }
1022 loop_information_->AddBackEdge(back_edge);
1023 }
1024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001025 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001028 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001029 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001030 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001032 HBasicBlock* GetDominator() const { return dominator_; }
1033 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001034 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1035
1036 void RemoveDominatedBlock(HBasicBlock* block) {
1037 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001038 }
Vladimir Marko60584552015-09-03 13:35:12 +00001039
1040 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1041 ReplaceElement(dominated_blocks_, existing, new_block);
1042 }
1043
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001044 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001045
1046 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001047 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001048 }
1049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001050 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1051 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001052 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001054 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1055 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001056
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001057 HInstruction* GetFirstInstructionDisregardMoves() const;
1058
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001060 successors_.push_back(block);
1061 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062 }
1063
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001064 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1065 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001066 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001067 new_block->predecessors_.push_back(this);
1068 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001069 }
1070
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1072 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001073 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001074 new_block->successors_.push_back(this);
1075 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001076 }
1077
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001078 // Insert `this` between `predecessor` and `successor. This method
1079 // preserves the indicies, and will update the first edge found between
1080 // `predecessor` and `successor`.
1081 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1082 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001083 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001084 successor->predecessors_[predecessor_index] = this;
1085 predecessor->successors_[successor_index] = this;
1086 successors_.push_back(successor);
1087 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001088 }
1089
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001090 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001091 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001092 }
1093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001095 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 }
1097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001099 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 }
1101
1102 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001103 predecessors_.push_back(block);
1104 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 }
1106
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001107 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001108 DCHECK_EQ(predecessors_.size(), 2u);
1109 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001110 }
1111
David Brazdil769c9e52015-04-27 13:54:09 +01001112 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001113 DCHECK_EQ(successors_.size(), 2u);
1114 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001115 }
1116
David Brazdilfc6a86a2015-06-26 10:33:45 +00001117 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001118 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001119 }
1120
David Brazdilfc6a86a2015-06-26 10:33:45 +00001121 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001122 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001123 }
1124
David Brazdilfc6a86a2015-06-26 10:33:45 +00001125 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001126 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001127 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001128 }
1129
1130 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001131 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001132 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001133 }
1134
1135 // Returns whether the first occurrence of `predecessor` in the list of
1136 // predecessors is at index `idx`.
1137 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001138 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001139 return GetPredecessorIndexOf(predecessor) == idx;
1140 }
1141
David Brazdild7558da2015-09-22 13:04:14 +01001142 // Create a new block between this block and its predecessors. The new block
1143 // is added to the graph, all predecessor edges are relinked to it and an edge
1144 // is created to `this`. Returns the new empty block. Reverse post order or
1145 // loop and try/catch information are not updated.
1146 HBasicBlock* CreateImmediateDominator();
1147
David Brazdilfc6a86a2015-06-26 10:33:45 +00001148 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001149 // created, latter block. Note that this method will add the block to the
1150 // graph, create a Goto at the end of the former block and will create an edge
1151 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001152 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001153 HBasicBlock* SplitBefore(HInstruction* cursor);
1154
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001155 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001156 // created block. Note that this method just updates raw block information,
1157 // like predecessors, successors, dominators, and instruction list. It does not
1158 // update the graph, reverse post order, loop information, nor make sure the
1159 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001160 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1161
1162 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1163 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001164
1165 // Merge `other` at the end of `this`. Successors and dominated blocks of
1166 // `other` are changed to be successors and dominated blocks of `this`. Note
1167 // that this method does not update the graph, reverse post order, loop
1168 // information, nor make sure the blocks are consistent (for example ending
1169 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001170 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001171
1172 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1173 // of `this` are moved to `other`.
1174 // Note that this method does not update the graph, reverse post order, loop
1175 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001176 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001177 void ReplaceWith(HBasicBlock* other);
1178
Aart Bik6b69e0a2017-01-11 10:20:43 -08001179 // Merges the instructions of `other` at the end of `this`.
1180 void MergeInstructionsWith(HBasicBlock* other);
1181
David Brazdil2d7352b2015-04-20 14:52:42 +01001182 // Merge `other` at the end of `this`. This method updates loops, reverse post
1183 // order, links to predecessors, successors, dominators and deletes the block
1184 // from the graph. The two blocks must be successive, i.e. `this` the only
1185 // predecessor of `other` and vice versa.
1186 void MergeWith(HBasicBlock* other);
1187
1188 // Disconnects `this` from all its predecessors, successors and dominator,
1189 // removes it from all loops it is included in and eventually from the graph.
1190 // The block must not dominate any other block. Predecessors and successors
1191 // are safely updated.
1192 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001193
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001194 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001195 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001196 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001197 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001198 // Replace phi `initial` with `replacement` within this block.
1199 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001200 // Replace instruction `initial` with `replacement` within this block.
1201 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1202 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001203 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001204 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001205 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1206 // instruction list. With 'ensure_safety' set to true, it verifies that the
1207 // instruction is not in use and removes it from the use lists of its inputs.
1208 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1209 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001210 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001211
1212 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001213 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214 }
1215
Roland Levillain6b879dd2014-09-22 17:13:44 +01001216 bool IsLoopPreHeaderFirstPredecessor() const {
1217 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001218 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001219 }
1220
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001221 bool IsFirstPredecessorBackEdge() const {
1222 DCHECK(IsLoopHeader());
1223 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1224 }
1225
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226 HLoopInformation* GetLoopInformation() const {
1227 return loop_information_;
1228 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001229
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 void SetInLoop(HLoopInformation* info) {
1234 if (IsLoopHeader()) {
1235 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001236 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001237 loop_information_ = info;
1238 } else if (loop_information_->Contains(*info->GetHeader())) {
1239 // Block is currently part of an outer loop. Make it part of this inner loop.
1240 // Note that a non loop header having a loop information means this loop information
1241 // has already been populated
1242 loop_information_ = info;
1243 } else {
1244 // Block is part of an inner loop. Do not update the loop information.
1245 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1246 // at this point, because this method is being called while populating `info`.
1247 }
1248 }
1249
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001250 // Raw update of the loop information.
1251 void SetLoopInformation(HLoopInformation* info) {
1252 loop_information_ = info;
1253 }
1254
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001255 bool IsInLoop() const { return loop_information_ != nullptr; }
1256
David Brazdilec16f792015-08-19 15:04:01 +01001257 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1258
1259 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1260 try_catch_information_ = try_catch_information;
1261 }
1262
1263 bool IsTryBlock() const {
1264 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1265 }
1266
1267 bool IsCatchBlock() const {
1268 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1269 }
David Brazdilffee3d32015-07-06 11:48:53 +01001270
1271 // Returns the try entry that this block's successors should have. They will
1272 // be in the same try, unless the block ends in a try boundary. In that case,
1273 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001274 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001275
Aart Bik75ff2c92018-04-21 01:28:11 +00001276 bool HasThrowingInstructions() const;
1277
David Brazdila4b8c212015-05-07 09:59:30 +01001278 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001279 bool Dominates(HBasicBlock* block) const;
1280
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001281 size_t GetLifetimeStart() const { return lifetime_start_; }
1282 size_t GetLifetimeEnd() const { return lifetime_end_; }
1283
1284 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1285 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1286
David Brazdil8d5b8b22015-03-24 10:51:52 +00001287 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001288 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001289 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001290 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001291 bool HasSinglePhi() const;
1292
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001293 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001294 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001295 ArenaVector<HBasicBlock*> predecessors_;
1296 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001297 HInstructionList instructions_;
1298 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001299 HLoopInformation* loop_information_;
1300 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001301 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001302 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001303 // The dex program counter of the first instruction of this block.
1304 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001305 size_t lifetime_start_;
1306 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001307 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001308
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001309 friend class HGraph;
1310 friend class HInstruction;
1311
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001312 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1313};
1314
David Brazdilb2bd1c52015-03-25 11:17:37 +00001315// Iterates over the LoopInformation of all loops which contain 'block'
1316// from the innermost to the outermost.
1317class HLoopInformationOutwardIterator : public ValueObject {
1318 public:
1319 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1320 : current_(block.GetLoopInformation()) {}
1321
1322 bool Done() const { return current_ == nullptr; }
1323
1324 void Advance() {
1325 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001326 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001327 }
1328
1329 HLoopInformation* Current() const {
1330 DCHECK(!Done());
1331 return current_;
1332 }
1333
1334 private:
1335 HLoopInformation* current_;
1336
1337 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1338};
1339
Alexandre Ramesef20f712015-06-09 10:29:30 +01001340#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001341 M(Above, Condition) \
1342 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001343 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001345 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(ArrayGet, Instruction) \
1347 M(ArrayLength, Instruction) \
1348 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001349 M(Below, Condition) \
1350 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001351 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001353 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001354 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001355 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001356 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001357 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001358 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001359 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001360 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001361 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001362 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001364 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001367 M(Exit, Instruction) \
1368 M(FloatConstant, Constant) \
1369 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(GreaterThan, Condition) \
1371 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001373 M(InstanceFieldGet, Instruction) \
1374 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001375 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001377 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001378 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001379 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001380 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001381 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001382 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001383 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001384 M(LessThan, Condition) \
1385 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001386 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001387 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001388 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001389 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001390 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001391 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001392 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001393 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001394 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001395 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001397 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001398 M(Neg, UnaryOperation) \
1399 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001400 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001401 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001402 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001403 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001404 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001405 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001406 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001407 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001408 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001409 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001410 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001411 M(Return, Instruction) \
1412 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001413 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001414 M(Shl, BinaryOperation) \
1415 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001416 M(StaticFieldGet, Instruction) \
1417 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001418 M(UnresolvedInstanceFieldGet, Instruction) \
1419 M(UnresolvedInstanceFieldSet, Instruction) \
1420 M(UnresolvedStaticFieldGet, Instruction) \
1421 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001422 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001424 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001425 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001426 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001427 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001428 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001429 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001430 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001431 M(VecExtractScalar, VecUnaryOperation) \
1432 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001433 M(VecCnv, VecUnaryOperation) \
1434 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001435 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001436 M(VecNot, VecUnaryOperation) \
1437 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001438 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001439 M(VecSub, VecBinaryOperation) \
1440 M(VecMul, VecBinaryOperation) \
1441 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001442 M(VecMin, VecBinaryOperation) \
1443 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001444 M(VecAnd, VecBinaryOperation) \
1445 M(VecAndNot, VecBinaryOperation) \
1446 M(VecOr, VecBinaryOperation) \
1447 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001448 M(VecSaturationAdd, VecBinaryOperation) \
1449 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001450 M(VecShl, VecBinaryOperation) \
1451 M(VecShr, VecBinaryOperation) \
1452 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001453 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001454 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001455 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001456 M(VecLoad, VecMemoryOperation) \
1457 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001458
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001459/*
1460 * Instructions, shared across several (not all) architectures.
1461 */
1462#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1463#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1464#else
1465#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001466 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001467 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001468 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001469 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001470#endif
1471
Alexandre Ramesef20f712015-06-09 10:29:30 +01001472#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1473
1474#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1475
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001476#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001477#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001478#else
1479#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1480 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001481 M(MipsPackedSwitch, Instruction) \
1482 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001483#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001484
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001485#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1486
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001487#ifndef ART_ENABLE_CODEGEN_x86
1488#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1489#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001490#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1491 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001492 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001493 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001494 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001495#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001496
1497#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1498
1499#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1500 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001501 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001502 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1503 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001504 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001505 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001506 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1507 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1508
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001509#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1510 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001511 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001512 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001513 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001514 M(Invoke, Instruction) \
1515 M(VecOperation, Instruction) \
1516 M(VecUnaryOperation, VecOperation) \
1517 M(VecBinaryOperation, VecOperation) \
1518 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001519
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001520#define FOR_EACH_INSTRUCTION(M) \
1521 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1522 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1523
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001524#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001525FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1526#undef FORWARD_DECLARATION
1527
Vladimir Marko372f10e2016-05-17 16:30:10 +01001528#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001529 private: \
1530 H##type& operator=(const H##type&) = delete; \
1531 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001532 const char* DebugName() const OVERRIDE { return #type; } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001533 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1534 DCHECK(IsClonable()); \
1535 return new (arena) H##type(*this->As##type()); \
1536 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001537 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001538
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001539#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001540 private: \
1541 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001542 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001543
Artem Serovcced8ba2017-07-19 18:18:09 +01001544#define DEFAULT_COPY_CONSTRUCTOR(type) \
1545 explicit H##type(const H##type& other) = default;
1546
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001547template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001548class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1549 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001550 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001551 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001552 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001553 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001554 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001555 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001556 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001557
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001558 private:
David Brazdiled596192015-01-23 10:39:45 +00001559 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001560 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001561
1562 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001563 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001564
Vladimir Marko46817b82016-03-29 12:21:58 +01001565 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001566
1567 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1568};
1569
David Brazdiled596192015-01-23 10:39:45 +00001570template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001571using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001572
David Brazdil1abb4192015-02-17 18:33:36 +00001573// This class is used by HEnvironment and HInstruction classes to record the
1574// instructions they use and pointers to the corresponding HUseListNodes kept
1575// by the used instructions.
1576template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001577class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001578 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001579 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1580 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001581
Vladimir Marko46817b82016-03-29 12:21:58 +01001582 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1583 : HUserRecord(old_record.instruction_, before_use_node) {}
1584 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1585 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001586 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001587 }
1588
1589 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001590 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1591 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001592
1593 private:
1594 // Instruction used by the user.
1595 HInstruction* instruction_;
1596
Vladimir Marko46817b82016-03-29 12:21:58 +01001597 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1598 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001599};
1600
Vladimir Markoe9004912016-06-16 16:50:52 +01001601// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1602// This is used for HInstruction::GetInputs() to return a container wrapper providing
1603// HInstruction* values even though the underlying container has HUserRecord<>s.
1604struct HInputExtractor {
1605 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1606 return record.GetInstruction();
1607 }
1608 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1609 return record.GetInstruction();
1610 }
1611};
1612
1613using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1614using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1615
Aart Bik854a02b2015-07-14 16:07:00 -07001616/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001618 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001619 * For write/read dependences on fields/arrays, the dependence analysis uses
1620 * type disambiguation (e.g. a float field write cannot modify the value of an
1621 * integer field read) and the access type (e.g. a reference array write cannot
1622 * modify the value of a reference field read [although it may modify the
1623 * reference fetch prior to reading the field, which is represented by its own
1624 * write/read dependence]). The analysis makes conservative points-to
1625 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1626 * the same, and any reference read depends on any reference read without
1627 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001628 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 * The internal representation uses 38-bit and is described in the table below.
1630 * The first line indicates the side effect, and for field/array accesses the
1631 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001632 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001633 * The two numbered lines below indicate the bit position in the bitfield (read
1634 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001635 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001636 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1637 * +-------------+---------+---------+--------------+---------+---------+
1638 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1639 * | 3 |333333322|222222221| 1 |111111110|000000000|
1640 * | 7 |654321098|765432109| 8 |765432109|876543210|
1641 *
1642 * Note that, to ease the implementation, 'changes' bits are least significant
1643 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001644 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001645class SideEffects : public ValueObject {
1646 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001647 SideEffects() : flags_(0) {}
1648
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001649 static SideEffects None() {
1650 return SideEffects(0);
1651 }
1652
1653 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001654 return SideEffects(kAllChangeBits | kAllDependOnBits);
1655 }
1656
1657 static SideEffects AllChanges() {
1658 return SideEffects(kAllChangeBits);
1659 }
1660
1661 static SideEffects AllDependencies() {
1662 return SideEffects(kAllDependOnBits);
1663 }
1664
1665 static SideEffects AllExceptGCDependency() {
1666 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1667 }
1668
1669 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001670 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001671 }
1672
Aart Bik34c3ba92015-07-20 14:08:59 -07001673 static SideEffects AllWrites() {
1674 return SideEffects(kAllWrites);
1675 }
1676
1677 static SideEffects AllReads() {
1678 return SideEffects(kAllReads);
1679 }
1680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001682 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001683 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001684 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001685 }
1686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001688 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001689 }
1690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001691 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001692 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001693 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001694 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001695 }
1696
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001697 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001698 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001699 }
1700
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001701 static SideEffects CanTriggerGC() {
1702 return SideEffects(1ULL << kCanTriggerGCBit);
1703 }
1704
1705 static SideEffects DependsOnGC() {
1706 return SideEffects(1ULL << kDependsOnGCBit);
1707 }
1708
Aart Bik854a02b2015-07-14 16:07:00 -07001709 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001710 SideEffects Union(SideEffects other) const {
1711 return SideEffects(flags_ | other.flags_);
1712 }
1713
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001714 SideEffects Exclusion(SideEffects other) const {
1715 return SideEffects(flags_ & ~other.flags_);
1716 }
1717
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001718 void Add(SideEffects other) {
1719 flags_ |= other.flags_;
1720 }
1721
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001722 bool Includes(SideEffects other) const {
1723 return (other.flags_ & flags_) == other.flags_;
1724 }
1725
1726 bool HasSideEffects() const {
1727 return (flags_ & kAllChangeBits);
1728 }
1729
1730 bool HasDependencies() const {
1731 return (flags_ & kAllDependOnBits);
1732 }
1733
1734 // Returns true if there are no side effects or dependencies.
1735 bool DoesNothing() const {
1736 return flags_ == 0;
1737 }
1738
Aart Bik854a02b2015-07-14 16:07:00 -07001739 // Returns true if something is written.
1740 bool DoesAnyWrite() const {
1741 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001742 }
1743
Aart Bik854a02b2015-07-14 16:07:00 -07001744 // Returns true if something is read.
1745 bool DoesAnyRead() const {
1746 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001747 }
1748
Aart Bik854a02b2015-07-14 16:07:00 -07001749 // Returns true if potentially everything is written and read
1750 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001751 bool DoesAllReadWrite() const {
1752 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1753 }
1754
Aart Bik854a02b2015-07-14 16:07:00 -07001755 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001757 }
1758
Roland Levillain0d5a2812015-11-13 10:07:31 +00001759 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001760 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001761 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1762 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001763 }
1764
1765 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001766 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001767 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001768 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001769 for (int s = kLastBit; s >= 0; s--) {
1770 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1771 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1772 // This is a bit for the GC side effect.
1773 if (current_bit_is_set) {
1774 flags += "GC";
1775 }
Aart Bik854a02b2015-07-14 16:07:00 -07001776 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001777 } else {
1778 // This is a bit for the array/field analysis.
1779 // The underscore character stands for the 'can trigger GC' bit.
1780 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1781 if (current_bit_is_set) {
1782 flags += kDebug[s];
1783 }
1784 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1785 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1786 flags += "|";
1787 }
1788 }
Aart Bik854a02b2015-07-14 16:07:00 -07001789 }
1790 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001791 }
1792
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001793 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001794
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001795 private:
1796 static constexpr int kFieldArrayAnalysisBits = 9;
1797
1798 static constexpr int kFieldWriteOffset = 0;
1799 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1800 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1801 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1802
1803 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1804
1805 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1806 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1807 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1808 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1809
1810 static constexpr int kLastBit = kDependsOnGCBit;
1811 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1812
1813 // Aliases.
1814
1815 static_assert(kChangeBits == kDependOnBits,
1816 "the 'change' bits should match the 'depend on' bits.");
1817
1818 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1819 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1820 static constexpr uint64_t kAllWrites =
1821 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1822 static constexpr uint64_t kAllReads =
1823 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001825 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001826 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001827 int shift;
1828 switch (type) {
1829 case DataType::Type::kReference: shift = 0; break;
1830 case DataType::Type::kBool: shift = 1; break;
1831 case DataType::Type::kInt8: shift = 2; break;
1832 case DataType::Type::kUint16: shift = 3; break;
1833 case DataType::Type::kInt16: shift = 4; break;
1834 case DataType::Type::kInt32: shift = 5; break;
1835 case DataType::Type::kInt64: shift = 6; break;
1836 case DataType::Type::kFloat32: shift = 7; break;
1837 case DataType::Type::kFloat64: shift = 8; break;
1838 default:
1839 LOG(FATAL) << "Unexpected data type " << type;
1840 UNREACHABLE();
1841 }
Aart Bik854a02b2015-07-14 16:07:00 -07001842 DCHECK_LE(kFieldWriteOffset, shift);
1843 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001844 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001845 }
1846
1847 // Private constructor on direct flags value.
1848 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1849
1850 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001851};
1852
David Brazdiled596192015-01-23 10:39:45 +00001853// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001854class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001855 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001856 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001857 size_t number_of_vregs,
1858 ArtMethod* method,
1859 uint32_t dex_pc,
1860 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001861 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1862 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001863 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001864 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001865 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001866 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001867 }
1868
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001869 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1870 const HEnvironment& to_copy,
1871 HInstruction* holder)
1872 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001873 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001874 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001875 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001876 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001877
Vladimir Markoec32f642017-06-02 10:51:55 +01001878 void AllocateLocations() {
1879 DCHECK(locations_.empty());
1880 locations_.resize(vregs_.size());
1881 }
1882
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001883 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001884 if (parent_ != nullptr) {
1885 parent_->SetAndCopyParentChain(allocator, parent);
1886 } else {
1887 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1888 parent_->CopyFrom(parent);
1889 if (parent->GetParent() != nullptr) {
1890 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1891 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001892 }
David Brazdiled596192015-01-23 10:39:45 +00001893 }
1894
Vladimir Marko69d310e2017-10-09 14:12:23 +01001895 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001896 void CopyFrom(HEnvironment* environment);
1897
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001898 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1899 // input to the loop phi instead. This is for inserting instructions that
1900 // require an environment (like HDeoptimization) in the loop pre-header.
1901 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001902
1903 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001904 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001905 }
1906
1907 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001908 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001909 }
1910
David Brazdil1abb4192015-02-17 18:33:36 +00001911 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001912
Artem Serovca210e32017-12-15 13:43:20 +00001913 // Replaces the input at the position 'index' with the replacement; the replacement and old
1914 // input instructions' env_uses_ lists are adjusted. The function works similar to
1915 // HInstruction::ReplaceInput.
1916 void ReplaceInput(HInstruction* replacement, size_t index);
1917
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001918 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001919
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001920 HEnvironment* GetParent() const { return parent_; }
1921
1922 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001923 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001924 }
1925
1926 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001927 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001928 }
1929
1930 uint32_t GetDexPc() const {
1931 return dex_pc_;
1932 }
1933
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001934 ArtMethod* GetMethod() const {
1935 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001936 }
1937
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001938 HInstruction* GetHolder() const {
1939 return holder_;
1940 }
1941
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001942
1943 bool IsFromInlinedInvoke() const {
1944 return GetParent() != nullptr;
1945 }
1946
David Brazdiled596192015-01-23 10:39:45 +00001947 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001948 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1949 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001950 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001951 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001952 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001953
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001954 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001955 HInstruction* const holder_;
1956
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001957 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001958
1959 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1960};
1961
Vladimir Markof9f64412015-09-02 14:05:49 +01001962class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001963 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301964#define DECLARE_KIND(type, super) k##type,
1965 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001966 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301967 kLastInstructionKind
1968 };
1969#undef DECLARE_KIND
1970
1971 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001972 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1973
1974 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001975 : previous_(nullptr),
1976 next_(nullptr),
1977 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001978 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001979 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001980 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001981 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001982 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001983 locations_(nullptr),
1984 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001985 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001986 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001987 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301988 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01001989 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001990 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1991 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001992
Dave Allison20dfc792014-06-16 20:44:29 -07001993 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001994
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001995
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001996 HInstruction* GetNext() const { return next_; }
1997 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001998
Calin Juravle77520bc2015-01-12 18:45:46 +00001999 HInstruction* GetNextDisregardingMoves() const;
2000 HInstruction* GetPreviousDisregardingMoves() const;
2001
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002002 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002003 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002004 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002005 bool IsInBlock() const { return block_ != nullptr; }
2006 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002007 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2008 bool IsIrreducibleLoopHeaderPhi() const {
2009 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2010 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002011
Vladimir Marko372f10e2016-05-17 16:30:10 +01002012 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2013
2014 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2015 // One virtual method is enough, just const_cast<> and then re-add the const.
2016 return ArrayRef<const HUserRecord<HInstruction*>>(
2017 const_cast<HInstruction*>(this)->GetInputRecords());
2018 }
2019
Vladimir Markoe9004912016-06-16 16:50:52 +01002020 HInputsRef GetInputs() {
2021 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002022 }
2023
Vladimir Markoe9004912016-06-16 16:50:52 +01002024 HConstInputsRef GetInputs() const {
2025 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002026 }
2027
2028 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002029 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002030
Aart Bik2767f4b2016-10-28 15:03:53 -07002031 bool HasInput(HInstruction* input) const {
2032 for (const HInstruction* i : GetInputs()) {
2033 if (i == input) {
2034 return true;
2035 }
2036 }
2037 return false;
2038 }
2039
Vladimir Marko372f10e2016-05-17 16:30:10 +01002040 void SetRawInputAt(size_t index, HInstruction* input) {
2041 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2042 }
2043
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002044 virtual void Accept(HGraphVisitor* visitor) = 0;
2045 virtual const char* DebugName() const = 0;
2046
Vladimir Markobd785672018-05-03 17:09:09 +01002047 DataType::Type GetType() const {
2048 return TypeField::Decode(GetPackedFields());
2049 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002050
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002051 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002052
2053 uint32_t GetDexPc() const { return dex_pc_; }
2054
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002055 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002056
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002057 // Can the instruction throw?
2058 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2059 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002060 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002061
2062 // Does the instruction always throw an exception unconditionally?
2063 virtual bool AlwaysThrows() const { return false; }
2064
David Brazdilec16f792015-08-19 15:04:01 +01002065 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002066
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002067 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002068 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002069
Calin Juravle10e244f2015-01-26 18:54:32 +00002070 // Does not apply for all instructions, but having this at top level greatly
2071 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002072 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002073 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002075 return true;
2076 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002077
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002078 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002079 return false;
2080 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002081
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002082 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002084 }
2085
Calin Juravle2e768302015-07-28 14:41:11 +00002086 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002087
Calin Juravle61d544b2015-02-23 16:46:57 +00002088 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002089 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002090 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002091 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002092 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002093
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002094 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002095 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002096 // Note: fixup_end remains valid across push_front().
2097 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2098 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002099 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002100 uses_.push_front(*new_node);
2101 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002102 }
2103
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002105 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002106 // Note: env_fixup_end remains valid across push_front().
2107 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2108 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002109 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002110 env_uses_.push_front(*new_node);
2111 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112 }
2113
David Brazdil1abb4192015-02-17 18:33:36 +00002114 void RemoveAsUserOfInput(size_t input) {
2115 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002116 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2117 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2118 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002119 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002120
Vladimir Marko372f10e2016-05-17 16:30:10 +01002121 void RemoveAsUserOfAllInputs() {
2122 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2123 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2124 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2125 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2126 }
2127 }
2128
David Brazdil1abb4192015-02-17 18:33:36 +00002129 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2130 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002131
Vladimir Marko46817b82016-03-29 12:21:58 +01002132 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2133 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2134 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002135 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002136 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002137 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002138
Aart Bikcc42be02016-10-20 16:14:16 -07002139 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002140 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002141 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002142 !CanThrow() &&
2143 !IsSuspendCheck() &&
2144 !IsControlFlow() &&
2145 !IsNativeDebugInfo() &&
2146 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002147 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002148 !IsMemoryBarrier() &&
2149 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002150 }
2151
Aart Bikcc42be02016-10-20 16:14:16 -07002152 bool IsDeadAndRemovable() const {
2153 return IsRemovable() && !HasUses();
2154 }
2155
Roland Levillain6c82d402014-10-13 16:10:27 +01002156 // Does this instruction strictly dominate `other_instruction`?
2157 // Returns false if this instruction and `other_instruction` are the same.
2158 // Aborts if this instruction and `other_instruction` are both phis.
2159 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002160
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002161 int GetId() const { return id_; }
2162 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002163
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002164 int GetSsaIndex() const { return ssa_index_; }
2165 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2166 bool HasSsaIndex() const { return ssa_index_ != -1; }
2167
2168 bool HasEnvironment() const { return environment_ != nullptr; }
2169 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002170 // Set the `environment_` field. Raw because this method does not
2171 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002172 void SetRawEnvironment(HEnvironment* environment) {
2173 DCHECK(environment_ == nullptr);
2174 DCHECK_EQ(environment->GetHolder(), this);
2175 environment_ = environment;
2176 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002177
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002178 void InsertRawEnvironment(HEnvironment* environment) {
2179 DCHECK(environment_ != nullptr);
2180 DCHECK_EQ(environment->GetHolder(), this);
2181 DCHECK(environment->GetParent() == nullptr);
2182 environment->parent_ = environment_;
2183 environment_ = environment;
2184 }
2185
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002186 void RemoveEnvironment();
2187
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002188 // Set the environment of this instruction, copying it from `environment`. While
2189 // copying, the uses lists are being updated.
2190 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002191 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002192 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002193 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002194 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002195 if (environment->GetParent() != nullptr) {
2196 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2197 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002198 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002199
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002200 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2201 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002202 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002203 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002204 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002205 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002206 if (environment->GetParent() != nullptr) {
2207 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2208 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002209 }
2210
Nicolas Geoffray39468442014-09-02 15:17:15 +01002211 // Returns the number of entries in the environment. Typically, that is the
2212 // number of dex registers in a method. It could be more in case of inlining.
2213 size_t EnvironmentSize() const;
2214
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002215 LocationSummary* GetLocations() const { return locations_; }
2216 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002217
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002218 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002219 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002220 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002221 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002222
Alexandre Rames188d4312015-04-09 18:30:21 +01002223 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2224 // uses of this instruction by `other` are *not* updated.
2225 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2226 ReplaceWith(other);
2227 other->ReplaceInput(this, use_index);
2228 }
2229
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002230 // Move `this` instruction before `cursor`
2231 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002232
Vladimir Markofb337ea2015-11-25 15:25:10 +00002233 // Move `this` before its first user and out of any loops. If there is no
2234 // out-of-loop user that dominates all other users, move the instruction
2235 // to the end of the out-of-loop common dominator of the user's blocks.
2236 //
2237 // This can be used only on non-throwing instructions with no side effects that
2238 // have at least one use but no environment uses.
2239 void MoveBeforeFirstUserAndOutOfLoops();
2240
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002241#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002242 bool Is##type() const;
2243
2244 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2245#undef INSTRUCTION_TYPE_CHECK
2246
2247#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002248 const H##type* As##type() const; \
2249 H##type* As##type();
2250
Vladimir Markoa90dd512018-05-04 15:04:45 +01002251 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2252#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002253
Artem Serovcced8ba2017-07-19 18:18:09 +01002254 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2255 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2256 // the instruction then the behaviour of this function is undefined.
2257 //
2258 // Note: It is semantically valid to create a clone of the instruction only until
2259 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2260 // copied.
2261 //
2262 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2263 // 'explicit HInstruction(const HInstruction& other)' for details.
2264 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2265 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2266 DebugName() << " " << GetId();
2267 UNREACHABLE();
2268 }
2269
2270 // Return whether instruction can be cloned (copied).
2271 virtual bool IsClonable() const { return false; }
2272
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002273 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002274 // TODO: this method is used by LICM and GVN with possibly different
2275 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002276 virtual bool CanBeMoved() const { return false; }
2277
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002278 // Returns whether any data encoded in the two instructions is equal.
2279 // This method does not look at the inputs. Both instructions must be
2280 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002281 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002282 return false;
2283 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002284
2285 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002286 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002288 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002289
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002290 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2291 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2292 // the virtual function because the __attribute__((__pure__)) doesn't really
2293 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302294 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002295
2296 virtual size_t ComputeHashCode() const {
2297 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002298 for (const HInstruction* input : GetInputs()) {
2299 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002300 }
2301 return result;
2302 }
2303
2304 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002305 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002306 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002307
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002308 size_t GetLifetimePosition() const { return lifetime_position_; }
2309 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2310 LiveInterval* GetLiveInterval() const { return live_interval_; }
2311 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2312 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2313
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002314 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2315
2316 // Returns whether the code generation of the instruction will require to have access
2317 // to the current method. Such instructions are:
2318 // (1): Instructions that require an environment, as calling the runtime requires
2319 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002320 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2321 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002322 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002323 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002324 }
2325
Vladimir Markodc151b22015-10-15 18:02:30 +01002326 // Returns whether the code generation of the instruction will require to have access
2327 // to the dex cache of the current method's declaring class via the current method.
2328 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002329
Mark Mendellc4701932015-04-10 13:18:51 -04002330 // Does this instruction have any use in an environment before
2331 // control flow hits 'other'?
2332 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2333
2334 // Remove all references to environment uses of this instruction.
2335 // The caller must ensure that this is safe to do.
2336 void RemoveEnvironmentUsers();
2337
Vladimir Markoa1de9182016-02-25 11:37:38 +00002338 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2339 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002340
David Brazdil1abb4192015-02-17 18:33:36 +00002341 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002342 // If set, the machine code for this instruction is assumed to be generated by
2343 // its users. Used by liveness analysis to compute use positions accordingly.
2344 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2345 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302346 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2347 static constexpr size_t kFieldInstructionKindSize =
2348 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002349 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302350 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002351 static constexpr size_t kFieldTypeSize =
2352 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2353 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002354 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2355
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302356 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2357 "Too many generic packed fields");
2358
Vladimir Markobd785672018-05-03 17:09:09 +01002359 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2360
Vladimir Marko372f10e2016-05-17 16:30:10 +01002361 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2362 return GetInputRecords()[i];
2363 }
2364
2365 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2366 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2367 input_records[index] = input;
2368 }
David Brazdil1abb4192015-02-17 18:33:36 +00002369
Vladimir Markoa1de9182016-02-25 11:37:38 +00002370 uint32_t GetPackedFields() const {
2371 return packed_fields_;
2372 }
2373
2374 template <size_t flag>
2375 bool GetPackedFlag() const {
2376 return (packed_fields_ & (1u << flag)) != 0u;
2377 }
2378
2379 template <size_t flag>
2380 void SetPackedFlag(bool value = true) {
2381 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2382 }
2383
2384 template <typename BitFieldType>
2385 typename BitFieldType::value_type GetPackedField() const {
2386 return BitFieldType::Decode(packed_fields_);
2387 }
2388
2389 template <typename BitFieldType>
2390 void SetPackedField(typename BitFieldType::value_type value) {
2391 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2392 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2393 }
2394
Artem Serovcced8ba2017-07-19 18:18:09 +01002395 // Copy construction for the instruction (used for Clone function).
2396 //
2397 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2398 // prepare_for_register_allocator are not copied (set to default values).
2399 //
2400 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2401 // fine for most of them. However for some of the instructions a custom copy constructor must be
2402 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2403 // for copying them).
2404 explicit HInstruction(const HInstruction& other)
2405 : previous_(nullptr),
2406 next_(nullptr),
2407 block_(nullptr),
2408 dex_pc_(other.dex_pc_),
2409 id_(-1),
2410 ssa_index_(-1),
2411 packed_fields_(other.packed_fields_),
2412 environment_(nullptr),
2413 locations_(nullptr),
2414 live_interval_(nullptr),
2415 lifetime_position_(kNoLifetime),
2416 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302417 reference_type_handle_(other.reference_type_handle_) {
2418 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002419
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002420 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302421 using InstructionKindField =
2422 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2423
Vladimir Marko46817b82016-03-29 12:21:58 +01002424 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2425 auto before_use_node = uses_.before_begin();
2426 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2427 HInstruction* user = use_node->GetUser();
2428 size_t input_index = use_node->GetIndex();
2429 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2430 before_use_node = use_node;
2431 }
2432 }
2433
2434 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2435 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2436 if (next != uses_.end()) {
2437 HInstruction* next_user = next->GetUser();
2438 size_t next_index = next->GetIndex();
2439 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2440 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2441 }
2442 }
2443
2444 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2445 auto before_env_use_node = env_uses_.before_begin();
2446 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2447 HEnvironment* user = env_use_node->GetUser();
2448 size_t input_index = env_use_node->GetIndex();
2449 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2450 before_env_use_node = env_use_node;
2451 }
2452 }
2453
2454 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2455 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2456 if (next != env_uses_.end()) {
2457 HEnvironment* next_user = next->GetUser();
2458 size_t next_index = next->GetIndex();
2459 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2460 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2461 }
2462 }
David Brazdil1abb4192015-02-17 18:33:36 +00002463
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002464 HInstruction* previous_;
2465 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002466 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002467 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002469 // An instruction gets an id when it is added to the graph.
2470 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002471 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002472 int id_;
2473
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002474 // When doing liveness analysis, instructions that have uses get an SSA index.
2475 int ssa_index_;
2476
Vladimir Markoa1de9182016-02-25 11:37:38 +00002477 // Packed fields.
2478 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002479
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002480 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002481 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002482
2483 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002484 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002485
Nicolas Geoffray39468442014-09-02 15:17:15 +01002486 // The environment associated with this instruction. Not null if the instruction
2487 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002488 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002489
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002490 // Set by the code generator.
2491 LocationSummary* locations_;
2492
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002493 // Set by the liveness analysis.
2494 LiveInterval* live_interval_;
2495
2496 // Set by the liveness analysis, this is the position in a linear
2497 // order of blocks where this instruction's live interval start.
2498 size_t lifetime_position_;
2499
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002500 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002501
Vladimir Markoa1de9182016-02-25 11:37:38 +00002502 // The reference handle part of the reference type info.
2503 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002504 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002505 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002506
David Brazdil1abb4192015-02-17 18:33:36 +00002507 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002508 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002509 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002510 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002511 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002513std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002514
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002515// Iterates over the instructions, while preserving the next instruction
2516// in case the current instruction gets removed from the list by the user
2517// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002518class HInstructionIterator : public ValueObject {
2519 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002520 explicit HInstructionIterator(const HInstructionList& instructions)
2521 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002522 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002523 }
2524
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002525 bool Done() const { return instruction_ == nullptr; }
2526 HInstruction* Current() const { return instruction_; }
2527 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002528 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002529 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002530 }
2531
2532 private:
2533 HInstruction* instruction_;
2534 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002535
2536 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002537};
2538
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002539// Iterates over the instructions without saving the next instruction,
2540// therefore handling changes in the graph potentially made by the user
2541// of this iterator.
2542class HInstructionIteratorHandleChanges : public ValueObject {
2543 public:
2544 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2545 : instruction_(instructions.first_instruction_) {
2546 }
2547
2548 bool Done() const { return instruction_ == nullptr; }
2549 HInstruction* Current() const { return instruction_; }
2550 void Advance() {
2551 instruction_ = instruction_->GetNext();
2552 }
2553
2554 private:
2555 HInstruction* instruction_;
2556
2557 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2558};
2559
2560
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002561class HBackwardInstructionIterator : public ValueObject {
2562 public:
2563 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2564 : instruction_(instructions.last_instruction_) {
2565 next_ = Done() ? nullptr : instruction_->GetPrevious();
2566 }
2567
2568 bool Done() const { return instruction_ == nullptr; }
2569 HInstruction* Current() const { return instruction_; }
2570 void Advance() {
2571 instruction_ = next_;
2572 next_ = Done() ? nullptr : instruction_->GetPrevious();
2573 }
2574
2575 private:
2576 HInstruction* instruction_;
2577 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002578
2579 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002580};
2581
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002582class HVariableInputSizeInstruction : public HInstruction {
2583 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002584 using HInstruction::GetInputRecords; // Keep the const version visible.
2585 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2586 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2587 }
2588
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002589 void AddInput(HInstruction* input);
2590 void InsertInputAt(size_t index, HInstruction* input);
2591 void RemoveInputAt(size_t index);
2592
Igor Murashkind01745e2017-04-05 16:40:31 -07002593 // Removes all the inputs.
2594 // Also removes this instructions from each input's use list
2595 // (for non-environment uses only).
2596 void RemoveAllInputs();
2597
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002598 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302599 HVariableInputSizeInstruction(InstructionKind inst_kind,
2600 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002601 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002602 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002603 size_t number_of_inputs,
2604 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302605 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002606 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002607 HVariableInputSizeInstruction(InstructionKind inst_kind,
2608 DataType::Type type,
2609 SideEffects side_effects,
2610 uint32_t dex_pc,
2611 ArenaAllocator* allocator,
2612 size_t number_of_inputs,
2613 ArenaAllocKind kind)
2614 : HInstruction(inst_kind, type, side_effects, dex_pc),
2615 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002616
Artem Serovcced8ba2017-07-19 18:18:09 +01002617 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002618
Artem Serovcced8ba2017-07-19 18:18:09 +01002619 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002620};
2621
Vladimir Markof9f64412015-09-02 14:05:49 +01002622template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002623class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002624 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002625 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302626 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002627 HExpression<N>(InstructionKind kind,
2628 DataType::Type type,
2629 SideEffects side_effects,
2630 uint32_t dex_pc)
2631 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2632 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002633
Vladimir Marko372f10e2016-05-17 16:30:10 +01002634 using HInstruction::GetInputRecords; // Keep the const version visible.
2635 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2636 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002637 }
2638
Artem Serovcced8ba2017-07-19 18:18:09 +01002639 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002640 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002641
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002642 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002643 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002644
2645 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002646};
2647
Vladimir Markobd785672018-05-03 17:09:09 +01002648// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002649template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002650class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002651 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002652 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002653
Vladimir Markobd785672018-05-03 17:09:09 +01002654 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002655
Vladimir Marko372f10e2016-05-17 16:30:10 +01002656 using HInstruction::GetInputRecords; // Keep the const version visible.
2657 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2658 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002659 }
2660
Artem Serovcced8ba2017-07-19 18:18:09 +01002661 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002662 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002663
Vladimir Markof9f64412015-09-02 14:05:49 +01002664 private:
2665 friend class SsaBuilder;
2666};
2667
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002668// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2669// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002670class HReturnVoid FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002671 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002672 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002673 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302674 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002675
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002676 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002677
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002678 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002679
Artem Serovcced8ba2017-07-19 18:18:09 +01002680 protected:
2681 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002682};
2683
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002684// Represents dex's RETURN opcodes. A HReturn is a control flow
2685// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002686class HReturn FINAL : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002687 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002688 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002689 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002690 SetRawInputAt(0, value);
2691 }
2692
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002693 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002694
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002695 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002696
Artem Serovcced8ba2017-07-19 18:18:09 +01002697 protected:
2698 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002699};
2700
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002701class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002702 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002703 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002704 uint32_t reg_number,
2705 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002707 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002708 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302709 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002710 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002711 SideEffects::None(),
2712 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002713 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002714 number_of_inputs,
2715 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002716 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002717 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002718 // Phis are constructed live and marked dead if conflicting or unused.
2719 // Individual steps of SsaBuilder should assume that if a phi has been
2720 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2721 SetPackedFlag<kFlagIsLive>(true);
2722 SetPackedFlag<kFlagCanBeNull>(true);
2723 }
2724
Artem Serovcced8ba2017-07-19 18:18:09 +01002725 bool IsClonable() const OVERRIDE { return true; }
2726
David Brazdildee58d62016-04-07 09:54:26 +00002727 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728 static DataType::Type ToPhiType(DataType::Type type) {
2729 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002730 }
2731
2732 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002735 // Make sure that only valid type changes occur. The following are allowed:
2736 // (1) int -> float/ref (primitive type propagation),
2737 // (2) long -> double (primitive type propagation).
2738 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2740 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2741 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002742 SetPackedField<TypeField>(new_type);
2743 }
2744
2745 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2746 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2747
2748 uint32_t GetRegNumber() const { return reg_number_; }
2749
2750 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2751 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2752 bool IsDead() const { return !IsLive(); }
2753 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2754
Vladimir Markoe9004912016-06-16 16:50:52 +01002755 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002756 return other != nullptr
2757 && other->IsPhi()
2758 && other->AsPhi()->GetBlock() == GetBlock()
2759 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2760 }
2761
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002762 bool HasEquivalentPhi() const {
2763 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2764 return true;
2765 }
2766 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2767 return true;
2768 }
2769 return false;
2770 }
2771
David Brazdildee58d62016-04-07 09:54:26 +00002772 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2773 // An equivalent phi is a phi having the same dex register and type.
2774 // It assumes that phis with the same dex register are adjacent.
2775 HPhi* GetNextEquivalentPhiWithSameType() {
2776 HInstruction* next = GetNext();
2777 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2778 if (next->GetType() == GetType()) {
2779 return next->AsPhi();
2780 }
2781 next = next->GetNext();
2782 }
2783 return nullptr;
2784 }
2785
2786 DECLARE_INSTRUCTION(Phi);
2787
Artem Serovcced8ba2017-07-19 18:18:09 +01002788 protected:
2789 DEFAULT_COPY_CONSTRUCTOR(Phi);
2790
David Brazdildee58d62016-04-07 09:54:26 +00002791 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002792 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002793 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2794 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2795 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002796
David Brazdildee58d62016-04-07 09:54:26 +00002797 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002798};
2799
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002800// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002801// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002802// exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002803class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002804 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302805 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002806 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302807 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002808
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002809 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002810
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002811 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002812
Artem Serovcced8ba2017-07-19 18:18:09 +01002813 protected:
2814 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002815};
2816
2817// Jumps from one block to another.
Vladimir Markobd785672018-05-03 17:09:09 +01002818class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002819 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302820 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002821 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302822 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002823
Artem Serovcced8ba2017-07-19 18:18:09 +01002824 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002825 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002826
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002827 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002828 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002829 }
2830
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002831 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002832
Artem Serovcced8ba2017-07-19 18:18:09 +01002833 protected:
2834 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002835};
2836
Roland Levillain9867bc72015-08-05 10:21:34 +01002837class HConstant : public HExpression<0> {
2838 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302839 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2840 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2841 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002842
2843 bool CanBeMoved() const OVERRIDE { return true; }
2844
Roland Levillain1a653882016-03-18 18:05:57 +00002845 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002846 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002847 // Is this constant 0 in the arithmetic sense?
2848 virtual bool IsArithmeticZero() const { return false; }
2849 // Is this constant a 0-bit pattern?
2850 virtual bool IsZeroBitPattern() const { return false; }
2851 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002852 virtual bool IsOne() const { return false; }
2853
David Brazdil77a48ae2015-09-15 12:34:04 +00002854 virtual uint64_t GetValueAsUint64() const = 0;
2855
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002856 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002857
Artem Serovcced8ba2017-07-19 18:18:09 +01002858 protected:
2859 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002860};
2861
Vladimir Markofcb503c2016-05-18 12:48:17 +01002862class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002863 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002864 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002865 return true;
2866 }
2867
David Brazdil77a48ae2015-09-15 12:34:04 +00002868 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2869
Roland Levillain9867bc72015-08-05 10:21:34 +01002870 size_t ComputeHashCode() const OVERRIDE { return 0; }
2871
Roland Levillain1a653882016-03-18 18:05:57 +00002872 // The null constant representation is a 0-bit pattern.
2873 virtual bool IsZeroBitPattern() const { return true; }
2874
Roland Levillain9867bc72015-08-05 10:21:34 +01002875 DECLARE_INSTRUCTION(NullConstant);
2876
Artem Serovcced8ba2017-07-19 18:18:09 +01002877 protected:
2878 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2879
Roland Levillain9867bc72015-08-05 10:21:34 +01002880 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002881 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302882 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2883 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002884
2885 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002886};
2887
2888// Constants of the type int. Those can be from Dex instructions, or
2889// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002890class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002891 public:
2892 int32_t GetValue() const { return value_; }
2893
David Brazdil9f389d42015-10-01 14:32:56 +01002894 uint64_t GetValueAsUint64() const OVERRIDE {
2895 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2896 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002897
Vladimir Marko372f10e2016-05-17 16:30:10 +01002898 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002899 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002900 return other->AsIntConstant()->value_ == value_;
2901 }
2902
2903 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2904
2905 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002906 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2907 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002908 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2909
Roland Levillain1a653882016-03-18 18:05:57 +00002910 // Integer constants are used to encode Boolean values as well,
2911 // where 1 means true and 0 means false.
2912 bool IsTrue() const { return GetValue() == 1; }
2913 bool IsFalse() const { return GetValue() == 0; }
2914
Roland Levillain9867bc72015-08-05 10:21:34 +01002915 DECLARE_INSTRUCTION(IntConstant);
2916
Artem Serovcced8ba2017-07-19 18:18:09 +01002917 protected:
2918 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2919
Roland Levillain9867bc72015-08-05 10:21:34 +01002920 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002921 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302922 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2923 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302925 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2926 value_(value ? 1 : 0) {
2927 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002928
2929 const int32_t value_;
2930
2931 friend class HGraph;
2932 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2933 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002934};
2935
Vladimir Markofcb503c2016-05-18 12:48:17 +01002936class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002937 public:
2938 int64_t GetValue() const { return value_; }
2939
David Brazdil77a48ae2015-09-15 12:34:04 +00002940 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2941
Vladimir Marko372f10e2016-05-17 16:30:10 +01002942 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002943 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002944 return other->AsLongConstant()->value_ == value_;
2945 }
2946
2947 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2948
2949 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002950 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2951 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002952 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2953
2954 DECLARE_INSTRUCTION(LongConstant);
2955
Artem Serovcced8ba2017-07-19 18:18:09 +01002956 protected:
2957 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2958
Roland Levillain9867bc72015-08-05 10:21:34 +01002959 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002960 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302961 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2962 value_(value) {
2963 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002964
2965 const int64_t value_;
2966
2967 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002968};
Dave Allison20dfc792014-06-16 20:44:29 -07002969
Vladimir Markofcb503c2016-05-18 12:48:17 +01002970class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002971 public:
2972 float GetValue() const { return value_; }
2973
2974 uint64_t GetValueAsUint64() const OVERRIDE {
2975 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2976 }
2977
Vladimir Marko372f10e2016-05-17 16:30:10 +01002978 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002979 DCHECK(other->IsFloatConstant()) << other->DebugName();
2980 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2981 }
2982
2983 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2984
2985 bool IsMinusOne() const OVERRIDE {
2986 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2987 }
Roland Levillain1a653882016-03-18 18:05:57 +00002988 bool IsArithmeticZero() const OVERRIDE {
2989 return std::fpclassify(value_) == FP_ZERO;
2990 }
2991 bool IsArithmeticPositiveZero() const {
2992 return IsArithmeticZero() && !std::signbit(value_);
2993 }
2994 bool IsArithmeticNegativeZero() const {
2995 return IsArithmeticZero() && std::signbit(value_);
2996 }
2997 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002998 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002999 }
3000 bool IsOne() const OVERRIDE {
3001 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3002 }
3003 bool IsNaN() const {
3004 return std::isnan(value_);
3005 }
3006
3007 DECLARE_INSTRUCTION(FloatConstant);
3008
Artem Serovcced8ba2017-07-19 18:18:09 +01003009 protected:
3010 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3011
Roland Levillain31dd3d62016-02-16 12:21:02 +00003012 private:
3013 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303014 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3015 value_(value) {
3016 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003017 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303018 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3019 value_(bit_cast<float, int32_t>(value)) {
3020 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003021
3022 const float value_;
3023
3024 // Only the SsaBuilder and HGraph can create floating-point constants.
3025 friend class SsaBuilder;
3026 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027};
3028
Vladimir Markofcb503c2016-05-18 12:48:17 +01003029class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003030 public:
3031 double GetValue() const { return value_; }
3032
3033 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3034
Vladimir Marko372f10e2016-05-17 16:30:10 +01003035 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3037 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3038 }
3039
3040 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3041
3042 bool IsMinusOne() const OVERRIDE {
3043 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3044 }
Roland Levillain1a653882016-03-18 18:05:57 +00003045 bool IsArithmeticZero() const OVERRIDE {
3046 return std::fpclassify(value_) == FP_ZERO;
3047 }
3048 bool IsArithmeticPositiveZero() const {
3049 return IsArithmeticZero() && !std::signbit(value_);
3050 }
3051 bool IsArithmeticNegativeZero() const {
3052 return IsArithmeticZero() && std::signbit(value_);
3053 }
3054 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003055 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003056 }
3057 bool IsOne() const OVERRIDE {
3058 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3059 }
3060 bool IsNaN() const {
3061 return std::isnan(value_);
3062 }
3063
3064 DECLARE_INSTRUCTION(DoubleConstant);
3065
Artem Serovcced8ba2017-07-19 18:18:09 +01003066 protected:
3067 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3068
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 private:
3070 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303071 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3072 value_(value) {
3073 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003074 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303075 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3076 value_(bit_cast<double, int64_t>(value)) {
3077 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078
3079 const double value_;
3080
3081 // Only the SsaBuilder and HGraph can create floating-point constants.
3082 friend class SsaBuilder;
3083 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003084};
3085
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003086// Conditional branch. A block ending with an HIf instruction must have
3087// two successors.
Vladimir Markobd785672018-05-03 17:09:09 +01003088class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003089 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003090 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003091 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003092 SetRawInputAt(0, input);
3093 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003094
Artem Serovcced8ba2017-07-19 18:18:09 +01003095 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003096 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003097
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003098 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003099 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003100 }
3101
3102 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003103 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003104 }
3105
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003106 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003107
Artem Serovcced8ba2017-07-19 18:18:09 +01003108 protected:
3109 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003110};
3111
David Brazdilfc6a86a2015-06-26 10:33:45 +00003112
3113// Abstract instruction which marks the beginning and/or end of a try block and
3114// links it to the respective exception handlers. Behaves the same as a Goto in
3115// non-exceptional control flow.
3116// Normal-flow successor is stored at index zero, exception handlers under
3117// higher indices in no particular order.
Vladimir Markobd785672018-05-03 17:09:09 +01003118class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003119 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003120 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003121 kEntry,
3122 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003123 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003124 };
3125
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003126 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003127 : HExpression(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003128 SetPackedField<BoundaryKindField>(kind);
3129 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003130
3131 bool IsControlFlow() const OVERRIDE { return true; }
3132
3133 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003134 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003135
David Brazdild26a4112015-11-10 11:07:31 +00003136 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3137 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3138 }
3139
David Brazdilfc6a86a2015-06-26 10:33:45 +00003140 // Returns whether `handler` is among its exception handlers (non-zero index
3141 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003142 bool HasExceptionHandler(const HBasicBlock& handler) const {
3143 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003144 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003145 }
3146
3147 // If not present already, adds `handler` to its block's list of exception
3148 // handlers.
3149 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003150 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003151 GetBlock()->AddSuccessor(handler);
3152 }
3153 }
3154
Vladimir Markoa1de9182016-02-25 11:37:38 +00003155 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3156 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003157
David Brazdilffee3d32015-07-06 11:48:53 +01003158 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3159
David Brazdilfc6a86a2015-06-26 10:33:45 +00003160 DECLARE_INSTRUCTION(TryBoundary);
3161
Artem Serovcced8ba2017-07-19 18:18:09 +01003162 protected:
3163 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3164
David Brazdilfc6a86a2015-06-26 10:33:45 +00003165 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003166 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3167 static constexpr size_t kFieldBoundaryKindSize =
3168 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3169 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3170 kFieldBoundaryKind + kFieldBoundaryKindSize;
3171 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3172 "Too many packed fields.");
3173 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003174};
3175
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003176// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003177class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003178 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003179 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3180 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003181 HDeoptimize(ArenaAllocator* allocator,
3182 HInstruction* cond,
3183 DeoptimizationKind kind,
3184 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003185 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303186 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003187 SideEffects::All(),
3188 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003189 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003190 /* number_of_inputs */ 1,
3191 kArenaAllocMisc) {
3192 SetPackedFlag<kFieldCanBeMoved>(false);
3193 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003194 SetRawInputAt(0, cond);
3195 }
3196
Artem Serovcced8ba2017-07-19 18:18:09 +01003197 bool IsClonable() const OVERRIDE { return true; }
3198
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003199 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3200 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3201 // instead of `guard`.
3202 // We set CanTriggerGC to prevent any intermediate address to be live
3203 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003204 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003205 HInstruction* cond,
3206 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003207 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003208 uint32_t dex_pc)
3209 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303210 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003211 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003212 SideEffects::CanTriggerGC(),
3213 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003214 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003215 /* number_of_inputs */ 2,
3216 kArenaAllocMisc) {
3217 SetPackedFlag<kFieldCanBeMoved>(true);
3218 SetPackedField<DeoptimizeKindField>(kind);
3219 SetRawInputAt(0, cond);
3220 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003221 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003222
3223 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3224
3225 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3226 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3227 }
3228
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003229 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003230
Aart Bik75ff2c92018-04-21 01:28:11 +00003231 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003232
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003233 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003234
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003235 bool GuardsAnInput() const {
3236 return InputCount() == 2;
3237 }
3238
3239 HInstruction* GuardedInput() const {
3240 DCHECK(GuardsAnInput());
3241 return InputAt(1);
3242 }
3243
3244 void RemoveGuard() {
3245 RemoveInputAt(1);
3246 }
3247
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003248 DECLARE_INSTRUCTION(Deoptimize);
3249
Artem Serovcced8ba2017-07-19 18:18:09 +01003250 protected:
3251 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3252
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003253 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003254 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3255 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3256 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003257 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003258 static constexpr size_t kNumberOfDeoptimizePackedBits =
3259 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3260 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3261 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003262 using DeoptimizeKindField =
3263 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003264};
3265
Mingyao Yang063fc772016-08-02 11:02:54 -07003266// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3267// The compiled code checks this flag value in a guard before devirtualized call and
3268// if it's true, starts to do deoptimization.
3269// It has a 4-byte slot on stack.
3270// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003271class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003272 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003273 // CHA guards are only optimized in a separate pass and it has no side effects
3274 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003275 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303276 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003277 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303278 SideEffects::None(),
3279 dex_pc,
3280 allocator,
3281 0,
3282 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003283 }
3284
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003285 // We do all CHA guard elimination/motion in a single pass, after which there is no
3286 // further guard elimination/motion since a guard might have been used for justification
3287 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3288 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003289 bool CanBeMoved() const OVERRIDE { return false; }
3290
3291 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3292
Artem Serovcced8ba2017-07-19 18:18:09 +01003293 protected:
3294 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003295};
3296
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003297// Represents the ArtMethod that was passed as a first argument to
3298// the method. It is used by instructions that depend on it, like
3299// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003300class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003301 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003302 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303303 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3304 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003305
3306 DECLARE_INSTRUCTION(CurrentMethod);
3307
Artem Serovcced8ba2017-07-19 18:18:09 +01003308 protected:
3309 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003310};
3311
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003312// Fetches an ArtMethod from the virtual table or the interface method table
3313// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003314class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003315 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003316 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003317 kVTable,
3318 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003319 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003320 };
3321 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003322 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003323 TableKind kind,
3324 size_t index,
3325 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303326 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003327 index_(index) {
3328 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003329 SetRawInputAt(0, cls);
3330 }
3331
Artem Serovcced8ba2017-07-19 18:18:09 +01003332 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003333 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003334 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003335 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003336 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003337 }
3338
Vladimir Markoa1de9182016-02-25 11:37:38 +00003339 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003340 size_t GetIndex() const { return index_; }
3341
3342 DECLARE_INSTRUCTION(ClassTableGet);
3343
Artem Serovcced8ba2017-07-19 18:18:09 +01003344 protected:
3345 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3346
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003347 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003348 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003349 static constexpr size_t kFieldTableKindSize =
3350 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3351 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3352 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3353 "Too many packed fields.");
3354 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3355
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003356 // The index of the ArtMethod in the table.
3357 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003358};
3359
Mark Mendellfe57faa2015-09-18 09:26:15 -04003360// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3361// have one successor for each entry in the switch table, and the final successor
3362// will be the block containing the next Dex opcode.
Vladimir Markobd785672018-05-03 17:09:09 +01003363class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003364 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003365 HPackedSwitch(int32_t start_value,
3366 uint32_t num_entries,
3367 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003368 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003369 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003370 start_value_(start_value),
3371 num_entries_(num_entries) {
3372 SetRawInputAt(0, input);
3373 }
3374
Artem Serovcced8ba2017-07-19 18:18:09 +01003375 bool IsClonable() const OVERRIDE { return true; }
3376
Mark Mendellfe57faa2015-09-18 09:26:15 -04003377 bool IsControlFlow() const OVERRIDE { return true; }
3378
3379 int32_t GetStartValue() const { return start_value_; }
3380
Vladimir Marko211c2112015-09-24 16:52:33 +01003381 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003382
3383 HBasicBlock* GetDefaultBlock() const {
3384 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003385 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003386 }
3387 DECLARE_INSTRUCTION(PackedSwitch);
3388
Artem Serovcced8ba2017-07-19 18:18:09 +01003389 protected:
3390 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3391
Mark Mendellfe57faa2015-09-18 09:26:15 -04003392 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003393 const int32_t start_value_;
3394 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003395};
3396
Roland Levillain88cb1752014-10-20 16:36:47 +01003397class HUnaryOperation : public HExpression<1> {
3398 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303399 HUnaryOperation(InstructionKind kind,
3400 DataType::Type result_type,
3401 HInstruction* input,
3402 uint32_t dex_pc = kNoDexPc)
3403 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003404 SetRawInputAt(0, input);
3405 }
3406
Artem Serovcced8ba2017-07-19 18:18:09 +01003407 // All of the UnaryOperation instructions are clonable.
3408 bool IsClonable() const OVERRIDE { return true; }
3409
Roland Levillain88cb1752014-10-20 16:36:47 +01003410 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003411 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003412
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003413 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003414 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003415 return true;
3416 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003417
Roland Levillain31dd3d62016-02-16 12:21:02 +00003418 // Try to statically evaluate `this` and return a HConstant
3419 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003420 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003421 HConstant* TryStaticEvaluation() const;
3422
3423 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003424 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3425 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003426 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3427 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003428
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003429 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003430
Artem Serovcced8ba2017-07-19 18:18:09 +01003431 protected:
3432 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003433};
3434
Dave Allison20dfc792014-06-16 20:44:29 -07003435class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003436 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303437 HBinaryOperation(InstructionKind kind,
3438 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003439 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003440 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003441 SideEffects side_effects = SideEffects::None(),
3442 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303443 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003444 SetRawInputAt(0, left);
3445 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003446 }
3447
Artem Serovcced8ba2017-07-19 18:18:09 +01003448 // All of the BinaryOperation instructions are clonable.
3449 bool IsClonable() const OVERRIDE { return true; }
3450
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003451 HInstruction* GetLeft() const { return InputAt(0); }
3452 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003453 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003454
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003455 virtual bool IsCommutative() const { return false; }
3456
3457 // Put constant on the right.
3458 // Returns whether order is changed.
3459 bool OrderInputsWithConstantOnTheRight() {
3460 HInstruction* left = InputAt(0);
3461 HInstruction* right = InputAt(1);
3462 if (left->IsConstant() && !right->IsConstant()) {
3463 ReplaceInput(right, 0);
3464 ReplaceInput(left, 1);
3465 return true;
3466 }
3467 return false;
3468 }
3469
3470 // Order inputs by instruction id, but favor constant on the right side.
3471 // This helps GVN for commutative ops.
3472 void OrderInputs() {
3473 DCHECK(IsCommutative());
3474 HInstruction* left = InputAt(0);
3475 HInstruction* right = InputAt(1);
3476 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3477 return;
3478 }
3479 if (OrderInputsWithConstantOnTheRight()) {
3480 return;
3481 }
3482 // Order according to instruction id.
3483 if (left->GetId() > right->GetId()) {
3484 ReplaceInput(right, 0);
3485 ReplaceInput(left, 1);
3486 }
3487 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003489 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003490 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003491 return true;
3492 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003493
Roland Levillain31dd3d62016-02-16 12:21:02 +00003494 // Try to statically evaluate `this` and return a HConstant
3495 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003496 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003497 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003498
3499 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3501 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003502 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3503 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003505 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3506 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003507 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3508 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003509 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3510 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003511 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003512 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3513 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003514
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003515 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003516 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003517 HConstant* GetConstantRight() const;
3518
3519 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003520 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003521 HInstruction* GetLeastConstantLeft() const;
3522
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003523 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003524
Artem Serovcced8ba2017-07-19 18:18:09 +01003525 protected:
3526 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003527};
3528
Mark Mendellc4701932015-04-10 13:18:51 -04003529// The comparison bias applies for floating point operations and indicates how NaN
3530// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003531enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003532 kNoBias, // bias is not applicable (i.e. for long operation)
3533 kGtBias, // return 1 for NaN comparisons
3534 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003535 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003536};
3537
Roland Levillain31dd3d62016-02-16 12:21:02 +00003538std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3539
Dave Allison20dfc792014-06-16 20:44:29 -07003540class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003541 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303542 HCondition(InstructionKind kind,
3543 HInstruction* first,
3544 HInstruction* second,
3545 uint32_t dex_pc = kNoDexPc)
3546 : HBinaryOperation(kind,
3547 DataType::Type::kBool,
3548 first,
3549 second,
3550 SideEffects::None(),
3551 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003552 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3553 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003554
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003555 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003556 // `instruction`, and disregard moves in between.
3557 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003558
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003559 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003560
3561 virtual IfCondition GetCondition() const = 0;
3562
Mark Mendellc4701932015-04-10 13:18:51 -04003563 virtual IfCondition GetOppositeCondition() const = 0;
3564
Vladimir Markoa1de9182016-02-25 11:37:38 +00003565 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003566 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3567
Vladimir Markoa1de9182016-02-25 11:37:38 +00003568 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3569 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003570
Vladimir Marko372f10e2016-05-17 16:30:10 +01003571 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003572 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003573 }
3574
Roland Levillain4fa13f62015-07-06 18:11:54 +01003575 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003576 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003577 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003578 if (if_cond == kCondNE) {
3579 return true;
3580 } else if (if_cond == kCondEQ) {
3581 return false;
3582 }
3583 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003584 }
3585
3586 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003587 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003588 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003589 if (if_cond == kCondEQ) {
3590 return true;
3591 } else if (if_cond == kCondNE) {
3592 return false;
3593 }
3594 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003595 }
3596
Roland Levillain31dd3d62016-02-16 12:21:02 +00003597 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003598 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003599 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003600 static constexpr size_t kFieldComparisonBiasSize =
3601 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3602 static constexpr size_t kNumberOfConditionPackedBits =
3603 kFieldComparisonBias + kFieldComparisonBiasSize;
3604 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3605 using ComparisonBiasField =
3606 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3607
Roland Levillain31dd3d62016-02-16 12:21:02 +00003608 template <typename T>
3609 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3610
3611 template <typename T>
3612 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003613 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003614 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3615 // Handle the bias.
3616 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3617 }
3618
3619 // Return an integer constant containing the result of a condition evaluated at compile time.
3620 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3621 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3622 }
3623
Artem Serovcced8ba2017-07-19 18:18:09 +01003624 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003625};
3626
3627// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003628class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003629 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003630 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303631 : HCondition(kEqual, first, second, dex_pc) {
3632 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003633
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003634 bool IsCommutative() const OVERRIDE { return true; }
3635
Vladimir Marko9e23df52015-11-10 17:14:35 +00003636 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3637 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003638 return MakeConstantCondition(true, GetDexPc());
3639 }
3640 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3641 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3642 }
3643 // In the following Evaluate methods, a HCompare instruction has
3644 // been merged into this HEqual instruction; evaluate it as
3645 // `Compare(x, y) == 0`.
3646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3647 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3648 GetDexPc());
3649 }
3650 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3651 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3652 }
3653 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3654 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003655 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003656
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003657 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003658
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003659 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003660 return kCondEQ;
3661 }
3662
Mark Mendellc4701932015-04-10 13:18:51 -04003663 IfCondition GetOppositeCondition() const OVERRIDE {
3664 return kCondNE;
3665 }
3666
Artem Serovcced8ba2017-07-19 18:18:09 +01003667 protected:
3668 DEFAULT_COPY_CONSTRUCTOR(Equal);
3669
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003670 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003671 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003672};
3673
Vladimir Markofcb503c2016-05-18 12:48:17 +01003674class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003675 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303676 HNotEqual(HInstruction* first, HInstruction* second,
3677 uint32_t dex_pc = kNoDexPc)
3678 : HCondition(kNotEqual, first, second, dex_pc) {
3679 }
Dave Allison20dfc792014-06-16 20:44:29 -07003680
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003681 bool IsCommutative() const OVERRIDE { return true; }
3682
Vladimir Marko9e23df52015-11-10 17:14:35 +00003683 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3684 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003685 return MakeConstantCondition(false, GetDexPc());
3686 }
3687 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3688 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3689 }
3690 // In the following Evaluate methods, a HCompare instruction has
3691 // been merged into this HNotEqual instruction; evaluate it as
3692 // `Compare(x, y) != 0`.
3693 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3694 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3695 }
3696 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3697 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3698 }
3699 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3700 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003701 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003702
Dave Allison20dfc792014-06-16 20:44:29 -07003703 DECLARE_INSTRUCTION(NotEqual);
3704
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003705 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003706 return kCondNE;
3707 }
3708
Mark Mendellc4701932015-04-10 13:18:51 -04003709 IfCondition GetOppositeCondition() const OVERRIDE {
3710 return kCondEQ;
3711 }
3712
Artem Serovcced8ba2017-07-19 18:18:09 +01003713 protected:
3714 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3715
Dave Allison20dfc792014-06-16 20:44:29 -07003716 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003717 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003718};
3719
Vladimir Markofcb503c2016-05-18 12:48:17 +01003720class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003721 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303722 HLessThan(HInstruction* first, HInstruction* second,
3723 uint32_t dex_pc = kNoDexPc)
3724 : HCondition(kLessThan, first, second, dex_pc) {
3725 }
Dave Allison20dfc792014-06-16 20:44:29 -07003726
Roland Levillain9867bc72015-08-05 10:21:34 +01003727 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003729 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003730 // In the following Evaluate methods, a HCompare instruction has
3731 // been merged into this HLessThan instruction; evaluate it as
3732 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003733 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003734 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3735 }
3736 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3737 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3738 }
3739 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3740 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003741 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003742
Dave Allison20dfc792014-06-16 20:44:29 -07003743 DECLARE_INSTRUCTION(LessThan);
3744
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003745 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003746 return kCondLT;
3747 }
3748
Mark Mendellc4701932015-04-10 13:18:51 -04003749 IfCondition GetOppositeCondition() const OVERRIDE {
3750 return kCondGE;
3751 }
3752
Artem Serovcced8ba2017-07-19 18:18:09 +01003753 protected:
3754 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3755
Dave Allison20dfc792014-06-16 20:44:29 -07003756 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003757 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003758};
3759
Vladimir Markofcb503c2016-05-18 12:48:17 +01003760class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003761 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303762 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3763 uint32_t dex_pc = kNoDexPc)
3764 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3765 }
Dave Allison20dfc792014-06-16 20:44:29 -07003766
Roland Levillain9867bc72015-08-05 10:21:34 +01003767 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003768 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003769 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003770 // In the following Evaluate methods, a HCompare instruction has
3771 // been merged into this HLessThanOrEqual instruction; evaluate it as
3772 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003773 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003774 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3775 }
3776 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3777 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3778 }
3779 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3780 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003781 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003782
Dave Allison20dfc792014-06-16 20:44:29 -07003783 DECLARE_INSTRUCTION(LessThanOrEqual);
3784
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003785 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003786 return kCondLE;
3787 }
3788
Mark Mendellc4701932015-04-10 13:18:51 -04003789 IfCondition GetOppositeCondition() const OVERRIDE {
3790 return kCondGT;
3791 }
3792
Artem Serovcced8ba2017-07-19 18:18:09 +01003793 protected:
3794 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3795
Dave Allison20dfc792014-06-16 20:44:29 -07003796 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003797 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003798};
3799
Vladimir Markofcb503c2016-05-18 12:48:17 +01003800class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003802 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303803 : HCondition(kGreaterThan, first, second, dex_pc) {
3804 }
Dave Allison20dfc792014-06-16 20:44:29 -07003805
Roland Levillain9867bc72015-08-05 10:21:34 +01003806 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003807 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003808 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003809 // In the following Evaluate methods, a HCompare instruction has
3810 // been merged into this HGreaterThan instruction; evaluate it as
3811 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003812 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003813 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3814 }
3815 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3816 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3817 }
3818 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3819 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003820 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003821
Dave Allison20dfc792014-06-16 20:44:29 -07003822 DECLARE_INSTRUCTION(GreaterThan);
3823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003824 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003825 return kCondGT;
3826 }
3827
Mark Mendellc4701932015-04-10 13:18:51 -04003828 IfCondition GetOppositeCondition() const OVERRIDE {
3829 return kCondLE;
3830 }
3831
Artem Serovcced8ba2017-07-19 18:18:09 +01003832 protected:
3833 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3834
Dave Allison20dfc792014-06-16 20:44:29 -07003835 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003836 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003837};
3838
Vladimir Markofcb503c2016-05-18 12:48:17 +01003839class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003840 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003841 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303842 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3843 }
Dave Allison20dfc792014-06-16 20:44:29 -07003844
Roland Levillain9867bc72015-08-05 10:21:34 +01003845 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003846 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003847 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003848 // In the following Evaluate methods, a HCompare instruction has
3849 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3850 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003851 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003852 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3853 }
3854 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3855 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3856 }
3857 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3858 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003859 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003860
Dave Allison20dfc792014-06-16 20:44:29 -07003861 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3862
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003863 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003864 return kCondGE;
3865 }
3866
Mark Mendellc4701932015-04-10 13:18:51 -04003867 IfCondition GetOppositeCondition() const OVERRIDE {
3868 return kCondLT;
3869 }
3870
Artem Serovcced8ba2017-07-19 18:18:09 +01003871 protected:
3872 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3873
Dave Allison20dfc792014-06-16 20:44:29 -07003874 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003875 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003876};
3877
Vladimir Markofcb503c2016-05-18 12:48:17 +01003878class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003879 public:
3880 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303881 : HCondition(kBelow, first, second, dex_pc) {
3882 }
Aart Bike9f37602015-10-09 11:15:55 -07003883
3884 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003885 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003886 }
3887 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003888 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3889 }
3890 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3891 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3892 LOG(FATAL) << DebugName() << " is not defined for float values";
3893 UNREACHABLE();
3894 }
3895 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3896 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3897 LOG(FATAL) << DebugName() << " is not defined for double values";
3898 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003899 }
3900
3901 DECLARE_INSTRUCTION(Below);
3902
3903 IfCondition GetCondition() const OVERRIDE {
3904 return kCondB;
3905 }
3906
3907 IfCondition GetOppositeCondition() const OVERRIDE {
3908 return kCondAE;
3909 }
3910
Artem Serovcced8ba2017-07-19 18:18:09 +01003911 protected:
3912 DEFAULT_COPY_CONSTRUCTOR(Below);
3913
Aart Bike9f37602015-10-09 11:15:55 -07003914 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003915 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003916 return MakeUnsigned(x) < MakeUnsigned(y);
3917 }
Aart Bike9f37602015-10-09 11:15:55 -07003918};
3919
Vladimir Markofcb503c2016-05-18 12:48:17 +01003920class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003921 public:
3922 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303923 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3924 }
Aart Bike9f37602015-10-09 11:15:55 -07003925
3926 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003927 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003928 }
3929 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003930 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3931 }
3932 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3933 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3934 LOG(FATAL) << DebugName() << " is not defined for float values";
3935 UNREACHABLE();
3936 }
3937 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3938 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3939 LOG(FATAL) << DebugName() << " is not defined for double values";
3940 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003941 }
3942
3943 DECLARE_INSTRUCTION(BelowOrEqual);
3944
3945 IfCondition GetCondition() const OVERRIDE {
3946 return kCondBE;
3947 }
3948
3949 IfCondition GetOppositeCondition() const OVERRIDE {
3950 return kCondA;
3951 }
3952
Artem Serovcced8ba2017-07-19 18:18:09 +01003953 protected:
3954 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3955
Aart Bike9f37602015-10-09 11:15:55 -07003956 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003957 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003958 return MakeUnsigned(x) <= MakeUnsigned(y);
3959 }
Aart Bike9f37602015-10-09 11:15:55 -07003960};
3961
Vladimir Markofcb503c2016-05-18 12:48:17 +01003962class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003963 public:
3964 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303965 : HCondition(kAbove, first, second, dex_pc) {
3966 }
Aart Bike9f37602015-10-09 11:15:55 -07003967
3968 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003969 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003970 }
3971 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003972 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3973 }
3974 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3975 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3976 LOG(FATAL) << DebugName() << " is not defined for float values";
3977 UNREACHABLE();
3978 }
3979 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3980 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3981 LOG(FATAL) << DebugName() << " is not defined for double values";
3982 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003983 }
3984
3985 DECLARE_INSTRUCTION(Above);
3986
3987 IfCondition GetCondition() const OVERRIDE {
3988 return kCondA;
3989 }
3990
3991 IfCondition GetOppositeCondition() const OVERRIDE {
3992 return kCondBE;
3993 }
3994
Artem Serovcced8ba2017-07-19 18:18:09 +01003995 protected:
3996 DEFAULT_COPY_CONSTRUCTOR(Above);
3997
Aart Bike9f37602015-10-09 11:15:55 -07003998 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003999 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004000 return MakeUnsigned(x) > MakeUnsigned(y);
4001 }
Aart Bike9f37602015-10-09 11:15:55 -07004002};
4003
Vladimir Markofcb503c2016-05-18 12:48:17 +01004004class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004005 public:
4006 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304007 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4008 }
Aart Bike9f37602015-10-09 11:15:55 -07004009
4010 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004011 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004012 }
4013 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004014 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4015 }
4016 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4017 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4018 LOG(FATAL) << DebugName() << " is not defined for float values";
4019 UNREACHABLE();
4020 }
4021 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4022 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4023 LOG(FATAL) << DebugName() << " is not defined for double values";
4024 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004025 }
4026
4027 DECLARE_INSTRUCTION(AboveOrEqual);
4028
4029 IfCondition GetCondition() const OVERRIDE {
4030 return kCondAE;
4031 }
4032
4033 IfCondition GetOppositeCondition() const OVERRIDE {
4034 return kCondB;
4035 }
4036
Artem Serovcced8ba2017-07-19 18:18:09 +01004037 protected:
4038 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4039
Aart Bike9f37602015-10-09 11:15:55 -07004040 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004041 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004042 return MakeUnsigned(x) >= MakeUnsigned(y);
4043 }
Aart Bike9f37602015-10-09 11:15:55 -07004044};
Dave Allison20dfc792014-06-16 20:44:29 -07004045
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004046// Instruction to check how two inputs compare to each other.
4047// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004048class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004049 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004050 // Note that `comparison_type` is the type of comparison performed
4051 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004052 // HCompare instruction (which is always DataType::Type::kInt).
4053 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004054 HInstruction* first,
4055 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004056 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004057 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304058 : HBinaryOperation(kCompare,
4059 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004060 first,
4061 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004062 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004063 dex_pc) {
4064 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004065 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4066 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004067 }
4068
Roland Levillain9867bc72015-08-05 10:21:34 +01004069 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004070 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4071
4072 template <typename T>
4073 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004074 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004075 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4076 // Handle the bias.
4077 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4078 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004079
Roland Levillain9867bc72015-08-05 10:21:34 +01004080 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004081 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4082 // reach this code path when processing a freshly built HIR
4083 // graph. However HCompare integer instructions can be synthesized
4084 // by the instruction simplifier to implement IntegerCompare and
4085 // IntegerSignum intrinsics, so we have to handle this case.
4086 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004087 }
4088 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004089 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4090 }
4091 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4092 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4093 }
4094 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4095 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004096 }
4097
Vladimir Marko372f10e2016-05-17 16:30:10 +01004098 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004099 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004100 }
4101
Vladimir Markoa1de9182016-02-25 11:37:38 +00004102 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004103
Roland Levillain31dd3d62016-02-16 12:21:02 +00004104 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004105 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004106 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004107 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004108 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004109 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004110
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004111 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004112 // Comparisons do not require a runtime call in any back end.
4113 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004114 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004115
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004116 DECLARE_INSTRUCTION(Compare);
4117
Roland Levillain31dd3d62016-02-16 12:21:02 +00004118 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004119 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004120 static constexpr size_t kFieldComparisonBiasSize =
4121 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4122 static constexpr size_t kNumberOfComparePackedBits =
4123 kFieldComparisonBias + kFieldComparisonBiasSize;
4124 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4125 using ComparisonBiasField =
4126 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4127
Roland Levillain31dd3d62016-02-16 12:21:02 +00004128 // Return an integer constant containing the result of a comparison evaluated at compile time.
4129 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4130 DCHECK(value == -1 || value == 0 || value == 1) << value;
4131 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4132 }
4133
Artem Serovcced8ba2017-07-19 18:18:09 +01004134 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004135};
4136
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004137class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004138 public:
4139 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004140 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004141 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004142 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004143 bool finalizable,
4144 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304145 : HExpression(kNewInstance,
4146 DataType::Type::kReference,
4147 SideEffects::CanTriggerGC(),
4148 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004149 type_index_(type_index),
4150 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004151 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004152 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004153 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004154 }
4155
Artem Serovcced8ba2017-07-19 18:18:09 +01004156 bool IsClonable() const OVERRIDE { return true; }
4157
Andreas Gampea5b09a62016-11-17 15:21:22 -08004158 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004159 const DexFile& GetDexFile() const { return dex_file_; }
4160
4161 // Calls runtime so needs an environment.
4162 bool NeedsEnvironment() const OVERRIDE { return true; }
4163
Mingyao Yang062157f2016-03-02 10:15:36 -08004164 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4165 bool CanThrow() const OVERRIDE { return true; }
4166
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004167 bool NeedsChecks() const {
4168 return entrypoint_ == kQuickAllocObjectWithChecks;
4169 }
David Brazdil6de19382016-01-08 17:37:10 +00004170
Vladimir Markoa1de9182016-02-25 11:37:38 +00004171 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004172
4173 bool CanBeNull() const OVERRIDE { return false; }
4174
4175 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4176
4177 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4178 entrypoint_ = entrypoint;
4179 }
4180
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004181 HLoadClass* GetLoadClass() const {
4182 HInstruction* input = InputAt(0);
4183 if (input->IsClinitCheck()) {
4184 input = input->InputAt(0);
4185 }
4186 DCHECK(input->IsLoadClass());
4187 return input->AsLoadClass();
4188 }
4189
David Brazdil6de19382016-01-08 17:37:10 +00004190 bool IsStringAlloc() const;
4191
4192 DECLARE_INSTRUCTION(NewInstance);
4193
Artem Serovcced8ba2017-07-19 18:18:09 +01004194 protected:
4195 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4196
David Brazdil6de19382016-01-08 17:37:10 +00004197 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004198 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004199 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4200 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4201 "Too many packed fields.");
4202
Andreas Gampea5b09a62016-11-17 15:21:22 -08004203 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004204 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004205 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004206};
4207
Agi Csaki05f20562015-08-19 14:58:14 -07004208enum IntrinsicNeedsEnvironmentOrCache {
4209 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4210 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004211};
4212
Aart Bik5d75afe2015-12-14 11:57:01 -08004213enum IntrinsicSideEffects {
4214 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4215 kReadSideEffects, // Intrinsic may read heap memory.
4216 kWriteSideEffects, // Intrinsic may write heap memory.
4217 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4218};
4219
4220enum IntrinsicExceptions {
4221 kNoThrow, // Intrinsic does not throw any exceptions.
4222 kCanThrow // Intrinsic may throw exceptions.
4223};
4224
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004225class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004226 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004227 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004228
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004229 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004230 SetRawInputAt(index, argument);
4231 }
4232
Roland Levillain3e3d7332015-04-28 11:00:54 +01004233 // Return the number of arguments. This number can be lower than
4234 // the number of inputs returned by InputCount(), as some invoke
4235 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4236 // inputs at the end of their list of inputs.
4237 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4238
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004239 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4240
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004241 InvokeType GetInvokeType() const {
4242 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004243 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004244
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004245 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004246 return intrinsic_;
4247 }
4248
Aart Bik5d75afe2015-12-14 11:57:01 -08004249 void SetIntrinsic(Intrinsics intrinsic,
4250 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4251 IntrinsicSideEffects side_effects,
4252 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004253
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004254 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004255 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004256 }
4257
Aart Bikff7d89c2016-11-07 08:49:28 -08004258 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4259
Vladimir Markoa1de9182016-02-25 11:37:38 +00004260 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004261
Aart Bika8b8e9b2018-01-09 11:01:02 -08004262 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4263
4264 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4265
Aart Bik71bf7b42016-11-16 10:17:46 -08004266 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004267
Vladimir Marko372f10e2016-05-17 16:30:10 +01004268 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004269 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4270 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004271
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004272 uint32_t* GetIntrinsicOptimizations() {
4273 return &intrinsic_optimizations_;
4274 }
4275
4276 const uint32_t* GetIntrinsicOptimizations() const {
4277 return &intrinsic_optimizations_;
4278 }
4279
4280 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4281
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004282 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004283 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004284
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004285 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004286
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004287 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004288 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4289 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004290 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004291 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004292 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4293 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004294 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004295 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004296
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304297 HInvoke(InstructionKind kind,
4298 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004299 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004300 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004301 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004302 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004303 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004304 ArtMethod* resolved_method,
4305 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004306 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304307 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004308 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004309 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4310 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004311 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004312 number_of_arguments + number_of_other_inputs,
4313 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004314 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004315 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004316 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004317 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004318 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004319 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004320 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004321 }
4322
Artem Serovcced8ba2017-07-19 18:18:09 +01004323 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4324
Roland Levillain3e3d7332015-04-28 11:00:54 +01004325 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004326 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004327 const uint32_t dex_method_index_;
4328 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004329
4330 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4331 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004332};
4333
Vladimir Markofcb503c2016-05-18 12:48:17 +01004334class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004335 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004336 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004337 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004338 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004339 uint32_t dex_pc,
4340 uint32_t dex_method_index,
4341 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304342 : HInvoke(kInvokeUnresolved,
4343 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004344 number_of_arguments,
4345 0u /* number_of_other_inputs */,
4346 return_type,
4347 dex_pc,
4348 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004349 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004350 invoke_type) {
4351 }
4352
Artem Serovcced8ba2017-07-19 18:18:09 +01004353 bool IsClonable() const OVERRIDE { return true; }
4354
Calin Juravle175dc732015-08-25 15:42:32 +01004355 DECLARE_INSTRUCTION(InvokeUnresolved);
4356
Artem Serovcced8ba2017-07-19 18:18:09 +01004357 protected:
4358 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004359};
4360
Orion Hodsonac141392017-01-13 11:53:47 +00004361class HInvokePolymorphic FINAL : public HInvoke {
4362 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004363 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004364 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004365 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004366 uint32_t dex_pc,
4367 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304368 : HInvoke(kInvokePolymorphic,
4369 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004370 number_of_arguments,
4371 0u /* number_of_other_inputs */,
4372 return_type,
4373 dex_pc,
4374 dex_method_index,
4375 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304376 kVirtual) {
4377 }
Orion Hodsonac141392017-01-13 11:53:47 +00004378
Artem Serovcced8ba2017-07-19 18:18:09 +01004379 bool IsClonable() const OVERRIDE { return true; }
4380
Orion Hodsonac141392017-01-13 11:53:47 +00004381 DECLARE_INSTRUCTION(InvokePolymorphic);
4382
Artem Serovcced8ba2017-07-19 18:18:09 +01004383 protected:
4384 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004385};
4386
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004387class HInvokeCustom FINAL : public HInvoke {
4388 public:
4389 HInvokeCustom(ArenaAllocator* allocator,
4390 uint32_t number_of_arguments,
4391 uint32_t call_site_index,
4392 DataType::Type return_type,
4393 uint32_t dex_pc)
4394 : HInvoke(kInvokeCustom,
4395 allocator,
4396 number_of_arguments,
4397 /* number_of_other_inputs */ 0u,
4398 return_type,
4399 dex_pc,
4400 /* dex_method_index */ dex::kDexNoIndex,
4401 /* resolved_method */ nullptr,
4402 kStatic),
4403 call_site_index_(call_site_index) {
4404 }
4405
4406 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4407
4408 bool IsClonable() const OVERRIDE { return true; }
4409
4410 DECLARE_INSTRUCTION(InvokeCustom);
4411
4412 protected:
4413 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4414
4415 private:
4416 uint32_t call_site_index_;
4417};
4418
Vladimir Markofcb503c2016-05-18 12:48:17 +01004419class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004420 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004421 // Requirements of this method call regarding the class
4422 // initialization (clinit) check of its declaring class.
4423 enum class ClinitCheckRequirement {
4424 kNone, // Class already initialized.
4425 kExplicit, // Static call having explicit clinit check as last input.
4426 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004427 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004428 };
4429
Vladimir Marko58155012015-08-19 12:49:41 +00004430 // Determines how to load the target ArtMethod*.
4431 enum class MethodLoadKind {
4432 // Use a String init ArtMethod* loaded from Thread entrypoints.
4433 kStringInit,
4434
4435 // Use the method's own ArtMethod* loaded by the register allocator.
4436 kRecursive,
4437
Vladimir Marko65979462017-05-19 17:25:12 +01004438 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4439 // Used for boot image methods referenced by boot image code.
4440 kBootImageLinkTimePcRelative,
4441
Vladimir Markob066d432018-01-03 13:14:37 +00004442 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4443 // Used for app->boot calls with relocatable image.
4444 kBootImageRelRo,
4445
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004446 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004447 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004448 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004449
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004450 // Use ArtMethod* at a known address, embed the direct address in the code.
4451 // Used for for JIT-compiled calls.
4452 kJitDirectAddress,
4453
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004454 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4455 // used when other kinds are unimplemented on a particular architecture.
4456 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004457 };
4458
4459 // Determines the location of the code pointer.
4460 enum class CodePtrLocation {
4461 // Recursive call, use local PC-relative call instruction.
4462 kCallSelf,
4463
Vladimir Marko58155012015-08-19 12:49:41 +00004464 // Use code pointer from the ArtMethod*.
4465 // Used when we don't know the target code. This is also the last-resort-kind used when
4466 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4467 kCallArtMethod,
4468 };
4469
4470 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004471 MethodLoadKind method_load_kind;
4472 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004473 // The method load data holds
4474 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4475 // Note that there are multiple string init methods, each having its own offset.
4476 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004477 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004478 };
4479
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004480 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004481 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004482 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004483 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004484 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004485 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004486 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004487 InvokeType invoke_type,
4488 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004489 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304490 : HInvoke(kInvokeStaticOrDirect,
4491 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004492 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004493 // There is potentially one extra argument for the HCurrentMethod node, and
4494 // potentially one other if the clinit check is explicit, and potentially
4495 // one other if the method is a string factory.
4496 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004497 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004498 return_type,
4499 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004500 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004501 resolved_method,
4502 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004503 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004504 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004505 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4506 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004507
Artem Serovcced8ba2017-07-19 18:18:09 +01004508 bool IsClonable() const OVERRIDE { return true; }
4509
Vladimir Markodc151b22015-10-15 18:02:30 +01004510 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004511 bool had_current_method_input = HasCurrentMethodInput();
4512 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4513
4514 // Using the current method is the default and once we find a better
4515 // method load kind, we should not go back to using the current method.
4516 DCHECK(had_current_method_input || !needs_current_method_input);
4517
4518 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004519 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4520 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004521 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004522 dispatch_info_ = dispatch_info;
4523 }
4524
Aart Bik6daebeb2017-04-03 14:35:41 -07004525 DispatchInfo GetDispatchInfo() const {
4526 return dispatch_info_;
4527 }
4528
Vladimir Markoc53c0792015-11-19 15:48:33 +00004529 void AddSpecialInput(HInstruction* input) {
4530 // We allow only one special input.
4531 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4532 DCHECK(InputCount() == GetSpecialInputIndex() ||
4533 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4534 InsertInputAt(GetSpecialInputIndex(), input);
4535 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004536
Vladimir Marko372f10e2016-05-17 16:30:10 +01004537 using HInstruction::GetInputRecords; // Keep the const version visible.
4538 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4539 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4540 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4541 DCHECK(!input_records.empty());
4542 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4543 HInstruction* last_input = input_records.back().GetInstruction();
4544 // Note: `last_input` may be null during arguments setup.
4545 if (last_input != nullptr) {
4546 // `last_input` is the last input of a static invoke marked as having
4547 // an explicit clinit check. It must either be:
4548 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4549 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4550 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4551 }
4552 }
4553 return input_records;
4554 }
4555
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004556 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004557 // We access the method via the dex cache so we can't do an implicit null check.
4558 // TODO: for intrinsics we can generate implicit null checks.
4559 return false;
4560 }
4561
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004562 bool CanBeNull() const OVERRIDE {
Vladimir Markobd785672018-05-03 17:09:09 +01004563 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004564 }
4565
Vladimir Markoc53c0792015-11-19 15:48:33 +00004566 // Get the index of the special input, if any.
4567 //
David Brazdil6de19382016-01-08 17:37:10 +00004568 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4569 // method pointer; otherwise there may be one platform-specific special input,
4570 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004571 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004572 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004573
Vladimir Marko58155012015-08-19 12:49:41 +00004574 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4575 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4576 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004577 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004578 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004579 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004580 bool HasPcRelativeMethodLoadKind() const {
4581 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004582 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004583 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004584 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004585 bool HasCurrentMethodInput() const {
4586 // This function can be called only after the invoke has been fully initialized by the builder.
4587 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004588 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004589 return true;
4590 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004591 DCHECK(InputCount() == GetSpecialInputIndex() ||
4592 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004593 return false;
4594 }
4595 }
Vladimir Marko58155012015-08-19 12:49:41 +00004596
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004597 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004598 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004599 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004600 }
4601
4602 uint64_t GetMethodAddress() const {
4603 DCHECK(HasMethodAddress());
4604 return dispatch_info_.method_load_data;
4605 }
4606
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004607 const DexFile& GetDexFileForPcRelativeDexCache() const;
4608
Vladimir Markoa1de9182016-02-25 11:37:38 +00004609 ClinitCheckRequirement GetClinitCheckRequirement() const {
4610 return GetPackedField<ClinitCheckRequirementField>();
4611 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004612
Roland Levillain4c0eb422015-04-24 16:43:49 +01004613 // Is this instruction a call to a static method?
4614 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004615 return GetInvokeType() == kStatic;
4616 }
4617
4618 MethodReference GetTargetMethod() const {
4619 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004620 }
4621
Vladimir Markofbb184a2015-11-13 14:47:00 +00004622 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4623 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4624 // instruction; only relevant for static calls with explicit clinit check.
4625 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004626 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004627 size_t last_input_index = inputs_.size() - 1u;
4628 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004629 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004630 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004631 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004632 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004633 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004634 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004635 }
4636
4637 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004638 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004639 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004640 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004641 }
4642
4643 // Is this a call to a static method whose declaring class has an
4644 // implicit intialization check requirement?
4645 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004646 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004647 }
4648
Vladimir Markob554b5a2015-11-06 12:57:55 +00004649 // Does this method load kind need the current method as an input?
4650 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004651 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004652 }
4653
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004654 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004655
Artem Serovcced8ba2017-07-19 18:18:09 +01004656 protected:
4657 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4658
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004659 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004660 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004661 static constexpr size_t kFieldClinitCheckRequirementSize =
4662 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4663 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4664 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4665 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4666 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004667 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4668 kFieldClinitCheckRequirement,
4669 kFieldClinitCheckRequirementSize>;
4670
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004671 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004672 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004673 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004674};
Vladimir Markof64242a2015-12-01 14:58:23 +00004675std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004676std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004677
Vladimir Markofcb503c2016-05-18 12:48:17 +01004678class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004679 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004680 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004681 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004682 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004683 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004684 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004685 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004686 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304687 : HInvoke(kInvokeVirtual,
4688 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004689 number_of_arguments,
4690 0u,
4691 return_type,
4692 dex_pc,
4693 dex_method_index,
4694 resolved_method,
4695 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304696 vtable_index_(vtable_index) {
4697 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004698
Artem Serovcced8ba2017-07-19 18:18:09 +01004699 bool IsClonable() const OVERRIDE { return true; }
4700
Aart Bik71bf7b42016-11-16 10:17:46 -08004701 bool CanBeNull() const OVERRIDE {
4702 switch (GetIntrinsic()) {
4703 case Intrinsics::kThreadCurrentThread:
4704 case Intrinsics::kStringBufferAppend:
4705 case Intrinsics::kStringBufferToString:
4706 case Intrinsics::kStringBuilderAppend:
4707 case Intrinsics::kStringBuilderToString:
4708 return false;
4709 default:
4710 return HInvoke::CanBeNull();
4711 }
4712 }
4713
Calin Juravle641547a2015-04-21 22:08:51 +01004714 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004715 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004716 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004717 }
4718
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004719 uint32_t GetVTableIndex() const { return vtable_index_; }
4720
4721 DECLARE_INSTRUCTION(InvokeVirtual);
4722
Artem Serovcced8ba2017-07-19 18:18:09 +01004723 protected:
4724 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4725
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004726 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004727 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004728 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004729};
4730
Vladimir Markofcb503c2016-05-18 12:48:17 +01004731class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004732 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004733 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004734 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004735 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004736 uint32_t dex_pc,
4737 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004738 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004739 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304740 : HInvoke(kInvokeInterface,
4741 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004742 number_of_arguments,
4743 0u,
4744 return_type,
4745 dex_pc,
4746 dex_method_index,
4747 resolved_method,
4748 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304749 imt_index_(imt_index) {
4750 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004751
Artem Serovcced8ba2017-07-19 18:18:09 +01004752 bool IsClonable() const OVERRIDE { return true; }
4753
Calin Juravle641547a2015-04-21 22:08:51 +01004754 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004755 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004756 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004757 }
4758
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004759 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4760 // The assembly stub currently needs it.
4761 return true;
4762 }
4763
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004764 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004765
4766 DECLARE_INSTRUCTION(InvokeInterface);
4767
Artem Serovcced8ba2017-07-19 18:18:09 +01004768 protected:
4769 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4770
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004771 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004772 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004773 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004774};
4775
Vladimir Markofcb503c2016-05-18 12:48:17 +01004776class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004777 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004778 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304779 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004780 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004781 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004782
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004783 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004784
4785 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004786 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004787 }
4788 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004789 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004790 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004791 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4792 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4793 }
4794 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4795 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4796 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004797
Roland Levillain88cb1752014-10-20 16:36:47 +01004798 DECLARE_INSTRUCTION(Neg);
4799
Artem Serovcced8ba2017-07-19 18:18:09 +01004800 protected:
4801 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004802};
4803
Vladimir Markofcb503c2016-05-18 12:48:17 +01004804class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004805 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004806 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304807 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004808 SetRawInputAt(0, cls);
4809 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004810 }
4811
Artem Serovcced8ba2017-07-19 18:18:09 +01004812 bool IsClonable() const OVERRIDE { return true; }
4813
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004814 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004815 bool NeedsEnvironment() const OVERRIDE { return true; }
4816
Mingyao Yang0c365e62015-03-31 15:09:29 -07004817 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4818 bool CanThrow() const OVERRIDE { return true; }
4819
Calin Juravle10e244f2015-01-26 18:54:32 +00004820 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004821
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004822 HLoadClass* GetLoadClass() const {
4823 DCHECK(InputAt(0)->IsLoadClass());
4824 return InputAt(0)->AsLoadClass();
4825 }
4826
4827 HInstruction* GetLength() const {
4828 return InputAt(1);
4829 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004830
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004831 DECLARE_INSTRUCTION(NewArray);
4832
Artem Serovcced8ba2017-07-19 18:18:09 +01004833 protected:
4834 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004835};
4836
Vladimir Markofcb503c2016-05-18 12:48:17 +01004837class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004838 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004839 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004840 HInstruction* left,
4841 HInstruction* right,
4842 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304843 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4844 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004846 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004847
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004848 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004849
4850 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 return GetBlock()->GetGraph()->GetIntConstant(
4852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004853 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004854 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004855 return GetBlock()->GetGraph()->GetLongConstant(
4856 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004857 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004858 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4859 return GetBlock()->GetGraph()->GetFloatConstant(
4860 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4861 }
4862 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4863 return GetBlock()->GetGraph()->GetDoubleConstant(
4864 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4865 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004866
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004867 DECLARE_INSTRUCTION(Add);
4868
Artem Serovcced8ba2017-07-19 18:18:09 +01004869 protected:
4870 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004871};
4872
Vladimir Markofcb503c2016-05-18 12:48:17 +01004873class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004874 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004875 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004876 HInstruction* left,
4877 HInstruction* right,
4878 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304879 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4880 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004881
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004882 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004883
4884 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004885 return GetBlock()->GetGraph()->GetIntConstant(
4886 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004887 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004888 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004889 return GetBlock()->GetGraph()->GetLongConstant(
4890 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004891 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004892 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4893 return GetBlock()->GetGraph()->GetFloatConstant(
4894 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4895 }
4896 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4897 return GetBlock()->GetGraph()->GetDoubleConstant(
4898 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4899 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004900
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004901 DECLARE_INSTRUCTION(Sub);
4902
Artem Serovcced8ba2017-07-19 18:18:09 +01004903 protected:
4904 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004905};
4906
Vladimir Markofcb503c2016-05-18 12:48:17 +01004907class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004908 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004909 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004910 HInstruction* left,
4911 HInstruction* right,
4912 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304913 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4914 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004915
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004916 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004917
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004918 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004919
4920 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004921 return GetBlock()->GetGraph()->GetIntConstant(
4922 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004923 }
4924 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004925 return GetBlock()->GetGraph()->GetLongConstant(
4926 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004927 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004928 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4929 return GetBlock()->GetGraph()->GetFloatConstant(
4930 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4931 }
4932 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4933 return GetBlock()->GetGraph()->GetDoubleConstant(
4934 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4935 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004936
4937 DECLARE_INSTRUCTION(Mul);
4938
Artem Serovcced8ba2017-07-19 18:18:09 +01004939 protected:
4940 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004941};
4942
Vladimir Markofcb503c2016-05-18 12:48:17 +01004943class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004944 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004945 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004946 HInstruction* left,
4947 HInstruction* right,
4948 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304949 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4950 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004951
Roland Levillain9867bc72015-08-05 10:21:34 +01004952 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004953 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004954 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004955 // Our graph structure ensures we never have 0 for `y` during
4956 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004957 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004958 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004959 return (y == -1) ? -x : x / y;
4960 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004961
Roland Levillain31dd3d62016-02-16 12:21:02 +00004962 template <typename T>
4963 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004964 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004965 return x / y;
4966 }
4967
Roland Levillain9867bc72015-08-05 10:21:34 +01004968 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004969 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004970 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004971 }
4972 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004973 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004974 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4975 }
4976 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4977 return GetBlock()->GetGraph()->GetFloatConstant(
4978 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4979 }
4980 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4981 return GetBlock()->GetGraph()->GetDoubleConstant(
4982 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004983 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004984
4985 DECLARE_INSTRUCTION(Div);
4986
Artem Serovcced8ba2017-07-19 18:18:09 +01004987 protected:
4988 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004989};
4990
Vladimir Markofcb503c2016-05-18 12:48:17 +01004991class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004992 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004993 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 HInstruction* left,
4995 HInstruction* right,
4996 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304997 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4998 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004999
Roland Levillain9867bc72015-08-05 10:21:34 +01005000 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005001 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005002 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005003 // Our graph structure ensures we never have 0 for `y` during
5004 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005005 DCHECK_NE(y, 0);
5006 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5007 return (y == -1) ? 0 : x % y;
5008 }
5009
Roland Levillain31dd3d62016-02-16 12:21:02 +00005010 template <typename T>
5011 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005012 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005013 return std::fmod(x, y);
5014 }
5015
Roland Levillain9867bc72015-08-05 10:21:34 +01005016 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005017 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005018 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005019 }
5020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005021 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005022 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005023 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005024 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
5025 return GetBlock()->GetGraph()->GetFloatConstant(
5026 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5027 }
5028 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
5029 return GetBlock()->GetGraph()->GetDoubleConstant(
5030 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5031 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005032
5033 DECLARE_INSTRUCTION(Rem);
5034
Artem Serovcced8ba2017-07-19 18:18:09 +01005035 protected:
5036 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005037};
5038
Aart Bik1f8d51b2018-02-15 10:42:37 -08005039class HMin FINAL : public HBinaryOperation {
5040 public:
5041 HMin(DataType::Type result_type,
5042 HInstruction* left,
5043 HInstruction* right,
5044 uint32_t dex_pc)
5045 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5046
5047 bool IsCommutative() const OVERRIDE { return true; }
5048
5049 // Evaluation for integral values.
5050 template <typename T> static T ComputeIntegral(T x, T y) {
5051 return (x <= y) ? x : y;
5052 }
5053
5054 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5055 return GetBlock()->GetGraph()->GetIntConstant(
5056 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5057 }
5058 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5059 return GetBlock()->GetGraph()->GetLongConstant(
5060 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5061 }
5062 // TODO: Evaluation for floating-point values.
5063 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5064 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5065 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5066 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5067
5068 DECLARE_INSTRUCTION(Min);
5069
5070 protected:
5071 DEFAULT_COPY_CONSTRUCTOR(Min);
5072};
5073
5074class HMax FINAL : public HBinaryOperation {
5075 public:
5076 HMax(DataType::Type result_type,
5077 HInstruction* left,
5078 HInstruction* right,
5079 uint32_t dex_pc)
5080 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5081
5082 bool IsCommutative() const OVERRIDE { return true; }
5083
5084 // Evaluation for integral values.
5085 template <typename T> static T ComputeIntegral(T x, T y) {
5086 return (x >= y) ? x : y;
5087 }
5088
5089 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5090 return GetBlock()->GetGraph()->GetIntConstant(
5091 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5092 }
5093 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5094 return GetBlock()->GetGraph()->GetLongConstant(
5095 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5096 }
5097 // TODO: Evaluation for floating-point values.
5098 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5099 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5100 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5101 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5102
5103 DECLARE_INSTRUCTION(Max);
5104
5105 protected:
5106 DEFAULT_COPY_CONSTRUCTOR(Max);
5107};
5108
Aart Bik3dad3412018-02-28 12:01:46 -08005109class HAbs FINAL : public HUnaryOperation {
5110 public:
5111 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5112 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5113
5114 // Evaluation for integral values.
5115 template <typename T> static T ComputeIntegral(T x) {
5116 return x < 0 ? -x : x;
5117 }
5118
5119 // Evaluation for floating-point values.
5120 // Note, as a "quality of implementation", rather than pure "spec compliance",
5121 // we require that Math.abs() clears the sign bit (but changes nothing else)
5122 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5123 // http://b/30758343
5124 template <typename T, typename S> static T ComputeFP(T x) {
5125 S bits = bit_cast<S, T>(x);
5126 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5127 }
5128
5129 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5130 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5131 }
5132 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5133 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5134 }
5135 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5136 return GetBlock()->GetGraph()->GetFloatConstant(
5137 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5138 }
5139 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5140 return GetBlock()->GetGraph()->GetDoubleConstant(
5141 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5142 }
5143
5144 DECLARE_INSTRUCTION(Abs);
5145
5146 protected:
5147 DEFAULT_COPY_CONSTRUCTOR(Abs);
5148};
5149
Vladimir Markofcb503c2016-05-18 12:48:17 +01005150class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005151 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005152 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5153 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005154 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305155 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005156 SetRawInputAt(0, value);
5157 }
5158
Artem Serova6e26142018-06-19 14:55:17 +01005159 bool IsClonable() const OVERRIDE { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005160 bool CanBeMoved() const OVERRIDE { return true; }
5161
Vladimir Marko372f10e2016-05-17 16:30:10 +01005162 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005163 return true;
5164 }
5165
5166 bool NeedsEnvironment() const OVERRIDE { return true; }
5167 bool CanThrow() const OVERRIDE { return true; }
5168
Calin Juravled0d48522014-11-04 16:40:20 +00005169 DECLARE_INSTRUCTION(DivZeroCheck);
5170
Artem Serovcced8ba2017-07-19 18:18:09 +01005171 protected:
5172 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005173};
5174
Vladimir Markofcb503c2016-05-18 12:48:17 +01005175class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005176 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005177 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005178 HInstruction* value,
5179 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005180 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305181 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005182 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5183 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005184 }
5185
Roland Levillain5b5b9312016-03-22 14:57:31 +00005186 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005187 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005188 return value << (distance & max_shift_distance);
5189 }
5190
5191 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005192 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005193 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005194 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005195 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005196 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005197 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005198 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005199 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5200 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5201 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5202 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005203 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005204 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5205 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005206 LOG(FATAL) << DebugName() << " is not defined for float values";
5207 UNREACHABLE();
5208 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005209 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5210 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005211 LOG(FATAL) << DebugName() << " is not defined for double values";
5212 UNREACHABLE();
5213 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005214
5215 DECLARE_INSTRUCTION(Shl);
5216
Artem Serovcced8ba2017-07-19 18:18:09 +01005217 protected:
5218 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005219};
5220
Vladimir Markofcb503c2016-05-18 12:48:17 +01005221class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005222 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005223 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005224 HInstruction* value,
5225 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005226 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305227 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005228 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5229 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005230 }
5231
Roland Levillain5b5b9312016-03-22 14:57:31 +00005232 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005233 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005234 return value >> (distance & max_shift_distance);
5235 }
5236
5237 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005238 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005239 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005240 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005241 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005242 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005243 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005244 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005245 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5246 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5247 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5248 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005249 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005250 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5251 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005252 LOG(FATAL) << DebugName() << " is not defined for float values";
5253 UNREACHABLE();
5254 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005255 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5256 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005257 LOG(FATAL) << DebugName() << " is not defined for double values";
5258 UNREACHABLE();
5259 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005260
5261 DECLARE_INSTRUCTION(Shr);
5262
Artem Serovcced8ba2017-07-19 18:18:09 +01005263 protected:
5264 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005265};
5266
Vladimir Markofcb503c2016-05-18 12:48:17 +01005267class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005268 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005269 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005270 HInstruction* value,
5271 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005272 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305273 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005274 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5275 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005276 }
5277
Roland Levillain5b5b9312016-03-22 14:57:31 +00005278 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005279 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005280 typedef typename std::make_unsigned<T>::type V;
5281 V ux = static_cast<V>(value);
5282 return static_cast<T>(ux >> (distance & max_shift_distance));
5283 }
5284
5285 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005286 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005287 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005288 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005289 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005290 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005291 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005292 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005293 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5294 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5295 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5296 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005297 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005298 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5299 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005300 LOG(FATAL) << DebugName() << " is not defined for float values";
5301 UNREACHABLE();
5302 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005303 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5304 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005305 LOG(FATAL) << DebugName() << " is not defined for double values";
5306 UNREACHABLE();
5307 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005308
5309 DECLARE_INSTRUCTION(UShr);
5310
Artem Serovcced8ba2017-07-19 18:18:09 +01005311 protected:
5312 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005313};
5314
Vladimir Markofcb503c2016-05-18 12:48:17 +01005315class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005316 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005317 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005318 HInstruction* left,
5319 HInstruction* right,
5320 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305321 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5322 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005323
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005324 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005325
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005326 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005327
5328 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005329 return GetBlock()->GetGraph()->GetIntConstant(
5330 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005331 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005332 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005333 return GetBlock()->GetGraph()->GetLongConstant(
5334 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005335 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005336 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5337 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5338 LOG(FATAL) << DebugName() << " is not defined for float values";
5339 UNREACHABLE();
5340 }
5341 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5342 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5343 LOG(FATAL) << DebugName() << " is not defined for double values";
5344 UNREACHABLE();
5345 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005346
5347 DECLARE_INSTRUCTION(And);
5348
Artem Serovcced8ba2017-07-19 18:18:09 +01005349 protected:
5350 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005351};
5352
Vladimir Markofcb503c2016-05-18 12:48:17 +01005353class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005354 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005355 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005356 HInstruction* left,
5357 HInstruction* right,
5358 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305359 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5360 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005361
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005362 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005363
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005364 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005365
5366 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005367 return GetBlock()->GetGraph()->GetIntConstant(
5368 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005369 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005370 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005371 return GetBlock()->GetGraph()->GetLongConstant(
5372 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005373 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005374 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5375 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5376 LOG(FATAL) << DebugName() << " is not defined for float values";
5377 UNREACHABLE();
5378 }
5379 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5380 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5381 LOG(FATAL) << DebugName() << " is not defined for double values";
5382 UNREACHABLE();
5383 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005384
5385 DECLARE_INSTRUCTION(Or);
5386
Artem Serovcced8ba2017-07-19 18:18:09 +01005387 protected:
5388 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005389};
5390
Vladimir Markofcb503c2016-05-18 12:48:17 +01005391class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005392 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005393 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005394 HInstruction* left,
5395 HInstruction* right,
5396 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305397 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5398 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005399
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005400 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005401
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005402 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005403
5404 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005405 return GetBlock()->GetGraph()->GetIntConstant(
5406 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005407 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005408 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005409 return GetBlock()->GetGraph()->GetLongConstant(
5410 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005411 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005412 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5413 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5414 LOG(FATAL) << DebugName() << " is not defined for float values";
5415 UNREACHABLE();
5416 }
5417 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5418 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5419 LOG(FATAL) << DebugName() << " is not defined for double values";
5420 UNREACHABLE();
5421 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005422
5423 DECLARE_INSTRUCTION(Xor);
5424
Artem Serovcced8ba2017-07-19 18:18:09 +01005425 protected:
5426 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005427};
5428
Vladimir Markofcb503c2016-05-18 12:48:17 +01005429class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005430 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005431 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305432 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005433 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5434 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005435 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005436
Roland Levillain5b5b9312016-03-22 14:57:31 +00005437 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005438 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005439 typedef typename std::make_unsigned<T>::type V;
5440 V ux = static_cast<V>(value);
5441 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005442 return static_cast<T>(ux);
5443 } else {
5444 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005445 return static_cast<T>(ux >> (distance & max_shift_value)) |
5446 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005447 }
5448 }
5449
Roland Levillain5b5b9312016-03-22 14:57:31 +00005450 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005451 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005452 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005453 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005454 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005455 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005456 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005457 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005458 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5459 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5460 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5461 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005462 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005463 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5464 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005465 LOG(FATAL) << DebugName() << " is not defined for float values";
5466 UNREACHABLE();
5467 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005468 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5469 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005470 LOG(FATAL) << DebugName() << " is not defined for double values";
5471 UNREACHABLE();
5472 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005473
5474 DECLARE_INSTRUCTION(Ror);
5475
Artem Serovcced8ba2017-07-19 18:18:09 +01005476 protected:
5477 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005478};
5479
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005480// The value of a parameter in this method. Its location depends on
5481// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005482class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005483 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005484 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005485 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005486 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005487 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005488 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305489 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005490 dex_file_(dex_file),
5491 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005492 index_(index) {
5493 SetPackedFlag<kFlagIsThis>(is_this);
5494 SetPackedFlag<kFlagCanBeNull>(!is_this);
5495 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005496
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005497 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005498 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005499 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005500 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005501
Vladimir Markoa1de9182016-02-25 11:37:38 +00005502 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5503 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005504
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005505 DECLARE_INSTRUCTION(ParameterValue);
5506
Artem Serovcced8ba2017-07-19 18:18:09 +01005507 protected:
5508 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5509
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005510 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005511 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005512 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005513 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5514 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5515 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5516 "Too many packed fields.");
5517
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005518 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005519 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005520 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005521 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005522 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005523};
5524
Vladimir Markofcb503c2016-05-18 12:48:17 +01005525class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005526 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305528 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5529 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005530
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005531 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005532 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005533 return true;
5534 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005535
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005536 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005537
5538 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005539 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005540 }
5541 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005542 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005543 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005544 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5545 LOG(FATAL) << DebugName() << " is not defined for float values";
5546 UNREACHABLE();
5547 }
5548 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5549 LOG(FATAL) << DebugName() << " is not defined for double values";
5550 UNREACHABLE();
5551 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005552
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005553 DECLARE_INSTRUCTION(Not);
5554
Artem Serovcced8ba2017-07-19 18:18:09 +01005555 protected:
5556 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005557};
5558
Vladimir Markofcb503c2016-05-18 12:48:17 +01005559class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005560 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005561 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305562 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5563 }
David Brazdil66d126e2015-04-03 16:02:44 +01005564
5565 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005566 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005567 return true;
5568 }
5569
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005570 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005571 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005572 return !x;
5573 }
5574
Roland Levillain9867bc72015-08-05 10:21:34 +01005575 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005576 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005577 }
5578 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5579 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005580 UNREACHABLE();
5581 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005582 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5583 LOG(FATAL) << DebugName() << " is not defined for float values";
5584 UNREACHABLE();
5585 }
5586 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5587 LOG(FATAL) << DebugName() << " is not defined for double values";
5588 UNREACHABLE();
5589 }
David Brazdil66d126e2015-04-03 16:02:44 +01005590
5591 DECLARE_INSTRUCTION(BooleanNot);
5592
Artem Serovcced8ba2017-07-19 18:18:09 +01005593 protected:
5594 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005595};
5596
Vladimir Markofcb503c2016-05-18 12:48:17 +01005597class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005598 public:
5599 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005600 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305601 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005602 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005603 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005604 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005605 }
5606
5607 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005608 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5609 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005610
Artem Serova6e26142018-06-19 14:55:17 +01005611 bool IsClonable() const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00005612 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005613 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5614 return true;
5615 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005616
Mark Mendelle82549b2015-05-06 10:55:34 -04005617 // Try to statically evaluate the conversion and return a HConstant
5618 // containing the result. If the input cannot be converted, return nullptr.
5619 HConstant* TryStaticEvaluation() const;
5620
Roland Levillaindff1f282014-11-05 14:15:05 +00005621 DECLARE_INSTRUCTION(TypeConversion);
5622
Artem Serovcced8ba2017-07-19 18:18:09 +01005623 protected:
5624 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005625};
5626
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005627static constexpr uint32_t kNoRegNumber = -1;
5628
Vladimir Markofcb503c2016-05-18 12:48:17 +01005629class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005630 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005631 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5632 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005633 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305634 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005635 SetRawInputAt(0, value);
5636 }
5637
Artem Serovcced8ba2017-07-19 18:18:09 +01005638 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005639 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005640 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005641 return true;
5642 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005643
Calin Juravle10e244f2015-01-26 18:54:32 +00005644 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005645
Calin Juravle10e244f2015-01-26 18:54:32 +00005646 bool CanThrow() const OVERRIDE { return true; }
5647
5648 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005649
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005650 DECLARE_INSTRUCTION(NullCheck);
5651
Artem Serovcced8ba2017-07-19 18:18:09 +01005652 protected:
5653 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005654};
5655
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005656// Embeds an ArtField and all the information required by the compiler. We cache
5657// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005658class FieldInfo : public ValueObject {
5659 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005660 FieldInfo(ArtField* field,
5661 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005662 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005663 bool is_volatile,
5664 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005665 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005666 const DexFile& dex_file)
5667 : field_(field),
5668 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005669 field_type_(field_type),
5670 is_volatile_(is_volatile),
5671 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005672 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005673 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005674
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005675 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005676 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005677 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005678 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005679 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005680 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005681 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005682
5683 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005684 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005685 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005686 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005687 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005688 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005689 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005690 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005691};
5692
Vladimir Markofcb503c2016-05-18 12:48:17 +01005693class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005694 public:
5695 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005696 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005697 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005698 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005699 bool is_volatile,
5700 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005701 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005702 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005703 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305704 : HExpression(kInstanceFieldGet,
5705 field_type,
5706 SideEffects::FieldReadOfType(field_type, is_volatile),
5707 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005708 field_info_(field,
5709 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005710 field_type,
5711 is_volatile,
5712 field_idx,
5713 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005714 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005715 SetRawInputAt(0, value);
5716 }
5717
Artem Serovcced8ba2017-07-19 18:18:09 +01005718 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005719 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005720
Vladimir Marko372f10e2016-05-17 16:30:10 +01005721 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5722 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005723 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005724 }
5725
Calin Juravle641547a2015-04-21 22:08:51 +01005726 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005727 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005728 }
5729
5730 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005731 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5732 }
5733
Calin Juravle52c48962014-12-16 17:02:57 +00005734 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005735 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005736 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005737 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005738
Vladimir Marko61b92282017-10-11 13:23:17 +01005739 void SetType(DataType::Type new_type) {
5740 DCHECK(DataType::IsIntegralType(GetType()));
5741 DCHECK(DataType::IsIntegralType(new_type));
5742 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5743 SetPackedField<TypeField>(new_type);
5744 }
5745
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005746 DECLARE_INSTRUCTION(InstanceFieldGet);
5747
Artem Serovcced8ba2017-07-19 18:18:09 +01005748 protected:
5749 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5750
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005751 private:
5752 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005753};
5754
Vladimir Markobd785672018-05-03 17:09:09 +01005755class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005756 public:
5757 HInstanceFieldSet(HInstruction* object,
5758 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005759 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005760 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005761 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005762 bool is_volatile,
5763 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005764 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005765 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005766 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005767 : HExpression(kInstanceFieldSet,
5768 SideEffects::FieldWriteOfType(field_type, is_volatile),
5769 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005770 field_info_(field,
5771 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005772 field_type,
5773 is_volatile,
5774 field_idx,
5775 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005776 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005777 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005778 SetRawInputAt(0, object);
5779 SetRawInputAt(1, value);
5780 }
5781
Artem Serovcced8ba2017-07-19 18:18:09 +01005782 bool IsClonable() const OVERRIDE { return true; }
5783
Calin Juravle641547a2015-04-21 22:08:51 +01005784 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005785 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005786 }
5787
Calin Juravle52c48962014-12-16 17:02:57 +00005788 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005789 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005790 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005791 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005792 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005793 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5794 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005795
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005796 DECLARE_INSTRUCTION(InstanceFieldSet);
5797
Artem Serovcced8ba2017-07-19 18:18:09 +01005798 protected:
5799 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5800
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005801 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005802 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5803 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5804 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5805 "Too many packed fields.");
5806
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005807 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005808};
5809
Vladimir Markofcb503c2016-05-18 12:48:17 +01005810class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005811 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005812 HArrayGet(HInstruction* array,
5813 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005814 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005815 uint32_t dex_pc)
5816 : HArrayGet(array,
5817 index,
5818 type,
5819 SideEffects::ArrayReadOfType(type),
5820 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305821 /* is_string_char_at */ false) {
5822 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005823
5824 HArrayGet(HInstruction* array,
5825 HInstruction* index,
5826 DataType::Type type,
5827 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005828 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005829 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305830 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005831 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005832 SetRawInputAt(0, array);
5833 SetRawInputAt(1, index);
5834 }
5835
Artem Serovcced8ba2017-07-19 18:18:09 +01005836 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005837 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005838 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005839 return true;
5840 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005841 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005842 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005843 // Currently, unless the array is the result of NewArray, the array access is always
5844 // preceded by some form of null NullCheck necessary for the bounds check, usually
5845 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5846 // dynamic BCE. There are cases when these could be removed to produce better code.
5847 // If we ever add optimizations to do so we should allow an implicit check here
5848 // (as long as the address falls in the first page).
5849 //
5850 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5851 // a = cond ? new int[1] : null;
5852 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005853 return false;
5854 }
5855
David Brazdil4833f5a2015-12-16 10:37:39 +00005856 bool IsEquivalentOf(HArrayGet* other) const {
5857 bool result = (GetDexPc() == other->GetDexPc());
5858 if (kIsDebugBuild && result) {
5859 DCHECK_EQ(GetBlock(), other->GetBlock());
5860 DCHECK_EQ(GetArray(), other->GetArray());
5861 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005862 if (DataType::IsIntOrLongType(GetType())) {
5863 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005864 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005865 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5866 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005867 }
5868 }
5869 return result;
5870 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005871
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005872 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5873
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005874 HInstruction* GetArray() const { return InputAt(0); }
5875 HInstruction* GetIndex() const { return InputAt(1); }
5876
Vladimir Marko61b92282017-10-11 13:23:17 +01005877 void SetType(DataType::Type new_type) {
5878 DCHECK(DataType::IsIntegralType(GetType()));
5879 DCHECK(DataType::IsIntegralType(new_type));
5880 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5881 SetPackedField<TypeField>(new_type);
5882 }
5883
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005884 DECLARE_INSTRUCTION(ArrayGet);
5885
Artem Serovcced8ba2017-07-19 18:18:09 +01005886 protected:
5887 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5888
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005889 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005890 // We treat a String as an array, creating the HArrayGet from String.charAt()
5891 // intrinsic in the instruction simplifier. We can always determine whether
5892 // a particular HArrayGet is actually a String.charAt() by looking at the type
5893 // of the input but that requires holding the mutator lock, so we prefer to use
5894 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005895 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005896 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5897 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5898 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005899};
5900
Vladimir Markobd785672018-05-03 17:09:09 +01005901class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005902 public:
5903 HArraySet(HInstruction* array,
5904 HInstruction* index,
5905 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005906 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005907 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005908 : HArraySet(array,
5909 index,
5910 value,
5911 expected_component_type,
5912 // Make a best guess for side effects now, may be refined during SSA building.
5913 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305914 dex_pc) {
5915 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005916
5917 HArraySet(HInstruction* array,
5918 HInstruction* index,
5919 HInstruction* value,
5920 DataType::Type expected_component_type,
5921 SideEffects side_effects,
5922 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005923 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005924 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005925 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005926 SetPackedFlag<kFlagValueCanBeNull>(true);
5927 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005928 SetRawInputAt(0, array);
5929 SetRawInputAt(1, index);
5930 SetRawInputAt(2, value);
5931 }
5932
Artem Serovcced8ba2017-07-19 18:18:09 +01005933 bool IsClonable() const OVERRIDE { return true; }
5934
Calin Juravle77520bc2015-01-12 18:45:46 +00005935 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005936 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005937 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005938 }
5939
Mingyao Yang81014cb2015-06-02 03:16:27 -07005940 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005941 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005942
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005943 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005944 // TODO: Same as for ArrayGet.
5945 return false;
5946 }
5947
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005948 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005949 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005950 }
5951
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005952 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005953 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005954 }
5955
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005956 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005957 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005958 }
5959
Vladimir Markoa1de9182016-02-25 11:37:38 +00005960 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5961 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5962 bool StaticTypeOfArrayIsObjectArray() const {
5963 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5964 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005965
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005966 HInstruction* GetArray() const { return InputAt(0); }
5967 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005968 HInstruction* GetValue() const { return InputAt(2); }
5969
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005970 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005971 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5972 }
5973
5974 static DataType::Type GetComponentType(DataType::Type value_type,
5975 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005976 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005977 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005978 // be correct, we also check what is the value type. If it is a floating
5979 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005980 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005981 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005982 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005983 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005984
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005986 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005987 }
5988
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005989 static SideEffects ComputeSideEffects(DataType::Type type) {
5990 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005991 }
5992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005993 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5994 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5995 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005996 }
5997
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005998 DECLARE_INSTRUCTION(ArraySet);
5999
Artem Serovcced8ba2017-07-19 18:18:09 +01006000 protected:
6001 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006003 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6005 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006006 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006007 static constexpr size_t kFlagNeedsTypeCheck =
6008 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6009 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006010 // Cached information for the reference_type_info_ so that codegen
6011 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006012 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6013 static constexpr size_t kNumberOfArraySetPackedBits =
6014 kFlagStaticTypeOfArrayIsObjectArray + 1;
6015 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6016 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006017 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006018};
6019
Vladimir Markofcb503c2016-05-18 12:48:17 +01006020class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006021 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006022 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306023 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006024 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006025 // Note that arrays do not change length, so the instruction does not
6026 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006027 SetRawInputAt(0, array);
6028 }
6029
Artem Serovcced8ba2017-07-19 18:18:09 +01006030 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00006031 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006032 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006033 return true;
6034 }
Calin Juravle641547a2015-04-21 22:08:51 +01006035 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
6036 return obj == InputAt(0);
6037 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006038
Vladimir Markodce016e2016-04-28 13:10:02 +01006039 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6040
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006041 DECLARE_INSTRUCTION(ArrayLength);
6042
Artem Serovcced8ba2017-07-19 18:18:09 +01006043 protected:
6044 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6045
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006046 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006047 // We treat a String as an array, creating the HArrayLength from String.length()
6048 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6049 // determine whether a particular HArrayLength is actually a String.length() by
6050 // looking at the type of the input but that requires holding the mutator lock, so
6051 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006052 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006053 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6054 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6055 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006056};
6057
Vladimir Markofcb503c2016-05-18 12:48:17 +01006058class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006059 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006060 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6061 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006062 HBoundsCheck(HInstruction* index,
6063 HInstruction* length,
6064 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006065 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306066 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006067 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006068 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006069 SetRawInputAt(0, index);
6070 SetRawInputAt(1, length);
6071 }
6072
Artem Serovcced8ba2017-07-19 18:18:09 +01006073 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006074 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006075 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006076 return true;
6077 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006078
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006079 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006080
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006081 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006082
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006083 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006084
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006085 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006086
6087 DECLARE_INSTRUCTION(BoundsCheck);
6088
Artem Serovcced8ba2017-07-19 18:18:09 +01006089 protected:
6090 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6091
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006092 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006093 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006094};
6095
Vladimir Markobd785672018-05-03 17:09:09 +01006096class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006097 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006098 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006099 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306100 slow_path_(nullptr) {
6101 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006102
Artem Serovcced8ba2017-07-19 18:18:09 +01006103 bool IsClonable() const OVERRIDE { return true; }
6104
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006105 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006106 return true;
6107 }
6108
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006109 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6110 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006111
6112 DECLARE_INSTRUCTION(SuspendCheck);
6113
Artem Serovcced8ba2017-07-19 18:18:09 +01006114 protected:
6115 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6116
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006117 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006118 // Only used for code generation, in order to share the same slow path between back edges
6119 // of a same loop.
6120 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006121};
6122
David Srbecky0cf44932015-12-09 14:09:59 +00006123// Pseudo-instruction which provides the native debugger with mapping information.
6124// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006125class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006126 public:
6127 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006128 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306129 }
David Srbecky0cf44932015-12-09 14:09:59 +00006130
6131 bool NeedsEnvironment() const OVERRIDE {
6132 return true;
6133 }
6134
6135 DECLARE_INSTRUCTION(NativeDebugInfo);
6136
Artem Serovcced8ba2017-07-19 18:18:09 +01006137 protected:
6138 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006139};
6140
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006141/**
6142 * Instruction to load a Class object.
6143 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006144class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006145 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006146 // Determines how to load the Class.
6147 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006148 // We cannot load this class. See HSharpening::SharpenLoadClass.
6149 kInvalid = -1,
6150
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006151 // Use the Class* from the method's own ArtMethod*.
6152 kReferrersClass,
6153
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006154 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006155 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006156 kBootImageLinkTimePcRelative,
6157
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006158 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006159 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006160 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006161
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006162 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006163 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006164 kBssEntry,
6165
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006166 // Use a known boot image Class* address, embedded in the code by the codegen.
6167 // Used for boot image classes referenced by apps in JIT-compiled code.
6168 kJitBootImageAddress,
6169
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006170 // Load from the root table associated with the JIT compiled method.
6171 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006172
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006173 // Load using a simple runtime call. This is the fall-back load kind when
6174 // the codegen is unable to use another appropriate kind.
6175 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006176
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006177 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006178 };
6179
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006180 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006181 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006182 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006183 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006184 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006185 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006186 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006187 : HInstruction(kLoadClass,
6188 DataType::Type::kReference,
6189 SideEffectsForArchRuntimeCalls(),
6190 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006191 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006192 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006193 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006194 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006195 // Referrers class should not need access check. We never inline unverified
6196 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006197 DCHECK(!is_referrers_class || !needs_access_check);
6198
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006199 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006200 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006201 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006202 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006203 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006204 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006205 }
6206
Artem Serovcced8ba2017-07-19 18:18:09 +01006207 bool IsClonable() const OVERRIDE { return true; }
6208
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006209 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006210
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006211 LoadKind GetLoadKind() const {
6212 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006213 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006214
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006215 bool HasPcRelativeLoadKind() const {
6216 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6217 GetLoadKind() == LoadKind::kBootImageRelRo ||
6218 GetLoadKind() == LoadKind::kBssEntry;
6219 }
6220
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006221 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006222
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006223 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006224
Andreas Gampea5b09a62016-11-17 15:21:22 -08006225 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006226
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006227 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006228
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006229 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006230 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006231 }
6232
Calin Juravle0ba218d2015-05-19 18:46:01 +01006233 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006234 // The entrypoint the code generator is going to call does not do
6235 // clinit of the class.
6236 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006237 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006238 }
6239
6240 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006241 return NeedsAccessCheck() ||
6242 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006243 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006244 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006245 }
6246
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006247 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006248 return NeedsAccessCheck() ||
6249 MustGenerateClinitCheck() ||
6250 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006251 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006252 GetLoadKind() == LoadKind::kBssEntry) &&
6253 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006254 }
6255
Calin Juravleacf735c2015-02-12 15:25:22 +00006256 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006257 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6258 // Note: The is_exact flag from the return value should not be used.
6259 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6260 } else {
6261 return ReferenceTypeInfo::CreateInvalid();
6262 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006263 }
6264
Vladimir Marko175e7862018-03-27 09:03:13 +00006265 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6266 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6267 DCHECK(klass_ != nullptr);
6268 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006269 }
6270
Andreas Gampea5b09a62016-11-17 15:21:22 -08006271 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006272 const DexFile& GetDexFile() const { return dex_file_; }
6273
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006274 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006275 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006276 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006277
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006278 static SideEffects SideEffectsForArchRuntimeCalls() {
6279 return SideEffects::CanTriggerGC();
6280 }
6281
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006282 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006283 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006284 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006285 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006286
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006287 void MarkInBootImage() {
6288 SetPackedFlag<kFlagIsInBootImage>(true);
6289 }
6290
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006291 void AddSpecialInput(HInstruction* special_input);
6292
6293 using HInstruction::GetInputRecords; // Keep the const version visible.
6294 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6295 return ArrayRef<HUserRecord<HInstruction*>>(
6296 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6297 }
6298
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006299 Handle<mirror::Class> GetClass() const {
6300 return klass_;
6301 }
6302
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006303 DECLARE_INSTRUCTION(LoadClass);
6304
Artem Serovcced8ba2017-07-19 18:18:09 +01006305 protected:
6306 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6307
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006308 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006309 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006310 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006311 // Whether this instruction must generate the initialization check.
6312 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006313 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006314 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6315 static constexpr size_t kFieldLoadKindSize =
6316 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006317 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6318 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006319 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006320 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6321
6322 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006323 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006324 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006325 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006326 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006327 }
6328
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006329 void SetLoadKindInternal(LoadKind load_kind);
6330
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006331 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006332 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006333 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006334 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006335
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006336 // A type index and dex file where the class can be accessed. The dex file can be:
6337 // - The compiling method's dex file if the class is defined there too.
6338 // - The compiling method's dex file if the class is referenced there.
6339 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006340 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006341 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006342 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006343
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006344 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006345};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006346std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6347
6348// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006349inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6350 // The load kind should be determined before inserting the instruction to the graph.
6351 DCHECK(GetBlock() == nullptr);
6352 DCHECK(GetEnvironment() == nullptr);
6353 SetPackedField<LoadKindField>(load_kind);
6354 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6355 special_input_ = HUserRecord<HInstruction*>(nullptr);
6356 }
6357 if (!NeedsEnvironment()) {
6358 SetSideEffects(SideEffects::None());
6359 }
6360}
6361
6362// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006363inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006364 // The special input is used for PC-relative loads on some architectures,
6365 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006366 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006367 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006368 GetLoadKind() == LoadKind::kBssEntry ||
6369 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006370 DCHECK(special_input_.GetInstruction() == nullptr);
6371 special_input_ = HUserRecord<HInstruction*>(special_input);
6372 special_input->AddUseAt(this, 0);
6373}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006374
Vladimir Marko372f10e2016-05-17 16:30:10 +01006375class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006376 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006377 // Determines how to load the String.
6378 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006379 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006380 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006381 kBootImageLinkTimePcRelative,
6382
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006383 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006384 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006385 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006386
Vladimir Markoaad75c62016-10-03 08:46:48 +00006387 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006388 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006389 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006390
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006391 // Use a known boot image String* address, embedded in the code by the codegen.
6392 // Used for boot image strings referenced by apps in JIT-compiled code.
6393 kJitBootImageAddress,
6394
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006395 // Load from the root table associated with the JIT compiled method.
6396 kJitTableAddress,
6397
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006398 // Load using a simple runtime call. This is the fall-back load kind when
6399 // the codegen is unable to use another appropriate kind.
6400 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006401
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006402 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006403 };
6404
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006405 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006406 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006407 const DexFile& dex_file,
6408 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006409 : HInstruction(kLoadString,
6410 DataType::Type::kReference,
6411 SideEffectsForArchRuntimeCalls(),
6412 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006413 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006414 string_index_(string_index),
6415 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006416 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006417 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006418
Artem Serovcced8ba2017-07-19 18:18:09 +01006419 bool IsClonable() const OVERRIDE { return true; }
6420
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006421 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006422
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006423 LoadKind GetLoadKind() const {
6424 return GetPackedField<LoadKindField>();
6425 }
6426
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006427 bool HasPcRelativeLoadKind() const {
6428 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6429 GetLoadKind() == LoadKind::kBootImageRelRo ||
6430 GetLoadKind() == LoadKind::kBssEntry;
6431 }
6432
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006433 const DexFile& GetDexFile() const {
6434 return dex_file_;
6435 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006436
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006437 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006438 return string_index_;
6439 }
6440
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006441 Handle<mirror::String> GetString() const {
6442 return string_;
6443 }
6444
6445 void SetString(Handle<mirror::String> str) {
6446 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006447 }
6448
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006449 bool CanBeMoved() const OVERRIDE { return true; }
6450
Vladimir Marko372f10e2016-05-17 16:30:10 +01006451 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006452
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006453 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006454
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006455 // Will call the runtime if we need to load the string through
6456 // the dex cache and the string is not guaranteed to be there yet.
6457 bool NeedsEnvironment() const OVERRIDE {
6458 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006459 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006460 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006461 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006462 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006463 return false;
6464 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006465 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006466 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006467
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006468 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006469 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006470 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006471
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006472 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006473 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006474
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006475 static SideEffects SideEffectsForArchRuntimeCalls() {
6476 return SideEffects::CanTriggerGC();
6477 }
6478
Vladimir Marko372f10e2016-05-17 16:30:10 +01006479 void AddSpecialInput(HInstruction* special_input);
6480
6481 using HInstruction::GetInputRecords; // Keep the const version visible.
6482 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6483 return ArrayRef<HUserRecord<HInstruction*>>(
6484 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006485 }
6486
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006487 DECLARE_INSTRUCTION(LoadString);
6488
Artem Serovcced8ba2017-07-19 18:18:09 +01006489 protected:
6490 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6491
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006492 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006493 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006494 static constexpr size_t kFieldLoadKindSize =
6495 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6496 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006497 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006498 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006499
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006500 void SetLoadKindInternal(LoadKind load_kind);
6501
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006502 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006503 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006504 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006505 HUserRecord<HInstruction*> special_input_;
6506
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006507 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006508 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006509
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006510 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006511};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006512std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6513
6514// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006515inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6516 // The load kind should be determined before inserting the instruction to the graph.
6517 DCHECK(GetBlock() == nullptr);
6518 DCHECK(GetEnvironment() == nullptr);
6519 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6520 SetPackedField<LoadKindField>(load_kind);
6521 if (load_kind != LoadKind::kRuntimeCall) {
6522 special_input_ = HUserRecord<HInstruction*>(nullptr);
6523 }
6524 if (!NeedsEnvironment()) {
6525 SetSideEffects(SideEffects::None());
6526 }
6527}
6528
6529// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006530inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006531 // The special input is used for PC-relative loads on some architectures,
6532 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006533 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006534 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006535 GetLoadKind() == LoadKind::kBssEntry ||
6536 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006537 // HLoadString::GetInputRecords() returns an empty array at this point,
6538 // so use the GetInputRecords() from the base class to set the input record.
6539 DCHECK(special_input_.GetInstruction() == nullptr);
6540 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006541 special_input->AddUseAt(this, 0);
6542}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006543
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006544class HLoadMethodHandle FINAL : public HInstruction {
6545 public:
6546 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006547 uint16_t method_handle_idx,
6548 const DexFile& dex_file,
6549 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006550 : HInstruction(kLoadMethodHandle,
6551 DataType::Type::kReference,
6552 SideEffectsForArchRuntimeCalls(),
6553 dex_pc),
6554 special_input_(HUserRecord<HInstruction*>(current_method)),
6555 method_handle_idx_(method_handle_idx),
6556 dex_file_(dex_file) {
6557 }
6558
6559 using HInstruction::GetInputRecords; // Keep the const version visible.
6560 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6561 return ArrayRef<HUserRecord<HInstruction*>>(
6562 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6563 }
6564
6565 bool IsClonable() const OVERRIDE { return true; }
6566
6567 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6568
6569 const DexFile& GetDexFile() const { return dex_file_; }
6570
6571 static SideEffects SideEffectsForArchRuntimeCalls() {
6572 return SideEffects::CanTriggerGC();
6573 }
6574
6575 DECLARE_INSTRUCTION(LoadMethodHandle);
6576
6577 protected:
6578 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6579
6580 private:
6581 // The special input is the HCurrentMethod for kRuntimeCall.
6582 HUserRecord<HInstruction*> special_input_;
6583
6584 const uint16_t method_handle_idx_;
6585 const DexFile& dex_file_;
6586};
6587
Orion Hodson18259d72018-04-12 11:18:23 +01006588class HLoadMethodType FINAL : public HInstruction {
6589 public:
6590 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006591 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006592 const DexFile& dex_file,
6593 uint32_t dex_pc)
6594 : HInstruction(kLoadMethodType,
6595 DataType::Type::kReference,
6596 SideEffectsForArchRuntimeCalls(),
6597 dex_pc),
6598 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006599 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006600 dex_file_(dex_file) {
6601 }
6602
6603 using HInstruction::GetInputRecords; // Keep the const version visible.
6604 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6605 return ArrayRef<HUserRecord<HInstruction*>>(
6606 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6607 }
6608
6609 bool IsClonable() const OVERRIDE { return true; }
6610
Orion Hodson06d10a72018-05-14 08:53:38 +01006611 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006612
6613 const DexFile& GetDexFile() const { return dex_file_; }
6614
6615 static SideEffects SideEffectsForArchRuntimeCalls() {
6616 return SideEffects::CanTriggerGC();
6617 }
6618
6619 DECLARE_INSTRUCTION(LoadMethodType);
6620
6621 protected:
6622 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6623
6624 private:
6625 // The special input is the HCurrentMethod for kRuntimeCall.
6626 HUserRecord<HInstruction*> special_input_;
6627
Orion Hodson06d10a72018-05-14 08:53:38 +01006628 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006629 const DexFile& dex_file_;
6630};
6631
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006632/**
6633 * Performs an initialization check on its Class object input.
6634 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006635class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006636 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006637 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006638 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306639 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006640 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006641 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006642 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006643 SetRawInputAt(0, constant);
6644 }
Artem Serova6e26142018-06-19 14:55:17 +01006645 // TODO: Make ClinitCheck clonable.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006646 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006647 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006648 return true;
6649 }
6650
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006651 bool NeedsEnvironment() const OVERRIDE {
6652 // May call runtime to initialize the class.
6653 return true;
6654 }
6655
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006656 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006657
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006658 HLoadClass* GetLoadClass() const {
6659 DCHECK(InputAt(0)->IsLoadClass());
6660 return InputAt(0)->AsLoadClass();
6661 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006662
6663 DECLARE_INSTRUCTION(ClinitCheck);
6664
Artem Serovcced8ba2017-07-19 18:18:09 +01006665
6666 protected:
6667 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006668};
6669
Vladimir Markofcb503c2016-05-18 12:48:17 +01006670class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006671 public:
6672 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006673 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006674 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006675 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006676 bool is_volatile,
6677 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006678 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006679 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006680 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306681 : HExpression(kStaticFieldGet,
6682 field_type,
6683 SideEffects::FieldReadOfType(field_type, is_volatile),
6684 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006685 field_info_(field,
6686 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006687 field_type,
6688 is_volatile,
6689 field_idx,
6690 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006691 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006692 SetRawInputAt(0, cls);
6693 }
6694
Calin Juravle52c48962014-12-16 17:02:57 +00006695
Artem Serovcced8ba2017-07-19 18:18:09 +01006696 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006697 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006698
Vladimir Marko372f10e2016-05-17 16:30:10 +01006699 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6700 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006701 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006702 }
6703
6704 size_t ComputeHashCode() const OVERRIDE {
6705 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6706 }
6707
Calin Juravle52c48962014-12-16 17:02:57 +00006708 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006709 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006710 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006711 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006712
Vladimir Marko61b92282017-10-11 13:23:17 +01006713 void SetType(DataType::Type new_type) {
6714 DCHECK(DataType::IsIntegralType(GetType()));
6715 DCHECK(DataType::IsIntegralType(new_type));
6716 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6717 SetPackedField<TypeField>(new_type);
6718 }
6719
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006720 DECLARE_INSTRUCTION(StaticFieldGet);
6721
Artem Serovcced8ba2017-07-19 18:18:09 +01006722 protected:
6723 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6724
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006725 private:
6726 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006727};
6728
Vladimir Markobd785672018-05-03 17:09:09 +01006729class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006730 public:
6731 HStaticFieldSet(HInstruction* cls,
6732 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006733 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006734 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006735 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006736 bool is_volatile,
6737 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006738 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006739 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006740 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006741 : HExpression(kStaticFieldSet,
6742 SideEffects::FieldWriteOfType(field_type, is_volatile),
6743 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006744 field_info_(field,
6745 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006746 field_type,
6747 is_volatile,
6748 field_idx,
6749 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006750 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006751 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006752 SetRawInputAt(0, cls);
6753 SetRawInputAt(1, value);
6754 }
6755
Artem Serovcced8ba2017-07-19 18:18:09 +01006756 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006757 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006758 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006759 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006760 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006761
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006762 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006763 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6764 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006765
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006766 DECLARE_INSTRUCTION(StaticFieldSet);
6767
Artem Serovcced8ba2017-07-19 18:18:09 +01006768 protected:
6769 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6770
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006771 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006772 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6773 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6774 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6775 "Too many packed fields.");
6776
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006777 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006778};
6779
Vladimir Markofcb503c2016-05-18 12:48:17 +01006780class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006781 public:
6782 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006783 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006784 uint32_t field_index,
6785 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306786 : HExpression(kUnresolvedInstanceFieldGet,
6787 field_type,
6788 SideEffects::AllExceptGCDependency(),
6789 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006790 field_index_(field_index) {
6791 SetRawInputAt(0, obj);
6792 }
6793
Artem Serovcced8ba2017-07-19 18:18:09 +01006794 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006795 bool NeedsEnvironment() const OVERRIDE { return true; }
6796 bool CanThrow() const OVERRIDE { return true; }
6797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006798 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006799 uint32_t GetFieldIndex() const { return field_index_; }
6800
6801 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6802
Artem Serovcced8ba2017-07-19 18:18:09 +01006803 protected:
6804 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6805
Calin Juravlee460d1d2015-09-29 04:52:17 +01006806 private:
6807 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006808};
6809
Vladimir Markobd785672018-05-03 17:09:09 +01006810class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006811 public:
6812 HUnresolvedInstanceFieldSet(HInstruction* obj,
6813 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006814 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006815 uint32_t field_index,
6816 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006817 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006818 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006819 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006820 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006821 SetRawInputAt(0, obj);
6822 SetRawInputAt(1, value);
6823 }
6824
Artem Serovcced8ba2017-07-19 18:18:09 +01006825 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006826 bool NeedsEnvironment() const OVERRIDE { return true; }
6827 bool CanThrow() const OVERRIDE { return true; }
6828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006829 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006830 uint32_t GetFieldIndex() const { return field_index_; }
6831
6832 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6833
Artem Serovcced8ba2017-07-19 18:18:09 +01006834 protected:
6835 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6836
Calin Juravlee460d1d2015-09-29 04:52:17 +01006837 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006838 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6839 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006840 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006841 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6842 kFieldFieldType + kFieldFieldTypeSize;
6843 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6844 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006845 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006846
Calin Juravlee460d1d2015-09-29 04:52:17 +01006847 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006848};
6849
Vladimir Markofcb503c2016-05-18 12:48:17 +01006850class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006851 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006852 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006853 uint32_t field_index,
6854 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306855 : HExpression(kUnresolvedStaticFieldGet,
6856 field_type,
6857 SideEffects::AllExceptGCDependency(),
6858 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006859 field_index_(field_index) {
6860 }
6861
Artem Serovcced8ba2017-07-19 18:18:09 +01006862 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006863 bool NeedsEnvironment() const OVERRIDE { return true; }
6864 bool CanThrow() const OVERRIDE { return true; }
6865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006866 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006867 uint32_t GetFieldIndex() const { return field_index_; }
6868
6869 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6870
Artem Serovcced8ba2017-07-19 18:18:09 +01006871 protected:
6872 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6873
Calin Juravlee460d1d2015-09-29 04:52:17 +01006874 private:
6875 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006876};
6877
Vladimir Markobd785672018-05-03 17:09:09 +01006878class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006879 public:
6880 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006881 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006882 uint32_t field_index,
6883 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006884 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006885 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006886 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006887 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006888 SetRawInputAt(0, value);
6889 }
6890
Artem Serovcced8ba2017-07-19 18:18:09 +01006891 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006892 bool NeedsEnvironment() const OVERRIDE { return true; }
6893 bool CanThrow() const OVERRIDE { return true; }
6894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006895 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006896 uint32_t GetFieldIndex() const { return field_index_; }
6897
6898 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6899
Artem Serovcced8ba2017-07-19 18:18:09 +01006900 protected:
6901 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6902
Calin Juravlee460d1d2015-09-29 04:52:17 +01006903 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006904 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6905 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006906 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006907 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6908 kFieldFieldType + kFieldFieldTypeSize;
6909 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6910 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006911 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006912
Calin Juravlee460d1d2015-09-29 04:52:17 +01006913 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006914};
6915
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006916// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006917class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006918 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006919 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306920 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6921 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006922
David Brazdilbbd733e2015-08-18 17:48:17 +01006923 bool CanBeNull() const OVERRIDE { return false; }
6924
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006925 DECLARE_INSTRUCTION(LoadException);
6926
Artem Serovcced8ba2017-07-19 18:18:09 +01006927 protected:
6928 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006929};
6930
David Brazdilcb1c0552015-08-04 16:22:25 +01006931// Implicit part of move-exception which clears thread-local exception storage.
6932// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markobd785672018-05-03 17:09:09 +01006933class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006934 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006935 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006936 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306937 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006938
6939 DECLARE_INSTRUCTION(ClearException);
6940
Artem Serovcced8ba2017-07-19 18:18:09 +01006941 protected:
6942 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006943};
6944
Vladimir Markobd785672018-05-03 17:09:09 +01006945class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006946 public:
6947 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006948 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006949 SetRawInputAt(0, exception);
6950 }
6951
6952 bool IsControlFlow() const OVERRIDE { return true; }
6953
6954 bool NeedsEnvironment() const OVERRIDE { return true; }
6955
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006956 bool CanThrow() const OVERRIDE { return true; }
6957
Aart Bika8b8e9b2018-01-09 11:01:02 -08006958 bool AlwaysThrows() const OVERRIDE { return true; }
6959
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006960 DECLARE_INSTRUCTION(Throw);
6961
Artem Serovcced8ba2017-07-19 18:18:09 +01006962 protected:
6963 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006964};
6965
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006966/**
6967 * Implementation strategies for the code generator of a HInstanceOf
6968 * or `HCheckCast`.
6969 */
6970enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006971 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006972 kExactCheck, // Can do a single class compare.
6973 kClassHierarchyCheck, // Can just walk the super class chain.
6974 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6975 kInterfaceCheck, // No optimization yet when checking against an interface.
6976 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006977 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006978 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006979 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006980};
6981
Roland Levillain86503782016-02-11 19:07:30 +00006982std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6983
Vladimir Marko175e7862018-03-27 09:03:13 +00006984// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6985// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6986class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006987 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00006988 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006989 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006990 HInstruction* object,
6991 HInstruction* target_class_or_null,
6992 TypeCheckKind check_kind,
6993 Handle<mirror::Class> klass,
6994 uint32_t dex_pc,
6995 ArenaAllocator* allocator,
6996 HIntConstant* bitstring_path_to_root,
6997 HIntConstant* bitstring_mask,
6998 SideEffects side_effects)
6999 : HVariableInputSizeInstruction(
7000 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007001 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007002 side_effects,
7003 dex_pc,
7004 allocator,
7005 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
7006 kArenaAllocTypeCheckInputs),
7007 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007008 SetPackedField<TypeCheckKindField>(check_kind);
7009 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007010 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007011 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007012 SetRawInputAt(1, target_class_or_null);
7013 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7014 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7015 if (check_kind == TypeCheckKind::kBitstringCheck) {
7016 DCHECK(target_class_or_null->IsNullConstant());
7017 SetRawInputAt(2, bitstring_path_to_root);
7018 SetRawInputAt(3, bitstring_mask);
7019 } else {
7020 DCHECK(target_class_or_null->IsLoadClass());
7021 }
Vladimir Marko87584542017-12-12 17:47:52 +00007022 }
7023
7024 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007025 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007026 HInstruction* load_class = InputAt(1);
7027 DCHECK(load_class->IsLoadClass());
7028 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007029 }
7030
Vladimir Marko175e7862018-03-27 09:03:13 +00007031 uint32_t GetBitstringPathToRoot() const {
7032 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7033 HInstruction* path_to_root = InputAt(2);
7034 DCHECK(path_to_root->IsIntConstant());
7035 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7036 }
7037
7038 uint32_t GetBitstringMask() const {
7039 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7040 HInstruction* mask = InputAt(3);
7041 DCHECK(mask->IsIntConstant());
7042 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7043 }
7044
Artem Serovcced8ba2017-07-19 18:18:09 +01007045 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007046 bool CanBeMoved() const OVERRIDE { return true; }
7047
Vladimir Marko175e7862018-03-27 09:03:13 +00007048 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
7049 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7050 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007051 }
7052
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007053 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7054 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7055 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7056 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7057
Vladimir Marko175e7862018-03-27 09:03:13 +00007058 ReferenceTypeInfo GetTargetClassRTI() {
7059 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7060 // Note: The is_exact flag from the return value should not be used.
7061 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7062 } else {
7063 return ReferenceTypeInfo::CreateInvalid();
7064 }
7065 }
7066
7067 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7068 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7069 DCHECK(klass_ != nullptr);
7070 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7071 }
7072
7073 Handle<mirror::Class> GetClass() const {
7074 return klass_;
7075 }
7076
7077 protected:
7078 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7079
7080 private:
7081 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7082 static constexpr size_t kFieldTypeCheckKindSize =
7083 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7084 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7085 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7086 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7087 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7088 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7089
7090 Handle<mirror::Class> klass_;
7091};
7092
7093class HInstanceOf FINAL : public HTypeCheckInstruction {
7094 public:
7095 HInstanceOf(HInstruction* object,
7096 HInstruction* target_class_or_null,
7097 TypeCheckKind check_kind,
7098 Handle<mirror::Class> klass,
7099 uint32_t dex_pc,
7100 ArenaAllocator* allocator,
7101 HIntConstant* bitstring_path_to_root,
7102 HIntConstant* bitstring_mask)
7103 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007104 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007105 object,
7106 target_class_or_null,
7107 check_kind,
7108 klass,
7109 dex_pc,
7110 allocator,
7111 bitstring_path_to_root,
7112 bitstring_mask,
7113 SideEffectsForArchRuntimeCalls(check_kind)) {}
7114
Artem Serova6e26142018-06-19 14:55:17 +01007115 bool IsClonable() const OVERRIDE { return true; }
7116
Vladimir Marko175e7862018-03-27 09:03:13 +00007117 bool NeedsEnvironment() const OVERRIDE {
7118 return CanCallRuntime(GetTypeCheckKind());
7119 }
7120
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007121 static bool CanCallRuntime(TypeCheckKind check_kind) {
7122 // Mips currently does runtime calls for any other checks.
7123 return check_kind != TypeCheckKind::kExactCheck;
7124 }
7125
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007126 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007127 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007128 }
7129
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007130 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007131
Artem Serovcced8ba2017-07-19 18:18:09 +01007132 protected:
7133 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007134};
7135
Vladimir Markofcb503c2016-05-18 12:48:17 +01007136class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007137 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007138 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307139 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007140 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7141 SetPackedFlag<kFlagUpperCanBeNull>(true);
7142 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007143 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007144 SetRawInputAt(0, input);
7145 }
7146
Artem Serov4d277ba2018-06-05 20:54:42 +01007147 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Artem Serovcced8ba2017-07-19 18:18:09 +01007148 bool IsClonable() const OVERRIDE { return true; }
7149
David Brazdilf5552582015-12-27 13:36:12 +00007150 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007151 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007152 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007153 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007154
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007155 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007156 DCHECK(GetUpperCanBeNull() || !can_be_null);
7157 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007158 }
7159
Vladimir Markoa1de9182016-02-25 11:37:38 +00007160 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007161
Calin Juravleb1498f62015-02-16 13:13:29 +00007162 DECLARE_INSTRUCTION(BoundType);
7163
Artem Serovcced8ba2017-07-19 18:18:09 +01007164 protected:
7165 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7166
Calin Juravleb1498f62015-02-16 13:13:29 +00007167 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007168 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7169 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007170 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007171 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7172 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7173 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7174
Calin Juravleb1498f62015-02-16 13:13:29 +00007175 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007176 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7177 // It is used to bound the type in cases like:
7178 // if (x instanceof ClassX) {
7179 // // uper_bound_ will be ClassX
7180 // }
David Brazdilf5552582015-12-27 13:36:12 +00007181 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007182};
7183
Vladimir Marko175e7862018-03-27 09:03:13 +00007184class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007185 public:
7186 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007187 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007188 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007189 Handle<mirror::Class> klass,
7190 uint32_t dex_pc,
7191 ArenaAllocator* allocator,
7192 HIntConstant* bitstring_path_to_root,
7193 HIntConstant* bitstring_mask)
7194 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007195 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007196 object,
7197 target_class_or_null,
7198 check_kind,
7199 klass,
7200 dex_pc,
7201 allocator,
7202 bitstring_path_to_root,
7203 bitstring_mask,
7204 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007205
Artem Serova6e26142018-06-19 14:55:17 +01007206 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007207 bool NeedsEnvironment() const OVERRIDE {
7208 // Instruction may throw a CheckCastError.
7209 return true;
7210 }
7211
7212 bool CanThrow() const OVERRIDE { return true; }
7213
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007214 DECLARE_INSTRUCTION(CheckCast);
7215
Artem Serovcced8ba2017-07-19 18:18:09 +01007216 protected:
7217 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007218};
7219
Andreas Gampe26de38b2016-07-27 17:53:11 -07007220/**
7221 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7222 * @details We define the combined barrier types that are actually required
7223 * by the Java Memory Model, rather than using exactly the terminology from
7224 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7225 * primitives. Note that the JSR-133 cookbook generally does not deal with
7226 * store atomicity issues, and the recipes there are not always entirely sufficient.
7227 * The current recipe is as follows:
7228 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7229 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7230 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7231 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7232 * class has final fields.
7233 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7234 * store-to-memory instructions. Only generated together with non-temporal stores.
7235 */
7236enum MemBarrierKind {
7237 kAnyStore,
7238 kLoadAny,
7239 kStoreStore,
7240 kAnyAny,
7241 kNTStoreStore,
7242 kLastBarrierKind = kNTStoreStore
7243};
7244std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7245
Vladimir Markobd785672018-05-03 17:09:09 +01007246class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007247 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007248 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007249 : HExpression(kMemoryBarrier,
7250 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7251 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007252 SetPackedField<BarrierKindField>(barrier_kind);
7253 }
Calin Juravle27df7582015-04-17 19:12:31 +01007254
Artem Serovcced8ba2017-07-19 18:18:09 +01007255 bool IsClonable() const OVERRIDE { return true; }
7256
Vladimir Markoa1de9182016-02-25 11:37:38 +00007257 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007258
7259 DECLARE_INSTRUCTION(MemoryBarrier);
7260
Artem Serovcced8ba2017-07-19 18:18:09 +01007261 protected:
7262 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7263
Calin Juravle27df7582015-04-17 19:12:31 +01007264 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007265 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7266 static constexpr size_t kFieldBarrierKindSize =
7267 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7268 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7269 kFieldBarrierKind + kFieldBarrierKindSize;
7270 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7271 "Too many packed fields.");
7272 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007273};
7274
Igor Murashkind01745e2017-04-05 16:40:31 -07007275// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7276// the specified object(s), with respect to any subsequent store that might "publish"
7277// (i.e. make visible) the specified object to another thread.
7278//
7279// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7280// for all final fields (that were set) at the end of the invoked constructor.
7281//
7282// The constructor fence models the freeze actions for the final fields of an object
7283// being constructed (semantically at the end of the constructor). Constructor fences
7284// have a per-object affinity; two separate objects being constructed get two separate
7285// constructor fences.
7286//
7287// (Note: that if calling a super-constructor or forwarding to another constructor,
7288// the freezes would happen at the end of *that* constructor being invoked).
7289//
7290// The memory model guarantees that when the object being constructed is "published" after
7291// constructor completion (i.e. escapes the current thread via a store), then any final field
7292// writes must be observable on other threads (once they observe that publication).
7293//
7294// Further, anything written before the freeze, and read by dereferencing through the final field,
7295// must also be visible (so final object field could itself have an object with non-final fields;
7296// yet the freeze must also extend to them).
7297//
7298// Constructor example:
7299//
7300// class HasFinal {
7301// final int field; Optimizing IR for <init>()V:
7302// HasFinal() {
7303// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7304// // freeze(this.field); HConstructorFence(this)
7305// } HReturn
7306// }
7307//
7308// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7309// already-initialized classes; in that case the allocation must act as a "default-initializer"
7310// of the object which effectively writes the class pointer "final field".
7311//
7312// For example, we can model default-initialiation as roughly the equivalent of the following:
7313//
7314// class Object {
7315// private final Class header;
7316// }
7317//
7318// Java code: Optimizing IR:
7319//
7320// T new_instance<T>() {
7321// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7322// obj.header = T.class; // header write is done by above call.
7323// // freeze(obj.header) HConstructorFence(obj)
7324// return (T)obj;
7325// }
7326//
7327// See also:
7328// * CompilerDriver::RequiresConstructorBarrier
7329// * QuasiAtomic::ThreadFenceForConstructor
7330//
7331class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7332 // A fence has variable inputs because the inputs can be removed
7333 // after prepare_for_register_allocation phase.
7334 // (TODO: In the future a fence could freeze multiple objects
7335 // after merging two fences together.)
7336 public:
7337 // `fence_object` is the reference that needs to be protected for correct publication.
7338 //
7339 // It makes sense in the following situations:
7340 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7341 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7342 //
7343 // After construction the `fence_object` becomes the 0th input.
7344 // This is not an input in a real sense, but just a convenient place to stash the information
7345 // about the associated object.
7346 HConstructorFence(HInstruction* fence_object,
7347 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007348 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007349 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7350 // constraints described in the class header. We claim that these SideEffects constraints
7351 // enforce a superset of the real constraints.
7352 //
7353 // The ordering described above is conservatively modeled with SideEffects as follows:
7354 //
7355 // * To prevent reordering of the publication stores:
7356 // ----> "Reads of objects" is the initial SideEffect.
7357 // * For every primitive final field store in the constructor:
7358 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7359 // * If there are any stores to reference final fields in the constructor:
7360 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7361 // that are reachable from `fence_object` also need to be prevented for reordering
7362 // (and we do not want to do alias analysis to figure out what those stores are).
7363 //
7364 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7365 // even more conservative approach than the one described above, and prevents all of the
7366 // above reordering without analyzing any of the instructions in the constructor.
7367 //
7368 // If in a later phase we discover that there are no writes to reference final fields,
7369 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307370 : HVariableInputSizeInstruction(kConstructorFence,
7371 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007372 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007373 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007374 /* number_of_inputs */ 1,
7375 kArenaAllocConstructorFenceInputs) {
7376 DCHECK(fence_object != nullptr);
7377 SetRawInputAt(0, fence_object);
7378 }
7379
7380 // The object associated with this constructor fence.
7381 //
7382 // (Note: This will be null after the prepare_for_register_allocation phase,
7383 // as all constructor fence inputs are removed there).
7384 HInstruction* GetFenceObject() const {
7385 return InputAt(0);
7386 }
7387
7388 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7389 // - Delete `fence_use` from `this`'s use list.
7390 // - Delete `this` from `fence_use`'s inputs list.
7391 // - If the `fence_use` is dead, remove it from the graph.
7392 //
7393 // A fence is considered dead once it no longer has any uses
7394 // and all of the inputs are dead.
7395 //
7396 // This must *not* be called during/after prepare_for_register_allocation,
7397 // because that removes all the inputs to the fences but the fence is actually
7398 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007399 //
7400 // Returns how many HConstructorFence instructions were removed from graph.
7401 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007402
Igor Murashkindd018df2017-08-09 10:38:31 -07007403 // Combine all inputs of `this` and `other` instruction and remove
7404 // `other` from the graph.
7405 //
7406 // Inputs are unique after the merge.
7407 //
7408 // Requirement: `this` must not be the same as `other.
7409 void Merge(HConstructorFence* other);
7410
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007411 // Check if this constructor fence is protecting
7412 // an HNewInstance or HNewArray that is also the immediate
7413 // predecessor of `this`.
7414 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007415 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7416 // to be one of the inputs of `this`.
7417 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007418 // Returns the associated HNewArray or HNewInstance,
7419 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007420 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007421
Igor Murashkind01745e2017-04-05 16:40:31 -07007422 DECLARE_INSTRUCTION(ConstructorFence);
7423
Artem Serovcced8ba2017-07-19 18:18:09 +01007424 protected:
7425 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007426};
7427
Vladimir Markobd785672018-05-03 17:09:09 +01007428class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007429 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007430 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007431 kEnter,
7432 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007433 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007434 };
7435
7436 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007437 : HExpression(kMonitorOperation,
7438 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7439 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007440 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007441 SetRawInputAt(0, object);
7442 }
7443
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007444 // Instruction may go into runtime, so we need an environment.
7445 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007446
7447 bool CanThrow() const OVERRIDE {
7448 // Verifier guarantees that monitor-exit cannot throw.
7449 // This is important because it allows the HGraphBuilder to remove
7450 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7451 return IsEnter();
7452 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007453
Vladimir Markoa1de9182016-02-25 11:37:38 +00007454 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7455 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007456
7457 DECLARE_INSTRUCTION(MonitorOperation);
7458
Artem Serovcced8ba2017-07-19 18:18:09 +01007459 protected:
7460 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7461
Calin Juravle52c48962014-12-16 17:02:57 +00007462 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007463 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7464 static constexpr size_t kFieldOperationKindSize =
7465 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7466 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7467 kFieldOperationKind + kFieldOperationKindSize;
7468 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7469 "Too many packed fields.");
7470 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007471};
7472
Vladimir Markofcb503c2016-05-18 12:48:17 +01007473class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007474 public:
7475 HSelect(HInstruction* condition,
7476 HInstruction* true_value,
7477 HInstruction* false_value,
7478 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307479 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007480 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7481
7482 // First input must be `true_value` or `false_value` to allow codegens to
7483 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7484 // that architectures which implement HSelect as a conditional move also
7485 // will not need to invert the condition.
7486 SetRawInputAt(0, false_value);
7487 SetRawInputAt(1, true_value);
7488 SetRawInputAt(2, condition);
7489 }
7490
Artem Serovcced8ba2017-07-19 18:18:09 +01007491 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007492 HInstruction* GetFalseValue() const { return InputAt(0); }
7493 HInstruction* GetTrueValue() const { return InputAt(1); }
7494 HInstruction* GetCondition() const { return InputAt(2); }
7495
7496 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007497 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7498 return true;
7499 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007500
7501 bool CanBeNull() const OVERRIDE {
7502 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7503 }
7504
7505 DECLARE_INSTRUCTION(Select);
7506
Artem Serovcced8ba2017-07-19 18:18:09 +01007507 protected:
7508 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007509};
7510
Vladimir Markof9f64412015-09-02 14:05:49 +01007511class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007512 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007513 MoveOperands(Location source,
7514 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007515 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007516 HInstruction* instruction)
7517 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007518
7519 Location GetSource() const { return source_; }
7520 Location GetDestination() const { return destination_; }
7521
7522 void SetSource(Location value) { source_ = value; }
7523 void SetDestination(Location value) { destination_ = value; }
7524
7525 // The parallel move resolver marks moves as "in-progress" by clearing the
7526 // destination (but not the source).
7527 Location MarkPending() {
7528 DCHECK(!IsPending());
7529 Location dest = destination_;
7530 destination_ = Location::NoLocation();
7531 return dest;
7532 }
7533
7534 void ClearPending(Location dest) {
7535 DCHECK(IsPending());
7536 destination_ = dest;
7537 }
7538
7539 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007540 DCHECK(source_.IsValid() || destination_.IsInvalid());
7541 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007542 }
7543
7544 // True if this blocks a move from the given location.
7545 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007546 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007547 }
7548
7549 // A move is redundant if it's been eliminated, if its source and
7550 // destination are the same, or if its destination is unneeded.
7551 bool IsRedundant() const {
7552 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7553 }
7554
7555 // We clear both operands to indicate move that's been eliminated.
7556 void Eliminate() {
7557 source_ = destination_ = Location::NoLocation();
7558 }
7559
7560 bool IsEliminated() const {
7561 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7562 return source_.IsInvalid();
7563 }
7564
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007565 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007566
Nicolas Geoffray90218252015-04-15 11:56:51 +01007567 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007568 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007569 }
7570
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007571 HInstruction* GetInstruction() const { return instruction_; }
7572
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007573 private:
7574 Location source_;
7575 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007576 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007577 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007578 // The instruction this move is assocatied with. Null when this move is
7579 // for moving an input in the expected locations of user (including a phi user).
7580 // This is only used in debug mode, to ensure we do not connect interval siblings
7581 // in the same parallel move.
7582 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007583};
7584
Roland Levillainc9285912015-12-18 10:38:42 +00007585std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7586
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007587static constexpr size_t kDefaultNumberOfMoves = 4;
7588
Vladimir Markobd785672018-05-03 17:09:09 +01007589class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007590 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007591 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007592 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007593 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007594 moves_.reserve(kDefaultNumberOfMoves);
7595 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007596
Nicolas Geoffray90218252015-04-15 11:56:51 +01007597 void AddMove(Location source,
7598 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007599 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007600 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007601 DCHECK(source.IsValid());
7602 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007603 if (kIsDebugBuild) {
7604 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007605 for (const MoveOperands& move : moves_) {
7606 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007607 // Special case the situation where the move is for the spill slot
7608 // of the instruction.
7609 if ((GetPrevious() == instruction)
7610 || ((GetPrevious() == nullptr)
7611 && instruction->IsPhi()
7612 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007613 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007614 << "Doing parallel moves for the same instruction.";
7615 } else {
7616 DCHECK(false) << "Doing parallel moves for the same instruction.";
7617 }
7618 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007619 }
7620 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007621 for (const MoveOperands& move : moves_) {
7622 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007623 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007624 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007625 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007626 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007627 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007628 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007629 }
7630
Vladimir Marko225b6462015-09-28 12:17:40 +01007631 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007632 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007633 }
7634
Vladimir Marko225b6462015-09-28 12:17:40 +01007635 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007636
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007637 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007638
Artem Serovcced8ba2017-07-19 18:18:09 +01007639 protected:
7640 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7641
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007642 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007643 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007644};
7645
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007646// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7647// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7648// never used across anything that can trigger GC.
7649// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7650// So we represent it by the type `DataType::Type::kInt`.
7651class HIntermediateAddress FINAL : public HExpression<2> {
7652 public:
7653 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307654 : HExpression(kIntermediateAddress,
7655 DataType::Type::kInt32,
7656 SideEffects::DependsOnGC(),
7657 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007658 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7659 DataType::Size(DataType::Type::kReference))
7660 << "kPrimInt and kPrimNot have different sizes.";
7661 SetRawInputAt(0, base_address);
7662 SetRawInputAt(1, offset);
7663 }
7664
Artem Serovcced8ba2017-07-19 18:18:09 +01007665 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007666 bool CanBeMoved() const OVERRIDE { return true; }
7667 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7668 return true;
7669 }
7670 bool IsActualObject() const OVERRIDE { return false; }
7671
7672 HInstruction* GetBaseAddress() const { return InputAt(0); }
7673 HInstruction* GetOffset() const { return InputAt(1); }
7674
7675 DECLARE_INSTRUCTION(IntermediateAddress);
7676
Artem Serovcced8ba2017-07-19 18:18:09 +01007677 protected:
7678 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007679};
7680
7681
Mark Mendell0616ae02015-04-17 12:49:27 -04007682} // namespace art
7683
Aart Bikf8f5a162017-02-06 15:35:29 -08007684#include "nodes_vector.h"
7685
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007686#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7687#include "nodes_shared.h"
7688#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007689#ifdef ART_ENABLE_CODEGEN_mips
7690#include "nodes_mips.h"
7691#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007692#ifdef ART_ENABLE_CODEGEN_x86
7693#include "nodes_x86.h"
7694#endif
7695
7696namespace art {
7697
Igor Murashkin6ef45672017-08-08 13:59:55 -07007698class OptimizingCompilerStats;
7699
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007700class HGraphVisitor : public ValueObject {
7701 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007702 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7703 : stats_(stats),
7704 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007705 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007706
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007707 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007708 virtual void VisitBasicBlock(HBasicBlock* block);
7709
Roland Levillain633021e2014-10-01 14:12:25 +01007710 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007711 void VisitInsertionOrder();
7712
Roland Levillain633021e2014-10-01 14:12:25 +01007713 // Visit the graph following dominator tree reverse post-order.
7714 void VisitReversePostOrder();
7715
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007716 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007717
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007718 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007719#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007720 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7721
7722 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7723
7724#undef DECLARE_VISIT_INSTRUCTION
7725
Igor Murashkin6ef45672017-08-08 13:59:55 -07007726 protected:
7727 OptimizingCompilerStats* stats_;
7728
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007729 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007730 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007731
7732 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7733};
7734
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007735class HGraphDelegateVisitor : public HGraphVisitor {
7736 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007737 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7738 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007739 virtual ~HGraphDelegateVisitor() {}
7740
7741 // Visit functions that delegate to to super class.
7742#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007743 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007744
7745 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7746
7747#undef DECLARE_VISIT_INSTRUCTION
7748
7749 private:
7750 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7751};
7752
Artem Serovcced8ba2017-07-19 18:18:09 +01007753// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7754// and remove the old instruction.
7755HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7756
7757// Create a clone for each clonable instructions/phis and replace the original with the clone.
7758//
7759// Used for testing individual instruction cloner.
7760class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7761 public:
7762 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007763 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007764
7765 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7766 if (instruction->IsClonable()) {
7767 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007768 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007769 }
7770 }
7771
Artem Serov7f4aff62017-06-21 17:02:18 +01007772 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007773
7774 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007775 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007776
7777 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7778};
7779
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007780// Iterator over the blocks that art part of the loop. Includes blocks part
7781// of an inner loop. The order in which the blocks are iterated is on their
7782// block id.
7783class HBlocksInLoopIterator : public ValueObject {
7784 public:
7785 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7786 : blocks_in_loop_(info.GetBlocks()),
7787 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7788 index_(0) {
7789 if (!blocks_in_loop_.IsBitSet(index_)) {
7790 Advance();
7791 }
7792 }
7793
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007794 bool Done() const { return index_ == blocks_.size(); }
7795 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007796 void Advance() {
7797 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007798 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007799 if (blocks_in_loop_.IsBitSet(index_)) {
7800 break;
7801 }
7802 }
7803 }
7804
7805 private:
7806 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007807 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007808 size_t index_;
7809
7810 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7811};
7812
Mingyao Yang3584bce2015-05-19 16:01:59 -07007813// Iterator over the blocks that art part of the loop. Includes blocks part
7814// of an inner loop. The order in which the blocks are iterated is reverse
7815// post order.
7816class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7817 public:
7818 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7819 : blocks_in_loop_(info.GetBlocks()),
7820 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7821 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007822 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007823 Advance();
7824 }
7825 }
7826
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007827 bool Done() const { return index_ == blocks_.size(); }
7828 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007829 void Advance() {
7830 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007831 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7832 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007833 break;
7834 }
7835 }
7836 }
7837
7838 private:
7839 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007840 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007841 size_t index_;
7842
7843 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7844};
7845
Aart Bikf3e61ee2017-04-12 17:09:20 -07007846// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007847inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007848 if (constant->IsIntConstant()) {
7849 return constant->AsIntConstant()->GetValue();
7850 } else if (constant->IsLongConstant()) {
7851 return constant->AsLongConstant()->GetValue();
7852 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007853 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007854 return 0;
7855 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007856}
7857
Aart Bikf3e61ee2017-04-12 17:09:20 -07007858// Returns true iff instruction is an integral constant (and sets value on success).
7859inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7860 if (instruction->IsIntConstant()) {
7861 *value = instruction->AsIntConstant()->GetValue();
7862 return true;
7863 } else if (instruction->IsLongConstant()) {
7864 *value = instruction->AsLongConstant()->GetValue();
7865 return true;
7866 } else if (instruction->IsNullConstant()) {
7867 *value = 0;
7868 return true;
7869 }
7870 return false;
7871}
7872
Aart Bik0148de42017-09-05 09:25:01 -07007873// Returns true iff instruction is the given integral constant.
7874inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7875 int64_t val = 0;
7876 return IsInt64AndGet(instruction, &val) && val == value;
7877}
7878
7879// Returns true iff instruction is a zero bit pattern.
7880inline bool IsZeroBitPattern(HInstruction* instruction) {
7881 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7882}
7883
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007884// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007885#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007886 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7887 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7888#undef INSTRUCTION_TYPE_CHECK
7889
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007890// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007891#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7892 std::is_base_of<BaseType, H##type>::value,
7893#define INSTRUCTION_TYPE_CHECK(type, super) \
7894 inline bool HInstruction::Is##type() const { \
7895 DCHECK_LT(GetKind(), kLastInstructionKind); \
7896 using BaseType = H##type; \
7897 static constexpr bool results[] = { \
7898 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7899 }; \
7900 return results[static_cast<size_t>(GetKind())]; \
7901 }
7902
7903 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7904#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007905#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007906
7907#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007908 inline const H##type* HInstruction::As##type() const { \
7909 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7910 } \
7911 inline H##type* HInstruction::As##type() { \
7912 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7913 }
7914
Vladimir Markoa90dd512018-05-04 15:04:45 +01007915 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7916#undef INSTRUCTION_TYPE_CAST
7917
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007918
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007919// Create space in `blocks` for adding `number_of_new_blocks` entries
7920// starting at location `at`. Blocks after `at` are moved accordingly.
7921inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7922 size_t number_of_new_blocks,
7923 size_t after) {
7924 DCHECK_LT(after, blocks->size());
7925 size_t old_size = blocks->size();
7926 size_t new_size = old_size + number_of_new_blocks;
7927 blocks->resize(new_size);
7928 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7929}
7930
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007931/*
7932 * Hunt "under the hood" of array lengths (leading to array references),
7933 * null checks (also leading to array references), and new arrays
7934 * (leading to the actual length). This makes it more likely related
7935 * instructions become actually comparable.
7936 */
7937inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7938 while (instruction->IsArrayLength() ||
7939 instruction->IsNullCheck() ||
7940 instruction->IsNewArray()) {
7941 instruction = instruction->IsNewArray()
7942 ? instruction->AsNewArray()->GetLength()
7943 : instruction->InputAt(0);
7944 }
7945 return instruction;
7946}
7947
Artem Serov21c7e6f2017-07-27 16:04:42 +01007948void RemoveEnvironmentUses(HInstruction* instruction);
7949bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7950void ResetEnvironmentInputRecords(HInstruction* instruction);
7951
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007952} // namespace art
7953
7954#endif // ART_COMPILER_OPTIMIZING_NODES_H_