blob: d88b0364f5fdcd9b4658909cbcd018b3fe83073a [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000030#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010031#include "base/transform_array_ref.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010032#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file.h"
35#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080036#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070037#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000038#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000039#include "handle.h"
40#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010041#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010042#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000043#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010044#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010045#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010046#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000047
48namespace art {
49
Vladimir Markoca6fff82017-10-03 14:49:14 +010050class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000051class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070053class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010054class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000057class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000058class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000061class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000062class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000064class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070065class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010067class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010068class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010069class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000070class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010071class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000072class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Mathieu Chartier736b5602015-09-02 14:54:11 -070074namespace mirror {
75class DexCache;
76} // namespace mirror
77
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078static const int kDefaultNumberOfBlocks = 8;
79static const int kDefaultNumberOfSuccessors = 2;
80static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010081static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010082static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084
Roland Levillain5b5b9312016-03-22 14:57:31 +000085// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
86static constexpr int32_t kMaxIntShiftDistance = 0x1f;
87// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
88static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000089
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010090static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070091static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010092
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010093static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
94
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060095static constexpr uint32_t kNoDexPc = -1;
96
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010097inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
98 // For the purposes of the compiler, the dex files must actually be the same object
99 // if we want to safely treat them as the same. This is especially important for JIT
100 // as custom class loaders can open the same underlying file (or memory) multiple
101 // times and provide different class resolution but no two class loaders should ever
102 // use the same DexFile object - doing so is an unsupported hack that can lead to
103 // all sorts of weird failures.
104 return &lhs == &rhs;
105}
106
Dave Allison20dfc792014-06-16 20:44:29 -0700107enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700108 // All types.
109 kCondEQ, // ==
110 kCondNE, // !=
111 // Signed integers and floating-point numbers.
112 kCondLT, // <
113 kCondLE, // <=
114 kCondGT, // >
115 kCondGE, // >=
116 // Unsigned integers.
117 kCondB, // <
118 kCondBE, // <=
119 kCondA, // >
120 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100121 // First and last aliases.
122 kCondFirst = kCondEQ,
123 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700124};
125
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000127 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000128 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000129 kAnalysisFailThrowCatchLoop,
130 kAnalysisFailAmbiguousArrayOp,
131 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000132};
133
Andreas Gampe9186ced2016-12-12 14:28:21 -0800134template <typename T>
135static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
136 return static_cast<typename std::make_unsigned<T>::type>(x);
137}
138
Vladimir Markof9f64412015-09-02 14:05:49 +0100139class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 public:
141 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
142
143 void AddInstruction(HInstruction* instruction);
144 void RemoveInstruction(HInstruction* instruction);
145
David Brazdilc3d743f2015-04-22 13:40:50 +0100146 // Insert `instruction` before/after an existing instruction `cursor`.
147 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
148 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
149
Roland Levillain6b469232014-09-25 10:10:38 +0100150 // Return true if this list contains `instruction`.
151 bool Contains(HInstruction* instruction) const;
152
Roland Levillainccc07a92014-09-16 14:48:16 +0100153 // Return true if `instruction1` is found before `instruction2` in
154 // this instruction list and false otherwise. Abort if none
155 // of these instructions is found.
156 bool FoundBefore(const HInstruction* instruction1,
157 const HInstruction* instruction2) const;
158
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000159 bool IsEmpty() const { return first_instruction_ == nullptr; }
160 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
161
162 // Update the block of all instructions to be `block`.
163 void SetBlockOfInstructions(HBasicBlock* block) const;
164
165 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000166 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000167 void Add(const HInstructionList& instruction_list);
168
David Brazdil2d7352b2015-04-20 14:52:42 +0100169 // Return the number of instructions in the list. This is an expensive operation.
170 size_t CountSize() const;
171
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100172 private:
173 HInstruction* first_instruction_;
174 HInstruction* last_instruction_;
175
176 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000177 friend class HGraph;
178 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000180 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100181 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182
183 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
184};
185
David Brazdil4833f5a2015-12-16 10:37:39 +0000186class ReferenceTypeInfo : ValueObject {
187 public:
188 typedef Handle<mirror::Class> TypeHandle;
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
191
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100193 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
194 }
195
Vladimir Markoa1de9182016-02-25 11:37:38 +0000196 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return ReferenceTypeInfo(type_handle, is_exact);
198 }
199
200 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
201
Vladimir Markof39745e2016-01-26 12:16:55 +0000202 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000203 return handle.GetReference() != nullptr;
204 }
205
Vladimir Marko456307a2016-04-19 14:12:13 +0000206 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 return IsValidHandle(type_handle_);
208 }
209
210 bool IsExact() const { return is_exact_; }
211
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700212 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000213 DCHECK(IsValid());
214 return GetTypeHandle()->IsObjectClass();
215 }
216
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000218 DCHECK(IsValid());
219 return GetTypeHandle()->IsStringClass();
220 }
221
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000223 DCHECK(IsValid());
224 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
225 }
226
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000228 DCHECK(IsValid());
229 return GetTypeHandle()->IsInterface();
230 }
231
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000233 DCHECK(IsValid());
234 return GetTypeHandle()->IsArrayClass();
235 }
236
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000238 DCHECK(IsValid());
239 return GetTypeHandle()->IsPrimitiveArray();
240 }
241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 DCHECK(IsValid());
244 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
245 }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 if (!IsExact()) return false;
250 if (!IsArrayClass()) return false;
251 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
252 }
253
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000255 DCHECK(IsValid());
256 if (!IsExact()) return false;
257 if (!IsArrayClass()) return false;
258 if (!rti.IsArrayClass()) return false;
259 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
260 rti.GetTypeHandle()->GetComponentType());
261 }
262
263 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
269 }
270
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700271 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000272 DCHECK(IsValid());
273 DCHECK(rti.IsValid());
274 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
275 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
276 }
277
278 // Returns true if the type information provide the same amount of details.
279 // Note that it does not mean that the instructions have the same actual type
280 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700281 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000282 if (!IsValid() && !rti.IsValid()) {
283 // Invalid types are equal.
284 return true;
285 }
286 if (!IsValid() || !rti.IsValid()) {
287 // One is valid, the other not.
288 return false;
289 }
290 return IsExact() == rti.IsExact()
291 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
292 }
293
294 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000295 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
296 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
297 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000298
299 // The class of the object.
300 TypeHandle type_handle_;
301 // Whether or not the type is exact or a superclass of the actual type.
302 // Whether or not we have any information about this type.
303 bool is_exact_;
304};
305
306std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
307
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100309class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100311 HGraph(ArenaAllocator* allocator,
312 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 const DexFile& dex_file,
314 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100316 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000318 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100320 : allocator_(allocator),
321 arena_stack_(arena_stack),
322 blocks_(allocator->Adapter(kArenaAllocBlockList)),
323 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
324 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 entry_block_(nullptr),
326 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100327 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100328 number_of_vregs_(0),
329 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000330 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400331 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000332 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700333 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800334 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000335 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000336 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000337 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100338 dex_file_(dex_file),
339 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100340 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800342 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000344 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100345 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
347 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
348 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000349 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100350 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000351 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700352 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100353 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100354 blocks_.reserve(kDefaultNumberOfBlocks);
355 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
David Brazdilbadd8262016-02-02 16:28:56 +0000357 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700358 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000359
Vladimir Markoca6fff82017-10-03 14:49:14 +0100360 ArenaAllocator* GetAllocator() const { return allocator_; }
361 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100362 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
363
David Brazdil69ba7b72015-06-23 18:27:30 +0100364 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000365 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100366
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000367 HBasicBlock* GetEntryBlock() const { return entry_block_; }
368 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100369 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000370
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
372 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000373
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000374 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100376 void ComputeDominanceInformation();
377 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000378 void ClearLoopInformation();
379 void FindBackEdges(ArenaBitVector* visited);
380 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100381 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100382 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000383
David Brazdil4833f5a2015-12-16 10:37:39 +0000384 // Analyze all natural loops in this graph. Returns a code specifying that it
385 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000386 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000387 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388
David Brazdilffee3d32015-07-06 11:48:53 +0100389 // Iterate over blocks to compute try block membership. Needs reverse post
390 // order and loop information.
391 void ComputeTryBlockInformation();
392
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000393 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000394 // Returns the instruction to replace the invoke expression or null if the
395 // invoke is for a void method. Note that the caller is responsible for replacing
396 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000397 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000398
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000399 // Update the loop and try membership of `block`, which was spawned from `reference`.
400 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
401 // should be the new back edge.
402 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
403 HBasicBlock* reference,
404 bool replace_if_back_edge);
405
Mingyao Yang3584bce2015-05-19 16:01:59 -0700406 // Need to add a couple of blocks to test if the loop body is entered and
407 // put deoptimization instructions, etc.
408 void TransformLoopHeaderForBCE(HBasicBlock* header);
409
Aart Bikf8f5a162017-02-06 15:35:29 -0800410 // Adds a new loop directly after the loop with the given header and exit.
411 // Returns the new preheader.
412 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
413 HBasicBlock* body,
414 HBasicBlock* exit);
415
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000416 // Removes `block` from the graph. Assumes `block` has been disconnected from
417 // other blocks and has no instructions or phis.
418 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000419
David Brazdilfc6a86a2015-06-26 10:33:45 +0000420 // Splits the edge between `block` and `successor` while preserving the
421 // indices in the predecessor/successor lists. If there are multiple edges
422 // between the blocks, the lowest indices are used.
423 // Returns the new block which is empty and has the same dex pc as `successor`.
424 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
425
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100427 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000428
429 // Transform a loop into a format with a single preheader.
430 //
431 // Each phi in the header should be split: original one in the header should only hold
432 // inputs reachable from the back edges and a single input from the preheader. The newly created
433 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
434 //
435 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
436 // that no longer have this property.
437 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
438
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100439 void SimplifyLoop(HBasicBlock* header);
440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000441 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100442 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000443 return current_instruction_id_++;
444 }
445
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000446 int32_t GetCurrentInstructionId() const {
447 return current_instruction_id_;
448 }
449
450 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100451 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000452 current_instruction_id_ = id;
453 }
454
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100455 uint16_t GetMaximumNumberOfOutVRegs() const {
456 return maximum_number_of_out_vregs_;
457 }
458
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
460 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100461 }
462
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100463 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
464 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
465 }
466
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000467 void UpdateTemporariesVRegSlots(size_t slots) {
468 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100469 }
470
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000471 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100472 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000473 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 }
475
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100476 void SetNumberOfVRegs(uint16_t number_of_vregs) {
477 number_of_vregs_ = number_of_vregs;
478 }
479
480 uint16_t GetNumberOfVRegs() const {
481 return number_of_vregs_;
482 }
483
484 void SetNumberOfInVRegs(uint16_t value) {
485 number_of_in_vregs_ = value;
486 }
487
David Brazdildee58d62016-04-07 09:54:26 +0000488 uint16_t GetNumberOfInVRegs() const {
489 return number_of_in_vregs_;
490 }
491
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100492 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100493 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100494 return number_of_vregs_ - number_of_in_vregs_;
495 }
496
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100497 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100498 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100499 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100500
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100501 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
502 DCHECK(GetReversePostOrder()[0] == entry_block_);
503 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
504 }
505
506 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
507 return ReverseRange(GetReversePostOrder());
508 }
509
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100511 return linear_order_;
512 }
513
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100514 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
515 return ReverseRange(GetLinearOrder());
516 }
517
Mark Mendell1152c922015-04-24 17:06:35 -0400518 bool HasBoundsChecks() const {
519 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800520 }
521
Mark Mendell1152c922015-04-24 17:06:35 -0400522 void SetHasBoundsChecks(bool value) {
523 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800524 }
525
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000526 bool IsDebuggable() const { return debuggable_; }
527
David Brazdil8d5b8b22015-03-24 10:51:52 +0000528 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 // already, it is created and inserted into the graph. This method is only for
530 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100531 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000532
533 // TODO: This is problematic for the consistency of reference type propagation
534 // because it can be created anytime after the pass and thus it will be left
535 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600536 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000537
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600538 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
539 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000540 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600541 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
542 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000543 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600544 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
545 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000546 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600547 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
548 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000549 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000550
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100551 HCurrentMethod* GetCurrentMethod();
552
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100553 const DexFile& GetDexFile() const {
554 return dex_file_;
555 }
556
557 uint32_t GetMethodIdx() const {
558 return method_idx_;
559 }
560
Igor Murashkind01745e2017-04-05 16:40:31 -0700561 // Get the method name (without the signature), e.g. "<init>"
562 const char* GetMethodName() const;
563
564 // Get the pretty method name (class + name + optionally signature).
565 std::string PrettyMethod(bool with_signature = true) const;
566
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100567 InvokeType GetInvokeType() const {
568 return invoke_type_;
569 }
570
Mark Mendellc4701932015-04-10 13:18:51 -0400571 InstructionSet GetInstructionSet() const {
572 return instruction_set_;
573 }
574
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000575 bool IsCompilingOsr() const { return osr_; }
576
Mingyao Yang063fc772016-08-02 11:02:54 -0700577 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
578 return cha_single_implementation_list_;
579 }
580
581 void AddCHASingleImplementationDependency(ArtMethod* method) {
582 cha_single_implementation_list_.insert(method);
583 }
584
585 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800586 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700587 }
588
David Brazdil77a48ae2015-09-15 12:34:04 +0000589 bool HasTryCatch() const { return has_try_catch_; }
590 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100591
Aart Bikb13c65b2017-03-21 20:14:07 -0700592 bool HasSIMD() const { return has_simd_; }
593 void SetHasSIMD(bool value) { has_simd_ = value; }
594
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800595 bool HasLoops() const { return has_loops_; }
596 void SetHasLoops(bool value) { has_loops_ = value; }
597
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000598 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
599 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
600
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100601 ArtMethod* GetArtMethod() const { return art_method_; }
602 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
603
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100604 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500605 // The instruction has been inserted into the graph, either as a constant, or
606 // before cursor.
607 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
608
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000609 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
610
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800611 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
612 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
613 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
614
David Brazdil2d7352b2015-04-20 14:52:42 +0100615 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000616 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100617 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 template <class InstructionType, typename ValueType>
620 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600621 ArenaSafeMap<ValueType, InstructionType*>* cache,
622 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000623 // Try to find an existing constant of the given value.
624 InstructionType* constant = nullptr;
625 auto cached_constant = cache->find(value);
626 if (cached_constant != cache->end()) {
627 constant = cached_constant->second;
628 }
629
630 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100631 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100633 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000634 cache->Overwrite(value, constant);
635 InsertConstant(constant);
636 }
637 return constant;
638 }
639
David Brazdil8d5b8b22015-03-24 10:51:52 +0000640 void InsertConstant(HConstant* instruction);
641
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 // Cache a float constant into the graph. This method should only be
643 // called by the SsaBuilder when creating "equivalent" instructions.
644 void CacheFloatConstant(HFloatConstant* constant);
645
646 // See CacheFloatConstant comment.
647 void CacheDoubleConstant(HDoubleConstant* constant);
648
Vladimir Markoca6fff82017-10-03 14:49:14 +0100649 ArenaAllocator* const allocator_;
650 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000651
652 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100655 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657
Aart Bik281c6812016-08-26 11:31:48 -0700658 // List of blocks to perform a linear order tree traversal. Unlike the reverse
659 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100660 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100661
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000662 HBasicBlock* entry_block_;
663 HBasicBlock* exit_block_;
664
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100665 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100666 uint16_t maximum_number_of_out_vregs_;
667
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100668 // The number of virtual registers in this method. Contains the parameters.
669 uint16_t number_of_vregs_;
670
671 // The number of virtual registers used by parameters of this method.
672 uint16_t number_of_in_vregs_;
673
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000674 // Number of vreg size slots that the temporaries use (used in baseline compiler).
675 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100676
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800677 // Flag whether there are bounds checks in the graph. We can skip
678 // BCE if it's false. It's only best effort to keep it up to date in
679 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400680 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800681
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800682 // Flag whether there are try/catch blocks in the graph. We will skip
683 // try/catch-related passes if it's false. It's only best effort to keep
684 // it up to date in the presence of code elimination so there might be
685 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000686 bool has_try_catch_;
687
Aart Bikb13c65b2017-03-21 20:14:07 -0700688 // Flag whether SIMD instructions appear in the graph. If true, the
689 // code generators may have to be more careful spilling the wider
690 // contents of SIMD registers.
691 bool has_simd_;
692
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800693 // Flag whether there are any loops in the graph. We can skip loop
694 // optimization if it's false. It's only best effort to keep it up
695 // to date in the presence of code elimination so there might be false
696 // positives.
697 bool has_loops_;
698
699 // Flag whether there are any irreducible loops in the graph. It's only
700 // best effort to keep it up to date in the presence of code elimination
701 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000702 bool has_irreducible_loops_;
703
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000704 // Indicates whether the graph should be compiled in a way that
705 // ensures full debuggability. If false, we can apply more
706 // aggressive optimizations that may limit the level of debugging.
707 const bool debuggable_;
708
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000710 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000711
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100712 // The dex file from which the method is from.
713 const DexFile& dex_file_;
714
715 // The method index in the dex file.
716 const uint32_t method_idx_;
717
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100718 // If inlined, this encodes how the callee is being invoked.
719 const InvokeType invoke_type_;
720
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100721 // Whether the graph has been transformed to SSA form. Only used
722 // in debug mode to ensure we are not using properties only valid
723 // for non-SSA form (like the number of temporaries).
724 bool in_ssa_form_;
725
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800726 // Number of CHA guards in the graph. Used to short-circuit the
727 // CHA guard optimization pass when there is no CHA guard left.
728 uint32_t number_of_cha_guards_;
729
Mathieu Chartiere401d142015-04-22 13:56:20 -0700730 const InstructionSet instruction_set_;
731
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000732 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000733 HNullConstant* cached_null_constant_;
734 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000735 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000736 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000737 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000738
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100739 HCurrentMethod* cached_current_method_;
740
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100741 // The ArtMethod this graph is for. Note that for AOT, it may be null,
742 // for example for methods whose declaring class could not be resolved
743 // (such as when the superclass could not be found).
744 ArtMethod* art_method_;
745
David Brazdil4833f5a2015-12-16 10:37:39 +0000746 // Keep the RTI of inexact Object to avoid having to pass stack handle
747 // collection pointer to passes which may create NullConstant.
748 ReferenceTypeInfo inexact_object_rti_;
749
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000750 // Whether we are compiling this graph for on stack replacement: this will
751 // make all loops seen as irreducible and emit special stack maps to mark
752 // compiled code entries which the interpreter can directly jump to.
753 const bool osr_;
754
Mingyao Yang063fc772016-08-02 11:02:54 -0700755 // List of methods that are assumed to have single implementation.
756 ArenaSet<ArtMethod*> cha_single_implementation_list_;
757
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000758 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100759 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000760 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000761 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762 DISALLOW_COPY_AND_ASSIGN(HGraph);
763};
764
Vladimir Markof9f64412015-09-02 14:05:49 +0100765class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000766 public:
767 HLoopInformation(HBasicBlock* header, HGraph* graph)
768 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100769 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000770 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100771 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100772 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100773 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100774 blocks_(graph->GetAllocator(),
775 graph->GetBlocks().size(),
776 true,
777 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778 back_edges_.reserve(kDefaultNumberOfBackEdges);
779 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000781 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100782 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000783
784 void Dump(std::ostream& os);
785
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 HBasicBlock* GetHeader() const {
787 return header_;
788 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000789
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000790 void SetHeader(HBasicBlock* block) {
791 header_ = block;
792 }
793
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100794 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
795 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
796 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
797
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100799 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000800 }
801
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 }
805
David Brazdil46e2a392015-03-16 17:31:52 +0000806 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100807 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 }
809
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000810 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100811 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000812 }
813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100814 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100816 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100818 }
819
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100820 // Returns the lifetime position of the back edge that has the
821 // greatest lifetime position.
822 size_t GetLifetimeEnd() const;
823
824 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100825 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100826 }
827
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000828 // Finds blocks that are part of this loop.
829 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100830
Artem Serov7f4aff62017-06-21 17:02:18 +0100831 // Updates blocks population of the loop and all of its outer' ones recursively after the
832 // population of the inner loop is updated.
833 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
834
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100835 // Returns whether this loop information contains `block`.
836 // Note that this loop information *must* be populated before entering this function.
837 bool Contains(const HBasicBlock& block) const;
838
839 // Returns whether this loop information is an inner loop of `other`.
840 // Note that `other` *must* be populated before entering this function.
841 bool IsIn(const HLoopInformation& other) const;
842
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800843 // Returns true if instruction is not defined within this loop.
844 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700845
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100846 const ArenaBitVector& GetBlocks() const { return blocks_; }
847
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000849 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000851 void ClearAllBlocks() {
852 blocks_.ClearAllBits();
853 }
854
David Brazdil3f4a5222016-05-06 12:46:21 +0100855 bool HasBackEdgeNotDominatedByHeader() const;
856
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100857 bool IsPopulated() const {
858 return blocks_.GetHighestBitSet() != -1;
859 }
860
Anton Shaminf89381f2016-05-16 16:44:13 +0600861 bool DominatesAllBackEdges(HBasicBlock* block);
862
David Sehrc757dec2016-11-04 15:48:34 -0700863 bool HasExitEdge() const;
864
Artem Serov7f4aff62017-06-21 17:02:18 +0100865 // Resets back edge and blocks-in-loop data.
866 void ResetBasicBlockData() {
867 back_edges_.clear();
868 ClearAllBlocks();
869 }
870
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872 // Internal recursive implementation of `Populate`.
873 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100874 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100875
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000876 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100877 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000878 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100879 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100880 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100881 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000882
883 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
884};
885
David Brazdilec16f792015-08-19 15:04:01 +0100886// Stores try/catch information for basic blocks.
887// Note that HGraph is constructed so that catch blocks cannot simultaneously
888// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100889class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100890 public:
891 // Try block information constructor.
892 explicit TryCatchInformation(const HTryBoundary& try_entry)
893 : try_entry_(&try_entry),
894 catch_dex_file_(nullptr),
895 catch_type_index_(DexFile::kDexNoIndex16) {
896 DCHECK(try_entry_ != nullptr);
897 }
898
899 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800900 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100901 : try_entry_(nullptr),
902 catch_dex_file_(&dex_file),
903 catch_type_index_(catch_type_index) {}
904
905 bool IsTryBlock() const { return try_entry_ != nullptr; }
906
907 const HTryBoundary& GetTryEntry() const {
908 DCHECK(IsTryBlock());
909 return *try_entry_;
910 }
911
912 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
913
914 bool IsCatchAllTypeIndex() const {
915 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800916 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100917 }
918
Andreas Gampea5b09a62016-11-17 15:21:22 -0800919 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100920 DCHECK(IsCatchBlock());
921 return catch_type_index_;
922 }
923
924 const DexFile& GetCatchDexFile() const {
925 DCHECK(IsCatchBlock());
926 return *catch_dex_file_;
927 }
928
929 private:
930 // One of possibly several TryBoundary instructions entering the block's try.
931 // Only set for try blocks.
932 const HTryBoundary* try_entry_;
933
934 // Exception type information. Only set for catch blocks.
935 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800936 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100937};
938
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100940static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100941
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942// A block in a method. Contains the list of instructions represented
943// as a double linked list. Each block knows its predecessors and
944// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100945
Vladimir Markof9f64412015-09-02 14:05:49 +0100946class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700948 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100950 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
951 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000952 loop_information_(nullptr),
953 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100954 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100955 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100956 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100957 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000958 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000959 try_catch_information_(nullptr) {
960 predecessors_.reserve(kDefaultNumberOfPredecessors);
961 successors_.reserve(kDefaultNumberOfSuccessors);
962 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
963 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964
Vladimir Marko60584552015-09-03 13:35:12 +0000965 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 }
968
Vladimir Marko60584552015-09-03 13:35:12 +0000969 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100970 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971 }
972
David Brazdild26a4112015-11-10 11:07:31 +0000973 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
974 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
975
Vladimir Marko60584552015-09-03 13:35:12 +0000976 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
977 return ContainsElement(successors_, block, start_from);
978 }
979
980 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100981 return dominated_blocks_;
982 }
983
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100984 bool IsEntryBlock() const {
985 return graph_->GetEntryBlock() == this;
986 }
987
988 bool IsExitBlock() const {
989 return graph_->GetExitBlock() == this;
990 }
991
David Brazdil46e2a392015-03-16 17:31:52 +0000992 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200993 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700994 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000995 bool IsSingleTryBoundary() const;
996
997 // Returns true if this block emits nothing but a jump.
998 bool IsSingleJump() const {
999 HLoopInformation* loop_info = GetLoopInformation();
1000 return (IsSingleGoto() || IsSingleTryBoundary())
1001 // Back edges generate a suspend check.
1002 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1003 }
David Brazdil46e2a392015-03-16 17:31:52 +00001004
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001005 void AddBackEdge(HBasicBlock* back_edge) {
1006 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001007 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001008 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001009 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001010 loop_information_->AddBackEdge(back_edge);
1011 }
1012
Artem Serov7f4aff62017-06-21 17:02:18 +01001013 // Registers a back edge; if the block was not a loop header before the call associates a newly
1014 // created loop info with it.
1015 //
1016 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1017 // info for all blocks during back edges recalculation.
1018 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1019 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1020 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1021 }
1022 loop_information_->AddBackEdge(back_edge);
1023 }
1024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001025 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001028 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001029 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001030 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001032 HBasicBlock* GetDominator() const { return dominator_; }
1033 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001034 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1035
1036 void RemoveDominatedBlock(HBasicBlock* block) {
1037 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001038 }
Vladimir Marko60584552015-09-03 13:35:12 +00001039
1040 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1041 ReplaceElement(dominated_blocks_, existing, new_block);
1042 }
1043
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001044 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001045
1046 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001047 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001048 }
1049
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001050 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1051 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001052 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001054 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1055 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001056
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001057 HInstruction* GetFirstInstructionDisregardMoves() const;
1058
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001060 successors_.push_back(block);
1061 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062 }
1063
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001064 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1065 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001066 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001067 new_block->predecessors_.push_back(this);
1068 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001069 }
1070
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001071 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1072 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001073 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001074 new_block->successors_.push_back(this);
1075 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001076 }
1077
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001078 // Insert `this` between `predecessor` and `successor. This method
1079 // preserves the indicies, and will update the first edge found between
1080 // `predecessor` and `successor`.
1081 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1082 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001083 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001084 successor->predecessors_[predecessor_index] = this;
1085 predecessor->successors_[successor_index] = this;
1086 successors_.push_back(successor);
1087 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001088 }
1089
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001090 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001091 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001092 }
1093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001094 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001095 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 }
1097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001099 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 }
1101
1102 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001103 predecessors_.push_back(block);
1104 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 }
1106
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001107 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001108 DCHECK_EQ(predecessors_.size(), 2u);
1109 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001110 }
1111
David Brazdil769c9e52015-04-27 13:54:09 +01001112 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001113 DCHECK_EQ(successors_.size(), 2u);
1114 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001115 }
1116
David Brazdilfc6a86a2015-06-26 10:33:45 +00001117 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001118 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001119 }
1120
David Brazdilfc6a86a2015-06-26 10:33:45 +00001121 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001122 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001123 }
1124
David Brazdilfc6a86a2015-06-26 10:33:45 +00001125 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001126 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001127 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001128 }
1129
1130 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001131 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001132 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001133 }
1134
1135 // Returns whether the first occurrence of `predecessor` in the list of
1136 // predecessors is at index `idx`.
1137 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001138 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001139 return GetPredecessorIndexOf(predecessor) == idx;
1140 }
1141
David Brazdild7558da2015-09-22 13:04:14 +01001142 // Create a new block between this block and its predecessors. The new block
1143 // is added to the graph, all predecessor edges are relinked to it and an edge
1144 // is created to `this`. Returns the new empty block. Reverse post order or
1145 // loop and try/catch information are not updated.
1146 HBasicBlock* CreateImmediateDominator();
1147
David Brazdilfc6a86a2015-06-26 10:33:45 +00001148 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001149 // created, latter block. Note that this method will add the block to the
1150 // graph, create a Goto at the end of the former block and will create an edge
1151 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001152 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001153 HBasicBlock* SplitBefore(HInstruction* cursor);
1154
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001155 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001156 // created block. Note that this method just updates raw block information,
1157 // like predecessors, successors, dominators, and instruction list. It does not
1158 // update the graph, reverse post order, loop information, nor make sure the
1159 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001160 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1161
1162 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1163 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001164
1165 // Merge `other` at the end of `this`. Successors and dominated blocks of
1166 // `other` are changed to be successors and dominated blocks of `this`. Note
1167 // that this method does not update the graph, reverse post order, loop
1168 // information, nor make sure the blocks are consistent (for example ending
1169 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001170 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001171
1172 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1173 // of `this` are moved to `other`.
1174 // Note that this method does not update the graph, reverse post order, loop
1175 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001176 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001177 void ReplaceWith(HBasicBlock* other);
1178
Aart Bik6b69e0a2017-01-11 10:20:43 -08001179 // Merges the instructions of `other` at the end of `this`.
1180 void MergeInstructionsWith(HBasicBlock* other);
1181
David Brazdil2d7352b2015-04-20 14:52:42 +01001182 // Merge `other` at the end of `this`. This method updates loops, reverse post
1183 // order, links to predecessors, successors, dominators and deletes the block
1184 // from the graph. The two blocks must be successive, i.e. `this` the only
1185 // predecessor of `other` and vice versa.
1186 void MergeWith(HBasicBlock* other);
1187
1188 // Disconnects `this` from all its predecessors, successors and dominator,
1189 // removes it from all loops it is included in and eventually from the graph.
1190 // The block must not dominate any other block. Predecessors and successors
1191 // are safely updated.
1192 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001193
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001194 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001195 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001196 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001197 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001198 // Replace phi `initial` with `replacement` within this block.
1199 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001200 // Replace instruction `initial` with `replacement` within this block.
1201 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1202 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001203 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001204 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001205 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1206 // instruction list. With 'ensure_safety' set to true, it verifies that the
1207 // instruction is not in use and removes it from the use lists of its inputs.
1208 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1209 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001210 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001211
1212 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001213 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001214 }
1215
Roland Levillain6b879dd2014-09-22 17:13:44 +01001216 bool IsLoopPreHeaderFirstPredecessor() const {
1217 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001218 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001219 }
1220
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001221 bool IsFirstPredecessorBackEdge() const {
1222 DCHECK(IsLoopHeader());
1223 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1224 }
1225
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226 HLoopInformation* GetLoopInformation() const {
1227 return loop_information_;
1228 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001229
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 void SetInLoop(HLoopInformation* info) {
1234 if (IsLoopHeader()) {
1235 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001236 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001237 loop_information_ = info;
1238 } else if (loop_information_->Contains(*info->GetHeader())) {
1239 // Block is currently part of an outer loop. Make it part of this inner loop.
1240 // Note that a non loop header having a loop information means this loop information
1241 // has already been populated
1242 loop_information_ = info;
1243 } else {
1244 // Block is part of an inner loop. Do not update the loop information.
1245 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1246 // at this point, because this method is being called while populating `info`.
1247 }
1248 }
1249
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001250 // Raw update of the loop information.
1251 void SetLoopInformation(HLoopInformation* info) {
1252 loop_information_ = info;
1253 }
1254
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001255 bool IsInLoop() const { return loop_information_ != nullptr; }
1256
David Brazdilec16f792015-08-19 15:04:01 +01001257 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1258
1259 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1260 try_catch_information_ = try_catch_information;
1261 }
1262
1263 bool IsTryBlock() const {
1264 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1265 }
1266
1267 bool IsCatchBlock() const {
1268 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1269 }
David Brazdilffee3d32015-07-06 11:48:53 +01001270
1271 // Returns the try entry that this block's successors should have. They will
1272 // be in the same try, unless the block ends in a try boundary. In that case,
1273 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001274 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001275
Aart Bik75ff2c92018-04-21 01:28:11 +00001276 bool HasThrowingInstructions() const;
1277
David Brazdila4b8c212015-05-07 09:59:30 +01001278 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001279 bool Dominates(HBasicBlock* block) const;
1280
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001281 size_t GetLifetimeStart() const { return lifetime_start_; }
1282 size_t GetLifetimeEnd() const { return lifetime_end_; }
1283
1284 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1285 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1286
David Brazdil8d5b8b22015-03-24 10:51:52 +00001287 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001288 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001289 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001290 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001291 bool HasSinglePhi() const;
1292
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001293 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001294 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001295 ArenaVector<HBasicBlock*> predecessors_;
1296 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001297 HInstructionList instructions_;
1298 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001299 HLoopInformation* loop_information_;
1300 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001301 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001302 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001303 // The dex program counter of the first instruction of this block.
1304 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001305 size_t lifetime_start_;
1306 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001307 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001308
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001309 friend class HGraph;
1310 friend class HInstruction;
1311
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001312 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1313};
1314
David Brazdilb2bd1c52015-03-25 11:17:37 +00001315// Iterates over the LoopInformation of all loops which contain 'block'
1316// from the innermost to the outermost.
1317class HLoopInformationOutwardIterator : public ValueObject {
1318 public:
1319 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1320 : current_(block.GetLoopInformation()) {}
1321
1322 bool Done() const { return current_ == nullptr; }
1323
1324 void Advance() {
1325 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001326 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001327 }
1328
1329 HLoopInformation* Current() const {
1330 DCHECK(!Done());
1331 return current_;
1332 }
1333
1334 private:
1335 HLoopInformation* current_;
1336
1337 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1338};
1339
Alexandre Ramesef20f712015-06-09 10:29:30 +01001340#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001341 M(Above, Condition) \
1342 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001343 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001345 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(ArrayGet, Instruction) \
1347 M(ArrayLength, Instruction) \
1348 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001349 M(Below, Condition) \
1350 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001351 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001353 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001354 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001355 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001356 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001357 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001358 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001359 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001360 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001361 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001362 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001364 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001367 M(Exit, Instruction) \
1368 M(FloatConstant, Constant) \
1369 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(GreaterThan, Condition) \
1371 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001373 M(InstanceFieldGet, Instruction) \
1374 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001375 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001376 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001377 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001378 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001379 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001380 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001381 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001382 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001383 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001384 M(LessThan, Condition) \
1385 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001386 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001387 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001388 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001389 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001390 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001391 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001392 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001393 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001394 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001395 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001396 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001397 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001398 M(Neg, UnaryOperation) \
1399 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001400 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001401 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001402 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001403 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001404 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001405 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001406 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001407 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001408 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001409 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001410 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001411 M(Return, Instruction) \
1412 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001413 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001414 M(Shl, BinaryOperation) \
1415 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001416 M(StaticFieldGet, Instruction) \
1417 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001418 M(UnresolvedInstanceFieldGet, Instruction) \
1419 M(UnresolvedInstanceFieldSet, Instruction) \
1420 M(UnresolvedStaticFieldGet, Instruction) \
1421 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001422 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001424 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001425 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001426 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001427 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001428 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001429 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001430 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001431 M(VecExtractScalar, VecUnaryOperation) \
1432 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001433 M(VecCnv, VecUnaryOperation) \
1434 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001435 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001436 M(VecNot, VecUnaryOperation) \
1437 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001438 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001439 M(VecSub, VecBinaryOperation) \
1440 M(VecMul, VecBinaryOperation) \
1441 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001442 M(VecMin, VecBinaryOperation) \
1443 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001444 M(VecAnd, VecBinaryOperation) \
1445 M(VecAndNot, VecBinaryOperation) \
1446 M(VecOr, VecBinaryOperation) \
1447 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001448 M(VecSaturationAdd, VecBinaryOperation) \
1449 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001450 M(VecShl, VecBinaryOperation) \
1451 M(VecShr, VecBinaryOperation) \
1452 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001453 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001454 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001455 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001456 M(VecLoad, VecMemoryOperation) \
1457 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001458
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001459/*
1460 * Instructions, shared across several (not all) architectures.
1461 */
1462#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1463#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1464#else
1465#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001466 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001467 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001468 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001469 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001470#endif
1471
Alexandre Ramesef20f712015-06-09 10:29:30 +01001472#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1473
1474#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1475
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001476#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001477#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001478#else
1479#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1480 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001481 M(MipsPackedSwitch, Instruction) \
1482 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001483#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001484
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001485#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1486
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001487#ifndef ART_ENABLE_CODEGEN_x86
1488#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1489#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001490#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1491 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001492 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001493 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001494 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001495#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001496
1497#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1498
1499#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1500 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001501 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001502 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1503 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001504 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001505 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001506 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1507 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1508
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001509#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1510 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001511 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001512 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001513 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001514 M(Invoke, Instruction) \
1515 M(VecOperation, Instruction) \
1516 M(VecUnaryOperation, VecOperation) \
1517 M(VecBinaryOperation, VecOperation) \
1518 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001519
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001520#define FOR_EACH_INSTRUCTION(M) \
1521 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1522 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1523
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001524#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001525FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1526#undef FORWARD_DECLARATION
1527
Vladimir Marko372f10e2016-05-17 16:30:10 +01001528#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001529 private: \
1530 H##type& operator=(const H##type&) = delete; \
1531 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001532 const char* DebugName() const OVERRIDE { return #type; } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001533 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1534 DCHECK(IsClonable()); \
1535 return new (arena) H##type(*this->As##type()); \
1536 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001537 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001538
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001539#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001540 private: \
1541 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001542 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001543
Artem Serovcced8ba2017-07-19 18:18:09 +01001544#define DEFAULT_COPY_CONSTRUCTOR(type) \
1545 explicit H##type(const H##type& other) = default;
1546
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001547template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001548class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1549 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001550 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001551 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001552 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001553 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001554 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001555 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001556 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001557
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001558 private:
David Brazdiled596192015-01-23 10:39:45 +00001559 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001560 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001561
1562 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001563 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001564
Vladimir Marko46817b82016-03-29 12:21:58 +01001565 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001566
1567 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1568};
1569
David Brazdiled596192015-01-23 10:39:45 +00001570template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001571using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001572
David Brazdil1abb4192015-02-17 18:33:36 +00001573// This class is used by HEnvironment and HInstruction classes to record the
1574// instructions they use and pointers to the corresponding HUseListNodes kept
1575// by the used instructions.
1576template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001577class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001578 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001579 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1580 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001581
Vladimir Marko46817b82016-03-29 12:21:58 +01001582 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1583 : HUserRecord(old_record.instruction_, before_use_node) {}
1584 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1585 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001586 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001587 }
1588
1589 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001590 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1591 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001592
1593 private:
1594 // Instruction used by the user.
1595 HInstruction* instruction_;
1596
Vladimir Marko46817b82016-03-29 12:21:58 +01001597 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1598 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001599};
1600
Vladimir Markoe9004912016-06-16 16:50:52 +01001601// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1602// This is used for HInstruction::GetInputs() to return a container wrapper providing
1603// HInstruction* values even though the underlying container has HUserRecord<>s.
1604struct HInputExtractor {
1605 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1606 return record.GetInstruction();
1607 }
1608 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1609 return record.GetInstruction();
1610 }
1611};
1612
1613using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1614using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1615
Aart Bik854a02b2015-07-14 16:07:00 -07001616/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001618 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001619 * For write/read dependences on fields/arrays, the dependence analysis uses
1620 * type disambiguation (e.g. a float field write cannot modify the value of an
1621 * integer field read) and the access type (e.g. a reference array write cannot
1622 * modify the value of a reference field read [although it may modify the
1623 * reference fetch prior to reading the field, which is represented by its own
1624 * write/read dependence]). The analysis makes conservative points-to
1625 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1626 * the same, and any reference read depends on any reference read without
1627 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001628 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 * The internal representation uses 38-bit and is described in the table below.
1630 * The first line indicates the side effect, and for field/array accesses the
1631 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001632 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001633 * The two numbered lines below indicate the bit position in the bitfield (read
1634 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001635 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001636 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1637 * +-------------+---------+---------+--------------+---------+---------+
1638 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1639 * | 3 |333333322|222222221| 1 |111111110|000000000|
1640 * | 7 |654321098|765432109| 8 |765432109|876543210|
1641 *
1642 * Note that, to ease the implementation, 'changes' bits are least significant
1643 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001644 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001645class SideEffects : public ValueObject {
1646 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001647 SideEffects() : flags_(0) {}
1648
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001649 static SideEffects None() {
1650 return SideEffects(0);
1651 }
1652
1653 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001654 return SideEffects(kAllChangeBits | kAllDependOnBits);
1655 }
1656
1657 static SideEffects AllChanges() {
1658 return SideEffects(kAllChangeBits);
1659 }
1660
1661 static SideEffects AllDependencies() {
1662 return SideEffects(kAllDependOnBits);
1663 }
1664
1665 static SideEffects AllExceptGCDependency() {
1666 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1667 }
1668
1669 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001670 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001671 }
1672
Aart Bik34c3ba92015-07-20 14:08:59 -07001673 static SideEffects AllWrites() {
1674 return SideEffects(kAllWrites);
1675 }
1676
1677 static SideEffects AllReads() {
1678 return SideEffects(kAllReads);
1679 }
1680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001682 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001683 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001684 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001685 }
1686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001688 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001689 }
1690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001691 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001692 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001693 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001694 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001695 }
1696
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001697 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001698 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001699 }
1700
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001701 static SideEffects CanTriggerGC() {
1702 return SideEffects(1ULL << kCanTriggerGCBit);
1703 }
1704
1705 static SideEffects DependsOnGC() {
1706 return SideEffects(1ULL << kDependsOnGCBit);
1707 }
1708
Aart Bik854a02b2015-07-14 16:07:00 -07001709 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001710 SideEffects Union(SideEffects other) const {
1711 return SideEffects(flags_ | other.flags_);
1712 }
1713
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001714 SideEffects Exclusion(SideEffects other) const {
1715 return SideEffects(flags_ & ~other.flags_);
1716 }
1717
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001718 void Add(SideEffects other) {
1719 flags_ |= other.flags_;
1720 }
1721
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001722 bool Includes(SideEffects other) const {
1723 return (other.flags_ & flags_) == other.flags_;
1724 }
1725
1726 bool HasSideEffects() const {
1727 return (flags_ & kAllChangeBits);
1728 }
1729
1730 bool HasDependencies() const {
1731 return (flags_ & kAllDependOnBits);
1732 }
1733
1734 // Returns true if there are no side effects or dependencies.
1735 bool DoesNothing() const {
1736 return flags_ == 0;
1737 }
1738
Aart Bik854a02b2015-07-14 16:07:00 -07001739 // Returns true if something is written.
1740 bool DoesAnyWrite() const {
1741 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001742 }
1743
Aart Bik854a02b2015-07-14 16:07:00 -07001744 // Returns true if something is read.
1745 bool DoesAnyRead() const {
1746 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001747 }
1748
Aart Bik854a02b2015-07-14 16:07:00 -07001749 // Returns true if potentially everything is written and read
1750 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001751 bool DoesAllReadWrite() const {
1752 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1753 }
1754
Aart Bik854a02b2015-07-14 16:07:00 -07001755 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001757 }
1758
Roland Levillain0d5a2812015-11-13 10:07:31 +00001759 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001760 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001761 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1762 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001763 }
1764
1765 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001766 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001767 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001768 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001769 for (int s = kLastBit; s >= 0; s--) {
1770 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1771 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1772 // This is a bit for the GC side effect.
1773 if (current_bit_is_set) {
1774 flags += "GC";
1775 }
Aart Bik854a02b2015-07-14 16:07:00 -07001776 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001777 } else {
1778 // This is a bit for the array/field analysis.
1779 // The underscore character stands for the 'can trigger GC' bit.
1780 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1781 if (current_bit_is_set) {
1782 flags += kDebug[s];
1783 }
1784 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1785 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1786 flags += "|";
1787 }
1788 }
Aart Bik854a02b2015-07-14 16:07:00 -07001789 }
1790 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001791 }
1792
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001793 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001794
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001795 private:
1796 static constexpr int kFieldArrayAnalysisBits = 9;
1797
1798 static constexpr int kFieldWriteOffset = 0;
1799 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1800 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1801 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1802
1803 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1804
1805 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1806 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1807 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1808 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1809
1810 static constexpr int kLastBit = kDependsOnGCBit;
1811 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1812
1813 // Aliases.
1814
1815 static_assert(kChangeBits == kDependOnBits,
1816 "the 'change' bits should match the 'depend on' bits.");
1817
1818 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1819 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1820 static constexpr uint64_t kAllWrites =
1821 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1822 static constexpr uint64_t kAllReads =
1823 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001825 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001826 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001827 int shift;
1828 switch (type) {
1829 case DataType::Type::kReference: shift = 0; break;
1830 case DataType::Type::kBool: shift = 1; break;
1831 case DataType::Type::kInt8: shift = 2; break;
1832 case DataType::Type::kUint16: shift = 3; break;
1833 case DataType::Type::kInt16: shift = 4; break;
1834 case DataType::Type::kInt32: shift = 5; break;
1835 case DataType::Type::kInt64: shift = 6; break;
1836 case DataType::Type::kFloat32: shift = 7; break;
1837 case DataType::Type::kFloat64: shift = 8; break;
1838 default:
1839 LOG(FATAL) << "Unexpected data type " << type;
1840 UNREACHABLE();
1841 }
Aart Bik854a02b2015-07-14 16:07:00 -07001842 DCHECK_LE(kFieldWriteOffset, shift);
1843 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001844 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001845 }
1846
1847 // Private constructor on direct flags value.
1848 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1849
1850 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001851};
1852
David Brazdiled596192015-01-23 10:39:45 +00001853// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001854class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001855 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001856 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001857 size_t number_of_vregs,
1858 ArtMethod* method,
1859 uint32_t dex_pc,
1860 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001861 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1862 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001863 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001864 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001865 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001866 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001867 }
1868
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001869 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1870 const HEnvironment& to_copy,
1871 HInstruction* holder)
1872 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001873 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001874 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001875 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001876 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001877
Vladimir Markoec32f642017-06-02 10:51:55 +01001878 void AllocateLocations() {
1879 DCHECK(locations_.empty());
1880 locations_.resize(vregs_.size());
1881 }
1882
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001883 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001884 if (parent_ != nullptr) {
1885 parent_->SetAndCopyParentChain(allocator, parent);
1886 } else {
1887 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1888 parent_->CopyFrom(parent);
1889 if (parent->GetParent() != nullptr) {
1890 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1891 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001892 }
David Brazdiled596192015-01-23 10:39:45 +00001893 }
1894
Vladimir Marko69d310e2017-10-09 14:12:23 +01001895 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001896 void CopyFrom(HEnvironment* environment);
1897
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001898 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1899 // input to the loop phi instead. This is for inserting instructions that
1900 // require an environment (like HDeoptimization) in the loop pre-header.
1901 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001902
1903 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001904 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001905 }
1906
1907 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001908 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001909 }
1910
David Brazdil1abb4192015-02-17 18:33:36 +00001911 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001912
Artem Serovca210e32017-12-15 13:43:20 +00001913 // Replaces the input at the position 'index' with the replacement; the replacement and old
1914 // input instructions' env_uses_ lists are adjusted. The function works similar to
1915 // HInstruction::ReplaceInput.
1916 void ReplaceInput(HInstruction* replacement, size_t index);
1917
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001918 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001919
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001920 HEnvironment* GetParent() const { return parent_; }
1921
1922 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001923 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001924 }
1925
1926 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001927 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001928 }
1929
1930 uint32_t GetDexPc() const {
1931 return dex_pc_;
1932 }
1933
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001934 ArtMethod* GetMethod() const {
1935 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001936 }
1937
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001938 HInstruction* GetHolder() const {
1939 return holder_;
1940 }
1941
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001942
1943 bool IsFromInlinedInvoke() const {
1944 return GetParent() != nullptr;
1945 }
1946
David Brazdiled596192015-01-23 10:39:45 +00001947 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001948 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1949 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001950 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001951 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001952 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001953
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001954 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001955 HInstruction* const holder_;
1956
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001957 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001958
1959 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1960};
1961
Vladimir Markof9f64412015-09-02 14:05:49 +01001962class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001963 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301964#define DECLARE_KIND(type, super) k##type,
1965 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001966 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301967 kLastInstructionKind
1968 };
1969#undef DECLARE_KIND
1970
1971 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001972 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1973
1974 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001975 : previous_(nullptr),
1976 next_(nullptr),
1977 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001978 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001979 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001980 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001981 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001982 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001983 locations_(nullptr),
1984 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001985 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001986 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001987 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301988 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01001989 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001990 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1991 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001992
Dave Allison20dfc792014-06-16 20:44:29 -07001993 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001994
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001995
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001996 HInstruction* GetNext() const { return next_; }
1997 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001998
Calin Juravle77520bc2015-01-12 18:45:46 +00001999 HInstruction* GetNextDisregardingMoves() const;
2000 HInstruction* GetPreviousDisregardingMoves() const;
2001
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002002 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002003 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002004 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002005 bool IsInBlock() const { return block_ != nullptr; }
2006 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002007 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2008 bool IsIrreducibleLoopHeaderPhi() const {
2009 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2010 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002011
Vladimir Marko372f10e2016-05-17 16:30:10 +01002012 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2013
2014 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2015 // One virtual method is enough, just const_cast<> and then re-add the const.
2016 return ArrayRef<const HUserRecord<HInstruction*>>(
2017 const_cast<HInstruction*>(this)->GetInputRecords());
2018 }
2019
Vladimir Markoe9004912016-06-16 16:50:52 +01002020 HInputsRef GetInputs() {
2021 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002022 }
2023
Vladimir Markoe9004912016-06-16 16:50:52 +01002024 HConstInputsRef GetInputs() const {
2025 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002026 }
2027
2028 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002029 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002030
Aart Bik2767f4b2016-10-28 15:03:53 -07002031 bool HasInput(HInstruction* input) const {
2032 for (const HInstruction* i : GetInputs()) {
2033 if (i == input) {
2034 return true;
2035 }
2036 }
2037 return false;
2038 }
2039
Vladimir Marko372f10e2016-05-17 16:30:10 +01002040 void SetRawInputAt(size_t index, HInstruction* input) {
2041 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2042 }
2043
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002044 virtual void Accept(HGraphVisitor* visitor) = 0;
2045 virtual const char* DebugName() const = 0;
2046
Vladimir Markobd785672018-05-03 17:09:09 +01002047 DataType::Type GetType() const {
2048 return TypeField::Decode(GetPackedFields());
2049 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002050
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002051 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002052
2053 uint32_t GetDexPc() const { return dex_pc_; }
2054
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002055 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002056
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002057 // Can the instruction throw?
2058 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2059 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002060 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002061
2062 // Does the instruction always throw an exception unconditionally?
2063 virtual bool AlwaysThrows() const { return false; }
2064
David Brazdilec16f792015-08-19 15:04:01 +01002065 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002066
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002067 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002068 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002069
Calin Juravle10e244f2015-01-26 18:54:32 +00002070 // Does not apply for all instructions, but having this at top level greatly
2071 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002072 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002073 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002075 return true;
2076 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002077
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002078 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002079 return false;
2080 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002081
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002082 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2083 // with it. Otherwise return null.
2084 HNullCheck* GetImplicitNullCheck() const {
2085 // Find the first previous instruction which is not a move.
2086 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2087 if (first_prev_not_move != nullptr &&
2088 first_prev_not_move->IsNullCheck() &&
2089 first_prev_not_move->IsEmittedAtUseSite()) {
2090 return first_prev_not_move->AsNullCheck();
2091 }
2092 return nullptr;
2093 }
2094
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002095 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002096 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002097 }
2098
Calin Juravle2e768302015-07-28 14:41:11 +00002099 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002100
Calin Juravle61d544b2015-02-23 16:46:57 +00002101 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002103 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002104 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002105 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002106
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002107 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002108 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002109 // Note: fixup_end remains valid across push_front().
2110 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2111 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002112 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002113 uses_.push_front(*new_node);
2114 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002115 }
2116
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002117 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002118 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002119 // Note: env_fixup_end remains valid across push_front().
2120 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2121 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002122 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002123 env_uses_.push_front(*new_node);
2124 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002125 }
2126
David Brazdil1abb4192015-02-17 18:33:36 +00002127 void RemoveAsUserOfInput(size_t input) {
2128 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002129 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2130 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2131 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002132 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002133
Vladimir Marko372f10e2016-05-17 16:30:10 +01002134 void RemoveAsUserOfAllInputs() {
2135 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2136 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);
2139 }
2140 }
2141
David Brazdil1abb4192015-02-17 18:33:36 +00002142 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2143 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002144
Vladimir Marko46817b82016-03-29 12:21:58 +01002145 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2146 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2147 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002148 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002149 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002150 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002151
Aart Bikcc42be02016-10-20 16:14:16 -07002152 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002153 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002154 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002155 !CanThrow() &&
2156 !IsSuspendCheck() &&
2157 !IsControlFlow() &&
2158 !IsNativeDebugInfo() &&
2159 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002160 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002161 !IsMemoryBarrier() &&
2162 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002163 }
2164
Aart Bikcc42be02016-10-20 16:14:16 -07002165 bool IsDeadAndRemovable() const {
2166 return IsRemovable() && !HasUses();
2167 }
2168
Roland Levillain6c82d402014-10-13 16:10:27 +01002169 // Does this instruction strictly dominate `other_instruction`?
2170 // Returns false if this instruction and `other_instruction` are the same.
2171 // Aborts if this instruction and `other_instruction` are both phis.
2172 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002173
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002174 int GetId() const { return id_; }
2175 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002177 int GetSsaIndex() const { return ssa_index_; }
2178 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2179 bool HasSsaIndex() const { return ssa_index_ != -1; }
2180
2181 bool HasEnvironment() const { return environment_ != nullptr; }
2182 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002183 // Set the `environment_` field. Raw because this method does not
2184 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002185 void SetRawEnvironment(HEnvironment* environment) {
2186 DCHECK(environment_ == nullptr);
2187 DCHECK_EQ(environment->GetHolder(), this);
2188 environment_ = environment;
2189 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002190
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002191 void InsertRawEnvironment(HEnvironment* environment) {
2192 DCHECK(environment_ != nullptr);
2193 DCHECK_EQ(environment->GetHolder(), this);
2194 DCHECK(environment->GetParent() == nullptr);
2195 environment->parent_ = environment_;
2196 environment_ = environment;
2197 }
2198
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002199 void RemoveEnvironment();
2200
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002201 // Set the environment of this instruction, copying it from `environment`. While
2202 // copying, the uses lists are being updated.
2203 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002204 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002205 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002206 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002207 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002208 if (environment->GetParent() != nullptr) {
2209 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2210 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002211 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002212
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002213 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2214 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002215 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002216 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002217 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002218 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002219 if (environment->GetParent() != nullptr) {
2220 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2221 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002222 }
2223
Nicolas Geoffray39468442014-09-02 15:17:15 +01002224 // Returns the number of entries in the environment. Typically, that is the
2225 // number of dex registers in a method. It could be more in case of inlining.
2226 size_t EnvironmentSize() const;
2227
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002228 LocationSummary* GetLocations() const { return locations_; }
2229 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002230
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002231 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002232 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002233 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002234 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002235
Alexandre Rames188d4312015-04-09 18:30:21 +01002236 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2237 // uses of this instruction by `other` are *not* updated.
2238 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2239 ReplaceWith(other);
2240 other->ReplaceInput(this, use_index);
2241 }
2242
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002243 // Move `this` instruction before `cursor`
2244 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002245
Vladimir Markofb337ea2015-11-25 15:25:10 +00002246 // Move `this` before its first user and out of any loops. If there is no
2247 // out-of-loop user that dominates all other users, move the instruction
2248 // to the end of the out-of-loop common dominator of the user's blocks.
2249 //
2250 // This can be used only on non-throwing instructions with no side effects that
2251 // have at least one use but no environment uses.
2252 void MoveBeforeFirstUserAndOutOfLoops();
2253
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002254#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002255 bool Is##type() const;
2256
2257 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2258#undef INSTRUCTION_TYPE_CHECK
2259
2260#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002261 const H##type* As##type() const; \
2262 H##type* As##type();
2263
Vladimir Markoa90dd512018-05-04 15:04:45 +01002264 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2265#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002266
Artem Serovcced8ba2017-07-19 18:18:09 +01002267 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2268 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2269 // the instruction then the behaviour of this function is undefined.
2270 //
2271 // Note: It is semantically valid to create a clone of the instruction only until
2272 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2273 // copied.
2274 //
2275 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2276 // 'explicit HInstruction(const HInstruction& other)' for details.
2277 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2278 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2279 DebugName() << " " << GetId();
2280 UNREACHABLE();
2281 }
2282
2283 // Return whether instruction can be cloned (copied).
2284 virtual bool IsClonable() const { return false; }
2285
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002286 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002287 // TODO: this method is used by LICM and GVN with possibly different
2288 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002289 virtual bool CanBeMoved() const { return false; }
2290
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002291 // Returns whether any data encoded in the two instructions is equal.
2292 // This method does not look at the inputs. Both instructions must be
2293 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002294 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002295 return false;
2296 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002297
2298 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002299 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002300 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002301 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002302
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002303 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2304 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2305 // the virtual function because the __attribute__((__pure__)) doesn't really
2306 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302307 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002308
2309 virtual size_t ComputeHashCode() const {
2310 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002311 for (const HInstruction* input : GetInputs()) {
2312 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002313 }
2314 return result;
2315 }
2316
2317 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002318 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002319 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002320
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002321 size_t GetLifetimePosition() const { return lifetime_position_; }
2322 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2323 LiveInterval* GetLiveInterval() const { return live_interval_; }
2324 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2325 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2326
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002327 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2328
2329 // Returns whether the code generation of the instruction will require to have access
2330 // to the current method. Such instructions are:
2331 // (1): Instructions that require an environment, as calling the runtime requires
2332 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002333 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2334 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002335 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002336 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002337 }
2338
Vladimir Markodc151b22015-10-15 18:02:30 +01002339 // Returns whether the code generation of the instruction will require to have access
2340 // to the dex cache of the current method's declaring class via the current method.
2341 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002342
Mark Mendellc4701932015-04-10 13:18:51 -04002343 // Does this instruction have any use in an environment before
2344 // control flow hits 'other'?
2345 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2346
2347 // Remove all references to environment uses of this instruction.
2348 // The caller must ensure that this is safe to do.
2349 void RemoveEnvironmentUsers();
2350
Vladimir Markoa1de9182016-02-25 11:37:38 +00002351 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2352 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002353
David Brazdil1abb4192015-02-17 18:33:36 +00002354 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002355 // If set, the machine code for this instruction is assumed to be generated by
2356 // its users. Used by liveness analysis to compute use positions accordingly.
2357 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2358 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302359 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2360 static constexpr size_t kFieldInstructionKindSize =
2361 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002362 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302363 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002364 static constexpr size_t kFieldTypeSize =
2365 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2366 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002367 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2368
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302369 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2370 "Too many generic packed fields");
2371
Vladimir Markobd785672018-05-03 17:09:09 +01002372 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2373
Vladimir Marko372f10e2016-05-17 16:30:10 +01002374 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2375 return GetInputRecords()[i];
2376 }
2377
2378 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2379 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2380 input_records[index] = input;
2381 }
David Brazdil1abb4192015-02-17 18:33:36 +00002382
Vladimir Markoa1de9182016-02-25 11:37:38 +00002383 uint32_t GetPackedFields() const {
2384 return packed_fields_;
2385 }
2386
2387 template <size_t flag>
2388 bool GetPackedFlag() const {
2389 return (packed_fields_ & (1u << flag)) != 0u;
2390 }
2391
2392 template <size_t flag>
2393 void SetPackedFlag(bool value = true) {
2394 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2395 }
2396
2397 template <typename BitFieldType>
2398 typename BitFieldType::value_type GetPackedField() const {
2399 return BitFieldType::Decode(packed_fields_);
2400 }
2401
2402 template <typename BitFieldType>
2403 void SetPackedField(typename BitFieldType::value_type value) {
2404 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2405 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2406 }
2407
Artem Serovcced8ba2017-07-19 18:18:09 +01002408 // Copy construction for the instruction (used for Clone function).
2409 //
2410 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2411 // prepare_for_register_allocator are not copied (set to default values).
2412 //
2413 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2414 // fine for most of them. However for some of the instructions a custom copy constructor must be
2415 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2416 // for copying them).
2417 explicit HInstruction(const HInstruction& other)
2418 : previous_(nullptr),
2419 next_(nullptr),
2420 block_(nullptr),
2421 dex_pc_(other.dex_pc_),
2422 id_(-1),
2423 ssa_index_(-1),
2424 packed_fields_(other.packed_fields_),
2425 environment_(nullptr),
2426 locations_(nullptr),
2427 live_interval_(nullptr),
2428 lifetime_position_(kNoLifetime),
2429 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302430 reference_type_handle_(other.reference_type_handle_) {
2431 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002432
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002433 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302434 using InstructionKindField =
2435 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2436
Vladimir Marko46817b82016-03-29 12:21:58 +01002437 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2438 auto before_use_node = uses_.before_begin();
2439 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2440 HInstruction* user = use_node->GetUser();
2441 size_t input_index = use_node->GetIndex();
2442 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2443 before_use_node = use_node;
2444 }
2445 }
2446
2447 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2448 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2449 if (next != uses_.end()) {
2450 HInstruction* next_user = next->GetUser();
2451 size_t next_index = next->GetIndex();
2452 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2453 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2454 }
2455 }
2456
2457 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2458 auto before_env_use_node = env_uses_.before_begin();
2459 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2460 HEnvironment* user = env_use_node->GetUser();
2461 size_t input_index = env_use_node->GetIndex();
2462 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2463 before_env_use_node = env_use_node;
2464 }
2465 }
2466
2467 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2468 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2469 if (next != env_uses_.end()) {
2470 HEnvironment* next_user = next->GetUser();
2471 size_t next_index = next->GetIndex();
2472 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2473 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2474 }
2475 }
David Brazdil1abb4192015-02-17 18:33:36 +00002476
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002477 HInstruction* previous_;
2478 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002479 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002480 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002482 // An instruction gets an id when it is added to the graph.
2483 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002484 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002485 int id_;
2486
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002487 // When doing liveness analysis, instructions that have uses get an SSA index.
2488 int ssa_index_;
2489
Vladimir Markoa1de9182016-02-25 11:37:38 +00002490 // Packed fields.
2491 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002492
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002493 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002494 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002495
2496 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002497 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002498
Nicolas Geoffray39468442014-09-02 15:17:15 +01002499 // The environment associated with this instruction. Not null if the instruction
2500 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002501 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002502
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002503 // Set by the code generator.
2504 LocationSummary* locations_;
2505
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002506 // Set by the liveness analysis.
2507 LiveInterval* live_interval_;
2508
2509 // Set by the liveness analysis, this is the position in a linear
2510 // order of blocks where this instruction's live interval start.
2511 size_t lifetime_position_;
2512
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002513 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002514
Vladimir Markoa1de9182016-02-25 11:37:38 +00002515 // The reference handle part of the reference type info.
2516 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002517 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002518 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002519
David Brazdil1abb4192015-02-17 18:33:36 +00002520 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002521 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002522 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002523 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002524 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002525};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002526std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002527
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002528// Iterates over the instructions, while preserving the next instruction
2529// in case the current instruction gets removed from the list by the user
2530// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002531class HInstructionIterator : public ValueObject {
2532 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002533 explicit HInstructionIterator(const HInstructionList& instructions)
2534 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002535 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002536 }
2537
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002538 bool Done() const { return instruction_ == nullptr; }
2539 HInstruction* Current() const { return instruction_; }
2540 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002541 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002542 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002543 }
2544
2545 private:
2546 HInstruction* instruction_;
2547 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002548
2549 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002550};
2551
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002552// Iterates over the instructions without saving the next instruction,
2553// therefore handling changes in the graph potentially made by the user
2554// of this iterator.
2555class HInstructionIteratorHandleChanges : public ValueObject {
2556 public:
2557 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2558 : instruction_(instructions.first_instruction_) {
2559 }
2560
2561 bool Done() const { return instruction_ == nullptr; }
2562 HInstruction* Current() const { return instruction_; }
2563 void Advance() {
2564 instruction_ = instruction_->GetNext();
2565 }
2566
2567 private:
2568 HInstruction* instruction_;
2569
2570 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2571};
2572
2573
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002574class HBackwardInstructionIterator : public ValueObject {
2575 public:
2576 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2577 : instruction_(instructions.last_instruction_) {
2578 next_ = Done() ? nullptr : instruction_->GetPrevious();
2579 }
2580
2581 bool Done() const { return instruction_ == nullptr; }
2582 HInstruction* Current() const { return instruction_; }
2583 void Advance() {
2584 instruction_ = next_;
2585 next_ = Done() ? nullptr : instruction_->GetPrevious();
2586 }
2587
2588 private:
2589 HInstruction* instruction_;
2590 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002591
2592 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002593};
2594
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002595class HVariableInputSizeInstruction : public HInstruction {
2596 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002597 using HInstruction::GetInputRecords; // Keep the const version visible.
2598 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2599 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2600 }
2601
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002602 void AddInput(HInstruction* input);
2603 void InsertInputAt(size_t index, HInstruction* input);
2604 void RemoveInputAt(size_t index);
2605
Igor Murashkind01745e2017-04-05 16:40:31 -07002606 // Removes all the inputs.
2607 // Also removes this instructions from each input's use list
2608 // (for non-environment uses only).
2609 void RemoveAllInputs();
2610
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002611 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302612 HVariableInputSizeInstruction(InstructionKind inst_kind,
2613 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002614 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002615 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002616 size_t number_of_inputs,
2617 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302618 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002619 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002620 HVariableInputSizeInstruction(InstructionKind inst_kind,
2621 DataType::Type type,
2622 SideEffects side_effects,
2623 uint32_t dex_pc,
2624 ArenaAllocator* allocator,
2625 size_t number_of_inputs,
2626 ArenaAllocKind kind)
2627 : HInstruction(inst_kind, type, side_effects, dex_pc),
2628 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002629
Artem Serovcced8ba2017-07-19 18:18:09 +01002630 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002631
Artem Serovcced8ba2017-07-19 18:18:09 +01002632 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002633};
2634
Vladimir Markof9f64412015-09-02 14:05:49 +01002635template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002636class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002637 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002638 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302639 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002640 HExpression<N>(InstructionKind kind,
2641 DataType::Type type,
2642 SideEffects side_effects,
2643 uint32_t dex_pc)
2644 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2645 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002646
Vladimir Marko372f10e2016-05-17 16:30:10 +01002647 using HInstruction::GetInputRecords; // Keep the const version visible.
2648 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2649 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002650 }
2651
Artem Serovcced8ba2017-07-19 18:18:09 +01002652 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002653 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002654
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002655 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002656 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002657
2658 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659};
2660
Vladimir Markobd785672018-05-03 17:09:09 +01002661// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002662template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002663class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002664 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002665 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002666
Vladimir Markobd785672018-05-03 17:09:09 +01002667 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002668
Vladimir Marko372f10e2016-05-17 16:30:10 +01002669 using HInstruction::GetInputRecords; // Keep the const version visible.
2670 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2671 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002672 }
2673
Artem Serovcced8ba2017-07-19 18:18:09 +01002674 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002675 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002676
Vladimir Markof9f64412015-09-02 14:05:49 +01002677 private:
2678 friend class SsaBuilder;
2679};
2680
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002681// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2682// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002683class HReturnVoid FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002684 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002685 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002686 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302687 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002688
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002689 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002690
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002691 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002692
Artem Serovcced8ba2017-07-19 18:18:09 +01002693 protected:
2694 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002695};
2696
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002697// Represents dex's RETURN opcodes. A HReturn is a control flow
2698// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002699class HReturn FINAL : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002700 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002701 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002702 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002703 SetRawInputAt(0, value);
2704 }
2705
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002706 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002707
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002708 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002709
Artem Serovcced8ba2017-07-19 18:18:09 +01002710 protected:
2711 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002712};
2713
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002714class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002715 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002716 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002717 uint32_t reg_number,
2718 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002720 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002721 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302722 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002723 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002724 SideEffects::None(),
2725 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002726 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002727 number_of_inputs,
2728 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002729 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002730 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002731 // Phis are constructed live and marked dead if conflicting or unused.
2732 // Individual steps of SsaBuilder should assume that if a phi has been
2733 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2734 SetPackedFlag<kFlagIsLive>(true);
2735 SetPackedFlag<kFlagCanBeNull>(true);
2736 }
2737
Artem Serovcced8ba2017-07-19 18:18:09 +01002738 bool IsClonable() const OVERRIDE { return true; }
2739
David Brazdildee58d62016-04-07 09:54:26 +00002740 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 static DataType::Type ToPhiType(DataType::Type type) {
2742 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002743 }
2744
2745 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002747 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002748 // Make sure that only valid type changes occur. The following are allowed:
2749 // (1) int -> float/ref (primitive type propagation),
2750 // (2) long -> double (primitive type propagation).
2751 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002752 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2753 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2754 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002755 SetPackedField<TypeField>(new_type);
2756 }
2757
2758 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2759 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2760
2761 uint32_t GetRegNumber() const { return reg_number_; }
2762
2763 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2764 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2765 bool IsDead() const { return !IsLive(); }
2766 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2767
Vladimir Markoe9004912016-06-16 16:50:52 +01002768 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002769 return other != nullptr
2770 && other->IsPhi()
2771 && other->AsPhi()->GetBlock() == GetBlock()
2772 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2773 }
2774
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002775 bool HasEquivalentPhi() const {
2776 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2777 return true;
2778 }
2779 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2780 return true;
2781 }
2782 return false;
2783 }
2784
David Brazdildee58d62016-04-07 09:54:26 +00002785 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2786 // An equivalent phi is a phi having the same dex register and type.
2787 // It assumes that phis with the same dex register are adjacent.
2788 HPhi* GetNextEquivalentPhiWithSameType() {
2789 HInstruction* next = GetNext();
2790 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2791 if (next->GetType() == GetType()) {
2792 return next->AsPhi();
2793 }
2794 next = next->GetNext();
2795 }
2796 return nullptr;
2797 }
2798
2799 DECLARE_INSTRUCTION(Phi);
2800
Artem Serovcced8ba2017-07-19 18:18:09 +01002801 protected:
2802 DEFAULT_COPY_CONSTRUCTOR(Phi);
2803
David Brazdildee58d62016-04-07 09:54:26 +00002804 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002805 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002806 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2807 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2808 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002809
David Brazdildee58d62016-04-07 09:54:26 +00002810 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002811};
2812
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002813// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002814// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002815// exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002816class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002817 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302818 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002819 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302820 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002823
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002824 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002825
Artem Serovcced8ba2017-07-19 18:18:09 +01002826 protected:
2827 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002828};
2829
2830// Jumps from one block to another.
Vladimir Markobd785672018-05-03 17:09:09 +01002831class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002832 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302833 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002834 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302835 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002836
Artem Serovcced8ba2017-07-19 18:18:09 +01002837 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002838 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002839
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002840 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002841 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002842 }
2843
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002844 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002845
Artem Serovcced8ba2017-07-19 18:18:09 +01002846 protected:
2847 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002848};
2849
Roland Levillain9867bc72015-08-05 10:21:34 +01002850class HConstant : public HExpression<0> {
2851 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302852 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2853 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2854 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002855
2856 bool CanBeMoved() const OVERRIDE { return true; }
2857
Roland Levillain1a653882016-03-18 18:05:57 +00002858 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002859 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002860 // Is this constant 0 in the arithmetic sense?
2861 virtual bool IsArithmeticZero() const { return false; }
2862 // Is this constant a 0-bit pattern?
2863 virtual bool IsZeroBitPattern() const { return false; }
2864 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002865 virtual bool IsOne() const { return false; }
2866
David Brazdil77a48ae2015-09-15 12:34:04 +00002867 virtual uint64_t GetValueAsUint64() const = 0;
2868
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002869 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002870
Artem Serovcced8ba2017-07-19 18:18:09 +01002871 protected:
2872 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002873};
2874
Vladimir Markofcb503c2016-05-18 12:48:17 +01002875class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002876 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002877 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002878 return true;
2879 }
2880
David Brazdil77a48ae2015-09-15 12:34:04 +00002881 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2882
Roland Levillain9867bc72015-08-05 10:21:34 +01002883 size_t ComputeHashCode() const OVERRIDE { return 0; }
2884
Roland Levillain1a653882016-03-18 18:05:57 +00002885 // The null constant representation is a 0-bit pattern.
2886 virtual bool IsZeroBitPattern() const { return true; }
2887
Roland Levillain9867bc72015-08-05 10:21:34 +01002888 DECLARE_INSTRUCTION(NullConstant);
2889
Artem Serovcced8ba2017-07-19 18:18:09 +01002890 protected:
2891 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2892
Roland Levillain9867bc72015-08-05 10:21:34 +01002893 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302895 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2896 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002897
2898 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002899};
2900
2901// Constants of the type int. Those can be from Dex instructions, or
2902// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002903class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002904 public:
2905 int32_t GetValue() const { return value_; }
2906
David Brazdil9f389d42015-10-01 14:32:56 +01002907 uint64_t GetValueAsUint64() const OVERRIDE {
2908 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2909 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002910
Vladimir Marko372f10e2016-05-17 16:30:10 +01002911 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002912 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 return other->AsIntConstant()->value_ == value_;
2914 }
2915
2916 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2917
2918 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002919 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2920 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002921 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2922
Roland Levillain1a653882016-03-18 18:05:57 +00002923 // Integer constants are used to encode Boolean values as well,
2924 // where 1 means true and 0 means false.
2925 bool IsTrue() const { return GetValue() == 1; }
2926 bool IsFalse() const { return GetValue() == 0; }
2927
Roland Levillain9867bc72015-08-05 10:21:34 +01002928 DECLARE_INSTRUCTION(IntConstant);
2929
Artem Serovcced8ba2017-07-19 18:18:09 +01002930 protected:
2931 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2932
Roland Levillain9867bc72015-08-05 10:21:34 +01002933 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002934 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302935 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2936 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002937 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302938 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2939 value_(value ? 1 : 0) {
2940 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002941
2942 const int32_t value_;
2943
2944 friend class HGraph;
2945 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2946 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002947};
2948
Vladimir Markofcb503c2016-05-18 12:48:17 +01002949class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002950 public:
2951 int64_t GetValue() const { return value_; }
2952
David Brazdil77a48ae2015-09-15 12:34:04 +00002953 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2954
Vladimir Marko372f10e2016-05-17 16:30:10 +01002955 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002956 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002957 return other->AsLongConstant()->value_ == value_;
2958 }
2959
2960 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2961
2962 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002963 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2964 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002965 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2966
2967 DECLARE_INSTRUCTION(LongConstant);
2968
Artem Serovcced8ba2017-07-19 18:18:09 +01002969 protected:
2970 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2971
Roland Levillain9867bc72015-08-05 10:21:34 +01002972 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002973 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302974 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2975 value_(value) {
2976 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002977
2978 const int64_t value_;
2979
2980 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002981};
Dave Allison20dfc792014-06-16 20:44:29 -07002982
Vladimir Markofcb503c2016-05-18 12:48:17 +01002983class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002984 public:
2985 float GetValue() const { return value_; }
2986
2987 uint64_t GetValueAsUint64() const OVERRIDE {
2988 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2989 }
2990
Vladimir Marko372f10e2016-05-17 16:30:10 +01002991 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002992 DCHECK(other->IsFloatConstant()) << other->DebugName();
2993 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2994 }
2995
2996 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2997
2998 bool IsMinusOne() const OVERRIDE {
2999 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3000 }
Roland Levillain1a653882016-03-18 18:05:57 +00003001 bool IsArithmeticZero() const OVERRIDE {
3002 return std::fpclassify(value_) == FP_ZERO;
3003 }
3004 bool IsArithmeticPositiveZero() const {
3005 return IsArithmeticZero() && !std::signbit(value_);
3006 }
3007 bool IsArithmeticNegativeZero() const {
3008 return IsArithmeticZero() && std::signbit(value_);
3009 }
3010 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003011 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003012 }
3013 bool IsOne() const OVERRIDE {
3014 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3015 }
3016 bool IsNaN() const {
3017 return std::isnan(value_);
3018 }
3019
3020 DECLARE_INSTRUCTION(FloatConstant);
3021
Artem Serovcced8ba2017-07-19 18:18:09 +01003022 protected:
3023 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3024
Roland Levillain31dd3d62016-02-16 12:21:02 +00003025 private:
3026 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303027 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3028 value_(value) {
3029 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003030 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303031 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3032 value_(bit_cast<float, int32_t>(value)) {
3033 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003034
3035 const float value_;
3036
3037 // Only the SsaBuilder and HGraph can create floating-point constants.
3038 friend class SsaBuilder;
3039 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003040};
3041
Vladimir Markofcb503c2016-05-18 12:48:17 +01003042class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003043 public:
3044 double GetValue() const { return value_; }
3045
3046 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3047
Vladimir Marko372f10e2016-05-17 16:30:10 +01003048 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003049 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3050 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3051 }
3052
3053 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3054
3055 bool IsMinusOne() const OVERRIDE {
3056 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3057 }
Roland Levillain1a653882016-03-18 18:05:57 +00003058 bool IsArithmeticZero() const OVERRIDE {
3059 return std::fpclassify(value_) == FP_ZERO;
3060 }
3061 bool IsArithmeticPositiveZero() const {
3062 return IsArithmeticZero() && !std::signbit(value_);
3063 }
3064 bool IsArithmeticNegativeZero() const {
3065 return IsArithmeticZero() && std::signbit(value_);
3066 }
3067 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003068 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 }
3070 bool IsOne() const OVERRIDE {
3071 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3072 }
3073 bool IsNaN() const {
3074 return std::isnan(value_);
3075 }
3076
3077 DECLARE_INSTRUCTION(DoubleConstant);
3078
Artem Serovcced8ba2017-07-19 18:18:09 +01003079 protected:
3080 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3081
Roland Levillain31dd3d62016-02-16 12:21:02 +00003082 private:
3083 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303084 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3085 value_(value) {
3086 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003087 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303088 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3089 value_(bit_cast<double, int64_t>(value)) {
3090 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003091
3092 const double value_;
3093
3094 // Only the SsaBuilder and HGraph can create floating-point constants.
3095 friend class SsaBuilder;
3096 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003097};
3098
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003099// Conditional branch. A block ending with an HIf instruction must have
3100// two successors.
Vladimir Markobd785672018-05-03 17:09:09 +01003101class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003102 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003103 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003104 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003105 SetRawInputAt(0, input);
3106 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003107
Artem Serovcced8ba2017-07-19 18:18:09 +01003108 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003109 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003110
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003111 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003112 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003113 }
3114
3115 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003116 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003117 }
3118
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003119 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003120
Artem Serovcced8ba2017-07-19 18:18:09 +01003121 protected:
3122 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003123};
3124
David Brazdilfc6a86a2015-06-26 10:33:45 +00003125
3126// Abstract instruction which marks the beginning and/or end of a try block and
3127// links it to the respective exception handlers. Behaves the same as a Goto in
3128// non-exceptional control flow.
3129// Normal-flow successor is stored at index zero, exception handlers under
3130// higher indices in no particular order.
Vladimir Markobd785672018-05-03 17:09:09 +01003131class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003132 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003133 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003134 kEntry,
3135 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003136 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003137 };
3138
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003139 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003140 : HExpression(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003141 SetPackedField<BoundaryKindField>(kind);
3142 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003143
3144 bool IsControlFlow() const OVERRIDE { return true; }
3145
3146 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003147 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003148
David Brazdild26a4112015-11-10 11:07:31 +00003149 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3150 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3151 }
3152
David Brazdilfc6a86a2015-06-26 10:33:45 +00003153 // Returns whether `handler` is among its exception handlers (non-zero index
3154 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003155 bool HasExceptionHandler(const HBasicBlock& handler) const {
3156 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003157 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003158 }
3159
3160 // If not present already, adds `handler` to its block's list of exception
3161 // handlers.
3162 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003163 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003164 GetBlock()->AddSuccessor(handler);
3165 }
3166 }
3167
Vladimir Markoa1de9182016-02-25 11:37:38 +00003168 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3169 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003170
David Brazdilffee3d32015-07-06 11:48:53 +01003171 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3172
David Brazdilfc6a86a2015-06-26 10:33:45 +00003173 DECLARE_INSTRUCTION(TryBoundary);
3174
Artem Serovcced8ba2017-07-19 18:18:09 +01003175 protected:
3176 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3177
David Brazdilfc6a86a2015-06-26 10:33:45 +00003178 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003179 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3180 static constexpr size_t kFieldBoundaryKindSize =
3181 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3182 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3183 kFieldBoundaryKind + kFieldBoundaryKindSize;
3184 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3185 "Too many packed fields.");
3186 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003187};
3188
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003189// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003190class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003191 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003192 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3193 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003194 HDeoptimize(ArenaAllocator* allocator,
3195 HInstruction* cond,
3196 DeoptimizationKind kind,
3197 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003198 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303199 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003200 SideEffects::All(),
3201 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003202 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003203 /* number_of_inputs */ 1,
3204 kArenaAllocMisc) {
3205 SetPackedFlag<kFieldCanBeMoved>(false);
3206 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003207 SetRawInputAt(0, cond);
3208 }
3209
Artem Serovcced8ba2017-07-19 18:18:09 +01003210 bool IsClonable() const OVERRIDE { return true; }
3211
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003212 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3213 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3214 // instead of `guard`.
3215 // We set CanTriggerGC to prevent any intermediate address to be live
3216 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003217 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003218 HInstruction* cond,
3219 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003220 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003221 uint32_t dex_pc)
3222 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303223 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003224 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003225 SideEffects::CanTriggerGC(),
3226 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003227 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003228 /* number_of_inputs */ 2,
3229 kArenaAllocMisc) {
3230 SetPackedFlag<kFieldCanBeMoved>(true);
3231 SetPackedField<DeoptimizeKindField>(kind);
3232 SetRawInputAt(0, cond);
3233 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003234 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003235
3236 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3237
3238 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3239 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3240 }
3241
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003242 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003243
Aart Bik75ff2c92018-04-21 01:28:11 +00003244 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003245
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003246 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003247
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003248 bool GuardsAnInput() const {
3249 return InputCount() == 2;
3250 }
3251
3252 HInstruction* GuardedInput() const {
3253 DCHECK(GuardsAnInput());
3254 return InputAt(1);
3255 }
3256
3257 void RemoveGuard() {
3258 RemoveInputAt(1);
3259 }
3260
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003261 DECLARE_INSTRUCTION(Deoptimize);
3262
Artem Serovcced8ba2017-07-19 18:18:09 +01003263 protected:
3264 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3265
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003266 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003267 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3268 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3269 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003270 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003271 static constexpr size_t kNumberOfDeoptimizePackedBits =
3272 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3273 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3274 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003275 using DeoptimizeKindField =
3276 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003277};
3278
Mingyao Yang063fc772016-08-02 11:02:54 -07003279// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3280// The compiled code checks this flag value in a guard before devirtualized call and
3281// if it's true, starts to do deoptimization.
3282// It has a 4-byte slot on stack.
3283// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003284class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003285 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003286 // CHA guards are only optimized in a separate pass and it has no side effects
3287 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003288 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303289 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003290 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303291 SideEffects::None(),
3292 dex_pc,
3293 allocator,
3294 0,
3295 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003296 }
3297
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003298 // We do all CHA guard elimination/motion in a single pass, after which there is no
3299 // further guard elimination/motion since a guard might have been used for justification
3300 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3301 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003302 bool CanBeMoved() const OVERRIDE { return false; }
3303
3304 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3305
Artem Serovcced8ba2017-07-19 18:18:09 +01003306 protected:
3307 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003308};
3309
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003310// Represents the ArtMethod that was passed as a first argument to
3311// the method. It is used by instructions that depend on it, like
3312// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003313class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003314 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003315 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303316 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3317 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003318
3319 DECLARE_INSTRUCTION(CurrentMethod);
3320
Artem Serovcced8ba2017-07-19 18:18:09 +01003321 protected:
3322 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003323};
3324
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003325// Fetches an ArtMethod from the virtual table or the interface method table
3326// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003327class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003328 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003329 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003330 kVTable,
3331 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003332 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003333 };
3334 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003335 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003336 TableKind kind,
3337 size_t index,
3338 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303339 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003340 index_(index) {
3341 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003342 SetRawInputAt(0, cls);
3343 }
3344
Artem Serovcced8ba2017-07-19 18:18:09 +01003345 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003346 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003347 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003348 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003349 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003350 }
3351
Vladimir Markoa1de9182016-02-25 11:37:38 +00003352 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003353 size_t GetIndex() const { return index_; }
3354
3355 DECLARE_INSTRUCTION(ClassTableGet);
3356
Artem Serovcced8ba2017-07-19 18:18:09 +01003357 protected:
3358 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3359
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003360 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003361 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003362 static constexpr size_t kFieldTableKindSize =
3363 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3364 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3365 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3366 "Too many packed fields.");
3367 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3368
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003369 // The index of the ArtMethod in the table.
3370 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003371};
3372
Mark Mendellfe57faa2015-09-18 09:26:15 -04003373// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3374// have one successor for each entry in the switch table, and the final successor
3375// will be the block containing the next Dex opcode.
Vladimir Markobd785672018-05-03 17:09:09 +01003376class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003377 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003378 HPackedSwitch(int32_t start_value,
3379 uint32_t num_entries,
3380 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003381 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003382 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003383 start_value_(start_value),
3384 num_entries_(num_entries) {
3385 SetRawInputAt(0, input);
3386 }
3387
Artem Serovcced8ba2017-07-19 18:18:09 +01003388 bool IsClonable() const OVERRIDE { return true; }
3389
Mark Mendellfe57faa2015-09-18 09:26:15 -04003390 bool IsControlFlow() const OVERRIDE { return true; }
3391
3392 int32_t GetStartValue() const { return start_value_; }
3393
Vladimir Marko211c2112015-09-24 16:52:33 +01003394 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003395
3396 HBasicBlock* GetDefaultBlock() const {
3397 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003398 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003399 }
3400 DECLARE_INSTRUCTION(PackedSwitch);
3401
Artem Serovcced8ba2017-07-19 18:18:09 +01003402 protected:
3403 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3404
Mark Mendellfe57faa2015-09-18 09:26:15 -04003405 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003406 const int32_t start_value_;
3407 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003408};
3409
Roland Levillain88cb1752014-10-20 16:36:47 +01003410class HUnaryOperation : public HExpression<1> {
3411 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303412 HUnaryOperation(InstructionKind kind,
3413 DataType::Type result_type,
3414 HInstruction* input,
3415 uint32_t dex_pc = kNoDexPc)
3416 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003417 SetRawInputAt(0, input);
3418 }
3419
Artem Serovcced8ba2017-07-19 18:18:09 +01003420 // All of the UnaryOperation instructions are clonable.
3421 bool IsClonable() const OVERRIDE { return true; }
3422
Roland Levillain88cb1752014-10-20 16:36:47 +01003423 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003424 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003425
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003426 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003427 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003428 return true;
3429 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003430
Roland Levillain31dd3d62016-02-16 12:21:02 +00003431 // Try to statically evaluate `this` and return a HConstant
3432 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003433 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003434 HConstant* TryStaticEvaluation() const;
3435
3436 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003437 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3438 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003439 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3440 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003441
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003442 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003443
Artem Serovcced8ba2017-07-19 18:18:09 +01003444 protected:
3445 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003446};
3447
Dave Allison20dfc792014-06-16 20:44:29 -07003448class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003449 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303450 HBinaryOperation(InstructionKind kind,
3451 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003452 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003453 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003454 SideEffects side_effects = SideEffects::None(),
3455 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303456 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003457 SetRawInputAt(0, left);
3458 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003459 }
3460
Artem Serovcced8ba2017-07-19 18:18:09 +01003461 // All of the BinaryOperation instructions are clonable.
3462 bool IsClonable() const OVERRIDE { return true; }
3463
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003464 HInstruction* GetLeft() const { return InputAt(0); }
3465 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003466 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003467
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003468 virtual bool IsCommutative() const { return false; }
3469
3470 // Put constant on the right.
3471 // Returns whether order is changed.
3472 bool OrderInputsWithConstantOnTheRight() {
3473 HInstruction* left = InputAt(0);
3474 HInstruction* right = InputAt(1);
3475 if (left->IsConstant() && !right->IsConstant()) {
3476 ReplaceInput(right, 0);
3477 ReplaceInput(left, 1);
3478 return true;
3479 }
3480 return false;
3481 }
3482
3483 // Order inputs by instruction id, but favor constant on the right side.
3484 // This helps GVN for commutative ops.
3485 void OrderInputs() {
3486 DCHECK(IsCommutative());
3487 HInstruction* left = InputAt(0);
3488 HInstruction* right = InputAt(1);
3489 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3490 return;
3491 }
3492 if (OrderInputsWithConstantOnTheRight()) {
3493 return;
3494 }
3495 // Order according to instruction id.
3496 if (left->GetId() > right->GetId()) {
3497 ReplaceInput(right, 0);
3498 ReplaceInput(left, 1);
3499 }
3500 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003501
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003502 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003503 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003504 return true;
3505 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003506
Roland Levillain31dd3d62016-02-16 12:21:02 +00003507 // Try to statically evaluate `this` and return a HConstant
3508 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003509 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003510 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003511
3512 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003513 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3514 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003515 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3516 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003517 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003518 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3519 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003520 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3521 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003522 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3523 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003524 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003525 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3526 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003527
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003528 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003529 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003530 HConstant* GetConstantRight() const;
3531
3532 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003533 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003534 HInstruction* GetLeastConstantLeft() const;
3535
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003536 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003537
Artem Serovcced8ba2017-07-19 18:18:09 +01003538 protected:
3539 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003540};
3541
Mark Mendellc4701932015-04-10 13:18:51 -04003542// The comparison bias applies for floating point operations and indicates how NaN
3543// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003544enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003545 kNoBias, // bias is not applicable (i.e. for long operation)
3546 kGtBias, // return 1 for NaN comparisons
3547 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003548 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003549};
3550
Roland Levillain31dd3d62016-02-16 12:21:02 +00003551std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3552
Dave Allison20dfc792014-06-16 20:44:29 -07003553class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003554 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303555 HCondition(InstructionKind kind,
3556 HInstruction* first,
3557 HInstruction* second,
3558 uint32_t dex_pc = kNoDexPc)
3559 : HBinaryOperation(kind,
3560 DataType::Type::kBool,
3561 first,
3562 second,
3563 SideEffects::None(),
3564 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003565 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3566 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003567
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003568 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003569 // `instruction`, and disregard moves in between.
3570 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003571
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003572 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003573
3574 virtual IfCondition GetCondition() const = 0;
3575
Mark Mendellc4701932015-04-10 13:18:51 -04003576 virtual IfCondition GetOppositeCondition() const = 0;
3577
Vladimir Markoa1de9182016-02-25 11:37:38 +00003578 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003579 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3580
Vladimir Markoa1de9182016-02-25 11:37:38 +00003581 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3582 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003583
Vladimir Marko372f10e2016-05-17 16:30:10 +01003584 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003585 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003586 }
3587
Roland Levillain4fa13f62015-07-06 18:11:54 +01003588 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003589 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003590 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003591 if (if_cond == kCondNE) {
3592 return true;
3593 } else if (if_cond == kCondEQ) {
3594 return false;
3595 }
3596 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003597 }
3598
3599 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003600 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003601 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003602 if (if_cond == kCondEQ) {
3603 return true;
3604 } else if (if_cond == kCondNE) {
3605 return false;
3606 }
3607 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003608 }
3609
Roland Levillain31dd3d62016-02-16 12:21:02 +00003610 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003611 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003612 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003613 static constexpr size_t kFieldComparisonBiasSize =
3614 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3615 static constexpr size_t kNumberOfConditionPackedBits =
3616 kFieldComparisonBias + kFieldComparisonBiasSize;
3617 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3618 using ComparisonBiasField =
3619 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3620
Roland Levillain31dd3d62016-02-16 12:21:02 +00003621 template <typename T>
3622 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3623
3624 template <typename T>
3625 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003626 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003627 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3628 // Handle the bias.
3629 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3630 }
3631
3632 // Return an integer constant containing the result of a condition evaluated at compile time.
3633 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3634 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3635 }
3636
Artem Serovcced8ba2017-07-19 18:18:09 +01003637 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003638};
3639
3640// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003641class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003642 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003643 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303644 : HCondition(kEqual, first, second, dex_pc) {
3645 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003646
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003647 bool IsCommutative() const OVERRIDE { return true; }
3648
Vladimir Marko9e23df52015-11-10 17:14:35 +00003649 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3650 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003651 return MakeConstantCondition(true, GetDexPc());
3652 }
3653 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3654 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3655 }
3656 // In the following Evaluate methods, a HCompare instruction has
3657 // been merged into this HEqual instruction; evaluate it as
3658 // `Compare(x, y) == 0`.
3659 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3660 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3661 GetDexPc());
3662 }
3663 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3664 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3665 }
3666 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3667 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003668 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003669
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003670 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003671
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003672 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003673 return kCondEQ;
3674 }
3675
Mark Mendellc4701932015-04-10 13:18:51 -04003676 IfCondition GetOppositeCondition() const OVERRIDE {
3677 return kCondNE;
3678 }
3679
Artem Serovcced8ba2017-07-19 18:18:09 +01003680 protected:
3681 DEFAULT_COPY_CONSTRUCTOR(Equal);
3682
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003683 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003684 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003685};
3686
Vladimir Markofcb503c2016-05-18 12:48:17 +01003687class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003688 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303689 HNotEqual(HInstruction* first, HInstruction* second,
3690 uint32_t dex_pc = kNoDexPc)
3691 : HCondition(kNotEqual, first, second, dex_pc) {
3692 }
Dave Allison20dfc792014-06-16 20:44:29 -07003693
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003694 bool IsCommutative() const OVERRIDE { return true; }
3695
Vladimir Marko9e23df52015-11-10 17:14:35 +00003696 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3697 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003698 return MakeConstantCondition(false, GetDexPc());
3699 }
3700 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3701 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3702 }
3703 // In the following Evaluate methods, a HCompare instruction has
3704 // been merged into this HNotEqual instruction; evaluate it as
3705 // `Compare(x, y) != 0`.
3706 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3707 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3708 }
3709 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3710 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3711 }
3712 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3713 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003714 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003715
Dave Allison20dfc792014-06-16 20:44:29 -07003716 DECLARE_INSTRUCTION(NotEqual);
3717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003718 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003719 return kCondNE;
3720 }
3721
Mark Mendellc4701932015-04-10 13:18:51 -04003722 IfCondition GetOppositeCondition() const OVERRIDE {
3723 return kCondEQ;
3724 }
3725
Artem Serovcced8ba2017-07-19 18:18:09 +01003726 protected:
3727 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3728
Dave Allison20dfc792014-06-16 20:44:29 -07003729 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003730 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003731};
3732
Vladimir Markofcb503c2016-05-18 12:48:17 +01003733class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003734 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303735 HLessThan(HInstruction* first, HInstruction* second,
3736 uint32_t dex_pc = kNoDexPc)
3737 : HCondition(kLessThan, first, second, dex_pc) {
3738 }
Dave Allison20dfc792014-06-16 20:44:29 -07003739
Roland Levillain9867bc72015-08-05 10:21:34 +01003740 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003741 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003742 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 // In the following Evaluate methods, a HCompare instruction has
3744 // been merged into this HLessThan instruction; evaluate it as
3745 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003746 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003747 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3748 }
3749 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3750 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3751 }
3752 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3753 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003754 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003755
Dave Allison20dfc792014-06-16 20:44:29 -07003756 DECLARE_INSTRUCTION(LessThan);
3757
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003758 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003759 return kCondLT;
3760 }
3761
Mark Mendellc4701932015-04-10 13:18:51 -04003762 IfCondition GetOppositeCondition() const OVERRIDE {
3763 return kCondGE;
3764 }
3765
Artem Serovcced8ba2017-07-19 18:18:09 +01003766 protected:
3767 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3768
Dave Allison20dfc792014-06-16 20:44:29 -07003769 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003770 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003771};
3772
Vladimir Markofcb503c2016-05-18 12:48:17 +01003773class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003774 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303775 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3776 uint32_t dex_pc = kNoDexPc)
3777 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3778 }
Dave Allison20dfc792014-06-16 20:44:29 -07003779
Roland Levillain9867bc72015-08-05 10:21:34 +01003780 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003781 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003782 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003783 // In the following Evaluate methods, a HCompare instruction has
3784 // been merged into this HLessThanOrEqual instruction; evaluate it as
3785 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003786 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003787 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3788 }
3789 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3790 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3791 }
3792 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3793 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003794 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003795
Dave Allison20dfc792014-06-16 20:44:29 -07003796 DECLARE_INSTRUCTION(LessThanOrEqual);
3797
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003798 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003799 return kCondLE;
3800 }
3801
Mark Mendellc4701932015-04-10 13:18:51 -04003802 IfCondition GetOppositeCondition() const OVERRIDE {
3803 return kCondGT;
3804 }
3805
Artem Serovcced8ba2017-07-19 18:18:09 +01003806 protected:
3807 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3808
Dave Allison20dfc792014-06-16 20:44:29 -07003809 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003810 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003811};
3812
Vladimir Markofcb503c2016-05-18 12:48:17 +01003813class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003815 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303816 : HCondition(kGreaterThan, first, second, dex_pc) {
3817 }
Dave Allison20dfc792014-06-16 20:44:29 -07003818
Roland Levillain9867bc72015-08-05 10:21:34 +01003819 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003820 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003821 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003822 // In the following Evaluate methods, a HCompare instruction has
3823 // been merged into this HGreaterThan instruction; evaluate it as
3824 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003825 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003826 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3827 }
3828 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3829 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3830 }
3831 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3832 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003833 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003834
Dave Allison20dfc792014-06-16 20:44:29 -07003835 DECLARE_INSTRUCTION(GreaterThan);
3836
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003837 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003838 return kCondGT;
3839 }
3840
Mark Mendellc4701932015-04-10 13:18:51 -04003841 IfCondition GetOppositeCondition() const OVERRIDE {
3842 return kCondLE;
3843 }
3844
Artem Serovcced8ba2017-07-19 18:18:09 +01003845 protected:
3846 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3847
Dave Allison20dfc792014-06-16 20:44:29 -07003848 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003849 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003850};
3851
Vladimir Markofcb503c2016-05-18 12:48:17 +01003852class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003853 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003854 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303855 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3856 }
Dave Allison20dfc792014-06-16 20:44:29 -07003857
Roland Levillain9867bc72015-08-05 10:21:34 +01003858 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003859 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003860 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 // In the following Evaluate methods, a HCompare instruction has
3862 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3863 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003864 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003865 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3866 }
3867 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3868 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3869 }
3870 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3871 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003872 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003873
Dave Allison20dfc792014-06-16 20:44:29 -07003874 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003876 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003877 return kCondGE;
3878 }
3879
Mark Mendellc4701932015-04-10 13:18:51 -04003880 IfCondition GetOppositeCondition() const OVERRIDE {
3881 return kCondLT;
3882 }
3883
Artem Serovcced8ba2017-07-19 18:18:09 +01003884 protected:
3885 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3886
Dave Allison20dfc792014-06-16 20:44:29 -07003887 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003888 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003889};
3890
Vladimir Markofcb503c2016-05-18 12:48:17 +01003891class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003892 public:
3893 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303894 : HCondition(kBelow, first, second, dex_pc) {
3895 }
Aart Bike9f37602015-10-09 11:15:55 -07003896
3897 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003898 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003899 }
3900 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003901 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3902 }
3903 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3904 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3905 LOG(FATAL) << DebugName() << " is not defined for float values";
3906 UNREACHABLE();
3907 }
3908 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3909 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3910 LOG(FATAL) << DebugName() << " is not defined for double values";
3911 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003912 }
3913
3914 DECLARE_INSTRUCTION(Below);
3915
3916 IfCondition GetCondition() const OVERRIDE {
3917 return kCondB;
3918 }
3919
3920 IfCondition GetOppositeCondition() const OVERRIDE {
3921 return kCondAE;
3922 }
3923
Artem Serovcced8ba2017-07-19 18:18:09 +01003924 protected:
3925 DEFAULT_COPY_CONSTRUCTOR(Below);
3926
Aart Bike9f37602015-10-09 11:15:55 -07003927 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003928 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003929 return MakeUnsigned(x) < MakeUnsigned(y);
3930 }
Aart Bike9f37602015-10-09 11:15:55 -07003931};
3932
Vladimir Markofcb503c2016-05-18 12:48:17 +01003933class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003934 public:
3935 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303936 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3937 }
Aart Bike9f37602015-10-09 11:15:55 -07003938
3939 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003940 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003941 }
3942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003943 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3944 }
3945 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3946 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3947 LOG(FATAL) << DebugName() << " is not defined for float values";
3948 UNREACHABLE();
3949 }
3950 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3951 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3952 LOG(FATAL) << DebugName() << " is not defined for double values";
3953 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003954 }
3955
3956 DECLARE_INSTRUCTION(BelowOrEqual);
3957
3958 IfCondition GetCondition() const OVERRIDE {
3959 return kCondBE;
3960 }
3961
3962 IfCondition GetOppositeCondition() const OVERRIDE {
3963 return kCondA;
3964 }
3965
Artem Serovcced8ba2017-07-19 18:18:09 +01003966 protected:
3967 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3968
Aart Bike9f37602015-10-09 11:15:55 -07003969 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003970 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003971 return MakeUnsigned(x) <= MakeUnsigned(y);
3972 }
Aart Bike9f37602015-10-09 11:15:55 -07003973};
3974
Vladimir Markofcb503c2016-05-18 12:48:17 +01003975class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003976 public:
3977 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303978 : HCondition(kAbove, first, second, dex_pc) {
3979 }
Aart Bike9f37602015-10-09 11:15:55 -07003980
3981 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003982 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003983 }
3984 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003985 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3986 }
3987 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3988 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3989 LOG(FATAL) << DebugName() << " is not defined for float values";
3990 UNREACHABLE();
3991 }
3992 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3993 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3994 LOG(FATAL) << DebugName() << " is not defined for double values";
3995 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003996 }
3997
3998 DECLARE_INSTRUCTION(Above);
3999
4000 IfCondition GetCondition() const OVERRIDE {
4001 return kCondA;
4002 }
4003
4004 IfCondition GetOppositeCondition() const OVERRIDE {
4005 return kCondBE;
4006 }
4007
Artem Serovcced8ba2017-07-19 18:18:09 +01004008 protected:
4009 DEFAULT_COPY_CONSTRUCTOR(Above);
4010
Aart Bike9f37602015-10-09 11:15:55 -07004011 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004012 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004013 return MakeUnsigned(x) > MakeUnsigned(y);
4014 }
Aart Bike9f37602015-10-09 11:15:55 -07004015};
4016
Vladimir Markofcb503c2016-05-18 12:48:17 +01004017class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004018 public:
4019 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304020 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4021 }
Aart Bike9f37602015-10-09 11:15:55 -07004022
4023 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004024 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004025 }
4026 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004027 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4028 }
4029 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4030 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4031 LOG(FATAL) << DebugName() << " is not defined for float values";
4032 UNREACHABLE();
4033 }
4034 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4035 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4036 LOG(FATAL) << DebugName() << " is not defined for double values";
4037 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004038 }
4039
4040 DECLARE_INSTRUCTION(AboveOrEqual);
4041
4042 IfCondition GetCondition() const OVERRIDE {
4043 return kCondAE;
4044 }
4045
4046 IfCondition GetOppositeCondition() const OVERRIDE {
4047 return kCondB;
4048 }
4049
Artem Serovcced8ba2017-07-19 18:18:09 +01004050 protected:
4051 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4052
Aart Bike9f37602015-10-09 11:15:55 -07004053 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004054 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004055 return MakeUnsigned(x) >= MakeUnsigned(y);
4056 }
Aart Bike9f37602015-10-09 11:15:55 -07004057};
Dave Allison20dfc792014-06-16 20:44:29 -07004058
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004059// Instruction to check how two inputs compare to each other.
4060// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004061class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004062 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004063 // Note that `comparison_type` is the type of comparison performed
4064 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004065 // HCompare instruction (which is always DataType::Type::kInt).
4066 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004067 HInstruction* first,
4068 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004069 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004070 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304071 : HBinaryOperation(kCompare,
4072 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004073 first,
4074 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004075 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004076 dex_pc) {
4077 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004078 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4079 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004080 }
4081
Roland Levillain9867bc72015-08-05 10:21:34 +01004082 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004083 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4084
4085 template <typename T>
4086 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004087 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004088 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4089 // Handle the bias.
4090 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4091 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004092
Roland Levillain9867bc72015-08-05 10:21:34 +01004093 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004094 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4095 // reach this code path when processing a freshly built HIR
4096 // graph. However HCompare integer instructions can be synthesized
4097 // by the instruction simplifier to implement IntegerCompare and
4098 // IntegerSignum intrinsics, so we have to handle this case.
4099 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004100 }
4101 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004102 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4103 }
4104 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4105 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4106 }
4107 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4108 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004109 }
4110
Vladimir Marko372f10e2016-05-17 16:30:10 +01004111 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004113 }
4114
Vladimir Markoa1de9182016-02-25 11:37:38 +00004115 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004116
Roland Levillain31dd3d62016-02-16 12:21:02 +00004117 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004118 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004119 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004120 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004121 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004122 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004124 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004125 // Comparisons do not require a runtime call in any back end.
4126 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004128
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004129 DECLARE_INSTRUCTION(Compare);
4130
Roland Levillain31dd3d62016-02-16 12:21:02 +00004131 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004132 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004133 static constexpr size_t kFieldComparisonBiasSize =
4134 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4135 static constexpr size_t kNumberOfComparePackedBits =
4136 kFieldComparisonBias + kFieldComparisonBiasSize;
4137 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4138 using ComparisonBiasField =
4139 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4140
Roland Levillain31dd3d62016-02-16 12:21:02 +00004141 // Return an integer constant containing the result of a comparison evaluated at compile time.
4142 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4143 DCHECK(value == -1 || value == 0 || value == 1) << value;
4144 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4145 }
4146
Artem Serovcced8ba2017-07-19 18:18:09 +01004147 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004148};
4149
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004150class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004151 public:
4152 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004153 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004154 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004155 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004156 bool finalizable,
4157 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304158 : HExpression(kNewInstance,
4159 DataType::Type::kReference,
4160 SideEffects::CanTriggerGC(),
4161 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004162 type_index_(type_index),
4163 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004164 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004165 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004166 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004167 }
4168
Artem Serovcced8ba2017-07-19 18:18:09 +01004169 bool IsClonable() const OVERRIDE { return true; }
4170
Andreas Gampea5b09a62016-11-17 15:21:22 -08004171 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004172 const DexFile& GetDexFile() const { return dex_file_; }
4173
4174 // Calls runtime so needs an environment.
4175 bool NeedsEnvironment() const OVERRIDE { return true; }
4176
Mingyao Yang062157f2016-03-02 10:15:36 -08004177 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4178 bool CanThrow() const OVERRIDE { return true; }
4179
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004180 bool NeedsChecks() const {
4181 return entrypoint_ == kQuickAllocObjectWithChecks;
4182 }
David Brazdil6de19382016-01-08 17:37:10 +00004183
Vladimir Markoa1de9182016-02-25 11:37:38 +00004184 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004185
4186 bool CanBeNull() const OVERRIDE { return false; }
4187
4188 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4189
4190 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4191 entrypoint_ = entrypoint;
4192 }
4193
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004194 HLoadClass* GetLoadClass() const {
4195 HInstruction* input = InputAt(0);
4196 if (input->IsClinitCheck()) {
4197 input = input->InputAt(0);
4198 }
4199 DCHECK(input->IsLoadClass());
4200 return input->AsLoadClass();
4201 }
4202
David Brazdil6de19382016-01-08 17:37:10 +00004203 bool IsStringAlloc() const;
4204
4205 DECLARE_INSTRUCTION(NewInstance);
4206
Artem Serovcced8ba2017-07-19 18:18:09 +01004207 protected:
4208 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4209
David Brazdil6de19382016-01-08 17:37:10 +00004210 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004211 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004212 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4213 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4214 "Too many packed fields.");
4215
Andreas Gampea5b09a62016-11-17 15:21:22 -08004216 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004217 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004218 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004219};
4220
Agi Csaki05f20562015-08-19 14:58:14 -07004221enum IntrinsicNeedsEnvironmentOrCache {
4222 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4223 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004224};
4225
Aart Bik5d75afe2015-12-14 11:57:01 -08004226enum IntrinsicSideEffects {
4227 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4228 kReadSideEffects, // Intrinsic may read heap memory.
4229 kWriteSideEffects, // Intrinsic may write heap memory.
4230 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4231};
4232
4233enum IntrinsicExceptions {
4234 kNoThrow, // Intrinsic does not throw any exceptions.
4235 kCanThrow // Intrinsic may throw exceptions.
4236};
4237
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004238class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004239 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004240 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004241
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004242 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004243 SetRawInputAt(index, argument);
4244 }
4245
Roland Levillain3e3d7332015-04-28 11:00:54 +01004246 // Return the number of arguments. This number can be lower than
4247 // the number of inputs returned by InputCount(), as some invoke
4248 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4249 // inputs at the end of their list of inputs.
4250 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4251
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004252 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4253
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004254 InvokeType GetInvokeType() const {
4255 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004256 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004257
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004258 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004259 return intrinsic_;
4260 }
4261
Aart Bik5d75afe2015-12-14 11:57:01 -08004262 void SetIntrinsic(Intrinsics intrinsic,
4263 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4264 IntrinsicSideEffects side_effects,
4265 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004266
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004267 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004268 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004269 }
4270
Aart Bikff7d89c2016-11-07 08:49:28 -08004271 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4272
Vladimir Markoa1de9182016-02-25 11:37:38 +00004273 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004274
Aart Bika8b8e9b2018-01-09 11:01:02 -08004275 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4276
4277 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4278
Aart Bik71bf7b42016-11-16 10:17:46 -08004279 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004280
Vladimir Marko372f10e2016-05-17 16:30:10 +01004281 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004282 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4283 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004284
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004285 uint32_t* GetIntrinsicOptimizations() {
4286 return &intrinsic_optimizations_;
4287 }
4288
4289 const uint32_t* GetIntrinsicOptimizations() const {
4290 return &intrinsic_optimizations_;
4291 }
4292
4293 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4294
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004295 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004296 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004297
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004298 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004299
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004300 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004301 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4302 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004303 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004304 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004305 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4306 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004307 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004308 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004309
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304310 HInvoke(InstructionKind kind,
4311 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004312 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004313 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004314 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004315 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004316 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004317 ArtMethod* resolved_method,
4318 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004319 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304320 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004321 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004322 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4323 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004324 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004325 number_of_arguments + number_of_other_inputs,
4326 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004327 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004328 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004329 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004330 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004331 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004332 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004333 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004334 }
4335
Artem Serovcced8ba2017-07-19 18:18:09 +01004336 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4337
Roland Levillain3e3d7332015-04-28 11:00:54 +01004338 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004339 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004340 const uint32_t dex_method_index_;
4341 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004342
4343 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4344 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004345};
4346
Vladimir Markofcb503c2016-05-18 12:48:17 +01004347class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004348 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004349 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004350 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004351 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004352 uint32_t dex_pc,
4353 uint32_t dex_method_index,
4354 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304355 : HInvoke(kInvokeUnresolved,
4356 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004357 number_of_arguments,
4358 0u /* number_of_other_inputs */,
4359 return_type,
4360 dex_pc,
4361 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004362 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004363 invoke_type) {
4364 }
4365
Artem Serovcced8ba2017-07-19 18:18:09 +01004366 bool IsClonable() const OVERRIDE { return true; }
4367
Calin Juravle175dc732015-08-25 15:42:32 +01004368 DECLARE_INSTRUCTION(InvokeUnresolved);
4369
Artem Serovcced8ba2017-07-19 18:18:09 +01004370 protected:
4371 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004372};
4373
Orion Hodsonac141392017-01-13 11:53:47 +00004374class HInvokePolymorphic FINAL : public HInvoke {
4375 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004376 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004377 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004378 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004379 uint32_t dex_pc,
4380 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304381 : HInvoke(kInvokePolymorphic,
4382 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004383 number_of_arguments,
4384 0u /* number_of_other_inputs */,
4385 return_type,
4386 dex_pc,
4387 dex_method_index,
4388 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304389 kVirtual) {
4390 }
Orion Hodsonac141392017-01-13 11:53:47 +00004391
Artem Serovcced8ba2017-07-19 18:18:09 +01004392 bool IsClonable() const OVERRIDE { return true; }
4393
Orion Hodsonac141392017-01-13 11:53:47 +00004394 DECLARE_INSTRUCTION(InvokePolymorphic);
4395
Artem Serovcced8ba2017-07-19 18:18:09 +01004396 protected:
4397 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004398};
4399
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004400class HInvokeCustom FINAL : public HInvoke {
4401 public:
4402 HInvokeCustom(ArenaAllocator* allocator,
4403 uint32_t number_of_arguments,
4404 uint32_t call_site_index,
4405 DataType::Type return_type,
4406 uint32_t dex_pc)
4407 : HInvoke(kInvokeCustom,
4408 allocator,
4409 number_of_arguments,
4410 /* number_of_other_inputs */ 0u,
4411 return_type,
4412 dex_pc,
4413 /* dex_method_index */ dex::kDexNoIndex,
4414 /* resolved_method */ nullptr,
4415 kStatic),
4416 call_site_index_(call_site_index) {
4417 }
4418
4419 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4420
4421 bool IsClonable() const OVERRIDE { return true; }
4422
4423 DECLARE_INSTRUCTION(InvokeCustom);
4424
4425 protected:
4426 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4427
4428 private:
4429 uint32_t call_site_index_;
4430};
4431
Vladimir Markofcb503c2016-05-18 12:48:17 +01004432class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004433 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004434 // Requirements of this method call regarding the class
4435 // initialization (clinit) check of its declaring class.
4436 enum class ClinitCheckRequirement {
4437 kNone, // Class already initialized.
4438 kExplicit, // Static call having explicit clinit check as last input.
4439 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004440 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004441 };
4442
Vladimir Marko58155012015-08-19 12:49:41 +00004443 // Determines how to load the target ArtMethod*.
4444 enum class MethodLoadKind {
4445 // Use a String init ArtMethod* loaded from Thread entrypoints.
4446 kStringInit,
4447
4448 // Use the method's own ArtMethod* loaded by the register allocator.
4449 kRecursive,
4450
Vladimir Marko65979462017-05-19 17:25:12 +01004451 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4452 // Used for boot image methods referenced by boot image code.
4453 kBootImageLinkTimePcRelative,
4454
Vladimir Markob066d432018-01-03 13:14:37 +00004455 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4456 // Used for app->boot calls with relocatable image.
4457 kBootImageRelRo,
4458
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004459 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004460 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004461 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004462
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004463 // Use ArtMethod* at a known address, embed the direct address in the code.
4464 // Used for for JIT-compiled calls.
4465 kJitDirectAddress,
4466
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004467 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4468 // used when other kinds are unimplemented on a particular architecture.
4469 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004470 };
4471
4472 // Determines the location of the code pointer.
4473 enum class CodePtrLocation {
4474 // Recursive call, use local PC-relative call instruction.
4475 kCallSelf,
4476
Vladimir Marko58155012015-08-19 12:49:41 +00004477 // Use code pointer from the ArtMethod*.
4478 // Used when we don't know the target code. This is also the last-resort-kind used when
4479 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4480 kCallArtMethod,
4481 };
4482
4483 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004484 MethodLoadKind method_load_kind;
4485 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004486 // The method load data holds
4487 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4488 // Note that there are multiple string init methods, each having its own offset.
4489 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004490 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004491 };
4492
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004493 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004494 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004495 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004496 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004497 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004498 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004499 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004500 InvokeType invoke_type,
4501 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004502 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304503 : HInvoke(kInvokeStaticOrDirect,
4504 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004505 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004506 // There is potentially one extra argument for the HCurrentMethod node, and
4507 // potentially one other if the clinit check is explicit, and potentially
4508 // one other if the method is a string factory.
4509 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004510 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004511 return_type,
4512 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004513 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004514 resolved_method,
4515 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004516 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004517 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004518 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4519 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004520
Artem Serovcced8ba2017-07-19 18:18:09 +01004521 bool IsClonable() const OVERRIDE { return true; }
4522
Vladimir Markodc151b22015-10-15 18:02:30 +01004523 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004524 bool had_current_method_input = HasCurrentMethodInput();
4525 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4526
4527 // Using the current method is the default and once we find a better
4528 // method load kind, we should not go back to using the current method.
4529 DCHECK(had_current_method_input || !needs_current_method_input);
4530
4531 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004532 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4533 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004534 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004535 dispatch_info_ = dispatch_info;
4536 }
4537
Aart Bik6daebeb2017-04-03 14:35:41 -07004538 DispatchInfo GetDispatchInfo() const {
4539 return dispatch_info_;
4540 }
4541
Vladimir Markoc53c0792015-11-19 15:48:33 +00004542 void AddSpecialInput(HInstruction* input) {
4543 // We allow only one special input.
4544 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4545 DCHECK(InputCount() == GetSpecialInputIndex() ||
4546 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4547 InsertInputAt(GetSpecialInputIndex(), input);
4548 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004549
Vladimir Marko372f10e2016-05-17 16:30:10 +01004550 using HInstruction::GetInputRecords; // Keep the const version visible.
4551 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4552 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4553 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4554 DCHECK(!input_records.empty());
4555 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4556 HInstruction* last_input = input_records.back().GetInstruction();
4557 // Note: `last_input` may be null during arguments setup.
4558 if (last_input != nullptr) {
4559 // `last_input` is the last input of a static invoke marked as having
4560 // an explicit clinit check. It must either be:
4561 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4562 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4563 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4564 }
4565 }
4566 return input_records;
4567 }
4568
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004569 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004570 // We access the method via the dex cache so we can't do an implicit null check.
4571 // TODO: for intrinsics we can generate implicit null checks.
4572 return false;
4573 }
4574
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004575 bool CanBeNull() const OVERRIDE {
Vladimir Markobd785672018-05-03 17:09:09 +01004576 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004577 }
4578
Vladimir Markoc53c0792015-11-19 15:48:33 +00004579 // Get the index of the special input, if any.
4580 //
David Brazdil6de19382016-01-08 17:37:10 +00004581 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4582 // method pointer; otherwise there may be one platform-specific special input,
4583 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004584 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004585 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004586
Vladimir Marko58155012015-08-19 12:49:41 +00004587 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4588 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4589 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004590 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004591 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004592 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004593 bool HasPcRelativeMethodLoadKind() const {
4594 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004595 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004596 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004597 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004598 bool HasCurrentMethodInput() const {
4599 // This function can be called only after the invoke has been fully initialized by the builder.
4600 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004601 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004602 return true;
4603 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004604 DCHECK(InputCount() == GetSpecialInputIndex() ||
4605 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004606 return false;
4607 }
4608 }
Vladimir Marko58155012015-08-19 12:49:41 +00004609
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004610 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004611 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004612 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004613 }
4614
4615 uint64_t GetMethodAddress() const {
4616 DCHECK(HasMethodAddress());
4617 return dispatch_info_.method_load_data;
4618 }
4619
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004620 const DexFile& GetDexFileForPcRelativeDexCache() const;
4621
Vladimir Markoa1de9182016-02-25 11:37:38 +00004622 ClinitCheckRequirement GetClinitCheckRequirement() const {
4623 return GetPackedField<ClinitCheckRequirementField>();
4624 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004625
Roland Levillain4c0eb422015-04-24 16:43:49 +01004626 // Is this instruction a call to a static method?
4627 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004628 return GetInvokeType() == kStatic;
4629 }
4630
4631 MethodReference GetTargetMethod() const {
4632 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004633 }
4634
Vladimir Markofbb184a2015-11-13 14:47:00 +00004635 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4636 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4637 // instruction; only relevant for static calls with explicit clinit check.
4638 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004639 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004640 size_t last_input_index = inputs_.size() - 1u;
4641 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004642 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004643 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004644 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004645 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004646 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004647 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004648 }
4649
4650 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004651 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004652 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004653 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004654 }
4655
4656 // Is this a call to a static method whose declaring class has an
4657 // implicit intialization check requirement?
4658 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004659 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004660 }
4661
Vladimir Markob554b5a2015-11-06 12:57:55 +00004662 // Does this method load kind need the current method as an input?
4663 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004664 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004665 }
4666
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004667 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004668
Artem Serovcced8ba2017-07-19 18:18:09 +01004669 protected:
4670 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4671
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004672 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004673 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004674 static constexpr size_t kFieldClinitCheckRequirementSize =
4675 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4676 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4677 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4678 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4679 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004680 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4681 kFieldClinitCheckRequirement,
4682 kFieldClinitCheckRequirementSize>;
4683
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004684 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004685 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004686 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004687};
Vladimir Markof64242a2015-12-01 14:58:23 +00004688std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004689std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004690
Vladimir Markofcb503c2016-05-18 12:48:17 +01004691class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004692 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004693 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004694 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004695 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004696 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004697 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004698 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004699 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304700 : HInvoke(kInvokeVirtual,
4701 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004702 number_of_arguments,
4703 0u,
4704 return_type,
4705 dex_pc,
4706 dex_method_index,
4707 resolved_method,
4708 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304709 vtable_index_(vtable_index) {
4710 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004711
Artem Serovcced8ba2017-07-19 18:18:09 +01004712 bool IsClonable() const OVERRIDE { return true; }
4713
Aart Bik71bf7b42016-11-16 10:17:46 -08004714 bool CanBeNull() const OVERRIDE {
4715 switch (GetIntrinsic()) {
4716 case Intrinsics::kThreadCurrentThread:
4717 case Intrinsics::kStringBufferAppend:
4718 case Intrinsics::kStringBufferToString:
4719 case Intrinsics::kStringBuilderAppend:
4720 case Intrinsics::kStringBuilderToString:
4721 return false;
4722 default:
4723 return HInvoke::CanBeNull();
4724 }
4725 }
4726
Calin Juravle641547a2015-04-21 22:08:51 +01004727 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004728 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004729 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004730 }
4731
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004732 uint32_t GetVTableIndex() const { return vtable_index_; }
4733
4734 DECLARE_INSTRUCTION(InvokeVirtual);
4735
Artem Serovcced8ba2017-07-19 18:18:09 +01004736 protected:
4737 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4738
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004739 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004740 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004741 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004742};
4743
Vladimir Markofcb503c2016-05-18 12:48:17 +01004744class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004745 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004746 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004747 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004748 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004749 uint32_t dex_pc,
4750 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004751 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004752 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304753 : HInvoke(kInvokeInterface,
4754 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004755 number_of_arguments,
4756 0u,
4757 return_type,
4758 dex_pc,
4759 dex_method_index,
4760 resolved_method,
4761 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304762 imt_index_(imt_index) {
4763 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004764
Artem Serovcced8ba2017-07-19 18:18:09 +01004765 bool IsClonable() const OVERRIDE { return true; }
4766
Calin Juravle641547a2015-04-21 22:08:51 +01004767 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004768 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004769 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004770 }
4771
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004772 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4773 // The assembly stub currently needs it.
4774 return true;
4775 }
4776
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004777 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004778
4779 DECLARE_INSTRUCTION(InvokeInterface);
4780
Artem Serovcced8ba2017-07-19 18:18:09 +01004781 protected:
4782 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4783
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004784 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004785 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004786 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004787};
4788
Vladimir Markofcb503c2016-05-18 12:48:17 +01004789class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004790 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004791 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304792 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004793 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004794 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004795
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004796 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004797
4798 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004800 }
4801 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004803 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004804 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4805 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4806 }
4807 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4808 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4809 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004810
Roland Levillain88cb1752014-10-20 16:36:47 +01004811 DECLARE_INSTRUCTION(Neg);
4812
Artem Serovcced8ba2017-07-19 18:18:09 +01004813 protected:
4814 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004815};
4816
Vladimir Markofcb503c2016-05-18 12:48:17 +01004817class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004818 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004819 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304820 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004821 SetRawInputAt(0, cls);
4822 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004823 }
4824
Artem Serovcced8ba2017-07-19 18:18:09 +01004825 bool IsClonable() const OVERRIDE { return true; }
4826
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004827 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004828 bool NeedsEnvironment() const OVERRIDE { return true; }
4829
Mingyao Yang0c365e62015-03-31 15:09:29 -07004830 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4831 bool CanThrow() const OVERRIDE { return true; }
4832
Calin Juravle10e244f2015-01-26 18:54:32 +00004833 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004834
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004835 HLoadClass* GetLoadClass() const {
4836 DCHECK(InputAt(0)->IsLoadClass());
4837 return InputAt(0)->AsLoadClass();
4838 }
4839
4840 HInstruction* GetLength() const {
4841 return InputAt(1);
4842 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004843
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004844 DECLARE_INSTRUCTION(NewArray);
4845
Artem Serovcced8ba2017-07-19 18:18:09 +01004846 protected:
4847 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004848};
4849
Vladimir Markofcb503c2016-05-18 12:48:17 +01004850class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004851 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004853 HInstruction* left,
4854 HInstruction* right,
4855 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304856 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4857 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004858
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004859 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004860
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004861 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004862
4863 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004864 return GetBlock()->GetGraph()->GetIntConstant(
4865 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004866 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004867 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004868 return GetBlock()->GetGraph()->GetLongConstant(
4869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004870 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004871 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4872 return GetBlock()->GetGraph()->GetFloatConstant(
4873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4874 }
4875 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4876 return GetBlock()->GetGraph()->GetDoubleConstant(
4877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4878 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004879
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004880 DECLARE_INSTRUCTION(Add);
4881
Artem Serovcced8ba2017-07-19 18:18:09 +01004882 protected:
4883 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004884};
4885
Vladimir Markofcb503c2016-05-18 12:48:17 +01004886class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004887 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004888 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004889 HInstruction* left,
4890 HInstruction* right,
4891 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304892 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4893 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004894
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004895 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004896
4897 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004898 return GetBlock()->GetGraph()->GetIntConstant(
4899 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004900 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004901 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 return GetBlock()->GetGraph()->GetLongConstant(
4903 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004904 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004905 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4906 return GetBlock()->GetGraph()->GetFloatConstant(
4907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4908 }
4909 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4910 return GetBlock()->GetGraph()->GetDoubleConstant(
4911 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4912 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004913
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004914 DECLARE_INSTRUCTION(Sub);
4915
Artem Serovcced8ba2017-07-19 18:18:09 +01004916 protected:
4917 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004918};
4919
Vladimir Markofcb503c2016-05-18 12:48:17 +01004920class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004921 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004922 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004923 HInstruction* left,
4924 HInstruction* right,
4925 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304926 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4927 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004928
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004929 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004930
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004931 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004932
4933 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004934 return GetBlock()->GetGraph()->GetIntConstant(
4935 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004936 }
4937 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004938 return GetBlock()->GetGraph()->GetLongConstant(
4939 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004940 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004941 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4942 return GetBlock()->GetGraph()->GetFloatConstant(
4943 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4944 }
4945 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4946 return GetBlock()->GetGraph()->GetDoubleConstant(
4947 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4948 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004949
4950 DECLARE_INSTRUCTION(Mul);
4951
Artem Serovcced8ba2017-07-19 18:18:09 +01004952 protected:
4953 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004954};
4955
Vladimir Markofcb503c2016-05-18 12:48:17 +01004956class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004957 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004958 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004959 HInstruction* left,
4960 HInstruction* right,
4961 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304962 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4963 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004964
Roland Levillain9867bc72015-08-05 10:21:34 +01004965 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004966 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004967 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004968 // Our graph structure ensures we never have 0 for `y` during
4969 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004970 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004971 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004972 return (y == -1) ? -x : x / y;
4973 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004974
Roland Levillain31dd3d62016-02-16 12:21:02 +00004975 template <typename T>
4976 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004977 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004978 return x / y;
4979 }
4980
Roland Levillain9867bc72015-08-05 10:21:34 +01004981 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004982 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004983 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004984 }
4985 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004986 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004987 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4988 }
4989 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4990 return GetBlock()->GetGraph()->GetFloatConstant(
4991 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4992 }
4993 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4994 return GetBlock()->GetGraph()->GetDoubleConstant(
4995 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004996 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004997
4998 DECLARE_INSTRUCTION(Div);
4999
Artem Serovcced8ba2017-07-19 18:18:09 +01005000 protected:
5001 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005002};
5003
Vladimir Markofcb503c2016-05-18 12:48:17 +01005004class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005005 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005006 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005007 HInstruction* left,
5008 HInstruction* right,
5009 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305010 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5011 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005012
Roland Levillain9867bc72015-08-05 10:21:34 +01005013 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005014 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005015 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005016 // Our graph structure ensures we never have 0 for `y` during
5017 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005018 DCHECK_NE(y, 0);
5019 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5020 return (y == -1) ? 0 : x % y;
5021 }
5022
Roland Levillain31dd3d62016-02-16 12:21:02 +00005023 template <typename T>
5024 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005025 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005026 return std::fmod(x, y);
5027 }
5028
Roland Levillain9867bc72015-08-05 10:21:34 +01005029 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005030 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005031 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005032 }
5033 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005034 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005035 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005036 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005037 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
5038 return GetBlock()->GetGraph()->GetFloatConstant(
5039 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5040 }
5041 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
5042 return GetBlock()->GetGraph()->GetDoubleConstant(
5043 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5044 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005045
5046 DECLARE_INSTRUCTION(Rem);
5047
Artem Serovcced8ba2017-07-19 18:18:09 +01005048 protected:
5049 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005050};
5051
Aart Bik1f8d51b2018-02-15 10:42:37 -08005052class HMin FINAL : public HBinaryOperation {
5053 public:
5054 HMin(DataType::Type result_type,
5055 HInstruction* left,
5056 HInstruction* right,
5057 uint32_t dex_pc)
5058 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5059
5060 bool IsCommutative() const OVERRIDE { return true; }
5061
5062 // Evaluation for integral values.
5063 template <typename T> static T ComputeIntegral(T x, T y) {
5064 return (x <= y) ? x : y;
5065 }
5066
5067 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5068 return GetBlock()->GetGraph()->GetIntConstant(
5069 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5070 }
5071 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5072 return GetBlock()->GetGraph()->GetLongConstant(
5073 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5074 }
5075 // TODO: Evaluation for floating-point values.
5076 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5077 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5078 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5079 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5080
5081 DECLARE_INSTRUCTION(Min);
5082
5083 protected:
5084 DEFAULT_COPY_CONSTRUCTOR(Min);
5085};
5086
5087class HMax FINAL : public HBinaryOperation {
5088 public:
5089 HMax(DataType::Type result_type,
5090 HInstruction* left,
5091 HInstruction* right,
5092 uint32_t dex_pc)
5093 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5094
5095 bool IsCommutative() const OVERRIDE { return true; }
5096
5097 // Evaluation for integral values.
5098 template <typename T> static T ComputeIntegral(T x, T y) {
5099 return (x >= y) ? x : y;
5100 }
5101
5102 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5103 return GetBlock()->GetGraph()->GetIntConstant(
5104 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5105 }
5106 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5107 return GetBlock()->GetGraph()->GetLongConstant(
5108 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5109 }
5110 // TODO: Evaluation for floating-point values.
5111 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5112 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5113 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5114 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5115
5116 DECLARE_INSTRUCTION(Max);
5117
5118 protected:
5119 DEFAULT_COPY_CONSTRUCTOR(Max);
5120};
5121
Aart Bik3dad3412018-02-28 12:01:46 -08005122class HAbs FINAL : public HUnaryOperation {
5123 public:
5124 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5125 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5126
5127 // Evaluation for integral values.
5128 template <typename T> static T ComputeIntegral(T x) {
5129 return x < 0 ? -x : x;
5130 }
5131
5132 // Evaluation for floating-point values.
5133 // Note, as a "quality of implementation", rather than pure "spec compliance",
5134 // we require that Math.abs() clears the sign bit (but changes nothing else)
5135 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5136 // http://b/30758343
5137 template <typename T, typename S> static T ComputeFP(T x) {
5138 S bits = bit_cast<S, T>(x);
5139 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5140 }
5141
5142 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5143 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5144 }
5145 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5146 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5147 }
5148 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5149 return GetBlock()->GetGraph()->GetFloatConstant(
5150 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5151 }
5152 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5153 return GetBlock()->GetGraph()->GetDoubleConstant(
5154 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5155 }
5156
5157 DECLARE_INSTRUCTION(Abs);
5158
5159 protected:
5160 DEFAULT_COPY_CONSTRUCTOR(Abs);
5161};
5162
Vladimir Markofcb503c2016-05-18 12:48:17 +01005163class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005164 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005165 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5166 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005167 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305168 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005169 SetRawInputAt(0, value);
5170 }
5171
Artem Serova6e26142018-06-19 14:55:17 +01005172 bool IsClonable() const OVERRIDE { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005173 bool CanBeMoved() const OVERRIDE { return true; }
5174
Vladimir Marko372f10e2016-05-17 16:30:10 +01005175 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005176 return true;
5177 }
5178
5179 bool NeedsEnvironment() const OVERRIDE { return true; }
5180 bool CanThrow() const OVERRIDE { return true; }
5181
Calin Juravled0d48522014-11-04 16:40:20 +00005182 DECLARE_INSTRUCTION(DivZeroCheck);
5183
Artem Serovcced8ba2017-07-19 18:18:09 +01005184 protected:
5185 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005186};
5187
Vladimir Markofcb503c2016-05-18 12:48:17 +01005188class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005189 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005190 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005191 HInstruction* value,
5192 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005193 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305194 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005195 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5196 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005197 }
5198
Roland Levillain5b5b9312016-03-22 14:57:31 +00005199 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005200 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005201 return value << (distance & max_shift_distance);
5202 }
5203
5204 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005205 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005206 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005207 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005208 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005209 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005210 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005211 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005212 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5213 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5214 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5215 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005216 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005217 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5218 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005219 LOG(FATAL) << DebugName() << " is not defined for float values";
5220 UNREACHABLE();
5221 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005222 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5223 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005224 LOG(FATAL) << DebugName() << " is not defined for double values";
5225 UNREACHABLE();
5226 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005227
5228 DECLARE_INSTRUCTION(Shl);
5229
Artem Serovcced8ba2017-07-19 18:18:09 +01005230 protected:
5231 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005232};
5233
Vladimir Markofcb503c2016-05-18 12:48:17 +01005234class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005235 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005237 HInstruction* value,
5238 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005239 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305240 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005241 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5242 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005243 }
5244
Roland Levillain5b5b9312016-03-22 14:57:31 +00005245 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005246 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005247 return value >> (distance & max_shift_distance);
5248 }
5249
5250 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005251 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005252 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005253 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005254 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005255 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005256 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005257 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005258 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5259 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5260 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5261 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005262 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005263 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5264 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005265 LOG(FATAL) << DebugName() << " is not defined for float values";
5266 UNREACHABLE();
5267 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005268 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5269 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005270 LOG(FATAL) << DebugName() << " is not defined for double values";
5271 UNREACHABLE();
5272 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005273
5274 DECLARE_INSTRUCTION(Shr);
5275
Artem Serovcced8ba2017-07-19 18:18:09 +01005276 protected:
5277 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005278};
5279
Vladimir Markofcb503c2016-05-18 12:48:17 +01005280class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005281 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005282 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005283 HInstruction* value,
5284 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005285 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305286 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005287 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5288 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005289 }
5290
Roland Levillain5b5b9312016-03-22 14:57:31 +00005291 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005292 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005293 typedef typename std::make_unsigned<T>::type V;
5294 V ux = static_cast<V>(value);
5295 return static_cast<T>(ux >> (distance & max_shift_distance));
5296 }
5297
5298 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005299 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005300 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005301 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005302 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005303 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005304 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005305 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005306 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5307 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5308 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5309 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005310 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005311 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5312 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005313 LOG(FATAL) << DebugName() << " is not defined for float values";
5314 UNREACHABLE();
5315 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005316 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5317 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005318 LOG(FATAL) << DebugName() << " is not defined for double values";
5319 UNREACHABLE();
5320 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005321
5322 DECLARE_INSTRUCTION(UShr);
5323
Artem Serovcced8ba2017-07-19 18:18:09 +01005324 protected:
5325 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005326};
5327
Vladimir Markofcb503c2016-05-18 12:48:17 +01005328class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005329 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005330 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005331 HInstruction* left,
5332 HInstruction* right,
5333 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305334 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5335 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005336
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005337 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005338
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005339 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005340
5341 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005342 return GetBlock()->GetGraph()->GetIntConstant(
5343 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005344 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005345 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005346 return GetBlock()->GetGraph()->GetLongConstant(
5347 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005348 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005349 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5350 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5351 LOG(FATAL) << DebugName() << " is not defined for float values";
5352 UNREACHABLE();
5353 }
5354 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5355 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5356 LOG(FATAL) << DebugName() << " is not defined for double values";
5357 UNREACHABLE();
5358 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005359
5360 DECLARE_INSTRUCTION(And);
5361
Artem Serovcced8ba2017-07-19 18:18:09 +01005362 protected:
5363 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005364};
5365
Vladimir Markofcb503c2016-05-18 12:48:17 +01005366class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005367 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005368 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005369 HInstruction* left,
5370 HInstruction* right,
5371 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305372 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5373 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005374
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005375 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005376
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005377 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005378
5379 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005380 return GetBlock()->GetGraph()->GetIntConstant(
5381 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005382 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005383 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005384 return GetBlock()->GetGraph()->GetLongConstant(
5385 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005386 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005387 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5388 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5389 LOG(FATAL) << DebugName() << " is not defined for float values";
5390 UNREACHABLE();
5391 }
5392 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5393 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5394 LOG(FATAL) << DebugName() << " is not defined for double values";
5395 UNREACHABLE();
5396 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005397
5398 DECLARE_INSTRUCTION(Or);
5399
Artem Serovcced8ba2017-07-19 18:18:09 +01005400 protected:
5401 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005402};
5403
Vladimir Markofcb503c2016-05-18 12:48:17 +01005404class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005405 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005406 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005407 HInstruction* left,
5408 HInstruction* right,
5409 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305410 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5411 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005412
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005413 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005414
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005415 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005416
5417 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005418 return GetBlock()->GetGraph()->GetIntConstant(
5419 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005420 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005421 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005422 return GetBlock()->GetGraph()->GetLongConstant(
5423 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005424 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005425 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5426 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5427 LOG(FATAL) << DebugName() << " is not defined for float values";
5428 UNREACHABLE();
5429 }
5430 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5431 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5432 LOG(FATAL) << DebugName() << " is not defined for double values";
5433 UNREACHABLE();
5434 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005435
5436 DECLARE_INSTRUCTION(Xor);
5437
Artem Serovcced8ba2017-07-19 18:18:09 +01005438 protected:
5439 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005440};
5441
Vladimir Markofcb503c2016-05-18 12:48:17 +01005442class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005443 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005444 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305445 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005446 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5447 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005448 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005449
Roland Levillain5b5b9312016-03-22 14:57:31 +00005450 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005451 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005452 typedef typename std::make_unsigned<T>::type V;
5453 V ux = static_cast<V>(value);
5454 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005455 return static_cast<T>(ux);
5456 } else {
5457 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005458 return static_cast<T>(ux >> (distance & max_shift_value)) |
5459 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005460 }
5461 }
5462
Roland Levillain5b5b9312016-03-22 14:57:31 +00005463 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005464 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005465 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005466 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005467 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005468 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005469 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005470 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005471 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5472 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5473 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5474 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005475 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005476 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5477 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005478 LOG(FATAL) << DebugName() << " is not defined for float values";
5479 UNREACHABLE();
5480 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005481 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5482 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005483 LOG(FATAL) << DebugName() << " is not defined for double values";
5484 UNREACHABLE();
5485 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005486
5487 DECLARE_INSTRUCTION(Ror);
5488
Artem Serovcced8ba2017-07-19 18:18:09 +01005489 protected:
5490 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005491};
5492
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005493// The value of a parameter in this method. Its location depends on
5494// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005495class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005496 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005497 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005498 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005499 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005500 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005501 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305502 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005503 dex_file_(dex_file),
5504 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005505 index_(index) {
5506 SetPackedFlag<kFlagIsThis>(is_this);
5507 SetPackedFlag<kFlagCanBeNull>(!is_this);
5508 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005509
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005510 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005511 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005512 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005513 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005514
Vladimir Markoa1de9182016-02-25 11:37:38 +00005515 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5516 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005517
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005518 DECLARE_INSTRUCTION(ParameterValue);
5519
Artem Serovcced8ba2017-07-19 18:18:09 +01005520 protected:
5521 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5522
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005523 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005524 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005525 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005526 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5527 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5528 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5529 "Too many packed fields.");
5530
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005531 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005532 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005533 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005534 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005535 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005536};
5537
Vladimir Markofcb503c2016-05-18 12:48:17 +01005538class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005539 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005540 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305541 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5542 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005543
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005544 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005545 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005546 return true;
5547 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005548
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005549 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005550
5551 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005552 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005553 }
5554 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005555 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005556 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005557 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5558 LOG(FATAL) << DebugName() << " is not defined for float values";
5559 UNREACHABLE();
5560 }
5561 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5562 LOG(FATAL) << DebugName() << " is not defined for double values";
5563 UNREACHABLE();
5564 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005565
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005566 DECLARE_INSTRUCTION(Not);
5567
Artem Serovcced8ba2017-07-19 18:18:09 +01005568 protected:
5569 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005570};
5571
Vladimir Markofcb503c2016-05-18 12:48:17 +01005572class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005573 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005574 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305575 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5576 }
David Brazdil66d126e2015-04-03 16:02:44 +01005577
5578 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005579 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005580 return true;
5581 }
5582
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005583 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005584 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005585 return !x;
5586 }
5587
Roland Levillain9867bc72015-08-05 10:21:34 +01005588 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005589 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005590 }
5591 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5592 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005593 UNREACHABLE();
5594 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005595 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5596 LOG(FATAL) << DebugName() << " is not defined for float values";
5597 UNREACHABLE();
5598 }
5599 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5600 LOG(FATAL) << DebugName() << " is not defined for double values";
5601 UNREACHABLE();
5602 }
David Brazdil66d126e2015-04-03 16:02:44 +01005603
5604 DECLARE_INSTRUCTION(BooleanNot);
5605
Artem Serovcced8ba2017-07-19 18:18:09 +01005606 protected:
5607 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005608};
5609
Vladimir Markofcb503c2016-05-18 12:48:17 +01005610class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005611 public:
5612 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005613 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305614 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005615 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005616 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005617 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005618 }
5619
5620 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5622 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005623
Artem Serova6e26142018-06-19 14:55:17 +01005624 bool IsClonable() const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00005625 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005626 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5627 return true;
5628 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005629
Mark Mendelle82549b2015-05-06 10:55:34 -04005630 // Try to statically evaluate the conversion and return a HConstant
5631 // containing the result. If the input cannot be converted, return nullptr.
5632 HConstant* TryStaticEvaluation() const;
5633
Roland Levillaindff1f282014-11-05 14:15:05 +00005634 DECLARE_INSTRUCTION(TypeConversion);
5635
Artem Serovcced8ba2017-07-19 18:18:09 +01005636 protected:
5637 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005638};
5639
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005640static constexpr uint32_t kNoRegNumber = -1;
5641
Vladimir Markofcb503c2016-05-18 12:48:17 +01005642class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005643 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005644 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5645 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005646 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305647 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005648 SetRawInputAt(0, value);
5649 }
5650
Artem Serovcced8ba2017-07-19 18:18:09 +01005651 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005652 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005653 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005654 return true;
5655 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005656
Calin Juravle10e244f2015-01-26 18:54:32 +00005657 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005658
Calin Juravle10e244f2015-01-26 18:54:32 +00005659 bool CanThrow() const OVERRIDE { return true; }
5660
5661 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005662
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005663 DECLARE_INSTRUCTION(NullCheck);
5664
Artem Serovcced8ba2017-07-19 18:18:09 +01005665 protected:
5666 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005667};
5668
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005669// Embeds an ArtField and all the information required by the compiler. We cache
5670// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005671class FieldInfo : public ValueObject {
5672 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005673 FieldInfo(ArtField* field,
5674 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005675 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005676 bool is_volatile,
5677 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005678 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005679 const DexFile& dex_file)
5680 : field_(field),
5681 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005682 field_type_(field_type),
5683 is_volatile_(is_volatile),
5684 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005685 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005686 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005687
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005688 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005689 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005690 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005691 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005692 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005693 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005694 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005695
5696 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005697 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005698 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005699 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005700 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005701 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005702 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005703 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005704};
5705
Vladimir Markofcb503c2016-05-18 12:48:17 +01005706class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005707 public:
5708 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005709 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005710 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005711 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005712 bool is_volatile,
5713 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005714 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005715 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005716 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305717 : HExpression(kInstanceFieldGet,
5718 field_type,
5719 SideEffects::FieldReadOfType(field_type, is_volatile),
5720 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005721 field_info_(field,
5722 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005723 field_type,
5724 is_volatile,
5725 field_idx,
5726 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005727 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005728 SetRawInputAt(0, value);
5729 }
5730
Artem Serovcced8ba2017-07-19 18:18:09 +01005731 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005732 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005733
Vladimir Marko372f10e2016-05-17 16:30:10 +01005734 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5735 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005736 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005737 }
5738
Calin Juravle641547a2015-04-21 22:08:51 +01005739 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005740 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005741 }
5742
5743 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005744 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5745 }
5746
Calin Juravle52c48962014-12-16 17:02:57 +00005747 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005748 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005749 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005750 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005751
Vladimir Marko61b92282017-10-11 13:23:17 +01005752 void SetType(DataType::Type new_type) {
5753 DCHECK(DataType::IsIntegralType(GetType()));
5754 DCHECK(DataType::IsIntegralType(new_type));
5755 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5756 SetPackedField<TypeField>(new_type);
5757 }
5758
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005759 DECLARE_INSTRUCTION(InstanceFieldGet);
5760
Artem Serovcced8ba2017-07-19 18:18:09 +01005761 protected:
5762 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5763
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005764 private:
5765 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005766};
5767
Vladimir Markobd785672018-05-03 17:09:09 +01005768class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005769 public:
5770 HInstanceFieldSet(HInstruction* object,
5771 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005772 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005774 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005775 bool is_volatile,
5776 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005777 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005778 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005779 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005780 : HExpression(kInstanceFieldSet,
5781 SideEffects::FieldWriteOfType(field_type, is_volatile),
5782 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005783 field_info_(field,
5784 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005785 field_type,
5786 is_volatile,
5787 field_idx,
5788 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005789 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005790 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005791 SetRawInputAt(0, object);
5792 SetRawInputAt(1, value);
5793 }
5794
Artem Serovcced8ba2017-07-19 18:18:09 +01005795 bool IsClonable() const OVERRIDE { return true; }
5796
Calin Juravle641547a2015-04-21 22:08:51 +01005797 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005798 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005799 }
5800
Calin Juravle52c48962014-12-16 17:02:57 +00005801 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005802 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005803 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005804 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005805 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005806 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5807 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005808
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005809 DECLARE_INSTRUCTION(InstanceFieldSet);
5810
Artem Serovcced8ba2017-07-19 18:18:09 +01005811 protected:
5812 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5813
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005814 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005815 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5816 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5817 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5818 "Too many packed fields.");
5819
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005820 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005821};
5822
Vladimir Markofcb503c2016-05-18 12:48:17 +01005823class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005824 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005825 HArrayGet(HInstruction* array,
5826 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005827 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005828 uint32_t dex_pc)
5829 : HArrayGet(array,
5830 index,
5831 type,
5832 SideEffects::ArrayReadOfType(type),
5833 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305834 /* is_string_char_at */ false) {
5835 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005836
5837 HArrayGet(HInstruction* array,
5838 HInstruction* index,
5839 DataType::Type type,
5840 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005841 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005842 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305843 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005844 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005845 SetRawInputAt(0, array);
5846 SetRawInputAt(1, index);
5847 }
5848
Artem Serovcced8ba2017-07-19 18:18:09 +01005849 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005850 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005851 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005852 return true;
5853 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005854 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005855 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005856 // Currently, unless the array is the result of NewArray, the array access is always
5857 // preceded by some form of null NullCheck necessary for the bounds check, usually
5858 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5859 // dynamic BCE. There are cases when these could be removed to produce better code.
5860 // If we ever add optimizations to do so we should allow an implicit check here
5861 // (as long as the address falls in the first page).
5862 //
5863 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5864 // a = cond ? new int[1] : null;
5865 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005866 return false;
5867 }
5868
David Brazdil4833f5a2015-12-16 10:37:39 +00005869 bool IsEquivalentOf(HArrayGet* other) const {
5870 bool result = (GetDexPc() == other->GetDexPc());
5871 if (kIsDebugBuild && result) {
5872 DCHECK_EQ(GetBlock(), other->GetBlock());
5873 DCHECK_EQ(GetArray(), other->GetArray());
5874 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005875 if (DataType::IsIntOrLongType(GetType())) {
5876 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005877 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005878 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5879 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005880 }
5881 }
5882 return result;
5883 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005884
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005885 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5886
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005887 HInstruction* GetArray() const { return InputAt(0); }
5888 HInstruction* GetIndex() const { return InputAt(1); }
5889
Vladimir Marko61b92282017-10-11 13:23:17 +01005890 void SetType(DataType::Type new_type) {
5891 DCHECK(DataType::IsIntegralType(GetType()));
5892 DCHECK(DataType::IsIntegralType(new_type));
5893 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5894 SetPackedField<TypeField>(new_type);
5895 }
5896
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005897 DECLARE_INSTRUCTION(ArrayGet);
5898
Artem Serovcced8ba2017-07-19 18:18:09 +01005899 protected:
5900 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5901
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005902 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005903 // We treat a String as an array, creating the HArrayGet from String.charAt()
5904 // intrinsic in the instruction simplifier. We can always determine whether
5905 // a particular HArrayGet is actually a String.charAt() by looking at the type
5906 // of the input but that requires holding the mutator lock, so we prefer to use
5907 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005908 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005909 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5910 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5911 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005912};
5913
Vladimir Markobd785672018-05-03 17:09:09 +01005914class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005915 public:
5916 HArraySet(HInstruction* array,
5917 HInstruction* index,
5918 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005919 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005920 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005921 : HArraySet(array,
5922 index,
5923 value,
5924 expected_component_type,
5925 // Make a best guess for side effects now, may be refined during SSA building.
5926 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305927 dex_pc) {
5928 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005929
5930 HArraySet(HInstruction* array,
5931 HInstruction* index,
5932 HInstruction* value,
5933 DataType::Type expected_component_type,
5934 SideEffects side_effects,
5935 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005936 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005937 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005938 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005939 SetPackedFlag<kFlagValueCanBeNull>(true);
5940 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005941 SetRawInputAt(0, array);
5942 SetRawInputAt(1, index);
5943 SetRawInputAt(2, value);
5944 }
5945
Artem Serovcced8ba2017-07-19 18:18:09 +01005946 bool IsClonable() const OVERRIDE { return true; }
5947
Calin Juravle77520bc2015-01-12 18:45:46 +00005948 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005949 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005950 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005951 }
5952
Mingyao Yang81014cb2015-06-02 03:16:27 -07005953 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005954 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005955
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005956 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005957 // TODO: Same as for ArrayGet.
5958 return false;
5959 }
5960
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005961 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005962 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005963 }
5964
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005965 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005966 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005967 }
5968
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005969 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005970 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005971 }
5972
Vladimir Markoa1de9182016-02-25 11:37:38 +00005973 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5974 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5975 bool StaticTypeOfArrayIsObjectArray() const {
5976 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5977 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005978
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005979 HInstruction* GetArray() const { return InputAt(0); }
5980 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005981 HInstruction* GetValue() const { return InputAt(2); }
5982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005983 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005984 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5985 }
5986
5987 static DataType::Type GetComponentType(DataType::Type value_type,
5988 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005989 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005990 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005991 // be correct, we also check what is the value type. If it is a floating
5992 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005993 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005994 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005995 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005996 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005997
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005998 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005999 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006000 }
6001
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006002 static SideEffects ComputeSideEffects(DataType::Type type) {
6003 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006004 }
6005
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006006 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6007 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6008 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006009 }
6010
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006011 DECLARE_INSTRUCTION(ArraySet);
6012
Artem Serovcced8ba2017-07-19 18:18:09 +01006013 protected:
6014 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6015
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006016 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006017 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6018 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006019 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006020 static constexpr size_t kFlagNeedsTypeCheck =
6021 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6022 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006023 // Cached information for the reference_type_info_ so that codegen
6024 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6026 static constexpr size_t kNumberOfArraySetPackedBits =
6027 kFlagStaticTypeOfArrayIsObjectArray + 1;
6028 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6029 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006030 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006031};
6032
Vladimir Markofcb503c2016-05-18 12:48:17 +01006033class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006034 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006035 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306036 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006037 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006038 // Note that arrays do not change length, so the instruction does not
6039 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006040 SetRawInputAt(0, array);
6041 }
6042
Artem Serovcced8ba2017-07-19 18:18:09 +01006043 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00006044 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006045 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006046 return true;
6047 }
Calin Juravle641547a2015-04-21 22:08:51 +01006048 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
6049 return obj == InputAt(0);
6050 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006051
Vladimir Markodce016e2016-04-28 13:10:02 +01006052 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6053
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006054 DECLARE_INSTRUCTION(ArrayLength);
6055
Artem Serovcced8ba2017-07-19 18:18:09 +01006056 protected:
6057 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6058
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006059 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006060 // We treat a String as an array, creating the HArrayLength from String.length()
6061 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6062 // determine whether a particular HArrayLength is actually a String.length() by
6063 // looking at the type of the input but that requires holding the mutator lock, so
6064 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006065 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006066 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6067 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6068 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006069};
6070
Vladimir Markofcb503c2016-05-18 12:48:17 +01006071class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006072 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006073 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6074 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006075 HBoundsCheck(HInstruction* index,
6076 HInstruction* length,
6077 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006078 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306079 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006080 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006081 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006082 SetRawInputAt(0, index);
6083 SetRawInputAt(1, length);
6084 }
6085
Artem Serovcced8ba2017-07-19 18:18:09 +01006086 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006087 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006088 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006089 return true;
6090 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006091
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006092 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006093
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006094 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006095
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006096 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006097
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006098 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006099
6100 DECLARE_INSTRUCTION(BoundsCheck);
6101
Artem Serovcced8ba2017-07-19 18:18:09 +01006102 protected:
6103 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6104
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006105 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006106 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006107};
6108
Vladimir Markobd785672018-05-03 17:09:09 +01006109class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006110 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006111 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006112 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306113 slow_path_(nullptr) {
6114 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006115
Artem Serovcced8ba2017-07-19 18:18:09 +01006116 bool IsClonable() const OVERRIDE { return true; }
6117
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006118 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006119 return true;
6120 }
6121
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006122 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6123 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006124
6125 DECLARE_INSTRUCTION(SuspendCheck);
6126
Artem Serovcced8ba2017-07-19 18:18:09 +01006127 protected:
6128 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6129
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006130 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006131 // Only used for code generation, in order to share the same slow path between back edges
6132 // of a same loop.
6133 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006134};
6135
David Srbecky0cf44932015-12-09 14:09:59 +00006136// Pseudo-instruction which provides the native debugger with mapping information.
6137// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006138class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006139 public:
6140 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006141 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306142 }
David Srbecky0cf44932015-12-09 14:09:59 +00006143
6144 bool NeedsEnvironment() const OVERRIDE {
6145 return true;
6146 }
6147
6148 DECLARE_INSTRUCTION(NativeDebugInfo);
6149
Artem Serovcced8ba2017-07-19 18:18:09 +01006150 protected:
6151 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006152};
6153
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006154/**
6155 * Instruction to load a Class object.
6156 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006157class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006158 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006159 // Determines how to load the Class.
6160 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006161 // We cannot load this class. See HSharpening::SharpenLoadClass.
6162 kInvalid = -1,
6163
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006164 // Use the Class* from the method's own ArtMethod*.
6165 kReferrersClass,
6166
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006167 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006168 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006169 kBootImageLinkTimePcRelative,
6170
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006171 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006172 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006173 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006174
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006175 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006176 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006177 kBssEntry,
6178
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006179 // Use a known boot image Class* address, embedded in the code by the codegen.
6180 // Used for boot image classes referenced by apps in JIT-compiled code.
6181 kJitBootImageAddress,
6182
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006183 // Load from the root table associated with the JIT compiled method.
6184 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006185
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006186 // Load using a simple runtime call. This is the fall-back load kind when
6187 // the codegen is unable to use another appropriate kind.
6188 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006189
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006190 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006191 };
6192
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006193 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006194 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006195 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006196 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006197 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006198 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006199 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006200 : HInstruction(kLoadClass,
6201 DataType::Type::kReference,
6202 SideEffectsForArchRuntimeCalls(),
6203 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006204 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006205 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006206 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006207 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006208 // Referrers class should not need access check. We never inline unverified
6209 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006210 DCHECK(!is_referrers_class || !needs_access_check);
6211
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006212 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006213 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006214 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006215 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006216 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006217 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006218 }
6219
Artem Serovcced8ba2017-07-19 18:18:09 +01006220 bool IsClonable() const OVERRIDE { return true; }
6221
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006222 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006223
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006224 LoadKind GetLoadKind() const {
6225 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006226 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006227
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006228 bool HasPcRelativeLoadKind() const {
6229 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6230 GetLoadKind() == LoadKind::kBootImageRelRo ||
6231 GetLoadKind() == LoadKind::kBssEntry;
6232 }
6233
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006234 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.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006264 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006265 GetLoadKind() == LoadKind::kBssEntry) &&
6266 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006267 }
6268
Calin Juravleacf735c2015-02-12 15:25:22 +00006269 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006270 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6271 // Note: The is_exact flag from the return value should not be used.
6272 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6273 } else {
6274 return ReferenceTypeInfo::CreateInvalid();
6275 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006276 }
6277
Vladimir Marko175e7862018-03-27 09:03:13 +00006278 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6279 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6280 DCHECK(klass_ != nullptr);
6281 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006282 }
6283
Andreas Gampea5b09a62016-11-17 15:21:22 -08006284 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006285 const DexFile& GetDexFile() const { return dex_file_; }
6286
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006287 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006288 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006289 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006290
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006291 static SideEffects SideEffectsForArchRuntimeCalls() {
6292 return SideEffects::CanTriggerGC();
6293 }
6294
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006295 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006296 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006297 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006298 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006299
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006300 bool MustResolveTypeOnSlowPath() const {
6301 // Check that this instruction has a slow path.
6302 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6303 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6304 return GetLoadKind() == LoadKind::kBssEntry;
6305 }
6306
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006307 void MarkInBootImage() {
6308 SetPackedFlag<kFlagIsInBootImage>(true);
6309 }
6310
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006311 void AddSpecialInput(HInstruction* special_input);
6312
6313 using HInstruction::GetInputRecords; // Keep the const version visible.
6314 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6315 return ArrayRef<HUserRecord<HInstruction*>>(
6316 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6317 }
6318
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006319 Handle<mirror::Class> GetClass() const {
6320 return klass_;
6321 }
6322
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006323 DECLARE_INSTRUCTION(LoadClass);
6324
Artem Serovcced8ba2017-07-19 18:18:09 +01006325 protected:
6326 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6327
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006328 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006329 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006330 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006331 // Whether this instruction must generate the initialization check.
6332 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006333 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006334 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6335 static constexpr size_t kFieldLoadKindSize =
6336 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006337 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6338 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006339 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006340 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6341
6342 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006343 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006344 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006345 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006346 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006347 }
6348
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006349 void SetLoadKindInternal(LoadKind load_kind);
6350
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006351 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006352 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006353 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006354 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006355
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006356 // A type index and dex file where the class can be accessed. The dex file can be:
6357 // - The compiling method's dex file if the class is defined there too.
6358 // - The compiling method's dex file if the class is referenced there.
6359 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006360 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006361 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006362 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006363
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006364 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006365};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006366std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6367
6368// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006369inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6370 // The load kind should be determined before inserting the instruction to the graph.
6371 DCHECK(GetBlock() == nullptr);
6372 DCHECK(GetEnvironment() == nullptr);
6373 SetPackedField<LoadKindField>(load_kind);
6374 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6375 special_input_ = HUserRecord<HInstruction*>(nullptr);
6376 }
6377 if (!NeedsEnvironment()) {
6378 SetSideEffects(SideEffects::None());
6379 }
6380}
6381
6382// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006383inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006384 // The special input is used for PC-relative loads on some architectures,
6385 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006386 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006387 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006388 GetLoadKind() == LoadKind::kBssEntry ||
6389 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006390 DCHECK(special_input_.GetInstruction() == nullptr);
6391 special_input_ = HUserRecord<HInstruction*>(special_input);
6392 special_input->AddUseAt(this, 0);
6393}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006394
Vladimir Marko372f10e2016-05-17 16:30:10 +01006395class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006396 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006397 // Determines how to load the String.
6398 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006399 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006400 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006401 kBootImageLinkTimePcRelative,
6402
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006403 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006404 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006405 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006406
Vladimir Markoaad75c62016-10-03 08:46:48 +00006407 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006408 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006409 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006410
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006411 // Use a known boot image String* address, embedded in the code by the codegen.
6412 // Used for boot image strings referenced by apps in JIT-compiled code.
6413 kJitBootImageAddress,
6414
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006415 // Load from the root table associated with the JIT compiled method.
6416 kJitTableAddress,
6417
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006418 // Load using a simple runtime call. This is the fall-back load kind when
6419 // the codegen is unable to use another appropriate kind.
6420 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006421
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006422 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006423 };
6424
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006425 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006426 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006427 const DexFile& dex_file,
6428 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006429 : HInstruction(kLoadString,
6430 DataType::Type::kReference,
6431 SideEffectsForArchRuntimeCalls(),
6432 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006433 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006434 string_index_(string_index),
6435 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006436 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006437 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006438
Artem Serovcced8ba2017-07-19 18:18:09 +01006439 bool IsClonable() const OVERRIDE { return true; }
6440
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006441 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006442
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006443 LoadKind GetLoadKind() const {
6444 return GetPackedField<LoadKindField>();
6445 }
6446
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006447 bool HasPcRelativeLoadKind() const {
6448 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6449 GetLoadKind() == LoadKind::kBootImageRelRo ||
6450 GetLoadKind() == LoadKind::kBssEntry;
6451 }
6452
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006453 const DexFile& GetDexFile() const {
6454 return dex_file_;
6455 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006456
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006457 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006458 return string_index_;
6459 }
6460
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006461 Handle<mirror::String> GetString() const {
6462 return string_;
6463 }
6464
6465 void SetString(Handle<mirror::String> str) {
6466 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006467 }
6468
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006469 bool CanBeMoved() const OVERRIDE { return true; }
6470
Vladimir Marko372f10e2016-05-17 16:30:10 +01006471 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006472
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006473 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006474
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006475 // Will call the runtime if we need to load the string through
6476 // the dex cache and the string is not guaranteed to be there yet.
6477 bool NeedsEnvironment() const OVERRIDE {
6478 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006479 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006480 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006481 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006482 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006483 return false;
6484 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006485 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006486 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006487
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006488 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006489 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006490 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006491
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006492 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006493 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006494
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006495 static SideEffects SideEffectsForArchRuntimeCalls() {
6496 return SideEffects::CanTriggerGC();
6497 }
6498
Vladimir Marko372f10e2016-05-17 16:30:10 +01006499 void AddSpecialInput(HInstruction* special_input);
6500
6501 using HInstruction::GetInputRecords; // Keep the const version visible.
6502 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6503 return ArrayRef<HUserRecord<HInstruction*>>(
6504 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006505 }
6506
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006507 DECLARE_INSTRUCTION(LoadString);
6508
Artem Serovcced8ba2017-07-19 18:18:09 +01006509 protected:
6510 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6511
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006512 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006513 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006514 static constexpr size_t kFieldLoadKindSize =
6515 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6516 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006517 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006518 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006519
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006520 void SetLoadKindInternal(LoadKind load_kind);
6521
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006522 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006523 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006524 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006525 HUserRecord<HInstruction*> special_input_;
6526
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006527 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006528 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006529
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006530 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006531};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006532std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6533
6534// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006535inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6536 // The load kind should be determined before inserting the instruction to the graph.
6537 DCHECK(GetBlock() == nullptr);
6538 DCHECK(GetEnvironment() == nullptr);
6539 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6540 SetPackedField<LoadKindField>(load_kind);
6541 if (load_kind != LoadKind::kRuntimeCall) {
6542 special_input_ = HUserRecord<HInstruction*>(nullptr);
6543 }
6544 if (!NeedsEnvironment()) {
6545 SetSideEffects(SideEffects::None());
6546 }
6547}
6548
6549// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006550inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006551 // The special input is used for PC-relative loads on some architectures,
6552 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006553 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006554 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006555 GetLoadKind() == LoadKind::kBssEntry ||
6556 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006557 // HLoadString::GetInputRecords() returns an empty array at this point,
6558 // so use the GetInputRecords() from the base class to set the input record.
6559 DCHECK(special_input_.GetInstruction() == nullptr);
6560 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006561 special_input->AddUseAt(this, 0);
6562}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006563
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006564class HLoadMethodHandle FINAL : public HInstruction {
6565 public:
6566 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006567 uint16_t method_handle_idx,
6568 const DexFile& dex_file,
6569 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006570 : HInstruction(kLoadMethodHandle,
6571 DataType::Type::kReference,
6572 SideEffectsForArchRuntimeCalls(),
6573 dex_pc),
6574 special_input_(HUserRecord<HInstruction*>(current_method)),
6575 method_handle_idx_(method_handle_idx),
6576 dex_file_(dex_file) {
6577 }
6578
6579 using HInstruction::GetInputRecords; // Keep the const version visible.
6580 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6581 return ArrayRef<HUserRecord<HInstruction*>>(
6582 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6583 }
6584
6585 bool IsClonable() const OVERRIDE { return true; }
6586
6587 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6588
6589 const DexFile& GetDexFile() const { return dex_file_; }
6590
6591 static SideEffects SideEffectsForArchRuntimeCalls() {
6592 return SideEffects::CanTriggerGC();
6593 }
6594
6595 DECLARE_INSTRUCTION(LoadMethodHandle);
6596
6597 protected:
6598 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6599
6600 private:
6601 // The special input is the HCurrentMethod for kRuntimeCall.
6602 HUserRecord<HInstruction*> special_input_;
6603
6604 const uint16_t method_handle_idx_;
6605 const DexFile& dex_file_;
6606};
6607
Orion Hodson18259d72018-04-12 11:18:23 +01006608class HLoadMethodType FINAL : public HInstruction {
6609 public:
6610 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006611 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006612 const DexFile& dex_file,
6613 uint32_t dex_pc)
6614 : HInstruction(kLoadMethodType,
6615 DataType::Type::kReference,
6616 SideEffectsForArchRuntimeCalls(),
6617 dex_pc),
6618 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006619 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006620 dex_file_(dex_file) {
6621 }
6622
6623 using HInstruction::GetInputRecords; // Keep the const version visible.
6624 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6625 return ArrayRef<HUserRecord<HInstruction*>>(
6626 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6627 }
6628
6629 bool IsClonable() const OVERRIDE { return true; }
6630
Orion Hodson06d10a72018-05-14 08:53:38 +01006631 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006632
6633 const DexFile& GetDexFile() const { return dex_file_; }
6634
6635 static SideEffects SideEffectsForArchRuntimeCalls() {
6636 return SideEffects::CanTriggerGC();
6637 }
6638
6639 DECLARE_INSTRUCTION(LoadMethodType);
6640
6641 protected:
6642 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6643
6644 private:
6645 // The special input is the HCurrentMethod for kRuntimeCall.
6646 HUserRecord<HInstruction*> special_input_;
6647
Orion Hodson06d10a72018-05-14 08:53:38 +01006648 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006649 const DexFile& dex_file_;
6650};
6651
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006652/**
6653 * Performs an initialization check on its Class object input.
6654 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006655class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006656 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006657 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006658 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306659 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006660 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006661 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006662 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006663 SetRawInputAt(0, constant);
6664 }
Artem Serova6e26142018-06-19 14:55:17 +01006665 // TODO: Make ClinitCheck clonable.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006666 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006667 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006668 return true;
6669 }
6670
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006671 bool NeedsEnvironment() const OVERRIDE {
6672 // May call runtime to initialize the class.
6673 return true;
6674 }
6675
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006676 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006677
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006678 HLoadClass* GetLoadClass() const {
6679 DCHECK(InputAt(0)->IsLoadClass());
6680 return InputAt(0)->AsLoadClass();
6681 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006682
6683 DECLARE_INSTRUCTION(ClinitCheck);
6684
Artem Serovcced8ba2017-07-19 18:18:09 +01006685
6686 protected:
6687 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006688};
6689
Vladimir Markofcb503c2016-05-18 12:48:17 +01006690class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006691 public:
6692 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006693 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006694 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006695 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006696 bool is_volatile,
6697 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006698 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006699 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006700 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306701 : HExpression(kStaticFieldGet,
6702 field_type,
6703 SideEffects::FieldReadOfType(field_type, is_volatile),
6704 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006705 field_info_(field,
6706 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006707 field_type,
6708 is_volatile,
6709 field_idx,
6710 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006711 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006712 SetRawInputAt(0, cls);
6713 }
6714
Calin Juravle52c48962014-12-16 17:02:57 +00006715
Artem Serovcced8ba2017-07-19 18:18:09 +01006716 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006717 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006718
Vladimir Marko372f10e2016-05-17 16:30:10 +01006719 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6720 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006721 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006722 }
6723
6724 size_t ComputeHashCode() const OVERRIDE {
6725 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6726 }
6727
Calin Juravle52c48962014-12-16 17:02:57 +00006728 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006729 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006730 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006731 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006732
Vladimir Marko61b92282017-10-11 13:23:17 +01006733 void SetType(DataType::Type new_type) {
6734 DCHECK(DataType::IsIntegralType(GetType()));
6735 DCHECK(DataType::IsIntegralType(new_type));
6736 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6737 SetPackedField<TypeField>(new_type);
6738 }
6739
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006740 DECLARE_INSTRUCTION(StaticFieldGet);
6741
Artem Serovcced8ba2017-07-19 18:18:09 +01006742 protected:
6743 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6744
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006745 private:
6746 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006747};
6748
Vladimir Markobd785672018-05-03 17:09:09 +01006749class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006750 public:
6751 HStaticFieldSet(HInstruction* cls,
6752 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006753 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006754 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006755 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006756 bool is_volatile,
6757 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006758 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006759 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006760 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006761 : HExpression(kStaticFieldSet,
6762 SideEffects::FieldWriteOfType(field_type, is_volatile),
6763 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006764 field_info_(field,
6765 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006766 field_type,
6767 is_volatile,
6768 field_idx,
6769 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006770 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006771 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006772 SetRawInputAt(0, cls);
6773 SetRawInputAt(1, value);
6774 }
6775
Artem Serovcced8ba2017-07-19 18:18:09 +01006776 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006777 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006778 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006779 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006780 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006781
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006782 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006783 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6784 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006785
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006786 DECLARE_INSTRUCTION(StaticFieldSet);
6787
Artem Serovcced8ba2017-07-19 18:18:09 +01006788 protected:
6789 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6790
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006791 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006792 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6793 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6794 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6795 "Too many packed fields.");
6796
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006797 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006798};
6799
Vladimir Markofcb503c2016-05-18 12:48:17 +01006800class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006801 public:
6802 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006803 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006804 uint32_t field_index,
6805 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306806 : HExpression(kUnresolvedInstanceFieldGet,
6807 field_type,
6808 SideEffects::AllExceptGCDependency(),
6809 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006810 field_index_(field_index) {
6811 SetRawInputAt(0, obj);
6812 }
6813
Artem Serovcced8ba2017-07-19 18:18:09 +01006814 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006815 bool NeedsEnvironment() const OVERRIDE { return true; }
6816 bool CanThrow() const OVERRIDE { return true; }
6817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006818 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006819 uint32_t GetFieldIndex() const { return field_index_; }
6820
6821 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6822
Artem Serovcced8ba2017-07-19 18:18:09 +01006823 protected:
6824 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6825
Calin Juravlee460d1d2015-09-29 04:52:17 +01006826 private:
6827 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006828};
6829
Vladimir Markobd785672018-05-03 17:09:09 +01006830class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006831 public:
6832 HUnresolvedInstanceFieldSet(HInstruction* obj,
6833 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006834 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006835 uint32_t field_index,
6836 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006837 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006838 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006839 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006840 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006841 SetRawInputAt(0, obj);
6842 SetRawInputAt(1, value);
6843 }
6844
Artem Serovcced8ba2017-07-19 18:18:09 +01006845 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006846 bool NeedsEnvironment() const OVERRIDE { return true; }
6847 bool CanThrow() const OVERRIDE { return true; }
6848
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006849 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006850 uint32_t GetFieldIndex() const { return field_index_; }
6851
6852 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6853
Artem Serovcced8ba2017-07-19 18:18:09 +01006854 protected:
6855 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6856
Calin Juravlee460d1d2015-09-29 04:52:17 +01006857 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006858 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6859 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006860 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006861 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6862 kFieldFieldType + kFieldFieldTypeSize;
6863 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6864 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006865 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006866
Calin Juravlee460d1d2015-09-29 04:52:17 +01006867 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006868};
6869
Vladimir Markofcb503c2016-05-18 12:48:17 +01006870class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006871 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006872 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006873 uint32_t field_index,
6874 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306875 : HExpression(kUnresolvedStaticFieldGet,
6876 field_type,
6877 SideEffects::AllExceptGCDependency(),
6878 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006879 field_index_(field_index) {
6880 }
6881
Artem Serovcced8ba2017-07-19 18:18:09 +01006882 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006883 bool NeedsEnvironment() const OVERRIDE { return true; }
6884 bool CanThrow() const OVERRIDE { return true; }
6885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006886 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006887 uint32_t GetFieldIndex() const { return field_index_; }
6888
6889 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6890
Artem Serovcced8ba2017-07-19 18:18:09 +01006891 protected:
6892 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6893
Calin Juravlee460d1d2015-09-29 04:52:17 +01006894 private:
6895 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006896};
6897
Vladimir Markobd785672018-05-03 17:09:09 +01006898class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006899 public:
6900 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006901 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006902 uint32_t field_index,
6903 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006904 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006905 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006906 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006907 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006908 SetRawInputAt(0, value);
6909 }
6910
Artem Serovcced8ba2017-07-19 18:18:09 +01006911 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006912 bool NeedsEnvironment() const OVERRIDE { return true; }
6913 bool CanThrow() const OVERRIDE { return true; }
6914
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006915 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006916 uint32_t GetFieldIndex() const { return field_index_; }
6917
6918 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6919
Artem Serovcced8ba2017-07-19 18:18:09 +01006920 protected:
6921 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6922
Calin Juravlee460d1d2015-09-29 04:52:17 +01006923 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006924 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6925 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006926 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006927 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6928 kFieldFieldType + kFieldFieldTypeSize;
6929 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6930 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006931 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006932
Calin Juravlee460d1d2015-09-29 04:52:17 +01006933 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006934};
6935
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006936// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006937class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006938 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006939 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306940 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6941 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006942
David Brazdilbbd733e2015-08-18 17:48:17 +01006943 bool CanBeNull() const OVERRIDE { return false; }
6944
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006945 DECLARE_INSTRUCTION(LoadException);
6946
Artem Serovcced8ba2017-07-19 18:18:09 +01006947 protected:
6948 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006949};
6950
David Brazdilcb1c0552015-08-04 16:22:25 +01006951// Implicit part of move-exception which clears thread-local exception storage.
6952// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markobd785672018-05-03 17:09:09 +01006953class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006954 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006955 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006956 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306957 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006958
6959 DECLARE_INSTRUCTION(ClearException);
6960
Artem Serovcced8ba2017-07-19 18:18:09 +01006961 protected:
6962 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006963};
6964
Vladimir Markobd785672018-05-03 17:09:09 +01006965class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006966 public:
6967 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006968 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006969 SetRawInputAt(0, exception);
6970 }
6971
6972 bool IsControlFlow() const OVERRIDE { return true; }
6973
6974 bool NeedsEnvironment() const OVERRIDE { return true; }
6975
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006976 bool CanThrow() const OVERRIDE { return true; }
6977
Aart Bika8b8e9b2018-01-09 11:01:02 -08006978 bool AlwaysThrows() const OVERRIDE { return true; }
6979
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006980 DECLARE_INSTRUCTION(Throw);
6981
Artem Serovcced8ba2017-07-19 18:18:09 +01006982 protected:
6983 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006984};
6985
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006986/**
6987 * Implementation strategies for the code generator of a HInstanceOf
6988 * or `HCheckCast`.
6989 */
6990enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006991 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006992 kExactCheck, // Can do a single class compare.
6993 kClassHierarchyCheck, // Can just walk the super class chain.
6994 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6995 kInterfaceCheck, // No optimization yet when checking against an interface.
6996 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006997 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006998 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006999 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007000};
7001
Roland Levillain86503782016-02-11 19:07:30 +00007002std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7003
Vladimir Marko175e7862018-03-27 09:03:13 +00007004// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7005// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7006class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007007 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007008 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007009 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007010 HInstruction* object,
7011 HInstruction* target_class_or_null,
7012 TypeCheckKind check_kind,
7013 Handle<mirror::Class> klass,
7014 uint32_t dex_pc,
7015 ArenaAllocator* allocator,
7016 HIntConstant* bitstring_path_to_root,
7017 HIntConstant* bitstring_mask,
7018 SideEffects side_effects)
7019 : HVariableInputSizeInstruction(
7020 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007021 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007022 side_effects,
7023 dex_pc,
7024 allocator,
7025 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
7026 kArenaAllocTypeCheckInputs),
7027 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007028 SetPackedField<TypeCheckKindField>(check_kind);
7029 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007030 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007031 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007032 SetRawInputAt(1, target_class_or_null);
7033 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7034 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7035 if (check_kind == TypeCheckKind::kBitstringCheck) {
7036 DCHECK(target_class_or_null->IsNullConstant());
7037 SetRawInputAt(2, bitstring_path_to_root);
7038 SetRawInputAt(3, bitstring_mask);
7039 } else {
7040 DCHECK(target_class_or_null->IsLoadClass());
7041 }
Vladimir Marko87584542017-12-12 17:47:52 +00007042 }
7043
7044 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007045 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007046 HInstruction* load_class = InputAt(1);
7047 DCHECK(load_class->IsLoadClass());
7048 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007049 }
7050
Vladimir Marko175e7862018-03-27 09:03:13 +00007051 uint32_t GetBitstringPathToRoot() const {
7052 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7053 HInstruction* path_to_root = InputAt(2);
7054 DCHECK(path_to_root->IsIntConstant());
7055 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7056 }
7057
7058 uint32_t GetBitstringMask() const {
7059 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7060 HInstruction* mask = InputAt(3);
7061 DCHECK(mask->IsIntConstant());
7062 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7063 }
7064
Artem Serovcced8ba2017-07-19 18:18:09 +01007065 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007066 bool CanBeMoved() const OVERRIDE { return true; }
7067
Vladimir Marko175e7862018-03-27 09:03:13 +00007068 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
7069 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7070 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007071 }
7072
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007073 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7074 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7075 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7076 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7077
Vladimir Marko175e7862018-03-27 09:03:13 +00007078 ReferenceTypeInfo GetTargetClassRTI() {
7079 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7080 // Note: The is_exact flag from the return value should not be used.
7081 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7082 } else {
7083 return ReferenceTypeInfo::CreateInvalid();
7084 }
7085 }
7086
7087 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7088 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7089 DCHECK(klass_ != nullptr);
7090 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7091 }
7092
7093 Handle<mirror::Class> GetClass() const {
7094 return klass_;
7095 }
7096
7097 protected:
7098 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7099
7100 private:
7101 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7102 static constexpr size_t kFieldTypeCheckKindSize =
7103 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7104 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7105 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7106 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7107 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7108 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7109
7110 Handle<mirror::Class> klass_;
7111};
7112
7113class HInstanceOf FINAL : public HTypeCheckInstruction {
7114 public:
7115 HInstanceOf(HInstruction* object,
7116 HInstruction* target_class_or_null,
7117 TypeCheckKind check_kind,
7118 Handle<mirror::Class> klass,
7119 uint32_t dex_pc,
7120 ArenaAllocator* allocator,
7121 HIntConstant* bitstring_path_to_root,
7122 HIntConstant* bitstring_mask)
7123 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007124 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007125 object,
7126 target_class_or_null,
7127 check_kind,
7128 klass,
7129 dex_pc,
7130 allocator,
7131 bitstring_path_to_root,
7132 bitstring_mask,
7133 SideEffectsForArchRuntimeCalls(check_kind)) {}
7134
Artem Serova6e26142018-06-19 14:55:17 +01007135 bool IsClonable() const OVERRIDE { return true; }
7136
Vladimir Marko175e7862018-03-27 09:03:13 +00007137 bool NeedsEnvironment() const OVERRIDE {
7138 return CanCallRuntime(GetTypeCheckKind());
7139 }
7140
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007141 static bool CanCallRuntime(TypeCheckKind check_kind) {
7142 // Mips currently does runtime calls for any other checks.
7143 return check_kind != TypeCheckKind::kExactCheck;
7144 }
7145
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007146 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007147 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007148 }
7149
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007150 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007151
Artem Serovcced8ba2017-07-19 18:18:09 +01007152 protected:
7153 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007154};
7155
Vladimir Markofcb503c2016-05-18 12:48:17 +01007156class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007157 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007158 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307159 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007160 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7161 SetPackedFlag<kFlagUpperCanBeNull>(true);
7162 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007163 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007164 SetRawInputAt(0, input);
7165 }
7166
Artem Serov4d277ba2018-06-05 20:54:42 +01007167 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Artem Serovcced8ba2017-07-19 18:18:09 +01007168 bool IsClonable() const OVERRIDE { return true; }
7169
David Brazdilf5552582015-12-27 13:36:12 +00007170 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007171 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007172 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007173 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007174
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007175 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007176 DCHECK(GetUpperCanBeNull() || !can_be_null);
7177 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007178 }
7179
Vladimir Markoa1de9182016-02-25 11:37:38 +00007180 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007181
Calin Juravleb1498f62015-02-16 13:13:29 +00007182 DECLARE_INSTRUCTION(BoundType);
7183
Artem Serovcced8ba2017-07-19 18:18:09 +01007184 protected:
7185 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7186
Calin Juravleb1498f62015-02-16 13:13:29 +00007187 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007188 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7189 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007190 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007191 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7192 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7193 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7194
Calin Juravleb1498f62015-02-16 13:13:29 +00007195 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007196 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7197 // It is used to bound the type in cases like:
7198 // if (x instanceof ClassX) {
7199 // // uper_bound_ will be ClassX
7200 // }
David Brazdilf5552582015-12-27 13:36:12 +00007201 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007202};
7203
Vladimir Marko175e7862018-03-27 09:03:13 +00007204class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007205 public:
7206 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007207 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007208 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007209 Handle<mirror::Class> klass,
7210 uint32_t dex_pc,
7211 ArenaAllocator* allocator,
7212 HIntConstant* bitstring_path_to_root,
7213 HIntConstant* bitstring_mask)
7214 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007215 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007216 object,
7217 target_class_or_null,
7218 check_kind,
7219 klass,
7220 dex_pc,
7221 allocator,
7222 bitstring_path_to_root,
7223 bitstring_mask,
7224 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007225
Artem Serova6e26142018-06-19 14:55:17 +01007226 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007227 bool NeedsEnvironment() const OVERRIDE {
7228 // Instruction may throw a CheckCastError.
7229 return true;
7230 }
7231
7232 bool CanThrow() const OVERRIDE { return true; }
7233
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007234 DECLARE_INSTRUCTION(CheckCast);
7235
Artem Serovcced8ba2017-07-19 18:18:09 +01007236 protected:
7237 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007238};
7239
Andreas Gampe26de38b2016-07-27 17:53:11 -07007240/**
7241 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7242 * @details We define the combined barrier types that are actually required
7243 * by the Java Memory Model, rather than using exactly the terminology from
7244 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7245 * primitives. Note that the JSR-133 cookbook generally does not deal with
7246 * store atomicity issues, and the recipes there are not always entirely sufficient.
7247 * The current recipe is as follows:
7248 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7249 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7250 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7251 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7252 * class has final fields.
7253 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7254 * store-to-memory instructions. Only generated together with non-temporal stores.
7255 */
7256enum MemBarrierKind {
7257 kAnyStore,
7258 kLoadAny,
7259 kStoreStore,
7260 kAnyAny,
7261 kNTStoreStore,
7262 kLastBarrierKind = kNTStoreStore
7263};
7264std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7265
Vladimir Markobd785672018-05-03 17:09:09 +01007266class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007267 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007268 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007269 : HExpression(kMemoryBarrier,
7270 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7271 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007272 SetPackedField<BarrierKindField>(barrier_kind);
7273 }
Calin Juravle27df7582015-04-17 19:12:31 +01007274
Artem Serovcced8ba2017-07-19 18:18:09 +01007275 bool IsClonable() const OVERRIDE { return true; }
7276
Vladimir Markoa1de9182016-02-25 11:37:38 +00007277 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007278
7279 DECLARE_INSTRUCTION(MemoryBarrier);
7280
Artem Serovcced8ba2017-07-19 18:18:09 +01007281 protected:
7282 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7283
Calin Juravle27df7582015-04-17 19:12:31 +01007284 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007285 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7286 static constexpr size_t kFieldBarrierKindSize =
7287 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7288 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7289 kFieldBarrierKind + kFieldBarrierKindSize;
7290 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7291 "Too many packed fields.");
7292 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007293};
7294
Igor Murashkind01745e2017-04-05 16:40:31 -07007295// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7296// the specified object(s), with respect to any subsequent store that might "publish"
7297// (i.e. make visible) the specified object to another thread.
7298//
7299// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7300// for all final fields (that were set) at the end of the invoked constructor.
7301//
7302// The constructor fence models the freeze actions for the final fields of an object
7303// being constructed (semantically at the end of the constructor). Constructor fences
7304// have a per-object affinity; two separate objects being constructed get two separate
7305// constructor fences.
7306//
7307// (Note: that if calling a super-constructor or forwarding to another constructor,
7308// the freezes would happen at the end of *that* constructor being invoked).
7309//
7310// The memory model guarantees that when the object being constructed is "published" after
7311// constructor completion (i.e. escapes the current thread via a store), then any final field
7312// writes must be observable on other threads (once they observe that publication).
7313//
7314// Further, anything written before the freeze, and read by dereferencing through the final field,
7315// must also be visible (so final object field could itself have an object with non-final fields;
7316// yet the freeze must also extend to them).
7317//
7318// Constructor example:
7319//
7320// class HasFinal {
7321// final int field; Optimizing IR for <init>()V:
7322// HasFinal() {
7323// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7324// // freeze(this.field); HConstructorFence(this)
7325// } HReturn
7326// }
7327//
7328// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7329// already-initialized classes; in that case the allocation must act as a "default-initializer"
7330// of the object which effectively writes the class pointer "final field".
7331//
7332// For example, we can model default-initialiation as roughly the equivalent of the following:
7333//
7334// class Object {
7335// private final Class header;
7336// }
7337//
7338// Java code: Optimizing IR:
7339//
7340// T new_instance<T>() {
7341// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7342// obj.header = T.class; // header write is done by above call.
7343// // freeze(obj.header) HConstructorFence(obj)
7344// return (T)obj;
7345// }
7346//
7347// See also:
7348// * CompilerDriver::RequiresConstructorBarrier
7349// * QuasiAtomic::ThreadFenceForConstructor
7350//
7351class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7352 // A fence has variable inputs because the inputs can be removed
7353 // after prepare_for_register_allocation phase.
7354 // (TODO: In the future a fence could freeze multiple objects
7355 // after merging two fences together.)
7356 public:
7357 // `fence_object` is the reference that needs to be protected for correct publication.
7358 //
7359 // It makes sense in the following situations:
7360 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7361 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7362 //
7363 // After construction the `fence_object` becomes the 0th input.
7364 // This is not an input in a real sense, but just a convenient place to stash the information
7365 // about the associated object.
7366 HConstructorFence(HInstruction* fence_object,
7367 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007368 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007369 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7370 // constraints described in the class header. We claim that these SideEffects constraints
7371 // enforce a superset of the real constraints.
7372 //
7373 // The ordering described above is conservatively modeled with SideEffects as follows:
7374 //
7375 // * To prevent reordering of the publication stores:
7376 // ----> "Reads of objects" is the initial SideEffect.
7377 // * For every primitive final field store in the constructor:
7378 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7379 // * If there are any stores to reference final fields in the constructor:
7380 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7381 // that are reachable from `fence_object` also need to be prevented for reordering
7382 // (and we do not want to do alias analysis to figure out what those stores are).
7383 //
7384 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7385 // even more conservative approach than the one described above, and prevents all of the
7386 // above reordering without analyzing any of the instructions in the constructor.
7387 //
7388 // If in a later phase we discover that there are no writes to reference final fields,
7389 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307390 : HVariableInputSizeInstruction(kConstructorFence,
7391 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007392 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007393 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007394 /* number_of_inputs */ 1,
7395 kArenaAllocConstructorFenceInputs) {
7396 DCHECK(fence_object != nullptr);
7397 SetRawInputAt(0, fence_object);
7398 }
7399
7400 // The object associated with this constructor fence.
7401 //
7402 // (Note: This will be null after the prepare_for_register_allocation phase,
7403 // as all constructor fence inputs are removed there).
7404 HInstruction* GetFenceObject() const {
7405 return InputAt(0);
7406 }
7407
7408 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7409 // - Delete `fence_use` from `this`'s use list.
7410 // - Delete `this` from `fence_use`'s inputs list.
7411 // - If the `fence_use` is dead, remove it from the graph.
7412 //
7413 // A fence is considered dead once it no longer has any uses
7414 // and all of the inputs are dead.
7415 //
7416 // This must *not* be called during/after prepare_for_register_allocation,
7417 // because that removes all the inputs to the fences but the fence is actually
7418 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007419 //
7420 // Returns how many HConstructorFence instructions were removed from graph.
7421 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007422
Igor Murashkindd018df2017-08-09 10:38:31 -07007423 // Combine all inputs of `this` and `other` instruction and remove
7424 // `other` from the graph.
7425 //
7426 // Inputs are unique after the merge.
7427 //
7428 // Requirement: `this` must not be the same as `other.
7429 void Merge(HConstructorFence* other);
7430
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007431 // Check if this constructor fence is protecting
7432 // an HNewInstance or HNewArray that is also the immediate
7433 // predecessor of `this`.
7434 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007435 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7436 // to be one of the inputs of `this`.
7437 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007438 // Returns the associated HNewArray or HNewInstance,
7439 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007440 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007441
Igor Murashkind01745e2017-04-05 16:40:31 -07007442 DECLARE_INSTRUCTION(ConstructorFence);
7443
Artem Serovcced8ba2017-07-19 18:18:09 +01007444 protected:
7445 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007446};
7447
Vladimir Markobd785672018-05-03 17:09:09 +01007448class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007449 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007450 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007451 kEnter,
7452 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007453 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007454 };
7455
7456 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007457 : HExpression(kMonitorOperation,
7458 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7459 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007460 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007461 SetRawInputAt(0, object);
7462 }
7463
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007464 // Instruction may go into runtime, so we need an environment.
7465 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007466
7467 bool CanThrow() const OVERRIDE {
7468 // Verifier guarantees that monitor-exit cannot throw.
7469 // This is important because it allows the HGraphBuilder to remove
7470 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7471 return IsEnter();
7472 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007473
Vladimir Markoa1de9182016-02-25 11:37:38 +00007474 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7475 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007476
7477 DECLARE_INSTRUCTION(MonitorOperation);
7478
Artem Serovcced8ba2017-07-19 18:18:09 +01007479 protected:
7480 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7481
Calin Juravle52c48962014-12-16 17:02:57 +00007482 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007483 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7484 static constexpr size_t kFieldOperationKindSize =
7485 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7486 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7487 kFieldOperationKind + kFieldOperationKindSize;
7488 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7489 "Too many packed fields.");
7490 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007491};
7492
Vladimir Markofcb503c2016-05-18 12:48:17 +01007493class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007494 public:
7495 HSelect(HInstruction* condition,
7496 HInstruction* true_value,
7497 HInstruction* false_value,
7498 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307499 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007500 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7501
7502 // First input must be `true_value` or `false_value` to allow codegens to
7503 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7504 // that architectures which implement HSelect as a conditional move also
7505 // will not need to invert the condition.
7506 SetRawInputAt(0, false_value);
7507 SetRawInputAt(1, true_value);
7508 SetRawInputAt(2, condition);
7509 }
7510
Artem Serovcced8ba2017-07-19 18:18:09 +01007511 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007512 HInstruction* GetFalseValue() const { return InputAt(0); }
7513 HInstruction* GetTrueValue() const { return InputAt(1); }
7514 HInstruction* GetCondition() const { return InputAt(2); }
7515
7516 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007517 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7518 return true;
7519 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007520
7521 bool CanBeNull() const OVERRIDE {
7522 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7523 }
7524
7525 DECLARE_INSTRUCTION(Select);
7526
Artem Serovcced8ba2017-07-19 18:18:09 +01007527 protected:
7528 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007529};
7530
Vladimir Markof9f64412015-09-02 14:05:49 +01007531class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007532 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007533 MoveOperands(Location source,
7534 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007535 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007536 HInstruction* instruction)
7537 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007538
7539 Location GetSource() const { return source_; }
7540 Location GetDestination() const { return destination_; }
7541
7542 void SetSource(Location value) { source_ = value; }
7543 void SetDestination(Location value) { destination_ = value; }
7544
7545 // The parallel move resolver marks moves as "in-progress" by clearing the
7546 // destination (but not the source).
7547 Location MarkPending() {
7548 DCHECK(!IsPending());
7549 Location dest = destination_;
7550 destination_ = Location::NoLocation();
7551 return dest;
7552 }
7553
7554 void ClearPending(Location dest) {
7555 DCHECK(IsPending());
7556 destination_ = dest;
7557 }
7558
7559 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007560 DCHECK(source_.IsValid() || destination_.IsInvalid());
7561 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007562 }
7563
7564 // True if this blocks a move from the given location.
7565 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007566 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007567 }
7568
7569 // A move is redundant if it's been eliminated, if its source and
7570 // destination are the same, or if its destination is unneeded.
7571 bool IsRedundant() const {
7572 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7573 }
7574
7575 // We clear both operands to indicate move that's been eliminated.
7576 void Eliminate() {
7577 source_ = destination_ = Location::NoLocation();
7578 }
7579
7580 bool IsEliminated() const {
7581 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7582 return source_.IsInvalid();
7583 }
7584
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007585 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007586
Nicolas Geoffray90218252015-04-15 11:56:51 +01007587 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007588 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007589 }
7590
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007591 HInstruction* GetInstruction() const { return instruction_; }
7592
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007593 private:
7594 Location source_;
7595 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007596 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007597 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007598 // The instruction this move is assocatied with. Null when this move is
7599 // for moving an input in the expected locations of user (including a phi user).
7600 // This is only used in debug mode, to ensure we do not connect interval siblings
7601 // in the same parallel move.
7602 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007603};
7604
Roland Levillainc9285912015-12-18 10:38:42 +00007605std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7606
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007607static constexpr size_t kDefaultNumberOfMoves = 4;
7608
Vladimir Markobd785672018-05-03 17:09:09 +01007609class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007610 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007611 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007612 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007613 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007614 moves_.reserve(kDefaultNumberOfMoves);
7615 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007616
Nicolas Geoffray90218252015-04-15 11:56:51 +01007617 void AddMove(Location source,
7618 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007619 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007620 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007621 DCHECK(source.IsValid());
7622 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007623 if (kIsDebugBuild) {
7624 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007625 for (const MoveOperands& move : moves_) {
7626 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007627 // Special case the situation where the move is for the spill slot
7628 // of the instruction.
7629 if ((GetPrevious() == instruction)
7630 || ((GetPrevious() == nullptr)
7631 && instruction->IsPhi()
7632 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007633 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007634 << "Doing parallel moves for the same instruction.";
7635 } else {
7636 DCHECK(false) << "Doing parallel moves for the same instruction.";
7637 }
7638 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007639 }
7640 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007641 for (const MoveOperands& move : moves_) {
7642 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007643 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007644 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007645 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007646 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007647 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007648 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007649 }
7650
Vladimir Marko225b6462015-09-28 12:17:40 +01007651 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007652 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007653 }
7654
Vladimir Marko225b6462015-09-28 12:17:40 +01007655 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007656
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007657 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007658
Artem Serovcced8ba2017-07-19 18:18:09 +01007659 protected:
7660 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7661
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007662 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007663 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007664};
7665
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007666// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7667// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7668// never used across anything that can trigger GC.
7669// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7670// So we represent it by the type `DataType::Type::kInt`.
7671class HIntermediateAddress FINAL : public HExpression<2> {
7672 public:
7673 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307674 : HExpression(kIntermediateAddress,
7675 DataType::Type::kInt32,
7676 SideEffects::DependsOnGC(),
7677 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007678 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7679 DataType::Size(DataType::Type::kReference))
7680 << "kPrimInt and kPrimNot have different sizes.";
7681 SetRawInputAt(0, base_address);
7682 SetRawInputAt(1, offset);
7683 }
7684
Artem Serovcced8ba2017-07-19 18:18:09 +01007685 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007686 bool CanBeMoved() const OVERRIDE { return true; }
7687 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7688 return true;
7689 }
7690 bool IsActualObject() const OVERRIDE { return false; }
7691
7692 HInstruction* GetBaseAddress() const { return InputAt(0); }
7693 HInstruction* GetOffset() const { return InputAt(1); }
7694
7695 DECLARE_INSTRUCTION(IntermediateAddress);
7696
Artem Serovcced8ba2017-07-19 18:18:09 +01007697 protected:
7698 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007699};
7700
7701
Mark Mendell0616ae02015-04-17 12:49:27 -04007702} // namespace art
7703
Aart Bikf8f5a162017-02-06 15:35:29 -08007704#include "nodes_vector.h"
7705
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007706#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7707#include "nodes_shared.h"
7708#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007709#ifdef ART_ENABLE_CODEGEN_mips
7710#include "nodes_mips.h"
7711#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007712#ifdef ART_ENABLE_CODEGEN_x86
7713#include "nodes_x86.h"
7714#endif
7715
7716namespace art {
7717
Igor Murashkin6ef45672017-08-08 13:59:55 -07007718class OptimizingCompilerStats;
7719
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007720class HGraphVisitor : public ValueObject {
7721 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007722 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7723 : stats_(stats),
7724 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007725 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007726
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007727 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007728 virtual void VisitBasicBlock(HBasicBlock* block);
7729
Roland Levillain633021e2014-10-01 14:12:25 +01007730 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007731 void VisitInsertionOrder();
7732
Roland Levillain633021e2014-10-01 14:12:25 +01007733 // Visit the graph following dominator tree reverse post-order.
7734 void VisitReversePostOrder();
7735
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007736 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007737
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007738 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007739#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007740 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7741
7742 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7743
7744#undef DECLARE_VISIT_INSTRUCTION
7745
Igor Murashkin6ef45672017-08-08 13:59:55 -07007746 protected:
7747 OptimizingCompilerStats* stats_;
7748
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007749 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007750 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007751
7752 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7753};
7754
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007755class HGraphDelegateVisitor : public HGraphVisitor {
7756 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007757 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7758 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007759 virtual ~HGraphDelegateVisitor() {}
7760
7761 // Visit functions that delegate to to super class.
7762#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007763 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007764
7765 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7766
7767#undef DECLARE_VISIT_INSTRUCTION
7768
7769 private:
7770 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7771};
7772
Artem Serovcced8ba2017-07-19 18:18:09 +01007773// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7774// and remove the old instruction.
7775HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7776
7777// Create a clone for each clonable instructions/phis and replace the original with the clone.
7778//
7779// Used for testing individual instruction cloner.
7780class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7781 public:
7782 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007783 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007784
7785 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7786 if (instruction->IsClonable()) {
7787 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007788 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007789 }
7790 }
7791
Artem Serov7f4aff62017-06-21 17:02:18 +01007792 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007793
7794 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007795 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007796
7797 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7798};
7799
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007800// Iterator over the blocks that art part of the loop. Includes blocks part
7801// of an inner loop. The order in which the blocks are iterated is on their
7802// block id.
7803class HBlocksInLoopIterator : public ValueObject {
7804 public:
7805 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7806 : blocks_in_loop_(info.GetBlocks()),
7807 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7808 index_(0) {
7809 if (!blocks_in_loop_.IsBitSet(index_)) {
7810 Advance();
7811 }
7812 }
7813
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007814 bool Done() const { return index_ == blocks_.size(); }
7815 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007816 void Advance() {
7817 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007818 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007819 if (blocks_in_loop_.IsBitSet(index_)) {
7820 break;
7821 }
7822 }
7823 }
7824
7825 private:
7826 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007827 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007828 size_t index_;
7829
7830 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7831};
7832
Mingyao Yang3584bce2015-05-19 16:01:59 -07007833// Iterator over the blocks that art part of the loop. Includes blocks part
7834// of an inner loop. The order in which the blocks are iterated is reverse
7835// post order.
7836class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7837 public:
7838 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7839 : blocks_in_loop_(info.GetBlocks()),
7840 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7841 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007842 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007843 Advance();
7844 }
7845 }
7846
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007847 bool Done() const { return index_ == blocks_.size(); }
7848 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007849 void Advance() {
7850 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007851 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7852 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007853 break;
7854 }
7855 }
7856 }
7857
7858 private:
7859 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007860 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007861 size_t index_;
7862
7863 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7864};
7865
Aart Bikf3e61ee2017-04-12 17:09:20 -07007866// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007867inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007868 if (constant->IsIntConstant()) {
7869 return constant->AsIntConstant()->GetValue();
7870 } else if (constant->IsLongConstant()) {
7871 return constant->AsLongConstant()->GetValue();
7872 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007873 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007874 return 0;
7875 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007876}
7877
Aart Bikf3e61ee2017-04-12 17:09:20 -07007878// Returns true iff instruction is an integral constant (and sets value on success).
7879inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7880 if (instruction->IsIntConstant()) {
7881 *value = instruction->AsIntConstant()->GetValue();
7882 return true;
7883 } else if (instruction->IsLongConstant()) {
7884 *value = instruction->AsLongConstant()->GetValue();
7885 return true;
7886 } else if (instruction->IsNullConstant()) {
7887 *value = 0;
7888 return true;
7889 }
7890 return false;
7891}
7892
Aart Bik0148de42017-09-05 09:25:01 -07007893// Returns true iff instruction is the given integral constant.
7894inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7895 int64_t val = 0;
7896 return IsInt64AndGet(instruction, &val) && val == value;
7897}
7898
7899// Returns true iff instruction is a zero bit pattern.
7900inline bool IsZeroBitPattern(HInstruction* instruction) {
7901 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7902}
7903
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007904// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007905#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007906 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7907 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7908#undef INSTRUCTION_TYPE_CHECK
7909
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007910// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007911#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7912 std::is_base_of<BaseType, H##type>::value,
7913#define INSTRUCTION_TYPE_CHECK(type, super) \
7914 inline bool HInstruction::Is##type() const { \
7915 DCHECK_LT(GetKind(), kLastInstructionKind); \
7916 using BaseType = H##type; \
7917 static constexpr bool results[] = { \
7918 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7919 }; \
7920 return results[static_cast<size_t>(GetKind())]; \
7921 }
7922
7923 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7924#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007925#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007926
7927#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007928 inline const H##type* HInstruction::As##type() const { \
7929 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7930 } \
7931 inline H##type* HInstruction::As##type() { \
7932 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7933 }
7934
Vladimir Markoa90dd512018-05-04 15:04:45 +01007935 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7936#undef INSTRUCTION_TYPE_CAST
7937
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007938
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007939// Create space in `blocks` for adding `number_of_new_blocks` entries
7940// starting at location `at`. Blocks after `at` are moved accordingly.
7941inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7942 size_t number_of_new_blocks,
7943 size_t after) {
7944 DCHECK_LT(after, blocks->size());
7945 size_t old_size = blocks->size();
7946 size_t new_size = old_size + number_of_new_blocks;
7947 blocks->resize(new_size);
7948 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7949}
7950
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007951/*
7952 * Hunt "under the hood" of array lengths (leading to array references),
7953 * null checks (also leading to array references), and new arrays
7954 * (leading to the actual length). This makes it more likely related
7955 * instructions become actually comparable.
7956 */
7957inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7958 while (instruction->IsArrayLength() ||
7959 instruction->IsNullCheck() ||
7960 instruction->IsNewArray()) {
7961 instruction = instruction->IsNewArray()
7962 ? instruction->AsNewArray()->GetLength()
7963 : instruction->InputAt(0);
7964 }
7965 return instruction;
7966}
7967
Artem Serov21c7e6f2017-07-27 16:04:42 +01007968void RemoveEnvironmentUses(HInstruction* instruction);
7969bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7970void ResetEnvironmentInputRecords(HInstruction* instruction);
7971
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007972} // namespace art
7973
7974#endif // ART_COMPILER_OPTIMIZING_NODES_H_