blob: 096ca4d5e3be2577b9dc92df79957d9f456990d8 [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 Sehr8f4b0562018-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 Sehre6564f42018-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 Hodsonb426afb2018-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
David Brazdild7558da2015-09-22 13:04:14 +01001276 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 Bik1480f7f2018-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 Bik6e95b512018-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 Hodson20e7a6e2018-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 Hodson75046d12018-05-10 08:22:46 +01001388 M(LoadMethodHandle, Instruction) \
Orion Hodsonb426afb2018-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 Bik1c8fb9e2018-02-15 10:42:37 -08001392 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001393 M(MemoryBarrier, Instruction) \
Aart Bik1c8fb9e2018-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 Bik983b4472018-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 Markobe2667c2018-05-04 15:04:45 +01001542 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001543
Artem Serovcced8ba2017-07-19 18:18:09 +01001544#define DEFAULT_COPY_CONSTRUCTOR(type) \
1545 explicit H##type(const H##type& other) = default;
1546
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001547template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001548class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1549 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001550 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001551 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001552 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001553 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001554 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001555 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001556 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001557
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001558 private:
David Brazdiled596192015-01-23 10:39:45 +00001559 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001560 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001561
1562 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001563 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001564
Vladimir Marko46817b82016-03-29 12:21:58 +01001565 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001566
1567 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1568};
1569
David Brazdiled596192015-01-23 10:39:45 +00001570template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001571using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001572
David Brazdil1abb4192015-02-17 18:33:36 +00001573// This class is used by HEnvironment and HInstruction classes to record the
1574// instructions they use and pointers to the corresponding HUseListNodes kept
1575// by the used instructions.
1576template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001577class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001578 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001579 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1580 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001581
Vladimir Marko46817b82016-03-29 12:21:58 +01001582 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1583 : HUserRecord(old_record.instruction_, before_use_node) {}
1584 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1585 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001586 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001587 }
1588
1589 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001590 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1591 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001592
1593 private:
1594 // Instruction used by the user.
1595 HInstruction* instruction_;
1596
Vladimir Marko46817b82016-03-29 12:21:58 +01001597 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1598 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001599};
1600
Vladimir Markoe9004912016-06-16 16:50:52 +01001601// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1602// This is used for HInstruction::GetInputs() to return a container wrapper providing
1603// HInstruction* values even though the underlying container has HUserRecord<>s.
1604struct HInputExtractor {
1605 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1606 return record.GetInstruction();
1607 }
1608 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1609 return record.GetInstruction();
1610 }
1611};
1612
1613using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1614using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1615
Aart Bik854a02b2015-07-14 16:07:00 -07001616/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001618 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001619 * For write/read dependences on fields/arrays, the dependence analysis uses
1620 * type disambiguation (e.g. a float field write cannot modify the value of an
1621 * integer field read) and the access type (e.g. a reference array write cannot
1622 * modify the value of a reference field read [although it may modify the
1623 * reference fetch prior to reading the field, which is represented by its own
1624 * write/read dependence]). The analysis makes conservative points-to
1625 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1626 * the same, and any reference read depends on any reference read without
1627 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001628 *
Artem Serov34f8de32018-06-22 11:35:46 +01001629 * kDependsOnGCBit is defined in a following way: instruction with kDependsOnGCBit must not be
1630 * alive across the point where garbage collection might happen. kCanTriggerGCBit must be used for
1631 * instructions for which both are true that:
1632 * - GC might happen on this instruction.
1633 * - some IR instruction (from the compiler perspective) might be alive across that GC point.
1634 *
1635 * Notes:
1636 * - instructions with kCanTriggerGCBit do not depend on each other.
1637 * - instructions which can cause GC only on a fatal slow path do not need kCanTriggerGCBit as
1638 * the execution never returns to the instruction next to the exceptional one. However the
1639 * execution may return to compiled code if there is a catch block in the current method; for
1640 * this purpose TryBoundary instructuion has kCanTriggerGCBit set.
1641 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 * The internal representation uses 38-bit and is described in the table below.
1643 * The first line indicates the side effect, and for field/array accesses the
1644 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001645 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001646 * The two numbered lines below indicate the bit position in the bitfield (read
1647 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001648 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001649 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1650 * +-------------+---------+---------+--------------+---------+---------+
1651 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1652 * | 3 |333333322|222222221| 1 |111111110|000000000|
1653 * | 7 |654321098|765432109| 8 |765432109|876543210|
1654 *
1655 * Note that, to ease the implementation, 'changes' bits are least significant
1656 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001657 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001658class SideEffects : public ValueObject {
1659 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001660 SideEffects() : flags_(0) {}
1661
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001662 static SideEffects None() {
1663 return SideEffects(0);
1664 }
1665
1666 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001667 return SideEffects(kAllChangeBits | kAllDependOnBits);
1668 }
1669
1670 static SideEffects AllChanges() {
1671 return SideEffects(kAllChangeBits);
1672 }
1673
1674 static SideEffects AllDependencies() {
1675 return SideEffects(kAllDependOnBits);
1676 }
1677
1678 static SideEffects AllExceptGCDependency() {
1679 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1680 }
1681
1682 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001683 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684 }
1685
Aart Bik34c3ba92015-07-20 14:08:59 -07001686 static SideEffects AllWrites() {
1687 return SideEffects(kAllWrites);
1688 }
1689
1690 static SideEffects AllReads() {
1691 return SideEffects(kAllReads);
1692 }
1693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001694 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001695 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001696 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001697 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001698 }
1699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001700 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001701 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001702 }
1703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001704 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001705 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001706 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001707 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001708 }
1709
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001710 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001711 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001712 }
1713
Artem Serov34f8de32018-06-22 11:35:46 +01001714 // Returns whether GC might happen on the instruction and some other instructions might be alive
1715 // across it.
1716 //
1717 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001718 static SideEffects CanTriggerGC() {
1719 return SideEffects(1ULL << kCanTriggerGCBit);
1720 }
1721
Artem Serov34f8de32018-06-22 11:35:46 +01001722 // Returns whether the instruction must not be alive across a GC point.
1723 //
1724 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001725 static SideEffects DependsOnGC() {
1726 return SideEffects(1ULL << kDependsOnGCBit);
1727 }
1728
Aart Bik854a02b2015-07-14 16:07:00 -07001729 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001730 SideEffects Union(SideEffects other) const {
1731 return SideEffects(flags_ | other.flags_);
1732 }
1733
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001734 SideEffects Exclusion(SideEffects other) const {
1735 return SideEffects(flags_ & ~other.flags_);
1736 }
1737
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001738 void Add(SideEffects other) {
1739 flags_ |= other.flags_;
1740 }
1741
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001742 bool Includes(SideEffects other) const {
1743 return (other.flags_ & flags_) == other.flags_;
1744 }
1745
1746 bool HasSideEffects() const {
1747 return (flags_ & kAllChangeBits);
1748 }
1749
1750 bool HasDependencies() const {
1751 return (flags_ & kAllDependOnBits);
1752 }
1753
1754 // Returns true if there are no side effects or dependencies.
1755 bool DoesNothing() const {
1756 return flags_ == 0;
1757 }
1758
Aart Bik854a02b2015-07-14 16:07:00 -07001759 // Returns true if something is written.
1760 bool DoesAnyWrite() const {
1761 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001762 }
1763
Aart Bik854a02b2015-07-14 16:07:00 -07001764 // Returns true if something is read.
1765 bool DoesAnyRead() const {
1766 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001767 }
1768
Aart Bik854a02b2015-07-14 16:07:00 -07001769 // Returns true if potentially everything is written and read
1770 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001771 bool DoesAllReadWrite() const {
1772 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1773 }
1774
Aart Bik854a02b2015-07-14 16:07:00 -07001775 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001776 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001777 }
1778
Roland Levillain0d5a2812015-11-13 10:07:31 +00001779 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001780 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001781 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1782 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001783 }
1784
1785 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001786 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001787 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001788 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001789 for (int s = kLastBit; s >= 0; s--) {
1790 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1791 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1792 // This is a bit for the GC side effect.
1793 if (current_bit_is_set) {
1794 flags += "GC";
1795 }
Aart Bik854a02b2015-07-14 16:07:00 -07001796 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001797 } else {
1798 // This is a bit for the array/field analysis.
1799 // The underscore character stands for the 'can trigger GC' bit.
1800 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1801 if (current_bit_is_set) {
1802 flags += kDebug[s];
1803 }
1804 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1805 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1806 flags += "|";
1807 }
1808 }
Aart Bik854a02b2015-07-14 16:07:00 -07001809 }
1810 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001811 }
1812
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001813 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001814
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001815 private:
1816 static constexpr int kFieldArrayAnalysisBits = 9;
1817
1818 static constexpr int kFieldWriteOffset = 0;
1819 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1820 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1821 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1822
1823 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1824
1825 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1826 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1827 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1828 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1829
1830 static constexpr int kLastBit = kDependsOnGCBit;
1831 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1832
1833 // Aliases.
1834
1835 static_assert(kChangeBits == kDependOnBits,
1836 "the 'change' bits should match the 'depend on' bits.");
1837
1838 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1839 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1840 static constexpr uint64_t kAllWrites =
1841 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1842 static constexpr uint64_t kAllReads =
1843 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001844
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001845 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001846 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001847 int shift;
1848 switch (type) {
1849 case DataType::Type::kReference: shift = 0; break;
1850 case DataType::Type::kBool: shift = 1; break;
1851 case DataType::Type::kInt8: shift = 2; break;
1852 case DataType::Type::kUint16: shift = 3; break;
1853 case DataType::Type::kInt16: shift = 4; break;
1854 case DataType::Type::kInt32: shift = 5; break;
1855 case DataType::Type::kInt64: shift = 6; break;
1856 case DataType::Type::kFloat32: shift = 7; break;
1857 case DataType::Type::kFloat64: shift = 8; break;
1858 default:
1859 LOG(FATAL) << "Unexpected data type " << type;
1860 UNREACHABLE();
1861 }
Aart Bik854a02b2015-07-14 16:07:00 -07001862 DCHECK_LE(kFieldWriteOffset, shift);
1863 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001864 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001865 }
1866
1867 // Private constructor on direct flags value.
1868 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1869
1870 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001871};
1872
David Brazdiled596192015-01-23 10:39:45 +00001873// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001874class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001875 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001876 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001877 size_t number_of_vregs,
1878 ArtMethod* method,
1879 uint32_t dex_pc,
1880 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001881 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1882 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001883 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001884 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001885 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001886 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001887 }
1888
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001889 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1890 const HEnvironment& to_copy,
1891 HInstruction* holder)
1892 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001893 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001894 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001895 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001896 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001897
Vladimir Markoec32f642017-06-02 10:51:55 +01001898 void AllocateLocations() {
1899 DCHECK(locations_.empty());
1900 locations_.resize(vregs_.size());
1901 }
1902
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001903 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001904 if (parent_ != nullptr) {
1905 parent_->SetAndCopyParentChain(allocator, parent);
1906 } else {
1907 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1908 parent_->CopyFrom(parent);
1909 if (parent->GetParent() != nullptr) {
1910 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1911 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001912 }
David Brazdiled596192015-01-23 10:39:45 +00001913 }
1914
Vladimir Marko69d310e2017-10-09 14:12:23 +01001915 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001916 void CopyFrom(HEnvironment* environment);
1917
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001918 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1919 // input to the loop phi instead. This is for inserting instructions that
1920 // require an environment (like HDeoptimization) in the loop pre-header.
1921 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001922
1923 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001924 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001925 }
1926
1927 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001928 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001929 }
1930
David Brazdil1abb4192015-02-17 18:33:36 +00001931 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001932
Artem Serovdf7bbd92017-12-15 13:43:20 +00001933 // Replaces the input at the position 'index' with the replacement; the replacement and old
1934 // input instructions' env_uses_ lists are adjusted. The function works similar to
1935 // HInstruction::ReplaceInput.
1936 void ReplaceInput(HInstruction* replacement, size_t index);
1937
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001938 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001939
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001940 HEnvironment* GetParent() const { return parent_; }
1941
1942 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001943 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001944 }
1945
1946 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001947 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001948 }
1949
1950 uint32_t GetDexPc() const {
1951 return dex_pc_;
1952 }
1953
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001954 ArtMethod* GetMethod() const {
1955 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001956 }
1957
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001958 HInstruction* GetHolder() const {
1959 return holder_;
1960 }
1961
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001962
1963 bool IsFromInlinedInvoke() const {
1964 return GetParent() != nullptr;
1965 }
1966
David Brazdiled596192015-01-23 10:39:45 +00001967 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001968 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1969 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001970 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001971 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001972 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001973
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001974 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001975 HInstruction* const holder_;
1976
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001977 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001978
1979 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1980};
1981
Vladimir Markof9f64412015-09-02 14:05:49 +01001982class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001983 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301984#define DECLARE_KIND(type, super) k##type,
1985 enum InstructionKind {
Vladimir Marko6d1822b2018-05-04 14:18:47 +01001986 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301987 kLastInstructionKind
1988 };
1989#undef DECLARE_KIND
1990
1991 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01001992 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1993
1994 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001995 : previous_(nullptr),
1996 next_(nullptr),
1997 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001998 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001999 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002000 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002001 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002002 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002003 locations_(nullptr),
2004 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002005 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002006 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002007 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302008 SetPackedField<InstructionKindField>(kind);
Vladimir Markoc6760552018-05-03 17:09:09 +01002009 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002010 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2011 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002012
Dave Allison20dfc792014-06-16 20:44:29 -07002013 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002014
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002015
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002016 HInstruction* GetNext() const { return next_; }
2017 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018
Calin Juravle77520bc2015-01-12 18:45:46 +00002019 HInstruction* GetNextDisregardingMoves() const;
2020 HInstruction* GetPreviousDisregardingMoves() const;
2021
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002022 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002023 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002024 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002025 bool IsInBlock() const { return block_ != nullptr; }
2026 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002027 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2028 bool IsIrreducibleLoopHeaderPhi() const {
2029 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2030 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002031
Vladimir Marko372f10e2016-05-17 16:30:10 +01002032 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2033
2034 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2035 // One virtual method is enough, just const_cast<> and then re-add the const.
2036 return ArrayRef<const HUserRecord<HInstruction*>>(
2037 const_cast<HInstruction*>(this)->GetInputRecords());
2038 }
2039
Vladimir Markoe9004912016-06-16 16:50:52 +01002040 HInputsRef GetInputs() {
2041 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002042 }
2043
Vladimir Markoe9004912016-06-16 16:50:52 +01002044 HConstInputsRef GetInputs() const {
2045 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002046 }
2047
2048 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002049 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002050
Aart Bik2767f4b2016-10-28 15:03:53 -07002051 bool HasInput(HInstruction* input) const {
2052 for (const HInstruction* i : GetInputs()) {
2053 if (i == input) {
2054 return true;
2055 }
2056 }
2057 return false;
2058 }
2059
Vladimir Marko372f10e2016-05-17 16:30:10 +01002060 void SetRawInputAt(size_t index, HInstruction* input) {
2061 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2062 }
2063
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064 virtual void Accept(HGraphVisitor* visitor) = 0;
2065 virtual const char* DebugName() const = 0;
2066
Vladimir Markoc6760552018-05-03 17:09:09 +01002067 DataType::Type GetType() const {
2068 return TypeField::Decode(GetPackedFields());
2069 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002070
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002071 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002072
2073 uint32_t GetDexPc() const { return dex_pc_; }
2074
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002075 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002076
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002077 // Can the instruction throw?
2078 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2079 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002080 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002081
2082 // Does the instruction always throw an exception unconditionally?
2083 virtual bool AlwaysThrows() const { return false; }
2084
David Brazdilec16f792015-08-19 15:04:01 +01002085 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002086
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002087 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002088 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002089
Calin Juravle10e244f2015-01-26 18:54:32 +00002090 // Does not apply for all instructions, but having this at top level greatly
2091 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002092 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002093 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002094 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002095 return true;
2096 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002097
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002098 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002099 return false;
2100 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002101
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002102 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002103 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002104 }
2105
Calin Juravle2e768302015-07-28 14:41:11 +00002106 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002107
Calin Juravle61d544b2015-02-23 16:46:57 +00002108 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002110 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002111 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002112 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002113
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002114 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002115 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002116 // Note: fixup_end remains valid across push_front().
2117 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2118 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002119 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002120 uses_.push_front(*new_node);
2121 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002122 }
2123
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002124 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002125 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002126 // Note: env_fixup_end remains valid across push_front().
2127 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2128 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002129 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002130 env_uses_.push_front(*new_node);
2131 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002132 }
2133
David Brazdil1abb4192015-02-17 18:33:36 +00002134 void RemoveAsUserOfInput(size_t input) {
2135 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002136 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2137 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2138 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002139 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002140
Vladimir Marko372f10e2016-05-17 16:30:10 +01002141 void RemoveAsUserOfAllInputs() {
2142 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2143 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2144 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2145 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2146 }
2147 }
2148
David Brazdil1abb4192015-02-17 18:33:36 +00002149 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2150 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002151
Vladimir Marko46817b82016-03-29 12:21:58 +01002152 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2153 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2154 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002155 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002156 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002157 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002158
Aart Bikcc42be02016-10-20 16:14:16 -07002159 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002160 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002161 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002162 !CanThrow() &&
2163 !IsSuspendCheck() &&
2164 !IsControlFlow() &&
2165 !IsNativeDebugInfo() &&
2166 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002167 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002168 !IsMemoryBarrier() &&
2169 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002170 }
2171
Aart Bikcc42be02016-10-20 16:14:16 -07002172 bool IsDeadAndRemovable() const {
2173 return IsRemovable() && !HasUses();
2174 }
2175
Roland Levillain6c82d402014-10-13 16:10:27 +01002176 // Does this instruction strictly dominate `other_instruction`?
2177 // Returns false if this instruction and `other_instruction` are the same.
2178 // Aborts if this instruction and `other_instruction` are both phis.
2179 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002180
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002181 int GetId() const { return id_; }
2182 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002183
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002184 int GetSsaIndex() const { return ssa_index_; }
2185 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2186 bool HasSsaIndex() const { return ssa_index_ != -1; }
2187
2188 bool HasEnvironment() const { return environment_ != nullptr; }
2189 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002190 // Set the `environment_` field. Raw because this method does not
2191 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002192 void SetRawEnvironment(HEnvironment* environment) {
2193 DCHECK(environment_ == nullptr);
2194 DCHECK_EQ(environment->GetHolder(), this);
2195 environment_ = environment;
2196 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002197
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002198 void InsertRawEnvironment(HEnvironment* environment) {
2199 DCHECK(environment_ != nullptr);
2200 DCHECK_EQ(environment->GetHolder(), this);
2201 DCHECK(environment->GetParent() == nullptr);
2202 environment->parent_ = environment_;
2203 environment_ = environment;
2204 }
2205
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002206 void RemoveEnvironment();
2207
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002208 // Set the environment of this instruction, copying it from `environment`. While
2209 // copying, the uses lists are being updated.
2210 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002211 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002212 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002213 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002214 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002215 if (environment->GetParent() != nullptr) {
2216 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2217 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002218 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002219
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002220 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2221 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002222 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002223 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002224 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002225 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002226 if (environment->GetParent() != nullptr) {
2227 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2228 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002229 }
2230
Nicolas Geoffray39468442014-09-02 15:17:15 +01002231 // Returns the number of entries in the environment. Typically, that is the
2232 // number of dex registers in a method. It could be more in case of inlining.
2233 size_t EnvironmentSize() const;
2234
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002235 LocationSummary* GetLocations() const { return locations_; }
2236 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002237
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002238 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002239 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002240 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002241
Alexandre Rames188d4312015-04-09 18:30:21 +01002242 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2243 // uses of this instruction by `other` are *not* updated.
2244 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2245 ReplaceWith(other);
2246 other->ReplaceInput(this, use_index);
2247 }
2248
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002249 // Move `this` instruction before `cursor`
2250 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002251
Vladimir Markofb337ea2015-11-25 15:25:10 +00002252 // Move `this` before its first user and out of any loops. If there is no
2253 // out-of-loop user that dominates all other users, move the instruction
2254 // to the end of the out-of-loop common dominator of the user's blocks.
2255 //
2256 // This can be used only on non-throwing instructions with no side effects that
2257 // have at least one use but no environment uses.
2258 void MoveBeforeFirstUserAndOutOfLoops();
2259
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002260#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markobe2667c2018-05-04 15:04:45 +01002261 bool Is##type() const;
2262
2263 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2264#undef INSTRUCTION_TYPE_CHECK
2265
2266#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002267 const H##type* As##type() const; \
2268 H##type* As##type();
2269
Vladimir Markobe2667c2018-05-04 15:04:45 +01002270 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2271#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002272
Artem Serovcced8ba2017-07-19 18:18:09 +01002273 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2274 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2275 // the instruction then the behaviour of this function is undefined.
2276 //
2277 // Note: It is semantically valid to create a clone of the instruction only until
2278 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2279 // copied.
2280 //
2281 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2282 // 'explicit HInstruction(const HInstruction& other)' for details.
2283 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2284 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2285 DebugName() << " " << GetId();
2286 UNREACHABLE();
2287 }
2288
2289 // Return whether instruction can be cloned (copied).
2290 virtual bool IsClonable() const { return false; }
2291
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002292 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002293 // TODO: this method is used by LICM and GVN with possibly different
2294 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002295 virtual bool CanBeMoved() const { return false; }
2296
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002297 // Returns whether any data encoded in the two instructions is equal.
2298 // This method does not look at the inputs. Both instructions must be
2299 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002300 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002301 return false;
2302 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002303
2304 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002305 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002306 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002307 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002308
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002309 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2310 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2311 // the virtual function because the __attribute__((__pure__)) doesn't really
2312 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302313 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002314
2315 virtual size_t ComputeHashCode() const {
2316 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002317 for (const HInstruction* input : GetInputs()) {
2318 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002319 }
2320 return result;
2321 }
2322
2323 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002324 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002325 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002326
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002327 size_t GetLifetimePosition() const { return lifetime_position_; }
2328 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2329 LiveInterval* GetLiveInterval() const { return live_interval_; }
2330 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2331 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2332
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002333 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2334
2335 // Returns whether the code generation of the instruction will require to have access
2336 // to the current method. Such instructions are:
2337 // (1): Instructions that require an environment, as calling the runtime requires
2338 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002339 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2340 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002341 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002342 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002343 }
2344
Vladimir Markodc151b22015-10-15 18:02:30 +01002345 // Returns whether the code generation of the instruction will require to have access
2346 // to the dex cache of the current method's declaring class via the current method.
2347 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002348
Mark Mendellc4701932015-04-10 13:18:51 -04002349 // Does this instruction have any use in an environment before
2350 // control flow hits 'other'?
2351 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2352
2353 // Remove all references to environment uses of this instruction.
2354 // The caller must ensure that this is safe to do.
2355 void RemoveEnvironmentUsers();
2356
Vladimir Markoa1de9182016-02-25 11:37:38 +00002357 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2358 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002359
David Brazdil1abb4192015-02-17 18:33:36 +00002360 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002361 // If set, the machine code for this instruction is assumed to be generated by
2362 // its users. Used by liveness analysis to compute use positions accordingly.
2363 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2364 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302365 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2366 static constexpr size_t kFieldInstructionKindSize =
2367 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markoc6760552018-05-03 17:09:09 +01002368 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302369 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markoc6760552018-05-03 17:09:09 +01002370 static constexpr size_t kFieldTypeSize =
2371 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2372 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002373 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2374
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302375 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2376 "Too many generic packed fields");
2377
Vladimir Markoc6760552018-05-03 17:09:09 +01002378 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2379
Vladimir Marko372f10e2016-05-17 16:30:10 +01002380 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2381 return GetInputRecords()[i];
2382 }
2383
2384 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2385 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2386 input_records[index] = input;
2387 }
David Brazdil1abb4192015-02-17 18:33:36 +00002388
Vladimir Markoa1de9182016-02-25 11:37:38 +00002389 uint32_t GetPackedFields() const {
2390 return packed_fields_;
2391 }
2392
2393 template <size_t flag>
2394 bool GetPackedFlag() const {
2395 return (packed_fields_ & (1u << flag)) != 0u;
2396 }
2397
2398 template <size_t flag>
2399 void SetPackedFlag(bool value = true) {
2400 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2401 }
2402
2403 template <typename BitFieldType>
2404 typename BitFieldType::value_type GetPackedField() const {
2405 return BitFieldType::Decode(packed_fields_);
2406 }
2407
2408 template <typename BitFieldType>
2409 void SetPackedField(typename BitFieldType::value_type value) {
2410 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2411 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2412 }
2413
Artem Serovcced8ba2017-07-19 18:18:09 +01002414 // Copy construction for the instruction (used for Clone function).
2415 //
2416 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2417 // prepare_for_register_allocator are not copied (set to default values).
2418 //
2419 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2420 // fine for most of them. However for some of the instructions a custom copy constructor must be
2421 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2422 // for copying them).
2423 explicit HInstruction(const HInstruction& other)
2424 : previous_(nullptr),
2425 next_(nullptr),
2426 block_(nullptr),
2427 dex_pc_(other.dex_pc_),
2428 id_(-1),
2429 ssa_index_(-1),
2430 packed_fields_(other.packed_fields_),
2431 environment_(nullptr),
2432 locations_(nullptr),
2433 live_interval_(nullptr),
2434 lifetime_position_(kNoLifetime),
2435 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302436 reference_type_handle_(other.reference_type_handle_) {
2437 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002438
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002439 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302440 using InstructionKindField =
2441 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2442
Vladimir Marko46817b82016-03-29 12:21:58 +01002443 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2444 auto before_use_node = uses_.before_begin();
2445 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2446 HInstruction* user = use_node->GetUser();
2447 size_t input_index = use_node->GetIndex();
2448 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2449 before_use_node = use_node;
2450 }
2451 }
2452
2453 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2454 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2455 if (next != uses_.end()) {
2456 HInstruction* next_user = next->GetUser();
2457 size_t next_index = next->GetIndex();
2458 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2459 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2460 }
2461 }
2462
2463 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2464 auto before_env_use_node = env_uses_.before_begin();
2465 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2466 HEnvironment* user = env_use_node->GetUser();
2467 size_t input_index = env_use_node->GetIndex();
2468 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2469 before_env_use_node = env_use_node;
2470 }
2471 }
2472
2473 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2474 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2475 if (next != env_uses_.end()) {
2476 HEnvironment* next_user = next->GetUser();
2477 size_t next_index = next->GetIndex();
2478 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2479 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2480 }
2481 }
David Brazdil1abb4192015-02-17 18:33:36 +00002482
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483 HInstruction* previous_;
2484 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002485 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002486 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002487
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002488 // An instruction gets an id when it is added to the graph.
2489 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002490 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002491 int id_;
2492
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002493 // When doing liveness analysis, instructions that have uses get an SSA index.
2494 int ssa_index_;
2495
Vladimir Markoa1de9182016-02-25 11:37:38 +00002496 // Packed fields.
2497 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002498
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002499 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002500 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002501
2502 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002503 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002504
Nicolas Geoffray39468442014-09-02 15:17:15 +01002505 // The environment associated with this instruction. Not null if the instruction
2506 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002507 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002508
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002509 // Set by the code generator.
2510 LocationSummary* locations_;
2511
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002512 // Set by the liveness analysis.
2513 LiveInterval* live_interval_;
2514
2515 // Set by the liveness analysis, this is the position in a linear
2516 // order of blocks where this instruction's live interval start.
2517 size_t lifetime_position_;
2518
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002519 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002520
Vladimir Markoa1de9182016-02-25 11:37:38 +00002521 // The reference handle part of the reference type info.
2522 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002523 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002524 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002525
David Brazdil1abb4192015-02-17 18:33:36 +00002526 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002527 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002528 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002529 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002530 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002531};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002532std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002533
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002534// Iterates over the instructions, while preserving the next instruction
2535// in case the current instruction gets removed from the list by the user
2536// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002537class HInstructionIterator : public ValueObject {
2538 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002539 explicit HInstructionIterator(const HInstructionList& instructions)
2540 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002541 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002542 }
2543
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002544 bool Done() const { return instruction_ == nullptr; }
2545 HInstruction* Current() const { return instruction_; }
2546 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002547 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002548 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002549 }
2550
2551 private:
2552 HInstruction* instruction_;
2553 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002554
2555 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002556};
2557
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002558// Iterates over the instructions without saving the next instruction,
2559// therefore handling changes in the graph potentially made by the user
2560// of this iterator.
2561class HInstructionIteratorHandleChanges : public ValueObject {
2562 public:
2563 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2564 : instruction_(instructions.first_instruction_) {
2565 }
2566
2567 bool Done() const { return instruction_ == nullptr; }
2568 HInstruction* Current() const { return instruction_; }
2569 void Advance() {
2570 instruction_ = instruction_->GetNext();
2571 }
2572
2573 private:
2574 HInstruction* instruction_;
2575
2576 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2577};
2578
2579
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002580class HBackwardInstructionIterator : public ValueObject {
2581 public:
2582 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2583 : instruction_(instructions.last_instruction_) {
2584 next_ = Done() ? nullptr : instruction_->GetPrevious();
2585 }
2586
2587 bool Done() const { return instruction_ == nullptr; }
2588 HInstruction* Current() const { return instruction_; }
2589 void Advance() {
2590 instruction_ = next_;
2591 next_ = Done() ? nullptr : instruction_->GetPrevious();
2592 }
2593
2594 private:
2595 HInstruction* instruction_;
2596 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002597
2598 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002599};
2600
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002601class HVariableInputSizeInstruction : public HInstruction {
2602 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002603 using HInstruction::GetInputRecords; // Keep the const version visible.
2604 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2605 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2606 }
2607
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002608 void AddInput(HInstruction* input);
2609 void InsertInputAt(size_t index, HInstruction* input);
2610 void RemoveInputAt(size_t index);
2611
Igor Murashkind01745e2017-04-05 16:40:31 -07002612 // Removes all the inputs.
2613 // Also removes this instructions from each input's use list
2614 // (for non-environment uses only).
2615 void RemoveAllInputs();
2616
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002617 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302618 HVariableInputSizeInstruction(InstructionKind inst_kind,
2619 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002620 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002621 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002622 size_t number_of_inputs,
2623 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302624 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002625 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markoc6760552018-05-03 17:09:09 +01002626 HVariableInputSizeInstruction(InstructionKind inst_kind,
2627 DataType::Type type,
2628 SideEffects side_effects,
2629 uint32_t dex_pc,
2630 ArenaAllocator* allocator,
2631 size_t number_of_inputs,
2632 ArenaAllocKind kind)
2633 : HInstruction(inst_kind, type, side_effects, dex_pc),
2634 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002635
Artem Serovcced8ba2017-07-19 18:18:09 +01002636 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002637
Artem Serovcced8ba2017-07-19 18:18:09 +01002638 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002639};
2640
Vladimir Markof9f64412015-09-02 14:05:49 +01002641template<size_t N>
Vladimir Markoc6760552018-05-03 17:09:09 +01002642class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002643 public:
Vladimir Markoc6760552018-05-03 17:09:09 +01002644 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302645 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markoc6760552018-05-03 17:09:09 +01002646 HExpression<N>(InstructionKind kind,
2647 DataType::Type type,
2648 SideEffects side_effects,
2649 uint32_t dex_pc)
2650 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2651 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002652
Vladimir Marko372f10e2016-05-17 16:30:10 +01002653 using HInstruction::GetInputRecords; // Keep the const version visible.
2654 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2655 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002656 }
2657
Artem Serovcced8ba2017-07-19 18:18:09 +01002658 protected:
Vladimir Markoc6760552018-05-03 17:09:09 +01002659 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002660
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002661 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002662 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002663
2664 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002665};
2666
Vladimir Markoc6760552018-05-03 17:09:09 +01002667// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002668template<>
Vladimir Markoc6760552018-05-03 17:09:09 +01002669class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002670 public:
Vladimir Markoc6760552018-05-03 17:09:09 +01002671 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002672
Vladimir Markoc6760552018-05-03 17:09:09 +01002673 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002674
Vladimir Marko372f10e2016-05-17 16:30:10 +01002675 using HInstruction::GetInputRecords; // Keep the const version visible.
2676 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2677 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002678 }
2679
Artem Serovcced8ba2017-07-19 18:18:09 +01002680 protected:
Vladimir Markoc6760552018-05-03 17:09:09 +01002681 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002682
Vladimir Markof9f64412015-09-02 14:05:49 +01002683 private:
2684 friend class SsaBuilder;
2685};
2686
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002687// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2688// instruction that branches to the exit block.
Vladimir Markoc6760552018-05-03 17:09:09 +01002689class HReturnVoid FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002690 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002691 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01002692 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302693 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002694
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002695 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002696
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002697 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002698
Artem Serovcced8ba2017-07-19 18:18:09 +01002699 protected:
2700 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002701};
2702
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002703// Represents dex's RETURN opcodes. A HReturn is a control flow
2704// instruction that branches to the exit block.
Vladimir Markoc6760552018-05-03 17:09:09 +01002705class HReturn FINAL : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002706 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002707 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01002708 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002709 SetRawInputAt(0, value);
2710 }
2711
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002712 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002713
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002714 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002715
Artem Serovcced8ba2017-07-19 18:18:09 +01002716 protected:
2717 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002718};
2719
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002720class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002721 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002722 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002723 uint32_t reg_number,
2724 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002725 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002726 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002727 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302728 kPhi,
Vladimir Markoc6760552018-05-03 17:09:09 +01002729 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002730 SideEffects::None(),
2731 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002732 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002733 number_of_inputs,
2734 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002735 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002736 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002737 // Phis are constructed live and marked dead if conflicting or unused.
2738 // Individual steps of SsaBuilder should assume that if a phi has been
2739 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2740 SetPackedFlag<kFlagIsLive>(true);
2741 SetPackedFlag<kFlagCanBeNull>(true);
2742 }
2743
Artem Serovcced8ba2017-07-19 18:18:09 +01002744 bool IsClonable() const OVERRIDE { return true; }
2745
David Brazdildee58d62016-04-07 09:54:26 +00002746 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002747 static DataType::Type ToPhiType(DataType::Type type) {
2748 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002749 }
2750
2751 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002754 // Make sure that only valid type changes occur. The following are allowed:
2755 // (1) int -> float/ref (primitive type propagation),
2756 // (2) long -> double (primitive type propagation).
2757 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002758 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2759 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2760 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002761 SetPackedField<TypeField>(new_type);
2762 }
2763
2764 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2765 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2766
2767 uint32_t GetRegNumber() const { return reg_number_; }
2768
2769 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2770 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2771 bool IsDead() const { return !IsLive(); }
2772 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2773
Vladimir Markoe9004912016-06-16 16:50:52 +01002774 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002775 return other != nullptr
2776 && other->IsPhi()
2777 && other->AsPhi()->GetBlock() == GetBlock()
2778 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2779 }
2780
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002781 bool HasEquivalentPhi() const {
2782 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2783 return true;
2784 }
2785 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2786 return true;
2787 }
2788 return false;
2789 }
2790
David Brazdildee58d62016-04-07 09:54:26 +00002791 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2792 // An equivalent phi is a phi having the same dex register and type.
2793 // It assumes that phis with the same dex register are adjacent.
2794 HPhi* GetNextEquivalentPhiWithSameType() {
2795 HInstruction* next = GetNext();
2796 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2797 if (next->GetType() == GetType()) {
2798 return next->AsPhi();
2799 }
2800 next = next->GetNext();
2801 }
2802 return nullptr;
2803 }
2804
2805 DECLARE_INSTRUCTION(Phi);
2806
Artem Serovcced8ba2017-07-19 18:18:09 +01002807 protected:
2808 DEFAULT_COPY_CONSTRUCTOR(Phi);
2809
David Brazdildee58d62016-04-07 09:54:26 +00002810 private:
Vladimir Markoc6760552018-05-03 17:09:09 +01002811 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002812 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2813 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2814 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002815
David Brazdildee58d62016-04-07 09:54:26 +00002816 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002817};
2818
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002819// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002820// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002821// exit block.
Vladimir Markoc6760552018-05-03 17:09:09 +01002822class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002823 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302824 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01002825 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302826 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002827
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002828 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002829
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002830 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002831
Artem Serovcced8ba2017-07-19 18:18:09 +01002832 protected:
2833 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002834};
2835
2836// Jumps from one block to another.
Vladimir Markoc6760552018-05-03 17:09:09 +01002837class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002838 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302839 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01002840 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302841 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002842
Artem Serovcced8ba2017-07-19 18:18:09 +01002843 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002844 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002845
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002846 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002847 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002848 }
2849
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002850 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002851
Artem Serovcced8ba2017-07-19 18:18:09 +01002852 protected:
2853 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002854};
2855
Roland Levillain9867bc72015-08-05 10:21:34 +01002856class HConstant : public HExpression<0> {
2857 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302858 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2859 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2860 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002861
2862 bool CanBeMoved() const OVERRIDE { return true; }
2863
Roland Levillain1a653882016-03-18 18:05:57 +00002864 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002865 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002866 // Is this constant 0 in the arithmetic sense?
2867 virtual bool IsArithmeticZero() const { return false; }
2868 // Is this constant a 0-bit pattern?
2869 virtual bool IsZeroBitPattern() const { return false; }
2870 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002871 virtual bool IsOne() const { return false; }
2872
David Brazdil77a48ae2015-09-15 12:34:04 +00002873 virtual uint64_t GetValueAsUint64() const = 0;
2874
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002875 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002876
Artem Serovcced8ba2017-07-19 18:18:09 +01002877 protected:
2878 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002879};
2880
Vladimir Markofcb503c2016-05-18 12:48:17 +01002881class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002882 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002883 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002884 return true;
2885 }
2886
David Brazdil77a48ae2015-09-15 12:34:04 +00002887 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2888
Roland Levillain9867bc72015-08-05 10:21:34 +01002889 size_t ComputeHashCode() const OVERRIDE { return 0; }
2890
Roland Levillain1a653882016-03-18 18:05:57 +00002891 // The null constant representation is a 0-bit pattern.
2892 virtual bool IsZeroBitPattern() const { return true; }
2893
Roland Levillain9867bc72015-08-05 10:21:34 +01002894 DECLARE_INSTRUCTION(NullConstant);
2895
Artem Serovcced8ba2017-07-19 18:18:09 +01002896 protected:
2897 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2898
Roland Levillain9867bc72015-08-05 10:21:34 +01002899 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002900 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302901 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2902 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002903
2904 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002905};
2906
2907// Constants of the type int. Those can be from Dex instructions, or
2908// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002909class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 public:
2911 int32_t GetValue() const { return value_; }
2912
David Brazdil9f389d42015-10-01 14:32:56 +01002913 uint64_t GetValueAsUint64() const OVERRIDE {
2914 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2915 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002916
Vladimir Marko372f10e2016-05-17 16:30:10 +01002917 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002918 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002919 return other->AsIntConstant()->value_ == value_;
2920 }
2921
2922 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2923
2924 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002925 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2926 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002927 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2928
Roland Levillain1a653882016-03-18 18:05:57 +00002929 // Integer constants are used to encode Boolean values as well,
2930 // where 1 means true and 0 means false.
2931 bool IsTrue() const { return GetValue() == 1; }
2932 bool IsFalse() const { return GetValue() == 0; }
2933
Roland Levillain9867bc72015-08-05 10:21:34 +01002934 DECLARE_INSTRUCTION(IntConstant);
2935
Artem Serovcced8ba2017-07-19 18:18:09 +01002936 protected:
2937 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2938
Roland Levillain9867bc72015-08-05 10:21:34 +01002939 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002940 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302941 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2942 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002943 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302944 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2945 value_(value ? 1 : 0) {
2946 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002947
2948 const int32_t value_;
2949
2950 friend class HGraph;
2951 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2952 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002953};
2954
Vladimir Markofcb503c2016-05-18 12:48:17 +01002955class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002956 public:
2957 int64_t GetValue() const { return value_; }
2958
David Brazdil77a48ae2015-09-15 12:34:04 +00002959 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2960
Vladimir Marko372f10e2016-05-17 16:30:10 +01002961 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002962 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002963 return other->AsLongConstant()->value_ == value_;
2964 }
2965
2966 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2967
2968 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002969 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2970 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002971 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2972
2973 DECLARE_INSTRUCTION(LongConstant);
2974
Artem Serovcced8ba2017-07-19 18:18:09 +01002975 protected:
2976 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2977
Roland Levillain9867bc72015-08-05 10:21:34 +01002978 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002979 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302980 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2981 value_(value) {
2982 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002983
2984 const int64_t value_;
2985
2986 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002987};
Dave Allison20dfc792014-06-16 20:44:29 -07002988
Vladimir Markofcb503c2016-05-18 12:48:17 +01002989class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002990 public:
2991 float GetValue() const { return value_; }
2992
2993 uint64_t GetValueAsUint64() const OVERRIDE {
2994 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2995 }
2996
Vladimir Marko372f10e2016-05-17 16:30:10 +01002997 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002998 DCHECK(other->IsFloatConstant()) << other->DebugName();
2999 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3000 }
3001
3002 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3003
3004 bool IsMinusOne() const OVERRIDE {
3005 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3006 }
Roland Levillain1a653882016-03-18 18:05:57 +00003007 bool IsArithmeticZero() const OVERRIDE {
3008 return std::fpclassify(value_) == FP_ZERO;
3009 }
3010 bool IsArithmeticPositiveZero() const {
3011 return IsArithmeticZero() && !std::signbit(value_);
3012 }
3013 bool IsArithmeticNegativeZero() const {
3014 return IsArithmeticZero() && std::signbit(value_);
3015 }
3016 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003017 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018 }
3019 bool IsOne() const OVERRIDE {
3020 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3021 }
3022 bool IsNaN() const {
3023 return std::isnan(value_);
3024 }
3025
3026 DECLARE_INSTRUCTION(FloatConstant);
3027
Artem Serovcced8ba2017-07-19 18:18:09 +01003028 protected:
3029 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3030
Roland Levillain31dd3d62016-02-16 12:21:02 +00003031 private:
3032 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303033 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3034 value_(value) {
3035 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303037 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3038 value_(bit_cast<float, int32_t>(value)) {
3039 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003040
3041 const float value_;
3042
3043 // Only the SsaBuilder and HGraph can create floating-point constants.
3044 friend class SsaBuilder;
3045 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003046};
3047
Vladimir Markofcb503c2016-05-18 12:48:17 +01003048class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003049 public:
3050 double GetValue() const { return value_; }
3051
3052 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3053
Vladimir Marko372f10e2016-05-17 16:30:10 +01003054 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003055 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3056 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3057 }
3058
3059 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3060
3061 bool IsMinusOne() const OVERRIDE {
3062 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3063 }
Roland Levillain1a653882016-03-18 18:05:57 +00003064 bool IsArithmeticZero() const OVERRIDE {
3065 return std::fpclassify(value_) == FP_ZERO;
3066 }
3067 bool IsArithmeticPositiveZero() const {
3068 return IsArithmeticZero() && !std::signbit(value_);
3069 }
3070 bool IsArithmeticNegativeZero() const {
3071 return IsArithmeticZero() && std::signbit(value_);
3072 }
3073 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003074 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003075 }
3076 bool IsOne() const OVERRIDE {
3077 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3078 }
3079 bool IsNaN() const {
3080 return std::isnan(value_);
3081 }
3082
3083 DECLARE_INSTRUCTION(DoubleConstant);
3084
Artem Serovcced8ba2017-07-19 18:18:09 +01003085 protected:
3086 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3087
Roland Levillain31dd3d62016-02-16 12:21:02 +00003088 private:
3089 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303090 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3091 value_(value) {
3092 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003093 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303094 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3095 value_(bit_cast<double, int64_t>(value)) {
3096 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003097
3098 const double value_;
3099
3100 // Only the SsaBuilder and HGraph can create floating-point constants.
3101 friend class SsaBuilder;
3102 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003103};
3104
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003105// Conditional branch. A block ending with an HIf instruction must have
3106// two successors.
Vladimir Markoc6760552018-05-03 17:09:09 +01003107class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003108 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003109 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01003110 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003111 SetRawInputAt(0, input);
3112 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003113
Artem Serovcced8ba2017-07-19 18:18:09 +01003114 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003115 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003116
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003117 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003118 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003119 }
3120
3121 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003122 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003123 }
3124
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003125 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003126
Artem Serovcced8ba2017-07-19 18:18:09 +01003127 protected:
3128 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003129};
3130
David Brazdilfc6a86a2015-06-26 10:33:45 +00003131
3132// Abstract instruction which marks the beginning and/or end of a try block and
3133// links it to the respective exception handlers. Behaves the same as a Goto in
3134// non-exceptional control flow.
3135// Normal-flow successor is stored at index zero, exception handlers under
3136// higher indices in no particular order.
Vladimir Markoc6760552018-05-03 17:09:09 +01003137class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003138 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003139 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003140 kEntry,
3141 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003142 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003143 };
3144
Artem Serov34f8de32018-06-22 11:35:46 +01003145 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3146 // across the catch block boundaries as GC might happen during throwing an exception.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003147 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serov34f8de32018-06-22 11:35:46 +01003148 : HExpression(kTryBoundary, SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003149 SetPackedField<BoundaryKindField>(kind);
3150 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003151
3152 bool IsControlFlow() const OVERRIDE { return true; }
3153
3154 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003155 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003156
David Brazdild26a4112015-11-10 11:07:31 +00003157 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3158 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3159 }
3160
David Brazdilfc6a86a2015-06-26 10:33:45 +00003161 // Returns whether `handler` is among its exception handlers (non-zero index
3162 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003163 bool HasExceptionHandler(const HBasicBlock& handler) const {
3164 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003165 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003166 }
3167
3168 // If not present already, adds `handler` to its block's list of exception
3169 // handlers.
3170 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003171 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003172 GetBlock()->AddSuccessor(handler);
3173 }
3174 }
3175
Vladimir Markoa1de9182016-02-25 11:37:38 +00003176 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3177 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003178
David Brazdilffee3d32015-07-06 11:48:53 +01003179 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3180
David Brazdilfc6a86a2015-06-26 10:33:45 +00003181 DECLARE_INSTRUCTION(TryBoundary);
3182
Artem Serovcced8ba2017-07-19 18:18:09 +01003183 protected:
3184 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3185
David Brazdilfc6a86a2015-06-26 10:33:45 +00003186 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003187 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3188 static constexpr size_t kFieldBoundaryKindSize =
3189 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3190 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3191 kFieldBoundaryKind + kFieldBoundaryKindSize;
3192 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3193 "Too many packed fields.");
3194 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003195};
3196
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003197// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003198class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003199 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003200 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3201 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003202 HDeoptimize(ArenaAllocator* allocator,
3203 HInstruction* cond,
3204 DeoptimizationKind kind,
3205 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003206 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303207 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003208 SideEffects::All(),
3209 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003210 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003211 /* number_of_inputs */ 1,
3212 kArenaAllocMisc) {
3213 SetPackedFlag<kFieldCanBeMoved>(false);
3214 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003215 SetRawInputAt(0, cond);
3216 }
3217
Artem Serovcced8ba2017-07-19 18:18:09 +01003218 bool IsClonable() const OVERRIDE { return true; }
3219
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003220 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3221 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3222 // instead of `guard`.
3223 // We set CanTriggerGC to prevent any intermediate address to be live
3224 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003225 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003226 HInstruction* cond,
3227 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003228 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003229 uint32_t dex_pc)
3230 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303231 kDeoptimize,
Vladimir Markoc6760552018-05-03 17:09:09 +01003232 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003233 SideEffects::CanTriggerGC(),
3234 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003235 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003236 /* number_of_inputs */ 2,
3237 kArenaAllocMisc) {
3238 SetPackedFlag<kFieldCanBeMoved>(true);
3239 SetPackedField<DeoptimizeKindField>(kind);
3240 SetRawInputAt(0, cond);
3241 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003242 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003243
3244 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3245
3246 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3247 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3248 }
3249
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003250 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003251
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003252 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003253
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003254 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003255
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003256 bool GuardsAnInput() const {
3257 return InputCount() == 2;
3258 }
3259
3260 HInstruction* GuardedInput() const {
3261 DCHECK(GuardsAnInput());
3262 return InputAt(1);
3263 }
3264
3265 void RemoveGuard() {
3266 RemoveInputAt(1);
3267 }
3268
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003269 DECLARE_INSTRUCTION(Deoptimize);
3270
Artem Serovcced8ba2017-07-19 18:18:09 +01003271 protected:
3272 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3273
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003274 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003275 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3276 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3277 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003278 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003279 static constexpr size_t kNumberOfDeoptimizePackedBits =
3280 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3281 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3282 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003283 using DeoptimizeKindField =
3284 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003285};
3286
Mingyao Yang063fc772016-08-02 11:02:54 -07003287// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3288// The compiled code checks this flag value in a guard before devirtualized call and
3289// if it's true, starts to do deoptimization.
3290// It has a 4-byte slot on stack.
3291// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003292class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003293 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003294 // CHA guards are only optimized in a separate pass and it has no side effects
3295 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003296 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303297 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markoc6760552018-05-03 17:09:09 +01003298 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303299 SideEffects::None(),
3300 dex_pc,
3301 allocator,
3302 0,
3303 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003304 }
3305
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003306 // We do all CHA guard elimination/motion in a single pass, after which there is no
3307 // further guard elimination/motion since a guard might have been used for justification
3308 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3309 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003310 bool CanBeMoved() const OVERRIDE { return false; }
3311
3312 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3313
Artem Serovcced8ba2017-07-19 18:18:09 +01003314 protected:
3315 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003316};
3317
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003318// Represents the ArtMethod that was passed as a first argument to
3319// the method. It is used by instructions that depend on it, like
3320// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003321class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003322 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003323 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303324 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3325 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003326
3327 DECLARE_INSTRUCTION(CurrentMethod);
3328
Artem Serovcced8ba2017-07-19 18:18:09 +01003329 protected:
3330 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003331};
3332
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003333// Fetches an ArtMethod from the virtual table or the interface method table
3334// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003335class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003336 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003337 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003338 kVTable,
3339 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003340 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003341 };
3342 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003343 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003344 TableKind kind,
3345 size_t index,
3346 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303347 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003348 index_(index) {
3349 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003350 SetRawInputAt(0, cls);
3351 }
3352
Artem Serovcced8ba2017-07-19 18:18:09 +01003353 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003354 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003355 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003356 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003357 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003358 }
3359
Vladimir Markoa1de9182016-02-25 11:37:38 +00003360 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003361 size_t GetIndex() const { return index_; }
3362
3363 DECLARE_INSTRUCTION(ClassTableGet);
3364
Artem Serovcced8ba2017-07-19 18:18:09 +01003365 protected:
3366 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3367
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003368 private:
Vladimir Markoc6760552018-05-03 17:09:09 +01003369 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003370 static constexpr size_t kFieldTableKindSize =
3371 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3372 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3373 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3374 "Too many packed fields.");
3375 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3376
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003377 // The index of the ArtMethod in the table.
3378 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003379};
3380
Mark Mendellfe57faa2015-09-18 09:26:15 -04003381// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3382// have one successor for each entry in the switch table, and the final successor
3383// will be the block containing the next Dex opcode.
Vladimir Markoc6760552018-05-03 17:09:09 +01003384class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003385 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003386 HPackedSwitch(int32_t start_value,
3387 uint32_t num_entries,
3388 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003389 uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01003390 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003391 start_value_(start_value),
3392 num_entries_(num_entries) {
3393 SetRawInputAt(0, input);
3394 }
3395
Artem Serovcced8ba2017-07-19 18:18:09 +01003396 bool IsClonable() const OVERRIDE { return true; }
3397
Mark Mendellfe57faa2015-09-18 09:26:15 -04003398 bool IsControlFlow() const OVERRIDE { return true; }
3399
3400 int32_t GetStartValue() const { return start_value_; }
3401
Vladimir Marko211c2112015-09-24 16:52:33 +01003402 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003403
3404 HBasicBlock* GetDefaultBlock() const {
3405 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003406 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003407 }
3408 DECLARE_INSTRUCTION(PackedSwitch);
3409
Artem Serovcced8ba2017-07-19 18:18:09 +01003410 protected:
3411 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3412
Mark Mendellfe57faa2015-09-18 09:26:15 -04003413 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003414 const int32_t start_value_;
3415 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003416};
3417
Roland Levillain88cb1752014-10-20 16:36:47 +01003418class HUnaryOperation : public HExpression<1> {
3419 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303420 HUnaryOperation(InstructionKind kind,
3421 DataType::Type result_type,
3422 HInstruction* input,
3423 uint32_t dex_pc = kNoDexPc)
3424 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003425 SetRawInputAt(0, input);
3426 }
3427
Artem Serovcced8ba2017-07-19 18:18:09 +01003428 // All of the UnaryOperation instructions are clonable.
3429 bool IsClonable() const OVERRIDE { return true; }
3430
Roland Levillain88cb1752014-10-20 16:36:47 +01003431 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003432 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003433
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003434 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003435 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003436 return true;
3437 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003438
Roland Levillain31dd3d62016-02-16 12:21:02 +00003439 // Try to statically evaluate `this` and return a HConstant
3440 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003441 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003442 HConstant* TryStaticEvaluation() const;
3443
3444 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003445 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3446 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003447 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3448 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003449
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003450 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003451
Artem Serovcced8ba2017-07-19 18:18:09 +01003452 protected:
3453 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003454};
3455
Dave Allison20dfc792014-06-16 20:44:29 -07003456class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003457 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303458 HBinaryOperation(InstructionKind kind,
3459 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003460 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003461 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003462 SideEffects side_effects = SideEffects::None(),
3463 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303464 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003465 SetRawInputAt(0, left);
3466 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003467 }
3468
Artem Serovcced8ba2017-07-19 18:18:09 +01003469 // All of the BinaryOperation instructions are clonable.
3470 bool IsClonable() const OVERRIDE { return true; }
3471
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003472 HInstruction* GetLeft() const { return InputAt(0); }
3473 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003474 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003475
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003476 virtual bool IsCommutative() const { return false; }
3477
3478 // Put constant on the right.
3479 // Returns whether order is changed.
3480 bool OrderInputsWithConstantOnTheRight() {
3481 HInstruction* left = InputAt(0);
3482 HInstruction* right = InputAt(1);
3483 if (left->IsConstant() && !right->IsConstant()) {
3484 ReplaceInput(right, 0);
3485 ReplaceInput(left, 1);
3486 return true;
3487 }
3488 return false;
3489 }
3490
3491 // Order inputs by instruction id, but favor constant on the right side.
3492 // This helps GVN for commutative ops.
3493 void OrderInputs() {
3494 DCHECK(IsCommutative());
3495 HInstruction* left = InputAt(0);
3496 HInstruction* right = InputAt(1);
3497 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3498 return;
3499 }
3500 if (OrderInputsWithConstantOnTheRight()) {
3501 return;
3502 }
3503 // Order according to instruction id.
3504 if (left->GetId() > right->GetId()) {
3505 ReplaceInput(right, 0);
3506 ReplaceInput(left, 1);
3507 }
3508 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003509
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003510 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003511 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003512 return true;
3513 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003514
Roland Levillain31dd3d62016-02-16 12:21:02 +00003515 // Try to statically evaluate `this` and return a HConstant
3516 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003517 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003518 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003519
3520 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003521 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3522 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003523 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3524 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003525 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003526 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3527 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003528 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3529 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003530 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3531 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003532 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3534 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003535
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003536 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003537 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003538 HConstant* GetConstantRight() const;
3539
3540 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003541 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003542 HInstruction* GetLeastConstantLeft() const;
3543
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003544 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003545
Artem Serovcced8ba2017-07-19 18:18:09 +01003546 protected:
3547 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003548};
3549
Mark Mendellc4701932015-04-10 13:18:51 -04003550// The comparison bias applies for floating point operations and indicates how NaN
3551// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003552enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003553 kNoBias, // bias is not applicable (i.e. for long operation)
3554 kGtBias, // return 1 for NaN comparisons
3555 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003556 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003557};
3558
Roland Levillain31dd3d62016-02-16 12:21:02 +00003559std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3560
Dave Allison20dfc792014-06-16 20:44:29 -07003561class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003562 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303563 HCondition(InstructionKind kind,
3564 HInstruction* first,
3565 HInstruction* second,
3566 uint32_t dex_pc = kNoDexPc)
3567 : HBinaryOperation(kind,
3568 DataType::Type::kBool,
3569 first,
3570 second,
3571 SideEffects::None(),
3572 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003573 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3574 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003575
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003576 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003577 // `instruction`, and disregard moves in between.
3578 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003579
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003580 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003581
3582 virtual IfCondition GetCondition() const = 0;
3583
Mark Mendellc4701932015-04-10 13:18:51 -04003584 virtual IfCondition GetOppositeCondition() const = 0;
3585
Vladimir Markoa1de9182016-02-25 11:37:38 +00003586 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003587 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3588
Vladimir Markoa1de9182016-02-25 11:37:38 +00003589 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3590 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003591
Vladimir Marko372f10e2016-05-17 16:30:10 +01003592 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003593 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003594 }
3595
Roland Levillain4fa13f62015-07-06 18:11:54 +01003596 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003597 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003598 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003599 if (if_cond == kCondNE) {
3600 return true;
3601 } else if (if_cond == kCondEQ) {
3602 return false;
3603 }
3604 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003605 }
3606
3607 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003608 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003609 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003610 if (if_cond == kCondEQ) {
3611 return true;
3612 } else if (if_cond == kCondNE) {
3613 return false;
3614 }
3615 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003616 }
3617
Roland Levillain31dd3d62016-02-16 12:21:02 +00003618 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003619 // Needed if we merge a HCompare into a HCondition.
Vladimir Markoc6760552018-05-03 17:09:09 +01003620 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003621 static constexpr size_t kFieldComparisonBiasSize =
3622 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3623 static constexpr size_t kNumberOfConditionPackedBits =
3624 kFieldComparisonBias + kFieldComparisonBiasSize;
3625 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3626 using ComparisonBiasField =
3627 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3628
Roland Levillain31dd3d62016-02-16 12:21:02 +00003629 template <typename T>
3630 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3631
3632 template <typename T>
3633 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003634 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003635 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3636 // Handle the bias.
3637 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3638 }
3639
3640 // Return an integer constant containing the result of a condition evaluated at compile time.
3641 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3642 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3643 }
3644
Artem Serovcced8ba2017-07-19 18:18:09 +01003645 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003646};
3647
3648// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003649class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003650 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003651 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303652 : HCondition(kEqual, first, second, dex_pc) {
3653 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003654
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003655 bool IsCommutative() const OVERRIDE { return true; }
3656
Vladimir Marko9e23df52015-11-10 17:14:35 +00003657 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3658 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003659 return MakeConstantCondition(true, GetDexPc());
3660 }
3661 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3662 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3663 }
3664 // In the following Evaluate methods, a HCompare instruction has
3665 // been merged into this HEqual instruction; evaluate it as
3666 // `Compare(x, y) == 0`.
3667 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3668 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3669 GetDexPc());
3670 }
3671 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3672 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3673 }
3674 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3675 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003676 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003677
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003678 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003680 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003681 return kCondEQ;
3682 }
3683
Mark Mendellc4701932015-04-10 13:18:51 -04003684 IfCondition GetOppositeCondition() const OVERRIDE {
3685 return kCondNE;
3686 }
3687
Artem Serovcced8ba2017-07-19 18:18:09 +01003688 protected:
3689 DEFAULT_COPY_CONSTRUCTOR(Equal);
3690
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003691 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003692 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003693};
3694
Vladimir Markofcb503c2016-05-18 12:48:17 +01003695class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003696 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303697 HNotEqual(HInstruction* first, HInstruction* second,
3698 uint32_t dex_pc = kNoDexPc)
3699 : HCondition(kNotEqual, first, second, dex_pc) {
3700 }
Dave Allison20dfc792014-06-16 20:44:29 -07003701
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003702 bool IsCommutative() const OVERRIDE { return true; }
3703
Vladimir Marko9e23df52015-11-10 17:14:35 +00003704 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3705 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003706 return MakeConstantCondition(false, GetDexPc());
3707 }
3708 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3709 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3710 }
3711 // In the following Evaluate methods, a HCompare instruction has
3712 // been merged into this HNotEqual instruction; evaluate it as
3713 // `Compare(x, y) != 0`.
3714 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3715 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3716 }
3717 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3718 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3719 }
3720 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3721 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003723
Dave Allison20dfc792014-06-16 20:44:29 -07003724 DECLARE_INSTRUCTION(NotEqual);
3725
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003726 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003727 return kCondNE;
3728 }
3729
Mark Mendellc4701932015-04-10 13:18:51 -04003730 IfCondition GetOppositeCondition() const OVERRIDE {
3731 return kCondEQ;
3732 }
3733
Artem Serovcced8ba2017-07-19 18:18:09 +01003734 protected:
3735 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3736
Dave Allison20dfc792014-06-16 20:44:29 -07003737 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003738 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003739};
3740
Vladimir Markofcb503c2016-05-18 12:48:17 +01003741class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003742 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303743 HLessThan(HInstruction* first, HInstruction* second,
3744 uint32_t dex_pc = kNoDexPc)
3745 : HCondition(kLessThan, first, second, dex_pc) {
3746 }
Dave Allison20dfc792014-06-16 20:44:29 -07003747
Roland Levillain9867bc72015-08-05 10:21:34 +01003748 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003749 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003750 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003751 // In the following Evaluate methods, a HCompare instruction has
3752 // been merged into this HLessThan instruction; evaluate it as
3753 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003754 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003755 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3756 }
3757 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3758 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3759 }
3760 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3761 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003762 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003763
Dave Allison20dfc792014-06-16 20:44:29 -07003764 DECLARE_INSTRUCTION(LessThan);
3765
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003766 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003767 return kCondLT;
3768 }
3769
Mark Mendellc4701932015-04-10 13:18:51 -04003770 IfCondition GetOppositeCondition() const OVERRIDE {
3771 return kCondGE;
3772 }
3773
Artem Serovcced8ba2017-07-19 18:18:09 +01003774 protected:
3775 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3776
Dave Allison20dfc792014-06-16 20:44:29 -07003777 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003778 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003779};
3780
Vladimir Markofcb503c2016-05-18 12:48:17 +01003781class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003782 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303783 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3784 uint32_t dex_pc = kNoDexPc)
3785 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3786 }
Dave Allison20dfc792014-06-16 20:44:29 -07003787
Roland Levillain9867bc72015-08-05 10:21:34 +01003788 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003789 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003790 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003791 // In the following Evaluate methods, a HCompare instruction has
3792 // been merged into this HLessThanOrEqual instruction; evaluate it as
3793 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003794 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003795 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3796 }
3797 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3798 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3799 }
3800 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3801 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003802 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003803
Dave Allison20dfc792014-06-16 20:44:29 -07003804 DECLARE_INSTRUCTION(LessThanOrEqual);
3805
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003806 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003807 return kCondLE;
3808 }
3809
Mark Mendellc4701932015-04-10 13:18:51 -04003810 IfCondition GetOppositeCondition() const OVERRIDE {
3811 return kCondGT;
3812 }
3813
Artem Serovcced8ba2017-07-19 18:18:09 +01003814 protected:
3815 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3816
Dave Allison20dfc792014-06-16 20:44:29 -07003817 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003818 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003819};
3820
Vladimir Markofcb503c2016-05-18 12:48:17 +01003821class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003822 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003823 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303824 : HCondition(kGreaterThan, first, second, dex_pc) {
3825 }
Dave Allison20dfc792014-06-16 20:44:29 -07003826
Roland Levillain9867bc72015-08-05 10:21:34 +01003827 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003828 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003829 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003830 // In the following Evaluate methods, a HCompare instruction has
3831 // been merged into this HGreaterThan instruction; evaluate it as
3832 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003833 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003834 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3835 }
3836 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3837 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3838 }
3839 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3840 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003841 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003842
Dave Allison20dfc792014-06-16 20:44:29 -07003843 DECLARE_INSTRUCTION(GreaterThan);
3844
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003845 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003846 return kCondGT;
3847 }
3848
Mark Mendellc4701932015-04-10 13:18:51 -04003849 IfCondition GetOppositeCondition() const OVERRIDE {
3850 return kCondLE;
3851 }
3852
Artem Serovcced8ba2017-07-19 18:18:09 +01003853 protected:
3854 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3855
Dave Allison20dfc792014-06-16 20:44:29 -07003856 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003857 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003858};
3859
Vladimir Markofcb503c2016-05-18 12:48:17 +01003860class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003862 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303863 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3864 }
Dave Allison20dfc792014-06-16 20:44:29 -07003865
Roland Levillain9867bc72015-08-05 10:21:34 +01003866 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003867 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003868 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003869 // In the following Evaluate methods, a HCompare instruction has
3870 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3871 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003872 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003873 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3874 }
3875 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3876 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3877 }
3878 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3879 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003880 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003881
Dave Allison20dfc792014-06-16 20:44:29 -07003882 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3883
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003884 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003885 return kCondGE;
3886 }
3887
Mark Mendellc4701932015-04-10 13:18:51 -04003888 IfCondition GetOppositeCondition() const OVERRIDE {
3889 return kCondLT;
3890 }
3891
Artem Serovcced8ba2017-07-19 18:18:09 +01003892 protected:
3893 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3894
Dave Allison20dfc792014-06-16 20:44:29 -07003895 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003896 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003897};
3898
Vladimir Markofcb503c2016-05-18 12:48:17 +01003899class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003900 public:
3901 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303902 : HCondition(kBelow, first, second, dex_pc) {
3903 }
Aart Bike9f37602015-10-09 11:15:55 -07003904
3905 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003906 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003907 }
3908 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003909 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3910 }
3911 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3912 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3913 LOG(FATAL) << DebugName() << " is not defined for float values";
3914 UNREACHABLE();
3915 }
3916 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3917 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3918 LOG(FATAL) << DebugName() << " is not defined for double values";
3919 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003920 }
3921
3922 DECLARE_INSTRUCTION(Below);
3923
3924 IfCondition GetCondition() const OVERRIDE {
3925 return kCondB;
3926 }
3927
3928 IfCondition GetOppositeCondition() const OVERRIDE {
3929 return kCondAE;
3930 }
3931
Artem Serovcced8ba2017-07-19 18:18:09 +01003932 protected:
3933 DEFAULT_COPY_CONSTRUCTOR(Below);
3934
Aart Bike9f37602015-10-09 11:15:55 -07003935 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003936 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003937 return MakeUnsigned(x) < MakeUnsigned(y);
3938 }
Aart Bike9f37602015-10-09 11:15:55 -07003939};
3940
Vladimir Markofcb503c2016-05-18 12:48:17 +01003941class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003942 public:
3943 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303944 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3945 }
Aart Bike9f37602015-10-09 11:15:55 -07003946
3947 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003948 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003949 }
3950 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003951 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3952 }
3953 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3954 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3955 LOG(FATAL) << DebugName() << " is not defined for float values";
3956 UNREACHABLE();
3957 }
3958 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3959 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3960 LOG(FATAL) << DebugName() << " is not defined for double values";
3961 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003962 }
3963
3964 DECLARE_INSTRUCTION(BelowOrEqual);
3965
3966 IfCondition GetCondition() const OVERRIDE {
3967 return kCondBE;
3968 }
3969
3970 IfCondition GetOppositeCondition() const OVERRIDE {
3971 return kCondA;
3972 }
3973
Artem Serovcced8ba2017-07-19 18:18:09 +01003974 protected:
3975 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3976
Aart Bike9f37602015-10-09 11:15:55 -07003977 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003978 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003979 return MakeUnsigned(x) <= MakeUnsigned(y);
3980 }
Aart Bike9f37602015-10-09 11:15:55 -07003981};
3982
Vladimir Markofcb503c2016-05-18 12:48:17 +01003983class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003984 public:
3985 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303986 : HCondition(kAbove, first, second, dex_pc) {
3987 }
Aart Bike9f37602015-10-09 11:15:55 -07003988
3989 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003990 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003991 }
3992 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003993 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3994 }
3995 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3996 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3997 LOG(FATAL) << DebugName() << " is not defined for float values";
3998 UNREACHABLE();
3999 }
4000 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4001 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4002 LOG(FATAL) << DebugName() << " is not defined for double values";
4003 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004004 }
4005
4006 DECLARE_INSTRUCTION(Above);
4007
4008 IfCondition GetCondition() const OVERRIDE {
4009 return kCondA;
4010 }
4011
4012 IfCondition GetOppositeCondition() const OVERRIDE {
4013 return kCondBE;
4014 }
4015
Artem Serovcced8ba2017-07-19 18:18:09 +01004016 protected:
4017 DEFAULT_COPY_CONSTRUCTOR(Above);
4018
Aart Bike9f37602015-10-09 11:15:55 -07004019 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004020 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004021 return MakeUnsigned(x) > MakeUnsigned(y);
4022 }
Aart Bike9f37602015-10-09 11:15:55 -07004023};
4024
Vladimir Markofcb503c2016-05-18 12:48:17 +01004025class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004026 public:
4027 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304028 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4029 }
Aart Bike9f37602015-10-09 11:15:55 -07004030
4031 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004032 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004033 }
4034 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004035 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4036 }
4037 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4038 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4039 LOG(FATAL) << DebugName() << " is not defined for float values";
4040 UNREACHABLE();
4041 }
4042 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4043 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4044 LOG(FATAL) << DebugName() << " is not defined for double values";
4045 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004046 }
4047
4048 DECLARE_INSTRUCTION(AboveOrEqual);
4049
4050 IfCondition GetCondition() const OVERRIDE {
4051 return kCondAE;
4052 }
4053
4054 IfCondition GetOppositeCondition() const OVERRIDE {
4055 return kCondB;
4056 }
4057
Artem Serovcced8ba2017-07-19 18:18:09 +01004058 protected:
4059 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4060
Aart Bike9f37602015-10-09 11:15:55 -07004061 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004062 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004063 return MakeUnsigned(x) >= MakeUnsigned(y);
4064 }
Aart Bike9f37602015-10-09 11:15:55 -07004065};
Dave Allison20dfc792014-06-16 20:44:29 -07004066
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004067// Instruction to check how two inputs compare to each other.
4068// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004069class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004070 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004071 // Note that `comparison_type` is the type of comparison performed
4072 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004073 // HCompare instruction (which is always DataType::Type::kInt).
4074 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004075 HInstruction* first,
4076 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004077 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004078 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304079 : HBinaryOperation(kCompare,
4080 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004081 first,
4082 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004083 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004084 dex_pc) {
4085 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004086 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4087 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004088 }
4089
Roland Levillain9867bc72015-08-05 10:21:34 +01004090 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004091 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4092
4093 template <typename T>
4094 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004095 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004096 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4097 // Handle the bias.
4098 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4099 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004100
Roland Levillain9867bc72015-08-05 10:21:34 +01004101 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004102 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4103 // reach this code path when processing a freshly built HIR
4104 // graph. However HCompare integer instructions can be synthesized
4105 // by the instruction simplifier to implement IntegerCompare and
4106 // IntegerSignum intrinsics, so we have to handle this case.
4107 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004108 }
4109 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004110 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4111 }
4112 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4113 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4114 }
4115 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4116 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004117 }
4118
Vladimir Marko372f10e2016-05-17 16:30:10 +01004119 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004120 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004121 }
4122
Vladimir Markoa1de9182016-02-25 11:37:38 +00004123 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004124
Roland Levillain31dd3d62016-02-16 12:21:02 +00004125 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004126 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004127 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004128 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004129 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004130 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004132 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004133 // Comparisons do not require a runtime call in any back end.
4134 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004135 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004136
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004137 DECLARE_INSTRUCTION(Compare);
4138
Roland Levillain31dd3d62016-02-16 12:21:02 +00004139 protected:
Vladimir Markoc6760552018-05-03 17:09:09 +01004140 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004141 static constexpr size_t kFieldComparisonBiasSize =
4142 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4143 static constexpr size_t kNumberOfComparePackedBits =
4144 kFieldComparisonBias + kFieldComparisonBiasSize;
4145 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4146 using ComparisonBiasField =
4147 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4148
Roland Levillain31dd3d62016-02-16 12:21:02 +00004149 // Return an integer constant containing the result of a comparison evaluated at compile time.
4150 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4151 DCHECK(value == -1 || value == 0 || value == 1) << value;
4152 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4153 }
4154
Artem Serovcced8ba2017-07-19 18:18:09 +01004155 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004156};
4157
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004158class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004159 public:
4160 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004161 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004162 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004163 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004164 bool finalizable,
4165 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304166 : HExpression(kNewInstance,
4167 DataType::Type::kReference,
4168 SideEffects::CanTriggerGC(),
4169 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004170 type_index_(type_index),
4171 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004172 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004173 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004174 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004175 }
4176
Artem Serovcced8ba2017-07-19 18:18:09 +01004177 bool IsClonable() const OVERRIDE { return true; }
4178
Andreas Gampea5b09a62016-11-17 15:21:22 -08004179 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004180 const DexFile& GetDexFile() const { return dex_file_; }
4181
4182 // Calls runtime so needs an environment.
4183 bool NeedsEnvironment() const OVERRIDE { return true; }
4184
Mingyao Yang062157f2016-03-02 10:15:36 -08004185 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4186 bool CanThrow() const OVERRIDE { return true; }
4187
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004188 bool NeedsChecks() const {
4189 return entrypoint_ == kQuickAllocObjectWithChecks;
4190 }
David Brazdil6de19382016-01-08 17:37:10 +00004191
Vladimir Markoa1de9182016-02-25 11:37:38 +00004192 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004193
4194 bool CanBeNull() const OVERRIDE { return false; }
4195
4196 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4197
4198 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4199 entrypoint_ = entrypoint;
4200 }
4201
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004202 HLoadClass* GetLoadClass() const {
4203 HInstruction* input = InputAt(0);
4204 if (input->IsClinitCheck()) {
4205 input = input->InputAt(0);
4206 }
4207 DCHECK(input->IsLoadClass());
4208 return input->AsLoadClass();
4209 }
4210
David Brazdil6de19382016-01-08 17:37:10 +00004211 bool IsStringAlloc() const;
4212
4213 DECLARE_INSTRUCTION(NewInstance);
4214
Artem Serovcced8ba2017-07-19 18:18:09 +01004215 protected:
4216 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4217
David Brazdil6de19382016-01-08 17:37:10 +00004218 private:
Vladimir Markoc6760552018-05-03 17:09:09 +01004219 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004220 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4221 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4222 "Too many packed fields.");
4223
Andreas Gampea5b09a62016-11-17 15:21:22 -08004224 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004225 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004226 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004227};
4228
Agi Csaki05f20562015-08-19 14:58:14 -07004229enum IntrinsicNeedsEnvironmentOrCache {
4230 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4231 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004232};
4233
Aart Bik5d75afe2015-12-14 11:57:01 -08004234enum IntrinsicSideEffects {
4235 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4236 kReadSideEffects, // Intrinsic may read heap memory.
4237 kWriteSideEffects, // Intrinsic may write heap memory.
4238 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4239};
4240
4241enum IntrinsicExceptions {
4242 kNoThrow, // Intrinsic does not throw any exceptions.
4243 kCanThrow // Intrinsic may throw exceptions.
4244};
4245
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004246class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004247 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004248 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004249
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004250 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004251 SetRawInputAt(index, argument);
4252 }
4253
Roland Levillain3e3d7332015-04-28 11:00:54 +01004254 // Return the number of arguments. This number can be lower than
4255 // the number of inputs returned by InputCount(), as some invoke
4256 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4257 // inputs at the end of their list of inputs.
4258 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4259
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004260 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4261
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004262 InvokeType GetInvokeType() const {
4263 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004264 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004265
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004266 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004267 return intrinsic_;
4268 }
4269
Aart Bik5d75afe2015-12-14 11:57:01 -08004270 void SetIntrinsic(Intrinsics intrinsic,
4271 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4272 IntrinsicSideEffects side_effects,
4273 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004274
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004275 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004276 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004277 }
4278
Aart Bikff7d89c2016-11-07 08:49:28 -08004279 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4280
Vladimir Markoa1de9182016-02-25 11:37:38 +00004281 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004282
Aart Bika8b8e9b2018-01-09 11:01:02 -08004283 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4284
4285 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4286
Aart Bik71bf7b42016-11-16 10:17:46 -08004287 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004288
Vladimir Marko372f10e2016-05-17 16:30:10 +01004289 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004290 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4291 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004292
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004293 uint32_t* GetIntrinsicOptimizations() {
4294 return &intrinsic_optimizations_;
4295 }
4296
4297 const uint32_t* GetIntrinsicOptimizations() const {
4298 return &intrinsic_optimizations_;
4299 }
4300
4301 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4302
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004303 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004304 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004305
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004306 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004307
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004308 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004309 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4310 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004311 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markoc6760552018-05-03 17:09:09 +01004312 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004313 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4314 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004315 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004316 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004317
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304318 HInvoke(InstructionKind kind,
4319 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004320 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004321 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004322 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004323 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004324 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004325 ArtMethod* resolved_method,
4326 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004327 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304328 kind,
Vladimir Markoc6760552018-05-03 17:09:09 +01004329 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004330 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4331 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004332 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004333 number_of_arguments + number_of_other_inputs,
4334 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004335 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004336 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004337 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004338 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004339 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004340 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004341 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004342 }
4343
Artem Serovcced8ba2017-07-19 18:18:09 +01004344 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4345
Roland Levillain3e3d7332015-04-28 11:00:54 +01004346 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004347 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004348 const uint32_t dex_method_index_;
4349 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004350
4351 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4352 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004353};
4354
Vladimir Markofcb503c2016-05-18 12:48:17 +01004355class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004356 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004357 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004358 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004360 uint32_t dex_pc,
4361 uint32_t dex_method_index,
4362 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304363 : HInvoke(kInvokeUnresolved,
4364 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004365 number_of_arguments,
4366 0u /* number_of_other_inputs */,
4367 return_type,
4368 dex_pc,
4369 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004370 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004371 invoke_type) {
4372 }
4373
Artem Serovcced8ba2017-07-19 18:18:09 +01004374 bool IsClonable() const OVERRIDE { return true; }
4375
Calin Juravle175dc732015-08-25 15:42:32 +01004376 DECLARE_INSTRUCTION(InvokeUnresolved);
4377
Artem Serovcced8ba2017-07-19 18:18:09 +01004378 protected:
4379 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004380};
4381
Orion Hodsonac141392017-01-13 11:53:47 +00004382class HInvokePolymorphic FINAL : public HInvoke {
4383 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004384 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004385 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004386 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004387 uint32_t dex_pc,
4388 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304389 : HInvoke(kInvokePolymorphic,
4390 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004391 number_of_arguments,
4392 0u /* number_of_other_inputs */,
4393 return_type,
4394 dex_pc,
4395 dex_method_index,
4396 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304397 kVirtual) {
4398 }
Orion Hodsonac141392017-01-13 11:53:47 +00004399
Artem Serovcced8ba2017-07-19 18:18:09 +01004400 bool IsClonable() const OVERRIDE { return true; }
4401
Orion Hodsonac141392017-01-13 11:53:47 +00004402 DECLARE_INSTRUCTION(InvokePolymorphic);
4403
Artem Serovcced8ba2017-07-19 18:18:09 +01004404 protected:
4405 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004406};
4407
Orion Hodson20e7a6e2018-05-18 08:33:20 +01004408class HInvokeCustom FINAL : public HInvoke {
4409 public:
4410 HInvokeCustom(ArenaAllocator* allocator,
4411 uint32_t number_of_arguments,
4412 uint32_t call_site_index,
4413 DataType::Type return_type,
4414 uint32_t dex_pc)
4415 : HInvoke(kInvokeCustom,
4416 allocator,
4417 number_of_arguments,
4418 /* number_of_other_inputs */ 0u,
4419 return_type,
4420 dex_pc,
4421 /* dex_method_index */ dex::kDexNoIndex,
4422 /* resolved_method */ nullptr,
4423 kStatic),
4424 call_site_index_(call_site_index) {
4425 }
4426
4427 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4428
4429 bool IsClonable() const OVERRIDE { return true; }
4430
4431 DECLARE_INSTRUCTION(InvokeCustom);
4432
4433 protected:
4434 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4435
4436 private:
4437 uint32_t call_site_index_;
4438};
4439
Vladimir Markofcb503c2016-05-18 12:48:17 +01004440class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004441 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004442 // Requirements of this method call regarding the class
4443 // initialization (clinit) check of its declaring class.
4444 enum class ClinitCheckRequirement {
4445 kNone, // Class already initialized.
4446 kExplicit, // Static call having explicit clinit check as last input.
4447 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004448 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004449 };
4450
Vladimir Marko58155012015-08-19 12:49:41 +00004451 // Determines how to load the target ArtMethod*.
4452 enum class MethodLoadKind {
4453 // Use a String init ArtMethod* loaded from Thread entrypoints.
4454 kStringInit,
4455
4456 // Use the method's own ArtMethod* loaded by the register allocator.
4457 kRecursive,
4458
Vladimir Marko65979462017-05-19 17:25:12 +01004459 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4460 // Used for boot image methods referenced by boot image code.
4461 kBootImageLinkTimePcRelative,
4462
Vladimir Marko58155012015-08-19 12:49:41 +00004463 // Use ArtMethod* at a known address, embed the direct address in the code.
4464 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4465 kDirectAddress,
4466
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004467 // Load from an entry in the .bss section using a PC-relative load.
4468 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4469 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004470
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004471 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4472 // used when other kinds are unimplemented on a particular architecture.
4473 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004474 };
4475
4476 // Determines the location of the code pointer.
4477 enum class CodePtrLocation {
4478 // Recursive call, use local PC-relative call instruction.
4479 kCallSelf,
4480
Vladimir Marko58155012015-08-19 12:49:41 +00004481 // Use code pointer from the ArtMethod*.
4482 // Used when we don't know the target code. This is also the last-resort-kind used when
4483 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4484 kCallArtMethod,
4485 };
4486
4487 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004488 MethodLoadKind method_load_kind;
4489 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004490 // The method load data holds
4491 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4492 // Note that there are multiple string init methods, each having its own offset.
4493 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004494 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004495 };
4496
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004497 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004498 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004499 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004500 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004501 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004502 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004503 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004504 InvokeType invoke_type,
4505 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004506 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304507 : HInvoke(kInvokeStaticOrDirect,
4508 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004509 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004510 // There is potentially one extra argument for the HCurrentMethod node, and
4511 // potentially one other if the clinit check is explicit, and potentially
4512 // one other if the method is a string factory.
4513 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004514 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004515 return_type,
4516 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004517 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004518 resolved_method,
4519 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004520 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004521 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004522 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4523 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004524
Artem Serovcced8ba2017-07-19 18:18:09 +01004525 bool IsClonable() const OVERRIDE { return true; }
4526
Vladimir Markodc151b22015-10-15 18:02:30 +01004527 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004528 bool had_current_method_input = HasCurrentMethodInput();
4529 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4530
4531 // Using the current method is the default and once we find a better
4532 // method load kind, we should not go back to using the current method.
4533 DCHECK(had_current_method_input || !needs_current_method_input);
4534
4535 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004536 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4537 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004538 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004539 dispatch_info_ = dispatch_info;
4540 }
4541
Aart Bik6daebeb2017-04-03 14:35:41 -07004542 DispatchInfo GetDispatchInfo() const {
4543 return dispatch_info_;
4544 }
4545
Vladimir Markoc53c0792015-11-19 15:48:33 +00004546 void AddSpecialInput(HInstruction* input) {
4547 // We allow only one special input.
4548 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4549 DCHECK(InputCount() == GetSpecialInputIndex() ||
4550 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4551 InsertInputAt(GetSpecialInputIndex(), input);
4552 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004553
Vladimir Marko372f10e2016-05-17 16:30:10 +01004554 using HInstruction::GetInputRecords; // Keep the const version visible.
4555 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4556 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4557 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4558 DCHECK(!input_records.empty());
4559 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4560 HInstruction* last_input = input_records.back().GetInstruction();
4561 // Note: `last_input` may be null during arguments setup.
4562 if (last_input != nullptr) {
4563 // `last_input` is the last input of a static invoke marked as having
4564 // an explicit clinit check. It must either be:
4565 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4566 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4567 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4568 }
4569 }
4570 return input_records;
4571 }
4572
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004573 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004574 // We access the method via the dex cache so we can't do an implicit null check.
4575 // TODO: for intrinsics we can generate implicit null checks.
4576 return false;
4577 }
4578
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004579 bool CanBeNull() const OVERRIDE {
Vladimir Markoc6760552018-05-03 17:09:09 +01004580 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004581 }
4582
Vladimir Markoc53c0792015-11-19 15:48:33 +00004583 // Get the index of the special input, if any.
4584 //
David Brazdil6de19382016-01-08 17:37:10 +00004585 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4586 // method pointer; otherwise there may be one platform-specific special input,
4587 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004588 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004589 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004590
Vladimir Marko58155012015-08-19 12:49:41 +00004591 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4592 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4593 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004594 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004595 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004596 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004597 bool HasPcRelativeMethodLoadKind() const {
4598 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004599 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004600 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004601 bool HasCurrentMethodInput() const {
4602 // This function can be called only after the invoke has been fully initialized by the builder.
4603 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004604 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004605 return true;
4606 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004607 DCHECK(InputCount() == GetSpecialInputIndex() ||
4608 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004609 return false;
4610 }
4611 }
Vladimir Marko58155012015-08-19 12:49:41 +00004612
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004613 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004614 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004615 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004616 }
4617
4618 uint64_t GetMethodAddress() const {
4619 DCHECK(HasMethodAddress());
4620 return dispatch_info_.method_load_data;
4621 }
4622
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004623 const DexFile& GetDexFileForPcRelativeDexCache() const;
4624
Vladimir Markoa1de9182016-02-25 11:37:38 +00004625 ClinitCheckRequirement GetClinitCheckRequirement() const {
4626 return GetPackedField<ClinitCheckRequirementField>();
4627 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004628
Roland Levillain4c0eb422015-04-24 16:43:49 +01004629 // Is this instruction a call to a static method?
4630 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004631 return GetInvokeType() == kStatic;
4632 }
4633
4634 MethodReference GetTargetMethod() const {
4635 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004636 }
4637
Vladimir Markofbb184a2015-11-13 14:47:00 +00004638 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4639 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4640 // instruction; only relevant for static calls with explicit clinit check.
4641 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004642 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004643 size_t last_input_index = inputs_.size() - 1u;
4644 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004645 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004646 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004647 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004648 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004649 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004650 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004651 }
4652
4653 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004654 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004655 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004656 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004657 }
4658
4659 // Is this a call to a static method whose declaring class has an
4660 // implicit intialization check requirement?
4661 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004662 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004663 }
4664
Vladimir Markob554b5a2015-11-06 12:57:55 +00004665 // Does this method load kind need the current method as an input?
4666 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004667 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004668 }
4669
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004670 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004671
Artem Serovcced8ba2017-07-19 18:18:09 +01004672 protected:
4673 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4674
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004675 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004676 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004677 static constexpr size_t kFieldClinitCheckRequirementSize =
4678 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4679 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4680 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4681 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4682 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004683 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4684 kFieldClinitCheckRequirement,
4685 kFieldClinitCheckRequirementSize>;
4686
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004687 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004688 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004689 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004690};
Vladimir Markof64242a2015-12-01 14:58:23 +00004691std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004692std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004693
Vladimir Markofcb503c2016-05-18 12:48:17 +01004694class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004695 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004696 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004697 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004698 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004699 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004700 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004701 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004702 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304703 : HInvoke(kInvokeVirtual,
4704 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004705 number_of_arguments,
4706 0u,
4707 return_type,
4708 dex_pc,
4709 dex_method_index,
4710 resolved_method,
4711 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304712 vtable_index_(vtable_index) {
4713 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004714
Artem Serovcced8ba2017-07-19 18:18:09 +01004715 bool IsClonable() const OVERRIDE { return true; }
4716
Aart Bik71bf7b42016-11-16 10:17:46 -08004717 bool CanBeNull() const OVERRIDE {
4718 switch (GetIntrinsic()) {
4719 case Intrinsics::kThreadCurrentThread:
4720 case Intrinsics::kStringBufferAppend:
4721 case Intrinsics::kStringBufferToString:
4722 case Intrinsics::kStringBuilderAppend:
4723 case Intrinsics::kStringBuilderToString:
4724 return false;
4725 default:
4726 return HInvoke::CanBeNull();
4727 }
4728 }
4729
Calin Juravle641547a2015-04-21 22:08:51 +01004730 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004731 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004732 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004733 }
4734
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004735 uint32_t GetVTableIndex() const { return vtable_index_; }
4736
4737 DECLARE_INSTRUCTION(InvokeVirtual);
4738
Artem Serovcced8ba2017-07-19 18:18:09 +01004739 protected:
4740 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4741
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004742 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004743 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004744 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004745};
4746
Vladimir Markofcb503c2016-05-18 12:48:17 +01004747class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004748 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004749 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004750 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004751 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004752 uint32_t dex_pc,
4753 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004754 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004755 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304756 : HInvoke(kInvokeInterface,
4757 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004758 number_of_arguments,
4759 0u,
4760 return_type,
4761 dex_pc,
4762 dex_method_index,
4763 resolved_method,
4764 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304765 imt_index_(imt_index) {
4766 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004767
Artem Serovcced8ba2017-07-19 18:18:09 +01004768 bool IsClonable() const OVERRIDE { return true; }
4769
Calin Juravle641547a2015-04-21 22:08:51 +01004770 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004771 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004772 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004773 }
4774
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004775 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4776 // The assembly stub currently needs it.
4777 return true;
4778 }
4779
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004780 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004781
4782 DECLARE_INSTRUCTION(InvokeInterface);
4783
Artem Serovcced8ba2017-07-19 18:18:09 +01004784 protected:
4785 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4786
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004787 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004788 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004789 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004790};
4791
Vladimir Markofcb503c2016-05-18 12:48:17 +01004792class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004793 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004794 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304795 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004797 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004798
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004799 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004800
4801 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004803 }
4804 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004805 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004806 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004807 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4808 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4809 }
4810 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4811 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4812 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004813
Roland Levillain88cb1752014-10-20 16:36:47 +01004814 DECLARE_INSTRUCTION(Neg);
4815
Artem Serovcced8ba2017-07-19 18:18:09 +01004816 protected:
4817 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004818};
4819
Vladimir Markofcb503c2016-05-18 12:48:17 +01004820class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004821 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004822 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304823 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004824 SetRawInputAt(0, cls);
4825 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004826 }
4827
Artem Serovcced8ba2017-07-19 18:18:09 +01004828 bool IsClonable() const OVERRIDE { return true; }
4829
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004830 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004831 bool NeedsEnvironment() const OVERRIDE { return true; }
4832
Mingyao Yang0c365e62015-03-31 15:09:29 -07004833 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4834 bool CanThrow() const OVERRIDE { return true; }
4835
Calin Juravle10e244f2015-01-26 18:54:32 +00004836 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004837
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004838 HLoadClass* GetLoadClass() const {
4839 DCHECK(InputAt(0)->IsLoadClass());
4840 return InputAt(0)->AsLoadClass();
4841 }
4842
4843 HInstruction* GetLength() const {
4844 return InputAt(1);
4845 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004846
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004847 DECLARE_INSTRUCTION(NewArray);
4848
Artem Serovcced8ba2017-07-19 18:18:09 +01004849 protected:
4850 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004851};
4852
Vladimir Markofcb503c2016-05-18 12:48:17 +01004853class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004854 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004855 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004856 HInstruction* left,
4857 HInstruction* right,
4858 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304859 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4860 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004861
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004862 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004863
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004864 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004865
4866 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004867 return GetBlock()->GetGraph()->GetIntConstant(
4868 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004869 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004870 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004871 return GetBlock()->GetGraph()->GetLongConstant(
4872 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004873 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004874 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4875 return GetBlock()->GetGraph()->GetFloatConstant(
4876 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4877 }
4878 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4879 return GetBlock()->GetGraph()->GetDoubleConstant(
4880 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4881 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004882
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004883 DECLARE_INSTRUCTION(Add);
4884
Artem Serovcced8ba2017-07-19 18:18:09 +01004885 protected:
4886 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004887};
4888
Vladimir Markofcb503c2016-05-18 12:48:17 +01004889class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004890 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004891 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004892 HInstruction* left,
4893 HInstruction* right,
4894 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304895 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4896 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004897
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004898 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004899
4900 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004901 return GetBlock()->GetGraph()->GetIntConstant(
4902 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004903 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004904 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004905 return GetBlock()->GetGraph()->GetLongConstant(
4906 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004907 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004908 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4909 return GetBlock()->GetGraph()->GetFloatConstant(
4910 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4911 }
4912 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4913 return GetBlock()->GetGraph()->GetDoubleConstant(
4914 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4915 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004916
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004917 DECLARE_INSTRUCTION(Sub);
4918
Artem Serovcced8ba2017-07-19 18:18:09 +01004919 protected:
4920 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004921};
4922
Vladimir Markofcb503c2016-05-18 12:48:17 +01004923class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004924 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004925 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004926 HInstruction* left,
4927 HInstruction* right,
4928 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304929 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4930 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004931
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004932 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004933
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004934 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004935
4936 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004937 return GetBlock()->GetGraph()->GetIntConstant(
4938 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004939 }
4940 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004941 return GetBlock()->GetGraph()->GetLongConstant(
4942 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004943 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004944 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4945 return GetBlock()->GetGraph()->GetFloatConstant(
4946 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4947 }
4948 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4949 return GetBlock()->GetGraph()->GetDoubleConstant(
4950 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4951 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004952
4953 DECLARE_INSTRUCTION(Mul);
4954
Artem Serovcced8ba2017-07-19 18:18:09 +01004955 protected:
4956 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004957};
4958
Vladimir Markofcb503c2016-05-18 12:48:17 +01004959class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004960 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004961 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004962 HInstruction* left,
4963 HInstruction* right,
4964 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304965 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4966 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004967
Roland Levillain9867bc72015-08-05 10:21:34 +01004968 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004969 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004970 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004971 // Our graph structure ensures we never have 0 for `y` during
4972 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004973 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004974 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004975 return (y == -1) ? -x : x / y;
4976 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004977
Roland Levillain31dd3d62016-02-16 12:21:02 +00004978 template <typename T>
4979 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004980 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004981 return x / y;
4982 }
4983
Roland Levillain9867bc72015-08-05 10:21:34 +01004984 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004985 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004986 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004987 }
4988 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004989 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004990 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4991 }
4992 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4993 return GetBlock()->GetGraph()->GetFloatConstant(
4994 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4995 }
4996 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4997 return GetBlock()->GetGraph()->GetDoubleConstant(
4998 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004999 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005000
5001 DECLARE_INSTRUCTION(Div);
5002
Artem Serovcced8ba2017-07-19 18:18:09 +01005003 protected:
5004 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005005};
5006
Vladimir Markofcb503c2016-05-18 12:48:17 +01005007class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005008 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005009 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005010 HInstruction* left,
5011 HInstruction* right,
5012 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305013 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5014 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005015
Roland Levillain9867bc72015-08-05 10:21:34 +01005016 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005017 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005018 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005019 // Our graph structure ensures we never have 0 for `y` during
5020 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005021 DCHECK_NE(y, 0);
5022 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5023 return (y == -1) ? 0 : x % y;
5024 }
5025
Roland Levillain31dd3d62016-02-16 12:21:02 +00005026 template <typename T>
5027 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005028 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005029 return std::fmod(x, y);
5030 }
5031
Roland Levillain9867bc72015-08-05 10:21:34 +01005032 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005033 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005034 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005035 }
5036 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005037 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005038 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005039 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005040 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
5041 return GetBlock()->GetGraph()->GetFloatConstant(
5042 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5043 }
5044 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
5045 return GetBlock()->GetGraph()->GetDoubleConstant(
5046 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5047 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005048
5049 DECLARE_INSTRUCTION(Rem);
5050
Artem Serovcced8ba2017-07-19 18:18:09 +01005051 protected:
5052 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005053};
5054
Aart Bik1c8fb9e2018-02-15 10:42:37 -08005055class HMin FINAL : public HBinaryOperation {
5056 public:
5057 HMin(DataType::Type result_type,
5058 HInstruction* left,
5059 HInstruction* right,
5060 uint32_t dex_pc)
5061 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5062
5063 bool IsCommutative() const OVERRIDE { return true; }
5064
5065 // Evaluation for integral values.
5066 template <typename T> static T ComputeIntegral(T x, T y) {
5067 return (x <= y) ? x : y;
5068 }
5069
5070 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5071 return GetBlock()->GetGraph()->GetIntConstant(
5072 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5073 }
5074 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5075 return GetBlock()->GetGraph()->GetLongConstant(
5076 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5077 }
5078 // TODO: Evaluation for floating-point values.
5079 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5080 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5081 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5082 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5083
5084 DECLARE_INSTRUCTION(Min);
5085
5086 protected:
5087 DEFAULT_COPY_CONSTRUCTOR(Min);
5088};
5089
5090class HMax FINAL : public HBinaryOperation {
5091 public:
5092 HMax(DataType::Type result_type,
5093 HInstruction* left,
5094 HInstruction* right,
5095 uint32_t dex_pc)
5096 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5097
5098 bool IsCommutative() const OVERRIDE { return true; }
5099
5100 // Evaluation for integral values.
5101 template <typename T> static T ComputeIntegral(T x, T y) {
5102 return (x >= y) ? x : y;
5103 }
5104
5105 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5106 return GetBlock()->GetGraph()->GetIntConstant(
5107 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5108 }
5109 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5110 return GetBlock()->GetGraph()->GetLongConstant(
5111 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5112 }
5113 // TODO: Evaluation for floating-point values.
5114 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5115 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5116 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5117 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5118
5119 DECLARE_INSTRUCTION(Max);
5120
5121 protected:
5122 DEFAULT_COPY_CONSTRUCTOR(Max);
5123};
5124
Aart Bik6e95b512018-02-28 12:01:46 -08005125class HAbs FINAL : public HUnaryOperation {
5126 public:
5127 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5128 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5129
5130 // Evaluation for integral values.
5131 template <typename T> static T ComputeIntegral(T x) {
5132 return x < 0 ? -x : x;
5133 }
5134
5135 // Evaluation for floating-point values.
5136 // Note, as a "quality of implementation", rather than pure "spec compliance",
5137 // we require that Math.abs() clears the sign bit (but changes nothing else)
5138 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5139 // http://b/30758343
5140 template <typename T, typename S> static T ComputeFP(T x) {
5141 S bits = bit_cast<S, T>(x);
5142 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5143 }
5144
5145 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5146 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5147 }
5148 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5149 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5150 }
5151 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5152 return GetBlock()->GetGraph()->GetFloatConstant(
5153 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5154 }
5155 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5156 return GetBlock()->GetGraph()->GetDoubleConstant(
5157 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5158 }
5159
5160 DECLARE_INSTRUCTION(Abs);
5161
5162 protected:
5163 DEFAULT_COPY_CONSTRUCTOR(Abs);
5164};
5165
Vladimir Markofcb503c2016-05-18 12:48:17 +01005166class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005167 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005168 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serov34f8de32018-06-22 11:35:46 +01005169 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5170 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005171 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serov34f8de32018-06-22 11:35:46 +01005172 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005173 SetRawInputAt(0, value);
5174 }
5175
Artem Serove972e722018-06-19 14:55:17 +01005176 bool IsClonable() const OVERRIDE { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005177 bool CanBeMoved() const OVERRIDE { return true; }
5178
Vladimir Marko372f10e2016-05-17 16:30:10 +01005179 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005180 return true;
5181 }
5182
5183 bool NeedsEnvironment() const OVERRIDE { return true; }
5184 bool CanThrow() const OVERRIDE { return true; }
5185
Calin Juravled0d48522014-11-04 16:40:20 +00005186 DECLARE_INSTRUCTION(DivZeroCheck);
5187
Artem Serovcced8ba2017-07-19 18:18:09 +01005188 protected:
5189 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005190};
5191
Vladimir Markofcb503c2016-05-18 12:48:17 +01005192class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005193 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005194 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005195 HInstruction* value,
5196 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005197 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305198 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005199 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5200 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005201 }
5202
Roland Levillain5b5b9312016-03-22 14:57:31 +00005203 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005204 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005205 return value << (distance & max_shift_distance);
5206 }
5207
5208 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005209 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005210 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005211 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005212 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005213 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005214 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005215 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005216 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5217 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5218 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5219 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005220 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005221 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5222 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005223 LOG(FATAL) << DebugName() << " is not defined for float values";
5224 UNREACHABLE();
5225 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005226 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5227 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005228 LOG(FATAL) << DebugName() << " is not defined for double values";
5229 UNREACHABLE();
5230 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005231
5232 DECLARE_INSTRUCTION(Shl);
5233
Artem Serovcced8ba2017-07-19 18:18:09 +01005234 protected:
5235 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005236};
5237
Vladimir Markofcb503c2016-05-18 12:48:17 +01005238class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005239 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005240 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005241 HInstruction* value,
5242 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005243 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305244 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005245 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5246 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005247 }
5248
Roland Levillain5b5b9312016-03-22 14:57:31 +00005249 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005250 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005251 return value >> (distance & max_shift_distance);
5252 }
5253
5254 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005255 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005256 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005257 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005258 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005259 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005260 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005261 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005262 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5263 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5264 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5265 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005266 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005267 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5268 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005269 LOG(FATAL) << DebugName() << " is not defined for float values";
5270 UNREACHABLE();
5271 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005272 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5273 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005274 LOG(FATAL) << DebugName() << " is not defined for double values";
5275 UNREACHABLE();
5276 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005277
5278 DECLARE_INSTRUCTION(Shr);
5279
Artem Serovcced8ba2017-07-19 18:18:09 +01005280 protected:
5281 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005282};
5283
Vladimir Markofcb503c2016-05-18 12:48:17 +01005284class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005285 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005287 HInstruction* value,
5288 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005289 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305290 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005291 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5292 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005293 }
5294
Roland Levillain5b5b9312016-03-22 14:57:31 +00005295 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005296 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005297 typedef typename std::make_unsigned<T>::type V;
5298 V ux = static_cast<V>(value);
5299 return static_cast<T>(ux >> (distance & max_shift_distance));
5300 }
5301
5302 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005303 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005304 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005305 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005306 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005307 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005308 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005309 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005310 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5311 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5312 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5313 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005314 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005315 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5316 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005317 LOG(FATAL) << DebugName() << " is not defined for float values";
5318 UNREACHABLE();
5319 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005320 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5321 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005322 LOG(FATAL) << DebugName() << " is not defined for double values";
5323 UNREACHABLE();
5324 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005325
5326 DECLARE_INSTRUCTION(UShr);
5327
Artem Serovcced8ba2017-07-19 18:18:09 +01005328 protected:
5329 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005330};
5331
Vladimir Markofcb503c2016-05-18 12:48:17 +01005332class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005333 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005334 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005335 HInstruction* left,
5336 HInstruction* right,
5337 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305338 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5339 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005340
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005341 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005342
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005343 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005344
5345 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005346 return GetBlock()->GetGraph()->GetIntConstant(
5347 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005348 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005349 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005350 return GetBlock()->GetGraph()->GetLongConstant(
5351 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005352 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005353 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5354 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5355 LOG(FATAL) << DebugName() << " is not defined for float values";
5356 UNREACHABLE();
5357 }
5358 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5359 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5360 LOG(FATAL) << DebugName() << " is not defined for double values";
5361 UNREACHABLE();
5362 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005363
5364 DECLARE_INSTRUCTION(And);
5365
Artem Serovcced8ba2017-07-19 18:18:09 +01005366 protected:
5367 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005368};
5369
Vladimir Markofcb503c2016-05-18 12:48:17 +01005370class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005371 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005372 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005373 HInstruction* left,
5374 HInstruction* right,
5375 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305376 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5377 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005378
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005379 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005380
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005381 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005382
5383 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005384 return GetBlock()->GetGraph()->GetIntConstant(
5385 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005386 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005387 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005388 return GetBlock()->GetGraph()->GetLongConstant(
5389 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005390 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005391 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5392 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5393 LOG(FATAL) << DebugName() << " is not defined for float values";
5394 UNREACHABLE();
5395 }
5396 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5397 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5398 LOG(FATAL) << DebugName() << " is not defined for double values";
5399 UNREACHABLE();
5400 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005401
5402 DECLARE_INSTRUCTION(Or);
5403
Artem Serovcced8ba2017-07-19 18:18:09 +01005404 protected:
5405 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005406};
5407
Vladimir Markofcb503c2016-05-18 12:48:17 +01005408class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005409 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005410 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005411 HInstruction* left,
5412 HInstruction* right,
5413 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305414 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5415 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005416
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005417 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005418
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005419 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005420
5421 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005422 return GetBlock()->GetGraph()->GetIntConstant(
5423 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005424 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005425 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005426 return GetBlock()->GetGraph()->GetLongConstant(
5427 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005428 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005429 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5430 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5431 LOG(FATAL) << DebugName() << " is not defined for float values";
5432 UNREACHABLE();
5433 }
5434 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5435 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5436 LOG(FATAL) << DebugName() << " is not defined for double values";
5437 UNREACHABLE();
5438 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005439
5440 DECLARE_INSTRUCTION(Xor);
5441
Artem Serovcced8ba2017-07-19 18:18:09 +01005442 protected:
5443 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005444};
5445
Vladimir Markofcb503c2016-05-18 12:48:17 +01005446class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005447 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005448 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305449 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005450 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5451 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005452 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005453
Roland Levillain5b5b9312016-03-22 14:57:31 +00005454 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005455 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005456 typedef typename std::make_unsigned<T>::type V;
5457 V ux = static_cast<V>(value);
5458 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005459 return static_cast<T>(ux);
5460 } else {
5461 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005462 return static_cast<T>(ux >> (distance & max_shift_value)) |
5463 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005464 }
5465 }
5466
Roland Levillain5b5b9312016-03-22 14:57:31 +00005467 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005468 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005469 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005470 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005471 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005472 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005473 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005474 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005475 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5476 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5477 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5478 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005479 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005480 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5481 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005482 LOG(FATAL) << DebugName() << " is not defined for float values";
5483 UNREACHABLE();
5484 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005485 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5486 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005487 LOG(FATAL) << DebugName() << " is not defined for double values";
5488 UNREACHABLE();
5489 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005490
5491 DECLARE_INSTRUCTION(Ror);
5492
Artem Serovcced8ba2017-07-19 18:18:09 +01005493 protected:
5494 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005495};
5496
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005497// The value of a parameter in this method. Its location depends on
5498// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005499class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005500 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005501 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005502 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005503 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005504 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005505 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305506 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005507 dex_file_(dex_file),
5508 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005509 index_(index) {
5510 SetPackedFlag<kFlagIsThis>(is_this);
5511 SetPackedFlag<kFlagCanBeNull>(!is_this);
5512 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005513
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005514 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005515 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005516 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005517 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005518
Vladimir Markoa1de9182016-02-25 11:37:38 +00005519 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5520 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005521
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005522 DECLARE_INSTRUCTION(ParameterValue);
5523
Artem Serovcced8ba2017-07-19 18:18:09 +01005524 protected:
5525 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5526
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005527 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005528 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markoc6760552018-05-03 17:09:09 +01005529 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005530 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5531 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5532 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5533 "Too many packed fields.");
5534
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005535 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005536 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005537 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005538 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005539 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005540};
5541
Vladimir Markofcb503c2016-05-18 12:48:17 +01005542class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005543 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005544 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305545 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5546 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005548 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005549 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005550 return true;
5551 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005552
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005553 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005554
5555 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005556 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005557 }
5558 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005559 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005560 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005561 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5562 LOG(FATAL) << DebugName() << " is not defined for float values";
5563 UNREACHABLE();
5564 }
5565 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5566 LOG(FATAL) << DebugName() << " is not defined for double values";
5567 UNREACHABLE();
5568 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005569
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005570 DECLARE_INSTRUCTION(Not);
5571
Artem Serovcced8ba2017-07-19 18:18:09 +01005572 protected:
5573 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005574};
5575
Vladimir Markofcb503c2016-05-18 12:48:17 +01005576class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005577 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005578 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305579 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5580 }
David Brazdil66d126e2015-04-03 16:02:44 +01005581
5582 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005583 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005584 return true;
5585 }
5586
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005587 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005588 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005589 return !x;
5590 }
5591
Roland Levillain9867bc72015-08-05 10:21:34 +01005592 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005593 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005594 }
5595 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5596 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005597 UNREACHABLE();
5598 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005599 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5600 LOG(FATAL) << DebugName() << " is not defined for float values";
5601 UNREACHABLE();
5602 }
5603 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5604 LOG(FATAL) << DebugName() << " is not defined for double values";
5605 UNREACHABLE();
5606 }
David Brazdil66d126e2015-04-03 16:02:44 +01005607
5608 DECLARE_INSTRUCTION(BooleanNot);
5609
Artem Serovcced8ba2017-07-19 18:18:09 +01005610 protected:
5611 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005612};
5613
Vladimir Markofcb503c2016-05-18 12:48:17 +01005614class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005615 public:
5616 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005617 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305618 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005619 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005620 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005622 }
5623
5624 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5626 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005627
Artem Serove972e722018-06-19 14:55:17 +01005628 bool IsClonable() const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00005629 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005630 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5631 return true;
5632 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005633
Mark Mendelle82549b2015-05-06 10:55:34 -04005634 // Try to statically evaluate the conversion and return a HConstant
5635 // containing the result. If the input cannot be converted, return nullptr.
5636 HConstant* TryStaticEvaluation() const;
5637
Roland Levillaindff1f282014-11-05 14:15:05 +00005638 DECLARE_INSTRUCTION(TypeConversion);
5639
Artem Serovcced8ba2017-07-19 18:18:09 +01005640 protected:
5641 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005642};
5643
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005644static constexpr uint32_t kNoRegNumber = -1;
5645
Vladimir Markofcb503c2016-05-18 12:48:17 +01005646class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005647 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005648 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serov34f8de32018-06-22 11:35:46 +01005649 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5650 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005651 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serov34f8de32018-06-22 11:35:46 +01005652 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005653 SetRawInputAt(0, value);
5654 }
5655
Artem Serovcced8ba2017-07-19 18:18:09 +01005656 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005657 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005658 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005659 return true;
5660 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005661
Calin Juravle10e244f2015-01-26 18:54:32 +00005662 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005663
Calin Juravle10e244f2015-01-26 18:54:32 +00005664 bool CanThrow() const OVERRIDE { return true; }
5665
5666 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005667
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005668 DECLARE_INSTRUCTION(NullCheck);
5669
Artem Serovcced8ba2017-07-19 18:18:09 +01005670 protected:
5671 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005672};
5673
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005674// Embeds an ArtField and all the information required by the compiler. We cache
5675// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005676class FieldInfo : public ValueObject {
5677 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005678 FieldInfo(ArtField* field,
5679 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005681 bool is_volatile,
5682 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005683 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005684 const DexFile& dex_file)
5685 : field_(field),
5686 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005687 field_type_(field_type),
5688 is_volatile_(is_volatile),
5689 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005690 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005691 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005692
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005693 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005694 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005695 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005696 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005697 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005698 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005699 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005700
5701 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005702 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005703 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005704 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005705 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005706 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005707 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005708 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005709};
5710
Vladimir Markofcb503c2016-05-18 12:48:17 +01005711class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005712 public:
5713 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005714 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005716 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005717 bool is_volatile,
5718 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005719 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005720 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005721 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305722 : HExpression(kInstanceFieldGet,
5723 field_type,
5724 SideEffects::FieldReadOfType(field_type, is_volatile),
5725 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005726 field_info_(field,
5727 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005728 field_type,
5729 is_volatile,
5730 field_idx,
5731 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005732 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005733 SetRawInputAt(0, value);
5734 }
5735
Artem Serovcced8ba2017-07-19 18:18:09 +01005736 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005737 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005738
Vladimir Marko372f10e2016-05-17 16:30:10 +01005739 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5740 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005741 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005742 }
5743
Calin Juravle641547a2015-04-21 22:08:51 +01005744 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005745 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005746 }
5747
5748 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005749 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5750 }
5751
Calin Juravle52c48962014-12-16 17:02:57 +00005752 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005753 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005754 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005755 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005756
Vladimir Marko61b92282017-10-11 13:23:17 +01005757 void SetType(DataType::Type new_type) {
5758 DCHECK(DataType::IsIntegralType(GetType()));
5759 DCHECK(DataType::IsIntegralType(new_type));
5760 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5761 SetPackedField<TypeField>(new_type);
5762 }
5763
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005764 DECLARE_INSTRUCTION(InstanceFieldGet);
5765
Artem Serovcced8ba2017-07-19 18:18:09 +01005766 protected:
5767 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5768
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005769 private:
5770 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005771};
5772
Vladimir Markoc6760552018-05-03 17:09:09 +01005773class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005774 public:
5775 HInstanceFieldSet(HInstruction* object,
5776 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005777 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005778 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005779 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005780 bool is_volatile,
5781 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005782 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005783 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005784 uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01005785 : HExpression(kInstanceFieldSet,
5786 SideEffects::FieldWriteOfType(field_type, is_volatile),
5787 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005788 field_info_(field,
5789 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005790 field_type,
5791 is_volatile,
5792 field_idx,
5793 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005794 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005795 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005796 SetRawInputAt(0, object);
5797 SetRawInputAt(1, value);
5798 }
5799
Artem Serovcced8ba2017-07-19 18:18:09 +01005800 bool IsClonable() const OVERRIDE { return true; }
5801
Calin Juravle641547a2015-04-21 22:08:51 +01005802 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005803 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005804 }
5805
Calin Juravle52c48962014-12-16 17:02:57 +00005806 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005807 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005808 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005809 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005810 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005811 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5812 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005813
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005814 DECLARE_INSTRUCTION(InstanceFieldSet);
5815
Artem Serovcced8ba2017-07-19 18:18:09 +01005816 protected:
5817 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5818
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005819 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005820 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5821 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5822 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5823 "Too many packed fields.");
5824
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005825 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005826};
5827
Vladimir Markofcb503c2016-05-18 12:48:17 +01005828class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005829 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005830 HArrayGet(HInstruction* array,
5831 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005832 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005833 uint32_t dex_pc)
5834 : HArrayGet(array,
5835 index,
5836 type,
5837 SideEffects::ArrayReadOfType(type),
5838 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305839 /* is_string_char_at */ false) {
5840 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005841
5842 HArrayGet(HInstruction* array,
5843 HInstruction* index,
5844 DataType::Type type,
5845 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005846 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005847 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305848 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005849 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005850 SetRawInputAt(0, array);
5851 SetRawInputAt(1, index);
5852 }
5853
Artem Serovcced8ba2017-07-19 18:18:09 +01005854 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005855 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005856 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005857 return true;
5858 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005859 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005860 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005861 // Currently, unless the array is the result of NewArray, the array access is always
5862 // preceded by some form of null NullCheck necessary for the bounds check, usually
5863 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5864 // dynamic BCE. There are cases when these could be removed to produce better code.
5865 // If we ever add optimizations to do so we should allow an implicit check here
5866 // (as long as the address falls in the first page).
5867 //
5868 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5869 // a = cond ? new int[1] : null;
5870 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005871 return false;
5872 }
5873
David Brazdil4833f5a2015-12-16 10:37:39 +00005874 bool IsEquivalentOf(HArrayGet* other) const {
5875 bool result = (GetDexPc() == other->GetDexPc());
5876 if (kIsDebugBuild && result) {
5877 DCHECK_EQ(GetBlock(), other->GetBlock());
5878 DCHECK_EQ(GetArray(), other->GetArray());
5879 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005880 if (DataType::IsIntOrLongType(GetType())) {
5881 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005882 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005883 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5884 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005885 }
5886 }
5887 return result;
5888 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005889
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005890 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5891
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005892 HInstruction* GetArray() const { return InputAt(0); }
5893 HInstruction* GetIndex() const { return InputAt(1); }
5894
Vladimir Marko61b92282017-10-11 13:23:17 +01005895 void SetType(DataType::Type new_type) {
5896 DCHECK(DataType::IsIntegralType(GetType()));
5897 DCHECK(DataType::IsIntegralType(new_type));
5898 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5899 SetPackedField<TypeField>(new_type);
5900 }
5901
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005902 DECLARE_INSTRUCTION(ArrayGet);
5903
Artem Serovcced8ba2017-07-19 18:18:09 +01005904 protected:
5905 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5906
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005907 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005908 // We treat a String as an array, creating the HArrayGet from String.charAt()
5909 // intrinsic in the instruction simplifier. We can always determine whether
5910 // a particular HArrayGet is actually a String.charAt() by looking at the type
5911 // of the input but that requires holding the mutator lock, so we prefer to use
5912 // a flag, so that code generators don't need to do the locking.
Vladimir Markoc6760552018-05-03 17:09:09 +01005913 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005914 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5915 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5916 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005917};
5918
Vladimir Markoc6760552018-05-03 17:09:09 +01005919class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005920 public:
5921 HArraySet(HInstruction* array,
5922 HInstruction* index,
5923 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005924 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005925 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005926 : HArraySet(array,
5927 index,
5928 value,
5929 expected_component_type,
5930 // Make a best guess for side effects now, may be refined during SSA building.
5931 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305932 dex_pc) {
5933 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005934
5935 HArraySet(HInstruction* array,
5936 HInstruction* index,
5937 HInstruction* value,
5938 DataType::Type expected_component_type,
5939 SideEffects side_effects,
5940 uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01005941 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005942 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005943 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005944 SetPackedFlag<kFlagValueCanBeNull>(true);
5945 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005946 SetRawInputAt(0, array);
5947 SetRawInputAt(1, index);
5948 SetRawInputAt(2, value);
5949 }
5950
Artem Serovcced8ba2017-07-19 18:18:09 +01005951 bool IsClonable() const OVERRIDE { return true; }
5952
Calin Juravle77520bc2015-01-12 18:45:46 +00005953 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005954 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005955 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005956 }
5957
Mingyao Yang81014cb2015-06-02 03:16:27 -07005958 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005959 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005960
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005961 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005962 // TODO: Same as for ArrayGet.
5963 return false;
5964 }
5965
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005966 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005967 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005968 }
5969
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005970 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005971 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005972 }
5973
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005974 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005975 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005976 }
5977
Vladimir Markoa1de9182016-02-25 11:37:38 +00005978 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5979 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5980 bool StaticTypeOfArrayIsObjectArray() const {
5981 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5982 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005983
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005984 HInstruction* GetArray() const { return InputAt(0); }
5985 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005986 HInstruction* GetValue() const { return InputAt(2); }
5987
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005988 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005989 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5990 }
5991
5992 static DataType::Type GetComponentType(DataType::Type value_type,
5993 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005994 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005995 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005996 // be correct, we also check what is the value type. If it is a floating
5997 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005998 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005999 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006000 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006001 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006002
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006003 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006005 }
6006
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006007 static SideEffects ComputeSideEffects(DataType::Type type) {
6008 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006009 }
6010
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006011 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6012 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6013 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006014 }
6015
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006016 DECLARE_INSTRUCTION(ArraySet);
6017
Artem Serovcced8ba2017-07-19 18:18:09 +01006018 protected:
6019 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6020
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006021 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006022 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6023 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006024 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025 static constexpr size_t kFlagNeedsTypeCheck =
6026 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6027 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006028 // Cached information for the reference_type_info_ so that codegen
6029 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006030 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6031 static constexpr size_t kNumberOfArraySetPackedBits =
6032 kFlagStaticTypeOfArrayIsObjectArray + 1;
6033 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6034 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006035 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006036};
6037
Vladimir Markofcb503c2016-05-18 12:48:17 +01006038class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006039 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006040 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306041 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006042 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006043 // Note that arrays do not change length, so the instruction does not
6044 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006045 SetRawInputAt(0, array);
6046 }
6047
Artem Serovcced8ba2017-07-19 18:18:09 +01006048 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00006049 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006050 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006051 return true;
6052 }
Calin Juravle641547a2015-04-21 22:08:51 +01006053 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
6054 return obj == InputAt(0);
6055 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006056
Vladimir Markodce016e2016-04-28 13:10:02 +01006057 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6058
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006059 DECLARE_INSTRUCTION(ArrayLength);
6060
Artem Serovcced8ba2017-07-19 18:18:09 +01006061 protected:
6062 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6063
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006064 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006065 // We treat a String as an array, creating the HArrayLength from String.length()
6066 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6067 // determine whether a particular HArrayLength is actually a String.length() by
6068 // looking at the type of the input but that requires holding the mutator lock, so
6069 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markoc6760552018-05-03 17:09:09 +01006070 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006071 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6072 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6073 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006074};
6075
Vladimir Markofcb503c2016-05-18 12:48:17 +01006076class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006077 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006078 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serov34f8de32018-06-22 11:35:46 +01006079 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6080 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006081 HBoundsCheck(HInstruction* index,
6082 HInstruction* length,
6083 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006084 bool is_string_char_at = false)
Artem Serov34f8de32018-06-22 11:35:46 +01006085 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006086 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006087 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006088 SetRawInputAt(0, index);
6089 SetRawInputAt(1, length);
6090 }
6091
Artem Serovcced8ba2017-07-19 18:18:09 +01006092 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006093 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006094 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006095 return true;
6096 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006097
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006098 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006099
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006100 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006101
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006102 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006103
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006104 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006105
6106 DECLARE_INSTRUCTION(BoundsCheck);
6107
Artem Serovcced8ba2017-07-19 18:18:09 +01006108 protected:
6109 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6110
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006111 private:
Vladimir Markoc6760552018-05-03 17:09:09 +01006112 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006113};
6114
Vladimir Markoc6760552018-05-03 17:09:09 +01006115class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006116 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006117 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01006118 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306119 slow_path_(nullptr) {
6120 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006121
Artem Serovcced8ba2017-07-19 18:18:09 +01006122 bool IsClonable() const OVERRIDE { return true; }
6123
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006124 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006125 return true;
6126 }
6127
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006128 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6129 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006130
6131 DECLARE_INSTRUCTION(SuspendCheck);
6132
Artem Serovcced8ba2017-07-19 18:18:09 +01006133 protected:
6134 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6135
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006136 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006137 // Only used for code generation, in order to share the same slow path between back edges
6138 // of a same loop.
6139 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006140};
6141
David Srbecky0cf44932015-12-09 14:09:59 +00006142// Pseudo-instruction which provides the native debugger with mapping information.
6143// It ensures that we can generate line number and local variables at this point.
Vladimir Markoc6760552018-05-03 17:09:09 +01006144class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006145 public:
6146 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01006147 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306148 }
David Srbecky0cf44932015-12-09 14:09:59 +00006149
6150 bool NeedsEnvironment() const OVERRIDE {
6151 return true;
6152 }
6153
6154 DECLARE_INSTRUCTION(NativeDebugInfo);
6155
Artem Serovcced8ba2017-07-19 18:18:09 +01006156 protected:
6157 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006158};
6159
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006160/**
6161 * Instruction to load a Class object.
6162 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006163class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006164 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 // Determines how to load the Class.
6166 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006167 // We cannot load this class. See HSharpening::SharpenLoadClass.
6168 kInvalid = -1,
6169
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006170 // Use the Class* from the method's own ArtMethod*.
6171 kReferrersClass,
6172
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006173 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006174 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006175 kBootImageLinkTimePcRelative,
6176
6177 // Use a known boot image Class* address, embedded in the code by the codegen.
6178 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006179 kBootImageAddress,
6180
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006181 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
6182 // of the oat file.
6183 kBootImageClassTable,
6184
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006185 // Load from an entry in the .bss section using a PC-relative load.
6186 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6187 kBssEntry,
6188
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006189 // Load from the root table associated with the JIT compiled method.
6190 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006191
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006192 // Load using a simple runtime call. This is the fall-back load kind when
6193 // the codegen is unable to use another appropriate kind.
6194 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006195
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006196 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006197 };
6198
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006199 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006200 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006201 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006202 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006203 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006204 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006205 bool needs_access_check)
Vladimir Markoc6760552018-05-03 17:09:09 +01006206 : HInstruction(kLoadClass,
6207 DataType::Type::kReference,
6208 SideEffectsForArchRuntimeCalls(),
6209 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006210 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006211 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006212 dex_file_(dex_file),
Vladimir Markoaa6fb672018-03-27 09:03:13 +00006213 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006214 // Referrers class should not need access check. We never inline unverified
6215 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006216 DCHECK(!is_referrers_class || !needs_access_check);
6217
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006218 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006219 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006220 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006221 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006222 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markoaa6fb672018-03-27 09:03:13 +00006223 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006224 }
6225
Artem Serovcced8ba2017-07-19 18:18:09 +01006226 bool IsClonable() const OVERRIDE { return true; }
6227
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006228 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006229
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006230 LoadKind GetLoadKind() const {
6231 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006232 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006233
6234 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006235
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006236 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006237
Andreas Gampea5b09a62016-11-17 15:21:22 -08006238 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006239
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006240 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006241
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006242 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006243 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006244 }
6245
Calin Juravle0ba218d2015-05-19 18:46:01 +01006246 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006247 // The entrypoint the code generator is going to call does not do
6248 // clinit of the class.
6249 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006250 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006251 }
6252
6253 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006254 return NeedsAccessCheck() ||
6255 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006256 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006257 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006258 }
6259
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006260 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006261 return NeedsAccessCheck() ||
6262 MustGenerateClinitCheck() ||
6263 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6264 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6265 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006266 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006267 GetLoadKind() == LoadKind::kBssEntry) &&
6268 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006269 }
6270
Calin Juravleacf735c2015-02-12 15:25:22 +00006271 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Markoaa6fb672018-03-27 09:03:13 +00006272 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6273 // Note: The is_exact flag from the return value should not be used.
6274 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6275 } else {
6276 return ReferenceTypeInfo::CreateInvalid();
6277 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006278 }
6279
Vladimir Markoaa6fb672018-03-27 09:03:13 +00006280 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6281 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6282 DCHECK(klass_ != nullptr);
6283 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006284 }
6285
Andreas Gampea5b09a62016-11-17 15:21:22 -08006286 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006287 const DexFile& GetDexFile() const { return dex_file_; }
6288
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006289 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006290 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006291 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006292
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006293 static SideEffects SideEffectsForArchRuntimeCalls() {
6294 return SideEffects::CanTriggerGC();
6295 }
6296
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006297 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006298 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006299 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006300 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006301
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006302 void MarkInBootImage() {
6303 SetPackedFlag<kFlagIsInBootImage>(true);
6304 }
6305
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006306 void AddSpecialInput(HInstruction* special_input);
6307
6308 using HInstruction::GetInputRecords; // Keep the const version visible.
6309 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6310 return ArrayRef<HUserRecord<HInstruction*>>(
6311 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6312 }
6313
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006314 Handle<mirror::Class> GetClass() const {
6315 return klass_;
6316 }
6317
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006318 DECLARE_INSTRUCTION(LoadClass);
6319
Artem Serovcced8ba2017-07-19 18:18:09 +01006320 protected:
6321 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6322
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006323 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006324 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006325 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006326 // Whether this instruction must generate the initialization check.
6327 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006328 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006329 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6330 static constexpr size_t kFieldLoadKindSize =
6331 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Markoaa6fb672018-03-27 09:03:13 +00006332 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6333 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006334 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006335 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6336
6337 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006338 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006339 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006340 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006341 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006342 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006343 }
6344
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006345 void SetLoadKindInternal(LoadKind load_kind);
6346
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006347 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006348 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006349 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006350 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006351
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006352 // A type index and dex file where the class can be accessed. The dex file can be:
6353 // - The compiling method's dex file if the class is defined there too.
6354 // - The compiling method's dex file if the class is referenced there.
6355 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006356 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006357 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006358 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006359
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006360 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006361};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006362std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6363
6364// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006365inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6366 // The load kind should be determined before inserting the instruction to the graph.
6367 DCHECK(GetBlock() == nullptr);
6368 DCHECK(GetEnvironment() == nullptr);
6369 SetPackedField<LoadKindField>(load_kind);
6370 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6371 special_input_ = HUserRecord<HInstruction*>(nullptr);
6372 }
6373 if (!NeedsEnvironment()) {
6374 SetSideEffects(SideEffects::None());
6375 }
6376}
6377
6378// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006379inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006380 // The special input is used for PC-relative loads on some architectures,
6381 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006382 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006383 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006384 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006385 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006386 DCHECK(special_input_.GetInstruction() == nullptr);
6387 special_input_ = HUserRecord<HInstruction*>(special_input);
6388 special_input->AddUseAt(this, 0);
6389}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006390
Vladimir Marko372f10e2016-05-17 16:30:10 +01006391class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006392 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006393 // Determines how to load the String.
6394 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006395 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006396 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006397 kBootImageLinkTimePcRelative,
6398
6399 // Use a known boot image String* address, embedded in the code by the codegen.
6400 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006401 kBootImageAddress,
6402
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006403 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
6404 // of the oat file.
6405 kBootImageInternTable,
6406
Vladimir Markoaad75c62016-10-03 08:46:48 +00006407 // Load from an entry in the .bss section using a PC-relative load.
6408 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6409 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006410
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006411 // Load from the root table associated with the JIT compiled method.
6412 kJitTableAddress,
6413
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006414 // Load using a simple runtime call. This is the fall-back load kind when
6415 // the codegen is unable to use another appropriate kind.
6416 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006417
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006418 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006419 };
6420
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006421 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006422 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006423 const DexFile& dex_file,
6424 uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01006425 : HInstruction(kLoadString,
6426 DataType::Type::kReference,
6427 SideEffectsForArchRuntimeCalls(),
6428 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006429 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006430 string_index_(string_index),
6431 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006432 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006433 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006434
Artem Serovcced8ba2017-07-19 18:18:09 +01006435 bool IsClonable() const OVERRIDE { return true; }
6436
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006437 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006438
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006439 LoadKind GetLoadKind() const {
6440 return GetPackedField<LoadKindField>();
6441 }
6442
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006443 const DexFile& GetDexFile() const {
6444 return dex_file_;
6445 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006446
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006447 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006448 return string_index_;
6449 }
6450
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006451 Handle<mirror::String> GetString() const {
6452 return string_;
6453 }
6454
6455 void SetString(Handle<mirror::String> str) {
6456 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006457 }
6458
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006459 bool CanBeMoved() const OVERRIDE { return true; }
6460
Vladimir Marko372f10e2016-05-17 16:30:10 +01006461 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006462
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006463 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006464
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006465 // Will call the runtime if we need to load the string through
6466 // the dex cache and the string is not guaranteed to be there yet.
6467 bool NeedsEnvironment() const OVERRIDE {
6468 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006469 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006470 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006471 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006472 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006473 return false;
6474 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006475 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006476 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006477
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006478 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006479 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006480 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006481
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006482 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006483 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006484
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006485 static SideEffects SideEffectsForArchRuntimeCalls() {
6486 return SideEffects::CanTriggerGC();
6487 }
6488
Vladimir Marko372f10e2016-05-17 16:30:10 +01006489 void AddSpecialInput(HInstruction* special_input);
6490
6491 using HInstruction::GetInputRecords; // Keep the const version visible.
6492 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6493 return ArrayRef<HUserRecord<HInstruction*>>(
6494 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006495 }
6496
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006497 DECLARE_INSTRUCTION(LoadString);
6498
Artem Serovcced8ba2017-07-19 18:18:09 +01006499 protected:
6500 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6501
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006502 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006503 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006504 static constexpr size_t kFieldLoadKindSize =
6505 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6506 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006507 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006508 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006509
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006510 void SetLoadKindInternal(LoadKind load_kind);
6511
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006512 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006513 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006514 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006515 HUserRecord<HInstruction*> special_input_;
6516
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006517 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006518 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006519
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006520 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006521};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006522std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6523
6524// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006525inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6526 // The load kind should be determined before inserting the instruction to the graph.
6527 DCHECK(GetBlock() == nullptr);
6528 DCHECK(GetEnvironment() == nullptr);
6529 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6530 SetPackedField<LoadKindField>(load_kind);
6531 if (load_kind != LoadKind::kRuntimeCall) {
6532 special_input_ = HUserRecord<HInstruction*>(nullptr);
6533 }
6534 if (!NeedsEnvironment()) {
6535 SetSideEffects(SideEffects::None());
6536 }
6537}
6538
6539// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006540inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006541 // The special input is used for PC-relative loads on some architectures,
6542 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006543 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006544 GetLoadKind() == LoadKind::kBootImageAddress ||
6545 GetLoadKind() == LoadKind::kBootImageInternTable ||
6546 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006547 // HLoadString::GetInputRecords() returns an empty array at this point,
6548 // so use the GetInputRecords() from the base class to set the input record.
6549 DCHECK(special_input_.GetInstruction() == nullptr);
6550 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006551 special_input->AddUseAt(this, 0);
6552}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006553
Orion Hodson75046d12018-05-10 08:22:46 +01006554class HLoadMethodHandle FINAL : public HInstruction {
6555 public:
6556 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson20e7a6e2018-05-18 08:33:20 +01006557 uint16_t method_handle_idx,
6558 const DexFile& dex_file,
6559 uint32_t dex_pc)
Orion Hodson75046d12018-05-10 08:22:46 +01006560 : HInstruction(kLoadMethodHandle,
6561 DataType::Type::kReference,
6562 SideEffectsForArchRuntimeCalls(),
6563 dex_pc),
6564 special_input_(HUserRecord<HInstruction*>(current_method)),
6565 method_handle_idx_(method_handle_idx),
6566 dex_file_(dex_file) {
6567 }
6568
6569 using HInstruction::GetInputRecords; // Keep the const version visible.
6570 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6571 return ArrayRef<HUserRecord<HInstruction*>>(
6572 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6573 }
6574
6575 bool IsClonable() const OVERRIDE { return true; }
6576
6577 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6578
6579 const DexFile& GetDexFile() const { return dex_file_; }
6580
6581 static SideEffects SideEffectsForArchRuntimeCalls() {
6582 return SideEffects::CanTriggerGC();
6583 }
6584
6585 DECLARE_INSTRUCTION(LoadMethodHandle);
6586
6587 protected:
6588 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6589
6590 private:
6591 // The special input is the HCurrentMethod for kRuntimeCall.
6592 HUserRecord<HInstruction*> special_input_;
6593
6594 const uint16_t method_handle_idx_;
6595 const DexFile& dex_file_;
6596};
6597
Orion Hodsonb426afb2018-04-12 11:18:23 +01006598class HLoadMethodType FINAL : public HInstruction {
6599 public:
6600 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson16b2adf2018-05-14 08:53:38 +01006601 dex::ProtoIndex proto_index,
Orion Hodsonb426afb2018-04-12 11:18:23 +01006602 const DexFile& dex_file,
6603 uint32_t dex_pc)
6604 : HInstruction(kLoadMethodType,
6605 DataType::Type::kReference,
6606 SideEffectsForArchRuntimeCalls(),
6607 dex_pc),
6608 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson16b2adf2018-05-14 08:53:38 +01006609 proto_index_(proto_index),
Orion Hodsonb426afb2018-04-12 11:18:23 +01006610 dex_file_(dex_file) {
6611 }
6612
6613 using HInstruction::GetInputRecords; // Keep the const version visible.
6614 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6615 return ArrayRef<HUserRecord<HInstruction*>>(
6616 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6617 }
6618
6619 bool IsClonable() const OVERRIDE { return true; }
6620
Orion Hodson16b2adf2018-05-14 08:53:38 +01006621 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodsonb426afb2018-04-12 11:18:23 +01006622
6623 const DexFile& GetDexFile() const { return dex_file_; }
6624
6625 static SideEffects SideEffectsForArchRuntimeCalls() {
6626 return SideEffects::CanTriggerGC();
6627 }
6628
6629 DECLARE_INSTRUCTION(LoadMethodType);
6630
6631 protected:
6632 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6633
6634 private:
6635 // The special input is the HCurrentMethod for kRuntimeCall.
6636 HUserRecord<HInstruction*> special_input_;
6637
Orion Hodson16b2adf2018-05-14 08:53:38 +01006638 const dex::ProtoIndex proto_index_;
Orion Hodsonb426afb2018-04-12 11:18:23 +01006639 const DexFile& dex_file_;
6640};
6641
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006642/**
6643 * Performs an initialization check on its Class object input.
6644 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006645class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006646 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006647 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006648 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306649 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006651 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006652 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006653 SetRawInputAt(0, constant);
6654 }
Artem Serove972e722018-06-19 14:55:17 +01006655 // TODO: Make ClinitCheck clonable.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006656 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006657 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006658 return true;
6659 }
6660
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006661 bool NeedsEnvironment() const OVERRIDE {
6662 // May call runtime to initialize the class.
6663 return true;
6664 }
6665
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006666 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006667
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006668 HLoadClass* GetLoadClass() const {
6669 DCHECK(InputAt(0)->IsLoadClass());
6670 return InputAt(0)->AsLoadClass();
6671 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006672
6673 DECLARE_INSTRUCTION(ClinitCheck);
6674
Artem Serovcced8ba2017-07-19 18:18:09 +01006675
6676 protected:
6677 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006678};
6679
Vladimir Markofcb503c2016-05-18 12:48:17 +01006680class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006681 public:
6682 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006683 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006684 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006685 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006686 bool is_volatile,
6687 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006688 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006689 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006690 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306691 : HExpression(kStaticFieldGet,
6692 field_type,
6693 SideEffects::FieldReadOfType(field_type, is_volatile),
6694 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006695 field_info_(field,
6696 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006697 field_type,
6698 is_volatile,
6699 field_idx,
6700 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006701 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006702 SetRawInputAt(0, cls);
6703 }
6704
Calin Juravle52c48962014-12-16 17:02:57 +00006705
Artem Serovcced8ba2017-07-19 18:18:09 +01006706 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006707 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006708
Vladimir Marko372f10e2016-05-17 16:30:10 +01006709 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6710 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006711 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006712 }
6713
6714 size_t ComputeHashCode() const OVERRIDE {
6715 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6716 }
6717
Calin Juravle52c48962014-12-16 17:02:57 +00006718 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006719 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006720 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006721 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006722
Vladimir Marko61b92282017-10-11 13:23:17 +01006723 void SetType(DataType::Type new_type) {
6724 DCHECK(DataType::IsIntegralType(GetType()));
6725 DCHECK(DataType::IsIntegralType(new_type));
6726 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6727 SetPackedField<TypeField>(new_type);
6728 }
6729
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006730 DECLARE_INSTRUCTION(StaticFieldGet);
6731
Artem Serovcced8ba2017-07-19 18:18:09 +01006732 protected:
6733 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6734
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006735 private:
6736 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006737};
6738
Vladimir Markoc6760552018-05-03 17:09:09 +01006739class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006740 public:
6741 HStaticFieldSet(HInstruction* cls,
6742 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006743 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006744 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006745 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006746 bool is_volatile,
6747 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006748 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006749 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006750 uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01006751 : HExpression(kStaticFieldSet,
6752 SideEffects::FieldWriteOfType(field_type, is_volatile),
6753 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006754 field_info_(field,
6755 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006756 field_type,
6757 is_volatile,
6758 field_idx,
6759 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006760 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006761 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006762 SetRawInputAt(0, cls);
6763 SetRawInputAt(1, value);
6764 }
6765
Artem Serovcced8ba2017-07-19 18:18:09 +01006766 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006767 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006768 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006769 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006770 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006771
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006772 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006773 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6774 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006775
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006776 DECLARE_INSTRUCTION(StaticFieldSet);
6777
Artem Serovcced8ba2017-07-19 18:18:09 +01006778 protected:
6779 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6780
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006781 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006782 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6783 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6784 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6785 "Too many packed fields.");
6786
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006787 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006788};
6789
Vladimir Markofcb503c2016-05-18 12:48:17 +01006790class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006791 public:
6792 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006793 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006794 uint32_t field_index,
6795 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306796 : HExpression(kUnresolvedInstanceFieldGet,
6797 field_type,
6798 SideEffects::AllExceptGCDependency(),
6799 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006800 field_index_(field_index) {
6801 SetRawInputAt(0, obj);
6802 }
6803
Artem Serovcced8ba2017-07-19 18:18:09 +01006804 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006805 bool NeedsEnvironment() const OVERRIDE { return true; }
6806 bool CanThrow() const OVERRIDE { return true; }
6807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006808 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006809 uint32_t GetFieldIndex() const { return field_index_; }
6810
6811 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6812
Artem Serovcced8ba2017-07-19 18:18:09 +01006813 protected:
6814 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6815
Calin Juravlee460d1d2015-09-29 04:52:17 +01006816 private:
6817 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006818};
6819
Vladimir Markoc6760552018-05-03 17:09:09 +01006820class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006821 public:
6822 HUnresolvedInstanceFieldSet(HInstruction* obj,
6823 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006824 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006825 uint32_t field_index,
6826 uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01006827 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006828 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006829 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006830 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006831 SetRawInputAt(0, obj);
6832 SetRawInputAt(1, value);
6833 }
6834
Artem Serovcced8ba2017-07-19 18:18:09 +01006835 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006836 bool NeedsEnvironment() const OVERRIDE { return true; }
6837 bool CanThrow() const OVERRIDE { return true; }
6838
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006839 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006840 uint32_t GetFieldIndex() const { return field_index_; }
6841
6842 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6843
Artem Serovcced8ba2017-07-19 18:18:09 +01006844 protected:
6845 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6846
Calin Juravlee460d1d2015-09-29 04:52:17 +01006847 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006848 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6849 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006850 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006851 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6852 kFieldFieldType + kFieldFieldTypeSize;
6853 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6854 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006855 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006856
Calin Juravlee460d1d2015-09-29 04:52:17 +01006857 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006858};
6859
Vladimir Markofcb503c2016-05-18 12:48:17 +01006860class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006861 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006862 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006863 uint32_t field_index,
6864 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306865 : HExpression(kUnresolvedStaticFieldGet,
6866 field_type,
6867 SideEffects::AllExceptGCDependency(),
6868 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006869 field_index_(field_index) {
6870 }
6871
Artem Serovcced8ba2017-07-19 18:18:09 +01006872 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006873 bool NeedsEnvironment() const OVERRIDE { return true; }
6874 bool CanThrow() const OVERRIDE { return true; }
6875
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006876 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006877 uint32_t GetFieldIndex() const { return field_index_; }
6878
6879 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6880
Artem Serovcced8ba2017-07-19 18:18:09 +01006881 protected:
6882 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6883
Calin Juravlee460d1d2015-09-29 04:52:17 +01006884 private:
6885 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006886};
6887
Vladimir Markoc6760552018-05-03 17:09:09 +01006888class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006889 public:
6890 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006891 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006892 uint32_t field_index,
6893 uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01006894 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006895 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006896 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006897 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006898 SetRawInputAt(0, value);
6899 }
6900
Artem Serovcced8ba2017-07-19 18:18:09 +01006901 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006902 bool NeedsEnvironment() const OVERRIDE { return true; }
6903 bool CanThrow() const OVERRIDE { return true; }
6904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006905 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006906 uint32_t GetFieldIndex() const { return field_index_; }
6907
6908 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6909
Artem Serovcced8ba2017-07-19 18:18:09 +01006910 protected:
6911 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6912
Calin Juravlee460d1d2015-09-29 04:52:17 +01006913 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006914 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6915 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006916 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006917 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6918 kFieldFieldType + kFieldFieldTypeSize;
6919 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6920 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006921 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006922
Calin Juravlee460d1d2015-09-29 04:52:17 +01006923 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006924};
6925
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006926// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006927class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006928 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006929 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306930 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6931 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006932
David Brazdilbbd733e2015-08-18 17:48:17 +01006933 bool CanBeNull() const OVERRIDE { return false; }
6934
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006935 DECLARE_INSTRUCTION(LoadException);
6936
Artem Serovcced8ba2017-07-19 18:18:09 +01006937 protected:
6938 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006939};
6940
David Brazdilcb1c0552015-08-04 16:22:25 +01006941// Implicit part of move-exception which clears thread-local exception storage.
6942// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markoc6760552018-05-03 17:09:09 +01006943class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006944 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006945 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01006946 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306947 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006948
6949 DECLARE_INSTRUCTION(ClearException);
6950
Artem Serovcced8ba2017-07-19 18:18:09 +01006951 protected:
6952 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006953};
6954
Vladimir Markoc6760552018-05-03 17:09:09 +01006955class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006956 public:
6957 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01006958 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006959 SetRawInputAt(0, exception);
6960 }
6961
6962 bool IsControlFlow() const OVERRIDE { return true; }
6963
6964 bool NeedsEnvironment() const OVERRIDE { return true; }
6965
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006966 bool CanThrow() const OVERRIDE { return true; }
6967
Aart Bika8b8e9b2018-01-09 11:01:02 -08006968 bool AlwaysThrows() const OVERRIDE { return true; }
6969
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006970 DECLARE_INSTRUCTION(Throw);
6971
Artem Serovcced8ba2017-07-19 18:18:09 +01006972 protected:
6973 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006974};
6975
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006976/**
6977 * Implementation strategies for the code generator of a HInstanceOf
6978 * or `HCheckCast`.
6979 */
6980enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006981 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006982 kExactCheck, // Can do a single class compare.
6983 kClassHierarchyCheck, // Can just walk the super class chain.
6984 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6985 kInterfaceCheck, // No optimization yet when checking against an interface.
6986 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006987 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Markoaa6fb672018-03-27 09:03:13 +00006988 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006989 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006990};
6991
Roland Levillain86503782016-02-11 19:07:30 +00006992std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6993
Vladimir Markoaa6fb672018-03-27 09:03:13 +00006994// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6995// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6996class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006997 public:
Vladimir Markoaa6fb672018-03-27 09:03:13 +00006998 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markoc6760552018-05-03 17:09:09 +01006999 DataType::Type type,
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007000 HInstruction* object,
7001 HInstruction* target_class_or_null,
7002 TypeCheckKind check_kind,
7003 Handle<mirror::Class> klass,
7004 uint32_t dex_pc,
7005 ArenaAllocator* allocator,
7006 HIntConstant* bitstring_path_to_root,
7007 HIntConstant* bitstring_mask,
7008 SideEffects side_effects)
7009 : HVariableInputSizeInstruction(
7010 kind,
Vladimir Markoc6760552018-05-03 17:09:09 +01007011 type,
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007012 side_effects,
7013 dex_pc,
7014 allocator,
7015 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
7016 kArenaAllocTypeCheckInputs),
7017 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007018 SetPackedField<TypeCheckKindField>(check_kind);
7019 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007020 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007021 SetRawInputAt(0, object);
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007022 SetRawInputAt(1, target_class_or_null);
7023 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7024 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7025 if (check_kind == TypeCheckKind::kBitstringCheck) {
7026 DCHECK(target_class_or_null->IsNullConstant());
7027 SetRawInputAt(2, bitstring_path_to_root);
7028 SetRawInputAt(3, bitstring_mask);
7029 } else {
7030 DCHECK(target_class_or_null->IsLoadClass());
7031 }
Vladimir Marko87584542017-12-12 17:47:52 +00007032 }
7033
7034 HLoadClass* GetTargetClass() const {
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007035 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007036 HInstruction* load_class = InputAt(1);
7037 DCHECK(load_class->IsLoadClass());
7038 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007039 }
7040
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007041 uint32_t GetBitstringPathToRoot() const {
7042 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7043 HInstruction* path_to_root = InputAt(2);
7044 DCHECK(path_to_root->IsIntConstant());
7045 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7046 }
7047
7048 uint32_t GetBitstringMask() const {
7049 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7050 HInstruction* mask = InputAt(3);
7051 DCHECK(mask->IsIntConstant());
7052 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7053 }
7054
Artem Serovcced8ba2017-07-19 18:18:09 +01007055 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007056 bool CanBeMoved() const OVERRIDE { return true; }
7057
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007058 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
7059 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7060 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007061 }
7062
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007063 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7064 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7065 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7066 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7067
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007068 ReferenceTypeInfo GetTargetClassRTI() {
7069 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7070 // Note: The is_exact flag from the return value should not be used.
7071 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7072 } else {
7073 return ReferenceTypeInfo::CreateInvalid();
7074 }
7075 }
7076
7077 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7078 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7079 DCHECK(klass_ != nullptr);
7080 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7081 }
7082
7083 Handle<mirror::Class> GetClass() const {
7084 return klass_;
7085 }
7086
7087 protected:
7088 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7089
7090 private:
7091 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7092 static constexpr size_t kFieldTypeCheckKindSize =
7093 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7094 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7095 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7096 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7097 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7098 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7099
7100 Handle<mirror::Class> klass_;
7101};
7102
7103class HInstanceOf FINAL : public HTypeCheckInstruction {
7104 public:
7105 HInstanceOf(HInstruction* object,
7106 HInstruction* target_class_or_null,
7107 TypeCheckKind check_kind,
7108 Handle<mirror::Class> klass,
7109 uint32_t dex_pc,
7110 ArenaAllocator* allocator,
7111 HIntConstant* bitstring_path_to_root,
7112 HIntConstant* bitstring_mask)
7113 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markoc6760552018-05-03 17:09:09 +01007114 DataType::Type::kBool,
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007115 object,
7116 target_class_or_null,
7117 check_kind,
7118 klass,
7119 dex_pc,
7120 allocator,
7121 bitstring_path_to_root,
7122 bitstring_mask,
7123 SideEffectsForArchRuntimeCalls(check_kind)) {}
7124
Artem Serove972e722018-06-19 14:55:17 +01007125 bool IsClonable() const OVERRIDE { return true; }
7126
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007127 bool NeedsEnvironment() const OVERRIDE {
7128 return CanCallRuntime(GetTypeCheckKind());
7129 }
7130
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007131 static bool CanCallRuntime(TypeCheckKind check_kind) {
7132 // Mips currently does runtime calls for any other checks.
7133 return check_kind != TypeCheckKind::kExactCheck;
7134 }
7135
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007136 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007137 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007138 }
7139
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007140 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007141
Artem Serovcced8ba2017-07-19 18:18:09 +01007142 protected:
7143 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007144};
7145
Vladimir Markofcb503c2016-05-18 12:48:17 +01007146class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007147 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007148 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307149 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007150 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7151 SetPackedFlag<kFlagUpperCanBeNull>(true);
7152 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007153 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007154 SetRawInputAt(0, input);
7155 }
7156
Artem Serovd225f4b2018-06-05 20:54:42 +01007157 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Artem Serovcced8ba2017-07-19 18:18:09 +01007158 bool IsClonable() const OVERRIDE { return true; }
7159
David Brazdilf5552582015-12-27 13:36:12 +00007160 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007161 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007162 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007163 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007164
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007165 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007166 DCHECK(GetUpperCanBeNull() || !can_be_null);
7167 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007168 }
7169
Vladimir Markoa1de9182016-02-25 11:37:38 +00007170 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007171
Calin Juravleb1498f62015-02-16 13:13:29 +00007172 DECLARE_INSTRUCTION(BoundType);
7173
Artem Serovcced8ba2017-07-19 18:18:09 +01007174 protected:
7175 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7176
Calin Juravleb1498f62015-02-16 13:13:29 +00007177 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007178 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7179 // is false then CanBeNull() cannot be true).
Vladimir Markoc6760552018-05-03 17:09:09 +01007180 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007181 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7182 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7183 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7184
Calin Juravleb1498f62015-02-16 13:13:29 +00007185 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007186 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7187 // It is used to bound the type in cases like:
7188 // if (x instanceof ClassX) {
7189 // // uper_bound_ will be ClassX
7190 // }
David Brazdilf5552582015-12-27 13:36:12 +00007191 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007192};
7193
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007194class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007195 public:
7196 HCheckCast(HInstruction* object,
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007197 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007198 TypeCheckKind check_kind,
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007199 Handle<mirror::Class> klass,
7200 uint32_t dex_pc,
7201 ArenaAllocator* allocator,
7202 HIntConstant* bitstring_path_to_root,
7203 HIntConstant* bitstring_mask)
7204 : HTypeCheckInstruction(kCheckCast,
Vladimir Markoc6760552018-05-03 17:09:09 +01007205 DataType::Type::kVoid,
Vladimir Markoaa6fb672018-03-27 09:03:13 +00007206 object,
7207 target_class_or_null,
7208 check_kind,
7209 klass,
7210 dex_pc,
7211 allocator,
7212 bitstring_path_to_root,
7213 bitstring_mask,
7214 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007215
Artem Serove972e722018-06-19 14:55:17 +01007216 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007217 bool NeedsEnvironment() const OVERRIDE {
7218 // Instruction may throw a CheckCastError.
7219 return true;
7220 }
7221
7222 bool CanThrow() const OVERRIDE { return true; }
7223
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007224 DECLARE_INSTRUCTION(CheckCast);
7225
Artem Serovcced8ba2017-07-19 18:18:09 +01007226 protected:
7227 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007228};
7229
Andreas Gampe26de38b2016-07-27 17:53:11 -07007230/**
7231 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7232 * @details We define the combined barrier types that are actually required
7233 * by the Java Memory Model, rather than using exactly the terminology from
7234 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7235 * primitives. Note that the JSR-133 cookbook generally does not deal with
7236 * store atomicity issues, and the recipes there are not always entirely sufficient.
7237 * The current recipe is as follows:
7238 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7239 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7240 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7241 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7242 * class has final fields.
7243 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7244 * store-to-memory instructions. Only generated together with non-temporal stores.
7245 */
7246enum MemBarrierKind {
7247 kAnyStore,
7248 kLoadAny,
7249 kStoreStore,
7250 kAnyAny,
7251 kNTStoreStore,
7252 kLastBarrierKind = kNTStoreStore
7253};
7254std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7255
Vladimir Markoc6760552018-05-03 17:09:09 +01007256class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007257 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007258 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01007259 : HExpression(kMemoryBarrier,
7260 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7261 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007262 SetPackedField<BarrierKindField>(barrier_kind);
7263 }
Calin Juravle27df7582015-04-17 19:12:31 +01007264
Artem Serovcced8ba2017-07-19 18:18:09 +01007265 bool IsClonable() const OVERRIDE { return true; }
7266
Vladimir Markoa1de9182016-02-25 11:37:38 +00007267 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007268
7269 DECLARE_INSTRUCTION(MemoryBarrier);
7270
Artem Serovcced8ba2017-07-19 18:18:09 +01007271 protected:
7272 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7273
Calin Juravle27df7582015-04-17 19:12:31 +01007274 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007275 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7276 static constexpr size_t kFieldBarrierKindSize =
7277 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7278 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7279 kFieldBarrierKind + kFieldBarrierKindSize;
7280 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7281 "Too many packed fields.");
7282 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007283};
7284
Igor Murashkind01745e2017-04-05 16:40:31 -07007285// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7286// the specified object(s), with respect to any subsequent store that might "publish"
7287// (i.e. make visible) the specified object to another thread.
7288//
7289// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7290// for all final fields (that were set) at the end of the invoked constructor.
7291//
7292// The constructor fence models the freeze actions for the final fields of an object
7293// being constructed (semantically at the end of the constructor). Constructor fences
7294// have a per-object affinity; two separate objects being constructed get two separate
7295// constructor fences.
7296//
7297// (Note: that if calling a super-constructor or forwarding to another constructor,
7298// the freezes would happen at the end of *that* constructor being invoked).
7299//
7300// The memory model guarantees that when the object being constructed is "published" after
7301// constructor completion (i.e. escapes the current thread via a store), then any final field
7302// writes must be observable on other threads (once they observe that publication).
7303//
7304// Further, anything written before the freeze, and read by dereferencing through the final field,
7305// must also be visible (so final object field could itself have an object with non-final fields;
7306// yet the freeze must also extend to them).
7307//
7308// Constructor example:
7309//
7310// class HasFinal {
7311// final int field; Optimizing IR for <init>()V:
7312// HasFinal() {
7313// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7314// // freeze(this.field); HConstructorFence(this)
7315// } HReturn
7316// }
7317//
7318// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7319// already-initialized classes; in that case the allocation must act as a "default-initializer"
7320// of the object which effectively writes the class pointer "final field".
7321//
7322// For example, we can model default-initialiation as roughly the equivalent of the following:
7323//
7324// class Object {
7325// private final Class header;
7326// }
7327//
7328// Java code: Optimizing IR:
7329//
7330// T new_instance<T>() {
7331// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7332// obj.header = T.class; // header write is done by above call.
7333// // freeze(obj.header) HConstructorFence(obj)
7334// return (T)obj;
7335// }
7336//
7337// See also:
7338// * CompilerDriver::RequiresConstructorBarrier
7339// * QuasiAtomic::ThreadFenceForConstructor
7340//
7341class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7342 // A fence has variable inputs because the inputs can be removed
7343 // after prepare_for_register_allocation phase.
7344 // (TODO: In the future a fence could freeze multiple objects
7345 // after merging two fences together.)
7346 public:
7347 // `fence_object` is the reference that needs to be protected for correct publication.
7348 //
7349 // It makes sense in the following situations:
7350 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7351 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7352 //
7353 // After construction the `fence_object` becomes the 0th input.
7354 // This is not an input in a real sense, but just a convenient place to stash the information
7355 // about the associated object.
7356 HConstructorFence(HInstruction* fence_object,
7357 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007358 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007359 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7360 // constraints described in the class header. We claim that these SideEffects constraints
7361 // enforce a superset of the real constraints.
7362 //
7363 // The ordering described above is conservatively modeled with SideEffects as follows:
7364 //
7365 // * To prevent reordering of the publication stores:
7366 // ----> "Reads of objects" is the initial SideEffect.
7367 // * For every primitive final field store in the constructor:
7368 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7369 // * If there are any stores to reference final fields in the constructor:
7370 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7371 // that are reachable from `fence_object` also need to be prevented for reordering
7372 // (and we do not want to do alias analysis to figure out what those stores are).
7373 //
7374 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7375 // even more conservative approach than the one described above, and prevents all of the
7376 // above reordering without analyzing any of the instructions in the constructor.
7377 //
7378 // If in a later phase we discover that there are no writes to reference final fields,
7379 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307380 : HVariableInputSizeInstruction(kConstructorFence,
7381 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007382 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007383 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007384 /* number_of_inputs */ 1,
7385 kArenaAllocConstructorFenceInputs) {
7386 DCHECK(fence_object != nullptr);
7387 SetRawInputAt(0, fence_object);
7388 }
7389
7390 // The object associated with this constructor fence.
7391 //
7392 // (Note: This will be null after the prepare_for_register_allocation phase,
7393 // as all constructor fence inputs are removed there).
7394 HInstruction* GetFenceObject() const {
7395 return InputAt(0);
7396 }
7397
7398 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7399 // - Delete `fence_use` from `this`'s use list.
7400 // - Delete `this` from `fence_use`'s inputs list.
7401 // - If the `fence_use` is dead, remove it from the graph.
7402 //
7403 // A fence is considered dead once it no longer has any uses
7404 // and all of the inputs are dead.
7405 //
7406 // This must *not* be called during/after prepare_for_register_allocation,
7407 // because that removes all the inputs to the fences but the fence is actually
7408 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007409 //
7410 // Returns how many HConstructorFence instructions were removed from graph.
7411 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007412
Igor Murashkindd018df2017-08-09 10:38:31 -07007413 // Combine all inputs of `this` and `other` instruction and remove
7414 // `other` from the graph.
7415 //
7416 // Inputs are unique after the merge.
7417 //
7418 // Requirement: `this` must not be the same as `other.
7419 void Merge(HConstructorFence* other);
7420
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007421 // Check if this constructor fence is protecting
7422 // an HNewInstance or HNewArray that is also the immediate
7423 // predecessor of `this`.
7424 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007425 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7426 // to be one of the inputs of `this`.
7427 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007428 // Returns the associated HNewArray or HNewInstance,
7429 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007430 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007431
Igor Murashkind01745e2017-04-05 16:40:31 -07007432 DECLARE_INSTRUCTION(ConstructorFence);
7433
Artem Serovcced8ba2017-07-19 18:18:09 +01007434 protected:
7435 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007436};
7437
Vladimir Markoc6760552018-05-03 17:09:09 +01007438class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007439 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007440 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007441 kEnter,
7442 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007443 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007444 };
7445
7446 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markoc6760552018-05-03 17:09:09 +01007447 : HExpression(kMonitorOperation,
7448 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7449 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007450 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007451 SetRawInputAt(0, object);
7452 }
7453
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007454 // Instruction may go into runtime, so we need an environment.
7455 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007456
7457 bool CanThrow() const OVERRIDE {
7458 // Verifier guarantees that monitor-exit cannot throw.
7459 // This is important because it allows the HGraphBuilder to remove
7460 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7461 return IsEnter();
7462 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007463
Vladimir Markoa1de9182016-02-25 11:37:38 +00007464 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7465 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007466
7467 DECLARE_INSTRUCTION(MonitorOperation);
7468
Artem Serovcced8ba2017-07-19 18:18:09 +01007469 protected:
7470 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7471
Calin Juravle52c48962014-12-16 17:02:57 +00007472 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007473 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7474 static constexpr size_t kFieldOperationKindSize =
7475 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7476 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7477 kFieldOperationKind + kFieldOperationKindSize;
7478 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7479 "Too many packed fields.");
7480 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007481};
7482
Vladimir Markofcb503c2016-05-18 12:48:17 +01007483class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007484 public:
7485 HSelect(HInstruction* condition,
7486 HInstruction* true_value,
7487 HInstruction* false_value,
7488 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307489 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007490 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7491
7492 // First input must be `true_value` or `false_value` to allow codegens to
7493 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7494 // that architectures which implement HSelect as a conditional move also
7495 // will not need to invert the condition.
7496 SetRawInputAt(0, false_value);
7497 SetRawInputAt(1, true_value);
7498 SetRawInputAt(2, condition);
7499 }
7500
Artem Serovcced8ba2017-07-19 18:18:09 +01007501 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007502 HInstruction* GetFalseValue() const { return InputAt(0); }
7503 HInstruction* GetTrueValue() const { return InputAt(1); }
7504 HInstruction* GetCondition() const { return InputAt(2); }
7505
7506 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007507 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7508 return true;
7509 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007510
7511 bool CanBeNull() const OVERRIDE {
7512 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7513 }
7514
7515 DECLARE_INSTRUCTION(Select);
7516
Artem Serovcced8ba2017-07-19 18:18:09 +01007517 protected:
7518 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007519};
7520
Vladimir Markof9f64412015-09-02 14:05:49 +01007521class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007522 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007523 MoveOperands(Location source,
7524 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007525 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007526 HInstruction* instruction)
7527 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007528
7529 Location GetSource() const { return source_; }
7530 Location GetDestination() const { return destination_; }
7531
7532 void SetSource(Location value) { source_ = value; }
7533 void SetDestination(Location value) { destination_ = value; }
7534
7535 // The parallel move resolver marks moves as "in-progress" by clearing the
7536 // destination (but not the source).
7537 Location MarkPending() {
7538 DCHECK(!IsPending());
7539 Location dest = destination_;
7540 destination_ = Location::NoLocation();
7541 return dest;
7542 }
7543
7544 void ClearPending(Location dest) {
7545 DCHECK(IsPending());
7546 destination_ = dest;
7547 }
7548
7549 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007550 DCHECK(source_.IsValid() || destination_.IsInvalid());
7551 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007552 }
7553
7554 // True if this blocks a move from the given location.
7555 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007556 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007557 }
7558
7559 // A move is redundant if it's been eliminated, if its source and
7560 // destination are the same, or if its destination is unneeded.
7561 bool IsRedundant() const {
7562 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7563 }
7564
7565 // We clear both operands to indicate move that's been eliminated.
7566 void Eliminate() {
7567 source_ = destination_ = Location::NoLocation();
7568 }
7569
7570 bool IsEliminated() const {
7571 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7572 return source_.IsInvalid();
7573 }
7574
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007575 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007576
Nicolas Geoffray90218252015-04-15 11:56:51 +01007577 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007578 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007579 }
7580
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007581 HInstruction* GetInstruction() const { return instruction_; }
7582
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007583 private:
7584 Location source_;
7585 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007586 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007587 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007588 // The instruction this move is assocatied with. Null when this move is
7589 // for moving an input in the expected locations of user (including a phi user).
7590 // This is only used in debug mode, to ensure we do not connect interval siblings
7591 // in the same parallel move.
7592 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007593};
7594
Roland Levillainc9285912015-12-18 10:38:42 +00007595std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7596
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007597static constexpr size_t kDefaultNumberOfMoves = 4;
7598
Vladimir Markoc6760552018-05-03 17:09:09 +01007599class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007600 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007601 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markoc6760552018-05-03 17:09:09 +01007602 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007603 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007604 moves_.reserve(kDefaultNumberOfMoves);
7605 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007606
Nicolas Geoffray90218252015-04-15 11:56:51 +01007607 void AddMove(Location source,
7608 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007609 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007610 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007611 DCHECK(source.IsValid());
7612 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007613 if (kIsDebugBuild) {
7614 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007615 for (const MoveOperands& move : moves_) {
7616 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007617 // Special case the situation where the move is for the spill slot
7618 // of the instruction.
7619 if ((GetPrevious() == instruction)
7620 || ((GetPrevious() == nullptr)
7621 && instruction->IsPhi()
7622 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007623 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007624 << "Doing parallel moves for the same instruction.";
7625 } else {
7626 DCHECK(false) << "Doing parallel moves for the same instruction.";
7627 }
7628 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007629 }
7630 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007631 for (const MoveOperands& move : moves_) {
7632 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007633 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007634 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007635 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007636 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007637 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007638 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007639 }
7640
Vladimir Marko225b6462015-09-28 12:17:40 +01007641 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007642 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007643 }
7644
Vladimir Marko225b6462015-09-28 12:17:40 +01007645 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007646
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007647 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007648
Artem Serovcced8ba2017-07-19 18:18:09 +01007649 protected:
7650 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7651
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007652 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007653 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007654};
7655
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007656// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7657// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7658// never used across anything that can trigger GC.
7659// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7660// So we represent it by the type `DataType::Type::kInt`.
7661class HIntermediateAddress FINAL : public HExpression<2> {
7662 public:
7663 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307664 : HExpression(kIntermediateAddress,
7665 DataType::Type::kInt32,
7666 SideEffects::DependsOnGC(),
7667 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007668 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7669 DataType::Size(DataType::Type::kReference))
7670 << "kPrimInt and kPrimNot have different sizes.";
7671 SetRawInputAt(0, base_address);
7672 SetRawInputAt(1, offset);
7673 }
7674
Artem Serovcced8ba2017-07-19 18:18:09 +01007675 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007676 bool CanBeMoved() const OVERRIDE { return true; }
7677 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7678 return true;
7679 }
7680 bool IsActualObject() const OVERRIDE { return false; }
7681
7682 HInstruction* GetBaseAddress() const { return InputAt(0); }
7683 HInstruction* GetOffset() const { return InputAt(1); }
7684
7685 DECLARE_INSTRUCTION(IntermediateAddress);
7686
Artem Serovcced8ba2017-07-19 18:18:09 +01007687 protected:
7688 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007689};
7690
7691
Mark Mendell0616ae02015-04-17 12:49:27 -04007692} // namespace art
7693
Aart Bikf8f5a162017-02-06 15:35:29 -08007694#include "nodes_vector.h"
7695
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007696#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7697#include "nodes_shared.h"
7698#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007699#ifdef ART_ENABLE_CODEGEN_mips
7700#include "nodes_mips.h"
7701#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007702#ifdef ART_ENABLE_CODEGEN_x86
7703#include "nodes_x86.h"
7704#endif
7705
7706namespace art {
7707
Igor Murashkin6ef45672017-08-08 13:59:55 -07007708class OptimizingCompilerStats;
7709
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007710class HGraphVisitor : public ValueObject {
7711 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007712 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7713 : stats_(stats),
7714 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007715 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007716
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007717 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007718 virtual void VisitBasicBlock(HBasicBlock* block);
7719
Roland Levillain633021e2014-10-01 14:12:25 +01007720 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007721 void VisitInsertionOrder();
7722
Roland Levillain633021e2014-10-01 14:12:25 +01007723 // Visit the graph following dominator tree reverse post-order.
7724 void VisitReversePostOrder();
7725
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007726 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007727
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007728 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007729#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007730 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7731
7732 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7733
7734#undef DECLARE_VISIT_INSTRUCTION
7735
Igor Murashkin6ef45672017-08-08 13:59:55 -07007736 protected:
7737 OptimizingCompilerStats* stats_;
7738
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007739 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007740 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007741
7742 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7743};
7744
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007745class HGraphDelegateVisitor : public HGraphVisitor {
7746 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007747 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7748 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007749 virtual ~HGraphDelegateVisitor() {}
7750
7751 // Visit functions that delegate to to super class.
7752#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007753 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007754
7755 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7756
7757#undef DECLARE_VISIT_INSTRUCTION
7758
7759 private:
7760 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7761};
7762
Artem Serovcced8ba2017-07-19 18:18:09 +01007763// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7764// and remove the old instruction.
7765HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7766
7767// Create a clone for each clonable instructions/phis and replace the original with the clone.
7768//
7769// Used for testing individual instruction cloner.
7770class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7771 public:
7772 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007773 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007774
7775 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7776 if (instruction->IsClonable()) {
7777 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007778 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007779 }
7780 }
7781
Artem Serov7f4aff62017-06-21 17:02:18 +01007782 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007783
7784 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007785 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007786
7787 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7788};
7789
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007790// Iterator over the blocks that art part of the loop. Includes blocks part
7791// of an inner loop. The order in which the blocks are iterated is on their
7792// block id.
7793class HBlocksInLoopIterator : public ValueObject {
7794 public:
7795 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7796 : blocks_in_loop_(info.GetBlocks()),
7797 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7798 index_(0) {
7799 if (!blocks_in_loop_.IsBitSet(index_)) {
7800 Advance();
7801 }
7802 }
7803
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007804 bool Done() const { return index_ == blocks_.size(); }
7805 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007806 void Advance() {
7807 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007808 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007809 if (blocks_in_loop_.IsBitSet(index_)) {
7810 break;
7811 }
7812 }
7813 }
7814
7815 private:
7816 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007817 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007818 size_t index_;
7819
7820 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7821};
7822
Mingyao Yang3584bce2015-05-19 16:01:59 -07007823// Iterator over the blocks that art part of the loop. Includes blocks part
7824// of an inner loop. The order in which the blocks are iterated is reverse
7825// post order.
7826class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7827 public:
7828 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7829 : blocks_in_loop_(info.GetBlocks()),
7830 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7831 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007832 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007833 Advance();
7834 }
7835 }
7836
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007837 bool Done() const { return index_ == blocks_.size(); }
7838 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007839 void Advance() {
7840 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007841 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7842 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007843 break;
7844 }
7845 }
7846 }
7847
7848 private:
7849 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007850 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007851 size_t index_;
7852
7853 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7854};
7855
Aart Bikf3e61ee2017-04-12 17:09:20 -07007856// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007857inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007858 if (constant->IsIntConstant()) {
7859 return constant->AsIntConstant()->GetValue();
7860 } else if (constant->IsLongConstant()) {
7861 return constant->AsLongConstant()->GetValue();
7862 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007863 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007864 return 0;
7865 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007866}
7867
Aart Bikf3e61ee2017-04-12 17:09:20 -07007868// Returns true iff instruction is an integral constant (and sets value on success).
7869inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7870 if (instruction->IsIntConstant()) {
7871 *value = instruction->AsIntConstant()->GetValue();
7872 return true;
7873 } else if (instruction->IsLongConstant()) {
7874 *value = instruction->AsLongConstant()->GetValue();
7875 return true;
7876 } else if (instruction->IsNullConstant()) {
7877 *value = 0;
7878 return true;
7879 }
7880 return false;
7881}
7882
Aart Bik0148de42017-09-05 09:25:01 -07007883// Returns true iff instruction is the given integral constant.
7884inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7885 int64_t val = 0;
7886 return IsInt64AndGet(instruction, &val) && val == value;
7887}
7888
7889// Returns true iff instruction is a zero bit pattern.
7890inline bool IsZeroBitPattern(HInstruction* instruction) {
7891 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7892}
7893
Vladimir Markofbd27a92018-05-09 16:52:48 +01007894// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007895#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markobe2667c2018-05-04 15:04:45 +01007896 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7897 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7898#undef INSTRUCTION_TYPE_CHECK
7899
Vladimir Markofbd27a92018-05-09 16:52:48 +01007900// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markobe2667c2018-05-04 15:04:45 +01007901#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7902 std::is_base_of<BaseType, H##type>::value,
7903#define INSTRUCTION_TYPE_CHECK(type, super) \
7904 inline bool HInstruction::Is##type() const { \
7905 DCHECK_LT(GetKind(), kLastInstructionKind); \
7906 using BaseType = H##type; \
7907 static constexpr bool results[] = { \
7908 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7909 }; \
7910 return results[static_cast<size_t>(GetKind())]; \
7911 }
7912
7913 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7914#undef INSTRUCTION_TYPE_CHECK
Vladimir Markofbd27a92018-05-09 16:52:48 +01007915#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markobe2667c2018-05-04 15:04:45 +01007916
7917#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007918 inline const H##type* HInstruction::As##type() const { \
7919 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7920 } \
7921 inline H##type* HInstruction::As##type() { \
7922 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7923 }
7924
Vladimir Markobe2667c2018-05-04 15:04:45 +01007925 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7926#undef INSTRUCTION_TYPE_CAST
7927
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007928
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007929// Create space in `blocks` for adding `number_of_new_blocks` entries
7930// starting at location `at`. Blocks after `at` are moved accordingly.
7931inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7932 size_t number_of_new_blocks,
7933 size_t after) {
7934 DCHECK_LT(after, blocks->size());
7935 size_t old_size = blocks->size();
7936 size_t new_size = old_size + number_of_new_blocks;
7937 blocks->resize(new_size);
7938 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7939}
7940
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007941/*
7942 * Hunt "under the hood" of array lengths (leading to array references),
7943 * null checks (also leading to array references), and new arrays
7944 * (leading to the actual length). This makes it more likely related
7945 * instructions become actually comparable.
7946 */
7947inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7948 while (instruction->IsArrayLength() ||
7949 instruction->IsNullCheck() ||
7950 instruction->IsNewArray()) {
7951 instruction = instruction->IsNewArray()
7952 ? instruction->AsNewArray()->GetLength()
7953 : instruction->InputAt(0);
7954 }
7955 return instruction;
7956}
7957
Artem Serov21c7e6f2017-07-27 16:04:42 +01007958void RemoveEnvironmentUses(HInstruction* instruction);
7959bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7960void ResetEnvironmentInputRecords(HInstruction* instruction);
7961
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007962} // namespace art
7963
7964#endif // ART_COMPILER_OPTIMIZING_NODES_H_