blob: 9dcd741388246f43faac067a1609b4869888e8d7 [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"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010044#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010045#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
47namespace art {
48
Vladimir Markoca6fff82017-10-03 14:49:14 +010049class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000050class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070052class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010053class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000057class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000059class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000060class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000061class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000063class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070064class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010066class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010067class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010068class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000069class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010070class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000071class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072
Mathieu Chartier736b5602015-09-02 14:54:11 -070073namespace mirror {
74class DexCache;
75} // namespace mirror
76
Nicolas Geoffray818f2102014-02-18 16:43:35 +000077static const int kDefaultNumberOfBlocks = 8;
78static const int kDefaultNumberOfSuccessors = 2;
79static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010080static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010081static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000082static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083
Roland Levillain5b5b9312016-03-22 14:57:31 +000084// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
85static constexpr int32_t kMaxIntShiftDistance = 0x1f;
86// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
87static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000088
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010089static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070090static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010091
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010092static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
93
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060094static constexpr uint32_t kNoDexPc = -1;
95
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010096inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
97 // For the purposes of the compiler, the dex files must actually be the same object
98 // if we want to safely treat them as the same. This is especially important for JIT
99 // as custom class loaders can open the same underlying file (or memory) multiple
100 // times and provide different class resolution but no two class loaders should ever
101 // use the same DexFile object - doing so is an unsupported hack that can lead to
102 // all sorts of weird failures.
103 return &lhs == &rhs;
104}
105
Dave Allison20dfc792014-06-16 20:44:29 -0700106enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700107 // All types.
108 kCondEQ, // ==
109 kCondNE, // !=
110 // Signed integers and floating-point numbers.
111 kCondLT, // <
112 kCondLE, // <=
113 kCondGT, // >
114 kCondGE, // >=
115 // Unsigned integers.
116 kCondB, // <
117 kCondBE, // <=
118 kCondA, // >
119 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100120 // First and last aliases.
121 kCondFirst = kCondEQ,
122 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700123};
124
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000125enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000126 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000127 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000128 kAnalysisFailThrowCatchLoop,
129 kAnalysisFailAmbiguousArrayOp,
130 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000131};
132
Andreas Gampe9186ced2016-12-12 14:28:21 -0800133template <typename T>
134static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
135 return static_cast<typename std::make_unsigned<T>::type>(x);
136}
137
Vladimir Markof9f64412015-09-02 14:05:49 +0100138class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 public:
140 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
141
142 void AddInstruction(HInstruction* instruction);
143 void RemoveInstruction(HInstruction* instruction);
144
David Brazdilc3d743f2015-04-22 13:40:50 +0100145 // Insert `instruction` before/after an existing instruction `cursor`.
146 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
147 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
148
Roland Levillain6b469232014-09-25 10:10:38 +0100149 // Return true if this list contains `instruction`.
150 bool Contains(HInstruction* instruction) const;
151
Roland Levillainccc07a92014-09-16 14:48:16 +0100152 // Return true if `instruction1` is found before `instruction2` in
153 // this instruction list and false otherwise. Abort if none
154 // of these instructions is found.
155 bool FoundBefore(const HInstruction* instruction1,
156 const HInstruction* instruction2) const;
157
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000158 bool IsEmpty() const { return first_instruction_ == nullptr; }
159 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
160
161 // Update the block of all instructions to be `block`.
162 void SetBlockOfInstructions(HBasicBlock* block) const;
163
164 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000165 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000166 void Add(const HInstructionList& instruction_list);
167
David Brazdil2d7352b2015-04-20 14:52:42 +0100168 // Return the number of instructions in the list. This is an expensive operation.
169 size_t CountSize() const;
170
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100171 private:
172 HInstruction* first_instruction_;
173 HInstruction* last_instruction_;
174
175 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000176 friend class HGraph;
177 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100178 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000179 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100180 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100181
182 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
183};
184
David Brazdil4833f5a2015-12-16 10:37:39 +0000185class ReferenceTypeInfo : ValueObject {
186 public:
187 typedef Handle<mirror::Class> TypeHandle;
188
Vladimir Markoa1de9182016-02-25 11:37:38 +0000189 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
190
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700191 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100192 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
193 }
194
Vladimir Markoa1de9182016-02-25 11:37:38 +0000195 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000196 return ReferenceTypeInfo(type_handle, is_exact);
197 }
198
199 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
200
Vladimir Markof39745e2016-01-26 12:16:55 +0000201 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000202 return handle.GetReference() != nullptr;
203 }
204
Vladimir Marko456307a2016-04-19 14:12:13 +0000205 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000206 return IsValidHandle(type_handle_);
207 }
208
209 bool IsExact() const { return is_exact_; }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsObjectClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return GetTypeHandle()->IsStringClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsInterface();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsArrayClass();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
244 }
245
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000247 DCHECK(IsValid());
248 if (!IsExact()) return false;
249 if (!IsArrayClass()) return false;
250 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
251 }
252
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000254 DCHECK(IsValid());
255 if (!IsExact()) return false;
256 if (!IsArrayClass()) return false;
257 if (!rti.IsArrayClass()) return false;
258 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
259 rti.GetTypeHandle()->GetComponentType());
260 }
261
262 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
263
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000265 DCHECK(IsValid());
266 DCHECK(rti.IsValid());
267 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
268 }
269
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700270 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000271 DCHECK(IsValid());
272 DCHECK(rti.IsValid());
273 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
274 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
275 }
276
277 // Returns true if the type information provide the same amount of details.
278 // Note that it does not mean that the instructions have the same actual type
279 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000281 if (!IsValid() && !rti.IsValid()) {
282 // Invalid types are equal.
283 return true;
284 }
285 if (!IsValid() || !rti.IsValid()) {
286 // One is valid, the other not.
287 return false;
288 }
289 return IsExact() == rti.IsExact()
290 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
291 }
292
293 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000294 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
295 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
296 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000297
298 // The class of the object.
299 TypeHandle type_handle_;
300 // Whether or not the type is exact or a superclass of the actual type.
301 // Whether or not we have any information about this type.
302 bool is_exact_;
303};
304
305std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
306
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100308class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000309 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100310 HGraph(ArenaAllocator* allocator,
311 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 const DexFile& dex_file,
313 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100315 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100316 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000317 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100318 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100319 : allocator_(allocator),
320 arena_stack_(arena_stack),
321 blocks_(allocator->Adapter(kArenaAllocBlockList)),
322 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
323 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700324 entry_block_(nullptr),
325 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100326 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100327 number_of_vregs_(0),
328 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000329 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400330 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000331 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700332 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800333 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000334 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000335 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100337 dex_file_(dex_file),
338 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100339 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100340 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800341 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700342 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000343 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100344 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
345 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
346 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
347 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000348 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100349 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000350 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700351 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100352 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100353 blocks_.reserve(kDefaultNumberOfBlocks);
354 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355
David Brazdilbadd8262016-02-02 16:28:56 +0000356 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700357 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000358
Vladimir Markoca6fff82017-10-03 14:49:14 +0100359 ArenaAllocator* GetAllocator() const { return allocator_; }
360 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100361 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
362
David Brazdil69ba7b72015-06-23 18:27:30 +0100363 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000364 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100365
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000366 HBasicBlock* GetEntryBlock() const { return entry_block_; }
367 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100368 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000369
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000370 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
371 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000372
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100374
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100375 void ComputeDominanceInformation();
376 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000377 void ClearLoopInformation();
378 void FindBackEdges(ArenaBitVector* visited);
379 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100380 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100381 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000382
David Brazdil4833f5a2015-12-16 10:37:39 +0000383 // Analyze all natural loops in this graph. Returns a code specifying that it
384 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000385 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000386 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387
David Brazdilffee3d32015-07-06 11:48:53 +0100388 // Iterate over blocks to compute try block membership. Needs reverse post
389 // order and loop information.
390 void ComputeTryBlockInformation();
391
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000393 // Returns the instruction to replace the invoke expression or null if the
394 // invoke is for a void method. Note that the caller is responsible for replacing
395 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000396 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000397
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000398 // Update the loop and try membership of `block`, which was spawned from `reference`.
399 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
400 // should be the new back edge.
401 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
402 HBasicBlock* reference,
403 bool replace_if_back_edge);
404
Mingyao Yang3584bce2015-05-19 16:01:59 -0700405 // Need to add a couple of blocks to test if the loop body is entered and
406 // put deoptimization instructions, etc.
407 void TransformLoopHeaderForBCE(HBasicBlock* header);
408
Aart Bikf8f5a162017-02-06 15:35:29 -0800409 // Adds a new loop directly after the loop with the given header and exit.
410 // Returns the new preheader.
411 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
412 HBasicBlock* body,
413 HBasicBlock* exit);
414
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000415 // Removes `block` from the graph. Assumes `block` has been disconnected from
416 // other blocks and has no instructions or phis.
417 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000418
David Brazdilfc6a86a2015-06-26 10:33:45 +0000419 // Splits the edge between `block` and `successor` while preserving the
420 // indices in the predecessor/successor lists. If there are multiple edges
421 // between the blocks, the lowest indices are used.
422 // Returns the new block which is empty and has the same dex pc as `successor`.
423 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
424
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100425 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100426 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000427
428 // Transform a loop into a format with a single preheader.
429 //
430 // Each phi in the header should be split: original one in the header should only hold
431 // inputs reachable from the back edges and a single input from the preheader. The newly created
432 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
433 //
434 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
435 // that no longer have this property.
436 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
437
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100438 void SimplifyLoop(HBasicBlock* header);
439
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100441 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000442 return current_instruction_id_++;
443 }
444
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000445 int32_t GetCurrentInstructionId() const {
446 return current_instruction_id_;
447 }
448
449 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100450 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000451 current_instruction_id_ = id;
452 }
453
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100454 uint16_t GetMaximumNumberOfOutVRegs() const {
455 return maximum_number_of_out_vregs_;
456 }
457
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000458 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
459 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100460 }
461
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100462 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
463 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
464 }
465
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000466 void UpdateTemporariesVRegSlots(size_t slots) {
467 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100468 }
469
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000470 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100471 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000472 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100473 }
474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100475 void SetNumberOfVRegs(uint16_t number_of_vregs) {
476 number_of_vregs_ = number_of_vregs;
477 }
478
479 uint16_t GetNumberOfVRegs() const {
480 return number_of_vregs_;
481 }
482
483 void SetNumberOfInVRegs(uint16_t value) {
484 number_of_in_vregs_ = value;
485 }
486
David Brazdildee58d62016-04-07 09:54:26 +0000487 uint16_t GetNumberOfInVRegs() const {
488 return number_of_in_vregs_;
489 }
490
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100491 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100492 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100493 return number_of_vregs_ - number_of_in_vregs_;
494 }
495
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100496 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100497 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100498 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100499
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100500 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
501 DCHECK(GetReversePostOrder()[0] == entry_block_);
502 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
503 }
504
505 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
506 return ReverseRange(GetReversePostOrder());
507 }
508
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100509 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100510 return linear_order_;
511 }
512
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100513 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
514 return ReverseRange(GetLinearOrder());
515 }
516
Mark Mendell1152c922015-04-24 17:06:35 -0400517 bool HasBoundsChecks() const {
518 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800519 }
520
Mark Mendell1152c922015-04-24 17:06:35 -0400521 void SetHasBoundsChecks(bool value) {
522 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800523 }
524
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000525 bool IsDebuggable() const { return debuggable_; }
526
David Brazdil8d5b8b22015-03-24 10:51:52 +0000527 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 // already, it is created and inserted into the graph. This method is only for
529 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100530 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000531
532 // TODO: This is problematic for the consistency of reference type propagation
533 // because it can be created anytime after the pass and thus it will be left
534 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600535 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000536
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600537 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
538 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000539 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600540 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
541 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000542 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600543 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
544 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000545 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600546 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
547 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000549
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100550 HCurrentMethod* GetCurrentMethod();
551
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100552 const DexFile& GetDexFile() const {
553 return dex_file_;
554 }
555
556 uint32_t GetMethodIdx() const {
557 return method_idx_;
558 }
559
Igor Murashkind01745e2017-04-05 16:40:31 -0700560 // Get the method name (without the signature), e.g. "<init>"
561 const char* GetMethodName() const;
562
563 // Get the pretty method name (class + name + optionally signature).
564 std::string PrettyMethod(bool with_signature = true) const;
565
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100566 InvokeType GetInvokeType() const {
567 return invoke_type_;
568 }
569
Mark Mendellc4701932015-04-10 13:18:51 -0400570 InstructionSet GetInstructionSet() const {
571 return instruction_set_;
572 }
573
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000574 bool IsCompilingOsr() const { return osr_; }
575
Mingyao Yang063fc772016-08-02 11:02:54 -0700576 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
577 return cha_single_implementation_list_;
578 }
579
580 void AddCHASingleImplementationDependency(ArtMethod* method) {
581 cha_single_implementation_list_.insert(method);
582 }
583
584 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800585 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700586 }
587
David Brazdil77a48ae2015-09-15 12:34:04 +0000588 bool HasTryCatch() const { return has_try_catch_; }
589 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100590
Aart Bikb13c65b2017-03-21 20:14:07 -0700591 bool HasSIMD() const { return has_simd_; }
592 void SetHasSIMD(bool value) { has_simd_ = value; }
593
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800594 bool HasLoops() const { return has_loops_; }
595 void SetHasLoops(bool value) { has_loops_ = value; }
596
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000597 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
598 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
599
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100600 ArtMethod* GetArtMethod() const { return art_method_; }
601 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
602
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100603 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500604 // The instruction has been inserted into the graph, either as a constant, or
605 // before cursor.
606 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
607
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000608 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
609
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800610 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
611 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
612 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
613
David Brazdil2d7352b2015-04-20 14:52:42 +0100614 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000615 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100616 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000617
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000618 template <class InstructionType, typename ValueType>
619 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600620 ArenaSafeMap<ValueType, InstructionType*>* cache,
621 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 // Try to find an existing constant of the given value.
623 InstructionType* constant = nullptr;
624 auto cached_constant = cache->find(value);
625 if (cached_constant != cache->end()) {
626 constant = cached_constant->second;
627 }
628
629 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100630 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000631 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100632 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000633 cache->Overwrite(value, constant);
634 InsertConstant(constant);
635 }
636 return constant;
637 }
638
David Brazdil8d5b8b22015-03-24 10:51:52 +0000639 void InsertConstant(HConstant* instruction);
640
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000641 // Cache a float constant into the graph. This method should only be
642 // called by the SsaBuilder when creating "equivalent" instructions.
643 void CacheFloatConstant(HFloatConstant* constant);
644
645 // See CacheFloatConstant comment.
646 void CacheDoubleConstant(HDoubleConstant* constant);
647
Vladimir Markoca6fff82017-10-03 14:49:14 +0100648 ArenaAllocator* const allocator_;
649 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000650
651 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100654 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100655 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000656
Aart Bik281c6812016-08-26 11:31:48 -0700657 // List of blocks to perform a linear order tree traversal. Unlike the reverse
658 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100659 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100660
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000661 HBasicBlock* entry_block_;
662 HBasicBlock* exit_block_;
663
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100664 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100665 uint16_t maximum_number_of_out_vregs_;
666
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100667 // The number of virtual registers in this method. Contains the parameters.
668 uint16_t number_of_vregs_;
669
670 // The number of virtual registers used by parameters of this method.
671 uint16_t number_of_in_vregs_;
672
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000673 // Number of vreg size slots that the temporaries use (used in baseline compiler).
674 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100675
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800676 // Flag whether there are bounds checks in the graph. We can skip
677 // BCE if it's false. It's only best effort to keep it up to date in
678 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400679 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800680
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800681 // Flag whether there are try/catch blocks in the graph. We will skip
682 // try/catch-related passes if it's false. It's only best effort to keep
683 // it up to date in the presence of code elimination so there might be
684 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000685 bool has_try_catch_;
686
Aart Bikb13c65b2017-03-21 20:14:07 -0700687 // Flag whether SIMD instructions appear in the graph. If true, the
688 // code generators may have to be more careful spilling the wider
689 // contents of SIMD registers.
690 bool has_simd_;
691
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800692 // Flag whether there are any loops in the graph. We can skip loop
693 // optimization if it's false. It's only best effort to keep it up
694 // to date in the presence of code elimination so there might be false
695 // positives.
696 bool has_loops_;
697
698 // Flag whether there are any irreducible loops in the graph. It's only
699 // best effort to keep it up to date in the presence of code elimination
700 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000701 bool has_irreducible_loops_;
702
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000703 // Indicates whether the graph should be compiled in a way that
704 // ensures full debuggability. If false, we can apply more
705 // aggressive optimizations that may limit the level of debugging.
706 const bool debuggable_;
707
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000708 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000709 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000710
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100711 // The dex file from which the method is from.
712 const DexFile& dex_file_;
713
714 // The method index in the dex file.
715 const uint32_t method_idx_;
716
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100717 // If inlined, this encodes how the callee is being invoked.
718 const InvokeType invoke_type_;
719
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100720 // Whether the graph has been transformed to SSA form. Only used
721 // in debug mode to ensure we are not using properties only valid
722 // for non-SSA form (like the number of temporaries).
723 bool in_ssa_form_;
724
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800725 // Number of CHA guards in the graph. Used to short-circuit the
726 // CHA guard optimization pass when there is no CHA guard left.
727 uint32_t number_of_cha_guards_;
728
Mathieu Chartiere401d142015-04-22 13:56:20 -0700729 const InstructionSet instruction_set_;
730
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000731 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000732 HNullConstant* cached_null_constant_;
733 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000734 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000735 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000736 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000737
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100738 HCurrentMethod* cached_current_method_;
739
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100740 // The ArtMethod this graph is for. Note that for AOT, it may be null,
741 // for example for methods whose declaring class could not be resolved
742 // (such as when the superclass could not be found).
743 ArtMethod* art_method_;
744
David Brazdil4833f5a2015-12-16 10:37:39 +0000745 // Keep the RTI of inexact Object to avoid having to pass stack handle
746 // collection pointer to passes which may create NullConstant.
747 ReferenceTypeInfo inexact_object_rti_;
748
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000749 // Whether we are compiling this graph for on stack replacement: this will
750 // make all loops seen as irreducible and emit special stack maps to mark
751 // compiled code entries which the interpreter can directly jump to.
752 const bool osr_;
753
Mingyao Yang063fc772016-08-02 11:02:54 -0700754 // List of methods that are assumed to have single implementation.
755 ArenaSet<ArtMethod*> cha_single_implementation_list_;
756
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000757 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100758 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000759 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000760 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000761 DISALLOW_COPY_AND_ASSIGN(HGraph);
762};
763
Vladimir Markof9f64412015-09-02 14:05:49 +0100764class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000765 public:
766 HLoopInformation(HBasicBlock* header, HGraph* graph)
767 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100768 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000769 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100770 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100771 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100772 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100773 blocks_(graph->GetAllocator(),
774 graph->GetBlocks().size(),
775 true,
776 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100777 back_edges_.reserve(kDefaultNumberOfBackEdges);
778 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000780 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100781 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000782
783 void Dump(std::ostream& os);
784
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 HBasicBlock* GetHeader() const {
786 return header_;
787 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000788
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000789 void SetHeader(HBasicBlock* block) {
790 header_ = block;
791 }
792
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100793 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
794 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
795 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
796
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000797 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100798 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000799 }
800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100801 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100802 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100803 }
804
David Brazdil46e2a392015-03-16 17:31:52 +0000805 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100806 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 }
808
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000809 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100810 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000811 }
812
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100813 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100814
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100815 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100817 }
818
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100819 // Returns the lifetime position of the back edge that has the
820 // greatest lifetime position.
821 size_t GetLifetimeEnd() const;
822
823 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100824 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100825 }
826
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000827 // Finds blocks that are part of this loop.
828 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100829
Artem Serov7f4aff62017-06-21 17:02:18 +0100830 // Updates blocks population of the loop and all of its outer' ones recursively after the
831 // population of the inner loop is updated.
832 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
833
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100834 // Returns whether this loop information contains `block`.
835 // Note that this loop information *must* be populated before entering this function.
836 bool Contains(const HBasicBlock& block) const;
837
838 // Returns whether this loop information is an inner loop of `other`.
839 // Note that `other` *must* be populated before entering this function.
840 bool IsIn(const HLoopInformation& other) const;
841
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800842 // Returns true if instruction is not defined within this loop.
843 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700844
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100845 const ArenaBitVector& GetBlocks() const { return blocks_; }
846
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000847 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000848 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000849
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000850 void ClearAllBlocks() {
851 blocks_.ClearAllBits();
852 }
853
David Brazdil3f4a5222016-05-06 12:46:21 +0100854 bool HasBackEdgeNotDominatedByHeader() const;
855
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100856 bool IsPopulated() const {
857 return blocks_.GetHighestBitSet() != -1;
858 }
859
Anton Shaminf89381f2016-05-16 16:44:13 +0600860 bool DominatesAllBackEdges(HBasicBlock* block);
861
David Sehrc757dec2016-11-04 15:48:34 -0700862 bool HasExitEdge() const;
863
Artem Serov7f4aff62017-06-21 17:02:18 +0100864 // Resets back edge and blocks-in-loop data.
865 void ResetBasicBlockData() {
866 back_edges_.clear();
867 ClearAllBlocks();
868 }
869
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100871 // Internal recursive implementation of `Populate`.
872 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100873 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100874
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100876 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000877 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100878 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100879 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100880 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000881
882 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
883};
884
David Brazdilec16f792015-08-19 15:04:01 +0100885// Stores try/catch information for basic blocks.
886// Note that HGraph is constructed so that catch blocks cannot simultaneously
887// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100888class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100889 public:
890 // Try block information constructor.
891 explicit TryCatchInformation(const HTryBoundary& try_entry)
892 : try_entry_(&try_entry),
893 catch_dex_file_(nullptr),
894 catch_type_index_(DexFile::kDexNoIndex16) {
895 DCHECK(try_entry_ != nullptr);
896 }
897
898 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800899 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100900 : try_entry_(nullptr),
901 catch_dex_file_(&dex_file),
902 catch_type_index_(catch_type_index) {}
903
904 bool IsTryBlock() const { return try_entry_ != nullptr; }
905
906 const HTryBoundary& GetTryEntry() const {
907 DCHECK(IsTryBlock());
908 return *try_entry_;
909 }
910
911 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
912
913 bool IsCatchAllTypeIndex() const {
914 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800915 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100916 }
917
Andreas Gampea5b09a62016-11-17 15:21:22 -0800918 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100919 DCHECK(IsCatchBlock());
920 return catch_type_index_;
921 }
922
923 const DexFile& GetCatchDexFile() const {
924 DCHECK(IsCatchBlock());
925 return *catch_dex_file_;
926 }
927
928 private:
929 // One of possibly several TryBoundary instructions entering the block's try.
930 // Only set for try blocks.
931 const HTryBoundary* try_entry_;
932
933 // Exception type information. Only set for catch blocks.
934 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800935 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100936};
937
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100938static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100939static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941// A block in a method. Contains the list of instructions represented
942// as a double linked list. Each block knows its predecessors and
943// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100944
Vladimir Markof9f64412015-09-02 14:05:49 +0100945class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000946 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700947 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100949 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
950 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000951 loop_information_(nullptr),
952 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100953 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100954 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100955 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100956 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000957 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000958 try_catch_information_(nullptr) {
959 predecessors_.reserve(kDefaultNumberOfPredecessors);
960 successors_.reserve(kDefaultNumberOfSuccessors);
961 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
962 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963
Vladimir Marko60584552015-09-03 13:35:12 +0000964 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100965 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000966 }
967
Vladimir Marko60584552015-09-03 13:35:12 +0000968 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 }
971
David Brazdild26a4112015-11-10 11:07:31 +0000972 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
973 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
974
Vladimir Marko60584552015-09-03 13:35:12 +0000975 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
976 return ContainsElement(successors_, block, start_from);
977 }
978
979 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100980 return dominated_blocks_;
981 }
982
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100983 bool IsEntryBlock() const {
984 return graph_->GetEntryBlock() == this;
985 }
986
987 bool IsExitBlock() const {
988 return graph_->GetExitBlock() == this;
989 }
990
David Brazdil46e2a392015-03-16 17:31:52 +0000991 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200992 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700993 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 bool IsSingleTryBoundary() const;
995
996 // Returns true if this block emits nothing but a jump.
997 bool IsSingleJump() const {
998 HLoopInformation* loop_info = GetLoopInformation();
999 return (IsSingleGoto() || IsSingleTryBoundary())
1000 // Back edges generate a suspend check.
1001 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1002 }
David Brazdil46e2a392015-03-16 17:31:52 +00001003
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001004 void AddBackEdge(HBasicBlock* back_edge) {
1005 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001006 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001007 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001008 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001009 loop_information_->AddBackEdge(back_edge);
1010 }
1011
Artem Serov7f4aff62017-06-21 17:02:18 +01001012 // Registers a back edge; if the block was not a loop header before the call associates a newly
1013 // created loop info with it.
1014 //
1015 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1016 // info for all blocks during back edges recalculation.
1017 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1018 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1019 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1020 }
1021 loop_information_->AddBackEdge(back_edge);
1022 }
1023
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001024 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001027 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001028 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001029 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001031 HBasicBlock* GetDominator() const { return dominator_; }
1032 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001033 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1034
1035 void RemoveDominatedBlock(HBasicBlock* block) {
1036 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001037 }
Vladimir Marko60584552015-09-03 13:35:12 +00001038
1039 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1040 ReplaceElement(dominated_blocks_, existing, new_block);
1041 }
1042
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001043 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001044
1045 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001046 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001047 }
1048
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001049 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1050 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001051 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001052 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001053 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1054 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001056 HInstruction* GetFirstInstructionDisregardMoves() const;
1057
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001058 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001059 successors_.push_back(block);
1060 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061 }
1062
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001063 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1064 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001065 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001066 new_block->predecessors_.push_back(this);
1067 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001068 }
1069
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1071 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001073 new_block->successors_.push_back(this);
1074 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001075 }
1076
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001077 // Insert `this` between `predecessor` and `successor. This method
1078 // preserves the indicies, and will update the first edge found between
1079 // `predecessor` and `successor`.
1080 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1081 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001082 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001083 successor->predecessors_[predecessor_index] = this;
1084 predecessor->successors_[successor_index] = this;
1085 successors_.push_back(successor);
1086 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001087 }
1088
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001089 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001090 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001091 }
1092
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001093 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001094 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001095 }
1096
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001097 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001098 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001099 }
1100
1101 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001102 predecessors_.push_back(block);
1103 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001104 }
1105
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001106 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001107 DCHECK_EQ(predecessors_.size(), 2u);
1108 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001109 }
1110
David Brazdil769c9e52015-04-27 13:54:09 +01001111 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001112 DCHECK_EQ(successors_.size(), 2u);
1113 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001114 }
1115
David Brazdilfc6a86a2015-06-26 10:33:45 +00001116 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001117 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001118 }
1119
David Brazdilfc6a86a2015-06-26 10:33:45 +00001120 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001121 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001122 }
1123
David Brazdilfc6a86a2015-06-26 10:33:45 +00001124 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001125 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001126 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001127 }
1128
1129 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001130 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001131 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001132 }
1133
1134 // Returns whether the first occurrence of `predecessor` in the list of
1135 // predecessors is at index `idx`.
1136 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001137 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001138 return GetPredecessorIndexOf(predecessor) == idx;
1139 }
1140
David Brazdild7558da2015-09-22 13:04:14 +01001141 // Create a new block between this block and its predecessors. The new block
1142 // is added to the graph, all predecessor edges are relinked to it and an edge
1143 // is created to `this`. Returns the new empty block. Reverse post order or
1144 // loop and try/catch information are not updated.
1145 HBasicBlock* CreateImmediateDominator();
1146
David Brazdilfc6a86a2015-06-26 10:33:45 +00001147 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001148 // created, latter block. Note that this method will add the block to the
1149 // graph, create a Goto at the end of the former block and will create an edge
1150 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001151 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001152 HBasicBlock* SplitBefore(HInstruction* cursor);
1153
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001154 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001155 // created block. Note that this method just updates raw block information,
1156 // like predecessors, successors, dominators, and instruction list. It does not
1157 // update the graph, reverse post order, loop information, nor make sure the
1158 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001159 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1160
1161 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1162 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001163
1164 // Merge `other` at the end of `this`. Successors and dominated blocks of
1165 // `other` are changed to be successors and dominated blocks of `this`. Note
1166 // that this method does not update the graph, reverse post order, loop
1167 // information, nor make sure the blocks are consistent (for example ending
1168 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001169 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001170
1171 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1172 // of `this` are moved to `other`.
1173 // Note that this method does not update the graph, reverse post order, loop
1174 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001175 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001176 void ReplaceWith(HBasicBlock* other);
1177
Aart Bik6b69e0a2017-01-11 10:20:43 -08001178 // Merges the instructions of `other` at the end of `this`.
1179 void MergeInstructionsWith(HBasicBlock* other);
1180
David Brazdil2d7352b2015-04-20 14:52:42 +01001181 // Merge `other` at the end of `this`. This method updates loops, reverse post
1182 // order, links to predecessors, successors, dominators and deletes the block
1183 // from the graph. The two blocks must be successive, i.e. `this` the only
1184 // predecessor of `other` and vice versa.
1185 void MergeWith(HBasicBlock* other);
1186
1187 // Disconnects `this` from all its predecessors, successors and dominator,
1188 // removes it from all loops it is included in and eventually from the graph.
1189 // The block must not dominate any other block. Predecessors and successors
1190 // are safely updated.
1191 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001192
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001193 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001194 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001195 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001196 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001197 // Replace phi `initial` with `replacement` within this block.
1198 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001199 // Replace instruction `initial` with `replacement` within this block.
1200 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1201 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001203 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001204 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1205 // instruction list. With 'ensure_safety' set to true, it verifies that the
1206 // instruction is not in use and removes it from the use lists of its inputs.
1207 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1208 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001209 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001210
1211 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001212 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001213 }
1214
Roland Levillain6b879dd2014-09-22 17:13:44 +01001215 bool IsLoopPreHeaderFirstPredecessor() const {
1216 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001217 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001218 }
1219
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001220 bool IsFirstPredecessorBackEdge() const {
1221 DCHECK(IsLoopHeader());
1222 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1223 }
1224
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001225 HLoopInformation* GetLoopInformation() const {
1226 return loop_information_;
1227 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001228
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001229 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001230 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001231 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001232 void SetInLoop(HLoopInformation* info) {
1233 if (IsLoopHeader()) {
1234 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001235 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001236 loop_information_ = info;
1237 } else if (loop_information_->Contains(*info->GetHeader())) {
1238 // Block is currently part of an outer loop. Make it part of this inner loop.
1239 // Note that a non loop header having a loop information means this loop information
1240 // has already been populated
1241 loop_information_ = info;
1242 } else {
1243 // Block is part of an inner loop. Do not update the loop information.
1244 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1245 // at this point, because this method is being called while populating `info`.
1246 }
1247 }
1248
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001249 // Raw update of the loop information.
1250 void SetLoopInformation(HLoopInformation* info) {
1251 loop_information_ = info;
1252 }
1253
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001254 bool IsInLoop() const { return loop_information_ != nullptr; }
1255
David Brazdilec16f792015-08-19 15:04:01 +01001256 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1257
1258 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1259 try_catch_information_ = try_catch_information;
1260 }
1261
1262 bool IsTryBlock() const {
1263 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1264 }
1265
1266 bool IsCatchBlock() const {
1267 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1268 }
David Brazdilffee3d32015-07-06 11:48:53 +01001269
1270 // Returns the try entry that this block's successors should have. They will
1271 // be in the same try, unless the block ends in a try boundary. In that case,
1272 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001273 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001274
Aart Bik75ff2c92018-04-21 01:28:11 +00001275 bool HasThrowingInstructions() const;
1276
David Brazdila4b8c212015-05-07 09:59:30 +01001277 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001278 bool Dominates(HBasicBlock* block) const;
1279
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001280 size_t GetLifetimeStart() const { return lifetime_start_; }
1281 size_t GetLifetimeEnd() const { return lifetime_end_; }
1282
1283 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1284 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1285
David Brazdil8d5b8b22015-03-24 10:51:52 +00001286 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001287 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001288 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001289 bool HasSinglePhi() const;
1290
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001291 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001292 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001293 ArenaVector<HBasicBlock*> predecessors_;
1294 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001295 HInstructionList instructions_;
1296 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001297 HLoopInformation* loop_information_;
1298 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001299 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001300 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001301 // The dex program counter of the first instruction of this block.
1302 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001303 size_t lifetime_start_;
1304 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001305 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001306
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001307 friend class HGraph;
1308 friend class HInstruction;
1309
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001310 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1311};
1312
David Brazdilb2bd1c52015-03-25 11:17:37 +00001313// Iterates over the LoopInformation of all loops which contain 'block'
1314// from the innermost to the outermost.
1315class HLoopInformationOutwardIterator : public ValueObject {
1316 public:
1317 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1318 : current_(block.GetLoopInformation()) {}
1319
1320 bool Done() const { return current_ == nullptr; }
1321
1322 void Advance() {
1323 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001324 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001325 }
1326
1327 HLoopInformation* Current() const {
1328 DCHECK(!Done());
1329 return current_;
1330 }
1331
1332 private:
1333 HLoopInformation* current_;
1334
1335 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1336};
1337
Alexandre Ramesef20f712015-06-09 10:29:30 +01001338#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001339 M(Above, Condition) \
1340 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001341 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001342 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001343 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 M(ArrayGet, Instruction) \
1345 M(ArrayLength, Instruction) \
1346 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001347 M(Below, Condition) \
1348 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001349 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001350 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001351 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001352 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001353 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001354 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001355 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001356 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001357 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001358 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001359 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001360 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001361 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001362 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001363 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001364 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(Exit, Instruction) \
1366 M(FloatConstant, Constant) \
1367 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(GreaterThan, Condition) \
1369 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001371 M(InstanceFieldGet, Instruction) \
1372 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001373 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001374 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001375 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001376 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001377 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001378 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001379 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001380 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001381 M(LessThan, Condition) \
1382 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001383 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001384 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001385 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001386 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001387 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001388 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001389 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001390 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001391 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001392 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001393 M(Neg, UnaryOperation) \
1394 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001395 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001396 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001397 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001398 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001400 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001401 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001403 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001404 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001405 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001406 M(Return, Instruction) \
1407 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001408 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001409 M(Shl, BinaryOperation) \
1410 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001411 M(StaticFieldGet, Instruction) \
1412 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001413 M(UnresolvedInstanceFieldGet, Instruction) \
1414 M(UnresolvedInstanceFieldSet, Instruction) \
1415 M(UnresolvedStaticFieldGet, Instruction) \
1416 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001417 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001418 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001419 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001420 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001421 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001422 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001423 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001424 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001425 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001426 M(VecExtractScalar, VecUnaryOperation) \
1427 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001428 M(VecCnv, VecUnaryOperation) \
1429 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001430 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001431 M(VecNot, VecUnaryOperation) \
1432 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001433 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001434 M(VecSub, VecBinaryOperation) \
1435 M(VecMul, VecBinaryOperation) \
1436 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001437 M(VecMin, VecBinaryOperation) \
1438 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001439 M(VecAnd, VecBinaryOperation) \
1440 M(VecAndNot, VecBinaryOperation) \
1441 M(VecOr, VecBinaryOperation) \
1442 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001443 M(VecSaturationAdd, VecBinaryOperation) \
1444 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001445 M(VecShl, VecBinaryOperation) \
1446 M(VecShr, VecBinaryOperation) \
1447 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001448 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001449 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001450 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001451 M(VecLoad, VecMemoryOperation) \
1452 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001453
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001454/*
1455 * Instructions, shared across several (not all) architectures.
1456 */
1457#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1458#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1459#else
1460#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001461 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001462 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001463 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001464 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001465#endif
1466
Alexandre Ramesef20f712015-06-09 10:29:30 +01001467#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1468
1469#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1470
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001471#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001472#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001473#else
1474#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1475 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001476 M(MipsPackedSwitch, Instruction) \
1477 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001478#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001480#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1481
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001482#ifndef ART_ENABLE_CODEGEN_x86
1483#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1484#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001485#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1486 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001487 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001488 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001489 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001490#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001491
1492#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1493
1494#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1495 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001496 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001497 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1498 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001499 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001500 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001501 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1502 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1503
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001504#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1505 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001506 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001507 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001508 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001509 M(Invoke, Instruction) \
1510 M(VecOperation, Instruction) \
1511 M(VecUnaryOperation, VecOperation) \
1512 M(VecBinaryOperation, VecOperation) \
1513 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001514
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001515#define FOR_EACH_INSTRUCTION(M) \
1516 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1517 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1518
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001519#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001520FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1521#undef FORWARD_DECLARATION
1522
Vladimir Marko372f10e2016-05-17 16:30:10 +01001523#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001524 private: \
1525 H##type& operator=(const H##type&) = delete; \
1526 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001527 const char* DebugName() const OVERRIDE { return #type; } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001528 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1529 DCHECK(IsClonable()); \
1530 return new (arena) H##type(*this->As##type()); \
1531 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001532 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001533
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001534#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001535 private: \
1536 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001537 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001538
Artem Serovcced8ba2017-07-19 18:18:09 +01001539#define DEFAULT_COPY_CONSTRUCTOR(type) \
1540 explicit H##type(const H##type& other) = default;
1541
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001542template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001543class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1544 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001545 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001546 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001547 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001548 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001549 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001550 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001551 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001553 private:
David Brazdiled596192015-01-23 10:39:45 +00001554 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001555 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001556
1557 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001558 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001559
Vladimir Marko46817b82016-03-29 12:21:58 +01001560 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001561
1562 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1563};
1564
David Brazdiled596192015-01-23 10:39:45 +00001565template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001566using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001567
David Brazdil1abb4192015-02-17 18:33:36 +00001568// This class is used by HEnvironment and HInstruction classes to record the
1569// instructions they use and pointers to the corresponding HUseListNodes kept
1570// by the used instructions.
1571template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001572class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001573 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001574 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1575 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001576
Vladimir Marko46817b82016-03-29 12:21:58 +01001577 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1578 : HUserRecord(old_record.instruction_, before_use_node) {}
1579 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1580 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001581 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001582 }
1583
1584 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001585 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1586 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001587
1588 private:
1589 // Instruction used by the user.
1590 HInstruction* instruction_;
1591
Vladimir Marko46817b82016-03-29 12:21:58 +01001592 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1593 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001594};
1595
Vladimir Markoe9004912016-06-16 16:50:52 +01001596// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1597// This is used for HInstruction::GetInputs() to return a container wrapper providing
1598// HInstruction* values even though the underlying container has HUserRecord<>s.
1599struct HInputExtractor {
1600 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1601 return record.GetInstruction();
1602 }
1603 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1604 return record.GetInstruction();
1605 }
1606};
1607
1608using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1609using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1610
Aart Bik854a02b2015-07-14 16:07:00 -07001611/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001612 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001613 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001614 * For write/read dependences on fields/arrays, the dependence analysis uses
1615 * type disambiguation (e.g. a float field write cannot modify the value of an
1616 * integer field read) and the access type (e.g. a reference array write cannot
1617 * modify the value of a reference field read [although it may modify the
1618 * reference fetch prior to reading the field, which is represented by its own
1619 * write/read dependence]). The analysis makes conservative points-to
1620 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1621 * the same, and any reference read depends on any reference read without
1622 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001623 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001624 * The internal representation uses 38-bit and is described in the table below.
1625 * The first line indicates the side effect, and for field/array accesses the
1626 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001627 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 * The two numbered lines below indicate the bit position in the bitfield (read
1629 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001630 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1632 * +-------------+---------+---------+--------------+---------+---------+
1633 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1634 * | 3 |333333322|222222221| 1 |111111110|000000000|
1635 * | 7 |654321098|765432109| 8 |765432109|876543210|
1636 *
1637 * Note that, to ease the implementation, 'changes' bits are least significant
1638 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001639 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001640class SideEffects : public ValueObject {
1641 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001642 SideEffects() : flags_(0) {}
1643
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001644 static SideEffects None() {
1645 return SideEffects(0);
1646 }
1647
1648 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001649 return SideEffects(kAllChangeBits | kAllDependOnBits);
1650 }
1651
1652 static SideEffects AllChanges() {
1653 return SideEffects(kAllChangeBits);
1654 }
1655
1656 static SideEffects AllDependencies() {
1657 return SideEffects(kAllDependOnBits);
1658 }
1659
1660 static SideEffects AllExceptGCDependency() {
1661 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1662 }
1663
1664 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001665 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001666 }
1667
Aart Bik34c3ba92015-07-20 14:08:59 -07001668 static SideEffects AllWrites() {
1669 return SideEffects(kAllWrites);
1670 }
1671
1672 static SideEffects AllReads() {
1673 return SideEffects(kAllReads);
1674 }
1675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001676 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001677 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001678 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001679 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001680 }
1681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001682 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001683 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684 }
1685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001687 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001688 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001689 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001690 }
1691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001692 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001693 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001694 }
1695
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001696 static SideEffects CanTriggerGC() {
1697 return SideEffects(1ULL << kCanTriggerGCBit);
1698 }
1699
1700 static SideEffects DependsOnGC() {
1701 return SideEffects(1ULL << kDependsOnGCBit);
1702 }
1703
Aart Bik854a02b2015-07-14 16:07:00 -07001704 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001705 SideEffects Union(SideEffects other) const {
1706 return SideEffects(flags_ | other.flags_);
1707 }
1708
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001709 SideEffects Exclusion(SideEffects other) const {
1710 return SideEffects(flags_ & ~other.flags_);
1711 }
1712
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001713 void Add(SideEffects other) {
1714 flags_ |= other.flags_;
1715 }
1716
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001717 bool Includes(SideEffects other) const {
1718 return (other.flags_ & flags_) == other.flags_;
1719 }
1720
1721 bool HasSideEffects() const {
1722 return (flags_ & kAllChangeBits);
1723 }
1724
1725 bool HasDependencies() const {
1726 return (flags_ & kAllDependOnBits);
1727 }
1728
1729 // Returns true if there are no side effects or dependencies.
1730 bool DoesNothing() const {
1731 return flags_ == 0;
1732 }
1733
Aart Bik854a02b2015-07-14 16:07:00 -07001734 // Returns true if something is written.
1735 bool DoesAnyWrite() const {
1736 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001737 }
1738
Aart Bik854a02b2015-07-14 16:07:00 -07001739 // Returns true if something is read.
1740 bool DoesAnyRead() const {
1741 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001742 }
1743
Aart Bik854a02b2015-07-14 16:07:00 -07001744 // Returns true if potentially everything is written and read
1745 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001746 bool DoesAllReadWrite() const {
1747 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1748 }
1749
Aart Bik854a02b2015-07-14 16:07:00 -07001750 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001751 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001752 }
1753
Roland Levillain0d5a2812015-11-13 10:07:31 +00001754 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001755 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1757 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001758 }
1759
1760 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001761 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001762 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001763 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001764 for (int s = kLastBit; s >= 0; s--) {
1765 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1766 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1767 // This is a bit for the GC side effect.
1768 if (current_bit_is_set) {
1769 flags += "GC";
1770 }
Aart Bik854a02b2015-07-14 16:07:00 -07001771 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001772 } else {
1773 // This is a bit for the array/field analysis.
1774 // The underscore character stands for the 'can trigger GC' bit.
1775 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1776 if (current_bit_is_set) {
1777 flags += kDebug[s];
1778 }
1779 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1780 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1781 flags += "|";
1782 }
1783 }
Aart Bik854a02b2015-07-14 16:07:00 -07001784 }
1785 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001786 }
1787
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001788 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 private:
1791 static constexpr int kFieldArrayAnalysisBits = 9;
1792
1793 static constexpr int kFieldWriteOffset = 0;
1794 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1795 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1796 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1797
1798 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1799
1800 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1801 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1802 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1803 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1804
1805 static constexpr int kLastBit = kDependsOnGCBit;
1806 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1807
1808 // Aliases.
1809
1810 static_assert(kChangeBits == kDependOnBits,
1811 "the 'change' bits should match the 'depend on' bits.");
1812
1813 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1814 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1815 static constexpr uint64_t kAllWrites =
1816 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1817 static constexpr uint64_t kAllReads =
1818 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001819
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001820 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001821 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001822 int shift;
1823 switch (type) {
1824 case DataType::Type::kReference: shift = 0; break;
1825 case DataType::Type::kBool: shift = 1; break;
1826 case DataType::Type::kInt8: shift = 2; break;
1827 case DataType::Type::kUint16: shift = 3; break;
1828 case DataType::Type::kInt16: shift = 4; break;
1829 case DataType::Type::kInt32: shift = 5; break;
1830 case DataType::Type::kInt64: shift = 6; break;
1831 case DataType::Type::kFloat32: shift = 7; break;
1832 case DataType::Type::kFloat64: shift = 8; break;
1833 default:
1834 LOG(FATAL) << "Unexpected data type " << type;
1835 UNREACHABLE();
1836 }
Aart Bik854a02b2015-07-14 16:07:00 -07001837 DCHECK_LE(kFieldWriteOffset, shift);
1838 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001839 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001840 }
1841
1842 // Private constructor on direct flags value.
1843 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1844
1845 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001846};
1847
David Brazdiled596192015-01-23 10:39:45 +00001848// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001849class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001850 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001851 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001852 size_t number_of_vregs,
1853 ArtMethod* method,
1854 uint32_t dex_pc,
1855 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001856 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1857 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001858 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001859 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001860 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001861 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001862 }
1863
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001864 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1865 const HEnvironment& to_copy,
1866 HInstruction* holder)
1867 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001868 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001869 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001870 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001871 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001872
Vladimir Markoec32f642017-06-02 10:51:55 +01001873 void AllocateLocations() {
1874 DCHECK(locations_.empty());
1875 locations_.resize(vregs_.size());
1876 }
1877
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001878 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001879 if (parent_ != nullptr) {
1880 parent_->SetAndCopyParentChain(allocator, parent);
1881 } else {
1882 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1883 parent_->CopyFrom(parent);
1884 if (parent->GetParent() != nullptr) {
1885 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1886 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001887 }
David Brazdiled596192015-01-23 10:39:45 +00001888 }
1889
Vladimir Marko69d310e2017-10-09 14:12:23 +01001890 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001891 void CopyFrom(HEnvironment* environment);
1892
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001893 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1894 // input to the loop phi instead. This is for inserting instructions that
1895 // require an environment (like HDeoptimization) in the loop pre-header.
1896 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001897
1898 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001899 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001900 }
1901
1902 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001903 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001904 }
1905
David Brazdil1abb4192015-02-17 18:33:36 +00001906 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001907
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001908 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001909
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001910 HEnvironment* GetParent() const { return parent_; }
1911
1912 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001913 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001914 }
1915
1916 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001917 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 }
1919
1920 uint32_t GetDexPc() const {
1921 return dex_pc_;
1922 }
1923
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001924 ArtMethod* GetMethod() const {
1925 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001926 }
1927
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001928 HInstruction* GetHolder() const {
1929 return holder_;
1930 }
1931
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001932
1933 bool IsFromInlinedInvoke() const {
1934 return GetParent() != nullptr;
1935 }
1936
David Brazdiled596192015-01-23 10:39:45 +00001937 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001938 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1939 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001940 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001941 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001942 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001943
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001944 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001945 HInstruction* const holder_;
1946
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001947 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001948
1949 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1950};
1951
Vladimir Markof9f64412015-09-02 14:05:49 +01001952class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001953 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301954#define DECLARE_KIND(type, super) k##type,
1955 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001956 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301957 kLastInstructionKind
1958 };
1959#undef DECLARE_KIND
1960
1961 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001962 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1963
1964 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001965 : previous_(nullptr),
1966 next_(nullptr),
1967 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001968 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001970 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001971 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001972 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001973 locations_(nullptr),
1974 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001975 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001976 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001977 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301978 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01001979 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001980 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1981 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001982
Dave Allison20dfc792014-06-16 20:44:29 -07001983 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001984
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001985
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001986 HInstruction* GetNext() const { return next_; }
1987 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988
Calin Juravle77520bc2015-01-12 18:45:46 +00001989 HInstruction* GetNextDisregardingMoves() const;
1990 HInstruction* GetPreviousDisregardingMoves() const;
1991
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001992 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001993 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001994 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001995 bool IsInBlock() const { return block_ != nullptr; }
1996 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001997 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1998 bool IsIrreducibleLoopHeaderPhi() const {
1999 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2000 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002001
Vladimir Marko372f10e2016-05-17 16:30:10 +01002002 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2003
2004 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2005 // One virtual method is enough, just const_cast<> and then re-add the const.
2006 return ArrayRef<const HUserRecord<HInstruction*>>(
2007 const_cast<HInstruction*>(this)->GetInputRecords());
2008 }
2009
Vladimir Markoe9004912016-06-16 16:50:52 +01002010 HInputsRef GetInputs() {
2011 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002012 }
2013
Vladimir Markoe9004912016-06-16 16:50:52 +01002014 HConstInputsRef GetInputs() const {
2015 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002016 }
2017
2018 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002019 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002020
Aart Bik2767f4b2016-10-28 15:03:53 -07002021 bool HasInput(HInstruction* input) const {
2022 for (const HInstruction* i : GetInputs()) {
2023 if (i == input) {
2024 return true;
2025 }
2026 }
2027 return false;
2028 }
2029
Vladimir Marko372f10e2016-05-17 16:30:10 +01002030 void SetRawInputAt(size_t index, HInstruction* input) {
2031 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2032 }
2033
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002034 virtual void Accept(HGraphVisitor* visitor) = 0;
2035 virtual const char* DebugName() const = 0;
2036
Vladimir Markobd785672018-05-03 17:09:09 +01002037 DataType::Type GetType() const {
2038 return TypeField::Decode(GetPackedFields());
2039 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002040
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002041 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002042
2043 uint32_t GetDexPc() const { return dex_pc_; }
2044
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002045 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002046
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002047 // Can the instruction throw?
2048 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2049 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002050 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002051
2052 // Does the instruction always throw an exception unconditionally?
2053 virtual bool AlwaysThrows() const { return false; }
2054
David Brazdilec16f792015-08-19 15:04:01 +01002055 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002056
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002057 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002058 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002059
Calin Juravle10e244f2015-01-26 18:54:32 +00002060 // Does not apply for all instructions, but having this at top level greatly
2061 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002062 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002063 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002064 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002065 return true;
2066 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002067
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002068 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002069 return false;
2070 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002071
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002072 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002073 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002074 }
2075
Calin Juravle2e768302015-07-28 14:41:11 +00002076 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002077
Calin Juravle61d544b2015-02-23 16:46:57 +00002078 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002079 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002080 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002081 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002082 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002083
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002084 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002085 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002086 // Note: fixup_end remains valid across push_front().
2087 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2088 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002089 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002090 uses_.push_front(*new_node);
2091 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002092 }
2093
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002094 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002095 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002096 // Note: env_fixup_end remains valid across push_front().
2097 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2098 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002099 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002100 env_uses_.push_front(*new_node);
2101 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002102 }
2103
David Brazdil1abb4192015-02-17 18:33:36 +00002104 void RemoveAsUserOfInput(size_t input) {
2105 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002106 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2107 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2108 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002109 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002110
Vladimir Marko372f10e2016-05-17 16:30:10 +01002111 void RemoveAsUserOfAllInputs() {
2112 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2113 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2114 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2115 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2116 }
2117 }
2118
David Brazdil1abb4192015-02-17 18:33:36 +00002119 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2120 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002121
Vladimir Marko46817b82016-03-29 12:21:58 +01002122 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2123 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2124 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002125 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002126 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002127 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002128
Aart Bikcc42be02016-10-20 16:14:16 -07002129 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002130 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002131 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002132 !CanThrow() &&
2133 !IsSuspendCheck() &&
2134 !IsControlFlow() &&
2135 !IsNativeDebugInfo() &&
2136 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002137 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002138 !IsMemoryBarrier() &&
2139 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002140 }
2141
Aart Bikcc42be02016-10-20 16:14:16 -07002142 bool IsDeadAndRemovable() const {
2143 return IsRemovable() && !HasUses();
2144 }
2145
Roland Levillain6c82d402014-10-13 16:10:27 +01002146 // Does this instruction strictly dominate `other_instruction`?
2147 // Returns false if this instruction and `other_instruction` are the same.
2148 // Aborts if this instruction and `other_instruction` are both phis.
2149 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002150
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002151 int GetId() const { return id_; }
2152 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002153
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002154 int GetSsaIndex() const { return ssa_index_; }
2155 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2156 bool HasSsaIndex() const { return ssa_index_ != -1; }
2157
2158 bool HasEnvironment() const { return environment_ != nullptr; }
2159 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002160 // Set the `environment_` field. Raw because this method does not
2161 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002162 void SetRawEnvironment(HEnvironment* environment) {
2163 DCHECK(environment_ == nullptr);
2164 DCHECK_EQ(environment->GetHolder(), this);
2165 environment_ = environment;
2166 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002167
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002168 void InsertRawEnvironment(HEnvironment* environment) {
2169 DCHECK(environment_ != nullptr);
2170 DCHECK_EQ(environment->GetHolder(), this);
2171 DCHECK(environment->GetParent() == nullptr);
2172 environment->parent_ = environment_;
2173 environment_ = environment;
2174 }
2175
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002176 void RemoveEnvironment();
2177
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002178 // Set the environment of this instruction, copying it from `environment`. While
2179 // copying, the uses lists are being updated.
2180 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002181 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002182 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002183 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002184 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002185 if (environment->GetParent() != nullptr) {
2186 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2187 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002188 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002189
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002190 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2191 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002192 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002193 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002194 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002195 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002196 if (environment->GetParent() != nullptr) {
2197 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2198 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002199 }
2200
Nicolas Geoffray39468442014-09-02 15:17:15 +01002201 // Returns the number of entries in the environment. Typically, that is the
2202 // number of dex registers in a method. It could be more in case of inlining.
2203 size_t EnvironmentSize() const;
2204
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002205 LocationSummary* GetLocations() const { return locations_; }
2206 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002207
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002208 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002209 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002210 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002211
Alexandre Rames188d4312015-04-09 18:30:21 +01002212 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2213 // uses of this instruction by `other` are *not* updated.
2214 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2215 ReplaceWith(other);
2216 other->ReplaceInput(this, use_index);
2217 }
2218
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002219 // Move `this` instruction before `cursor`
2220 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002221
Vladimir Markofb337ea2015-11-25 15:25:10 +00002222 // Move `this` before its first user and out of any loops. If there is no
2223 // out-of-loop user that dominates all other users, move the instruction
2224 // to the end of the out-of-loop common dominator of the user's blocks.
2225 //
2226 // This can be used only on non-throwing instructions with no side effects that
2227 // have at least one use but no environment uses.
2228 void MoveBeforeFirstUserAndOutOfLoops();
2229
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002230#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002231 bool Is##type() const;
2232
2233 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2234#undef INSTRUCTION_TYPE_CHECK
2235
2236#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002237 const H##type* As##type() const; \
2238 H##type* As##type();
2239
Vladimir Markoa90dd512018-05-04 15:04:45 +01002240 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2241#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002242
Artem Serovcced8ba2017-07-19 18:18:09 +01002243 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2244 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2245 // the instruction then the behaviour of this function is undefined.
2246 //
2247 // Note: It is semantically valid to create a clone of the instruction only until
2248 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2249 // copied.
2250 //
2251 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2252 // 'explicit HInstruction(const HInstruction& other)' for details.
2253 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2254 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2255 DebugName() << " " << GetId();
2256 UNREACHABLE();
2257 }
2258
2259 // Return whether instruction can be cloned (copied).
2260 virtual bool IsClonable() const { return false; }
2261
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002262 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002263 // TODO: this method is used by LICM and GVN with possibly different
2264 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002265 virtual bool CanBeMoved() const { return false; }
2266
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002267 // Returns whether any data encoded in the two instructions is equal.
2268 // This method does not look at the inputs. Both instructions must be
2269 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002270 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002271 return false;
2272 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002273
2274 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002275 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002276 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002277 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002278
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002279 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2280 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2281 // the virtual function because the __attribute__((__pure__)) doesn't really
2282 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302283 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002284
2285 virtual size_t ComputeHashCode() const {
2286 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002287 for (const HInstruction* input : GetInputs()) {
2288 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002289 }
2290 return result;
2291 }
2292
2293 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002294 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002295 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002296
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002297 size_t GetLifetimePosition() const { return lifetime_position_; }
2298 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2299 LiveInterval* GetLiveInterval() const { return live_interval_; }
2300 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2301 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2302
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002303 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2304
2305 // Returns whether the code generation of the instruction will require to have access
2306 // to the current method. Such instructions are:
2307 // (1): Instructions that require an environment, as calling the runtime requires
2308 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002309 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2310 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002311 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002312 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002313 }
2314
Vladimir Markodc151b22015-10-15 18:02:30 +01002315 // Returns whether the code generation of the instruction will require to have access
2316 // to the dex cache of the current method's declaring class via the current method.
2317 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002318
Mark Mendellc4701932015-04-10 13:18:51 -04002319 // Does this instruction have any use in an environment before
2320 // control flow hits 'other'?
2321 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2322
2323 // Remove all references to environment uses of this instruction.
2324 // The caller must ensure that this is safe to do.
2325 void RemoveEnvironmentUsers();
2326
Vladimir Markoa1de9182016-02-25 11:37:38 +00002327 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2328 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002329
David Brazdil1abb4192015-02-17 18:33:36 +00002330 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002331 // If set, the machine code for this instruction is assumed to be generated by
2332 // its users. Used by liveness analysis to compute use positions accordingly.
2333 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2334 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302335 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2336 static constexpr size_t kFieldInstructionKindSize =
2337 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002338 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302339 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002340 static constexpr size_t kFieldTypeSize =
2341 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2342 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002343 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2344
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302345 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2346 "Too many generic packed fields");
2347
Vladimir Markobd785672018-05-03 17:09:09 +01002348 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2349
Vladimir Marko372f10e2016-05-17 16:30:10 +01002350 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2351 return GetInputRecords()[i];
2352 }
2353
2354 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2355 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2356 input_records[index] = input;
2357 }
David Brazdil1abb4192015-02-17 18:33:36 +00002358
Vladimir Markoa1de9182016-02-25 11:37:38 +00002359 uint32_t GetPackedFields() const {
2360 return packed_fields_;
2361 }
2362
2363 template <size_t flag>
2364 bool GetPackedFlag() const {
2365 return (packed_fields_ & (1u << flag)) != 0u;
2366 }
2367
2368 template <size_t flag>
2369 void SetPackedFlag(bool value = true) {
2370 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2371 }
2372
2373 template <typename BitFieldType>
2374 typename BitFieldType::value_type GetPackedField() const {
2375 return BitFieldType::Decode(packed_fields_);
2376 }
2377
2378 template <typename BitFieldType>
2379 void SetPackedField(typename BitFieldType::value_type value) {
2380 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2381 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2382 }
2383
Artem Serovcced8ba2017-07-19 18:18:09 +01002384 // Copy construction for the instruction (used for Clone function).
2385 //
2386 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2387 // prepare_for_register_allocator are not copied (set to default values).
2388 //
2389 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2390 // fine for most of them. However for some of the instructions a custom copy constructor must be
2391 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2392 // for copying them).
2393 explicit HInstruction(const HInstruction& other)
2394 : previous_(nullptr),
2395 next_(nullptr),
2396 block_(nullptr),
2397 dex_pc_(other.dex_pc_),
2398 id_(-1),
2399 ssa_index_(-1),
2400 packed_fields_(other.packed_fields_),
2401 environment_(nullptr),
2402 locations_(nullptr),
2403 live_interval_(nullptr),
2404 lifetime_position_(kNoLifetime),
2405 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302406 reference_type_handle_(other.reference_type_handle_) {
2407 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002408
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002409 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302410 using InstructionKindField =
2411 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2412
Vladimir Marko46817b82016-03-29 12:21:58 +01002413 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2414 auto before_use_node = uses_.before_begin();
2415 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2416 HInstruction* user = use_node->GetUser();
2417 size_t input_index = use_node->GetIndex();
2418 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2419 before_use_node = use_node;
2420 }
2421 }
2422
2423 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2424 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2425 if (next != uses_.end()) {
2426 HInstruction* next_user = next->GetUser();
2427 size_t next_index = next->GetIndex();
2428 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2429 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2430 }
2431 }
2432
2433 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2434 auto before_env_use_node = env_uses_.before_begin();
2435 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2436 HEnvironment* user = env_use_node->GetUser();
2437 size_t input_index = env_use_node->GetIndex();
2438 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2439 before_env_use_node = env_use_node;
2440 }
2441 }
2442
2443 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2444 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2445 if (next != env_uses_.end()) {
2446 HEnvironment* next_user = next->GetUser();
2447 size_t next_index = next->GetIndex();
2448 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2449 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2450 }
2451 }
David Brazdil1abb4192015-02-17 18:33:36 +00002452
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002453 HInstruction* previous_;
2454 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002455 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002456 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002457
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002458 // An instruction gets an id when it is added to the graph.
2459 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002460 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002461 int id_;
2462
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002463 // When doing liveness analysis, instructions that have uses get an SSA index.
2464 int ssa_index_;
2465
Vladimir Markoa1de9182016-02-25 11:37:38 +00002466 // Packed fields.
2467 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002468
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002469 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002470 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002471
2472 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002473 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002474
Nicolas Geoffray39468442014-09-02 15:17:15 +01002475 // The environment associated with this instruction. Not null if the instruction
2476 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002477 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002478
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002479 // Set by the code generator.
2480 LocationSummary* locations_;
2481
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002482 // Set by the liveness analysis.
2483 LiveInterval* live_interval_;
2484
2485 // Set by the liveness analysis, this is the position in a linear
2486 // order of blocks where this instruction's live interval start.
2487 size_t lifetime_position_;
2488
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002489 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002490
Vladimir Markoa1de9182016-02-25 11:37:38 +00002491 // The reference handle part of the reference type info.
2492 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002493 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002494 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002495
David Brazdil1abb4192015-02-17 18:33:36 +00002496 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002497 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002498 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002499 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002500 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002501};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002502std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002503
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002504// Iterates over the instructions, while preserving the next instruction
2505// in case the current instruction gets removed from the list by the user
2506// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002507class HInstructionIterator : public ValueObject {
2508 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002509 explicit HInstructionIterator(const HInstructionList& instructions)
2510 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002511 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512 }
2513
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002514 bool Done() const { return instruction_ == nullptr; }
2515 HInstruction* Current() const { return instruction_; }
2516 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002517 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002518 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002519 }
2520
2521 private:
2522 HInstruction* instruction_;
2523 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002524
2525 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002526};
2527
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002528// Iterates over the instructions without saving the next instruction,
2529// therefore handling changes in the graph potentially made by the user
2530// of this iterator.
2531class HInstructionIteratorHandleChanges : public ValueObject {
2532 public:
2533 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2534 : instruction_(instructions.first_instruction_) {
2535 }
2536
2537 bool Done() const { return instruction_ == nullptr; }
2538 HInstruction* Current() const { return instruction_; }
2539 void Advance() {
2540 instruction_ = instruction_->GetNext();
2541 }
2542
2543 private:
2544 HInstruction* instruction_;
2545
2546 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2547};
2548
2549
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002550class HBackwardInstructionIterator : public ValueObject {
2551 public:
2552 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2553 : instruction_(instructions.last_instruction_) {
2554 next_ = Done() ? nullptr : instruction_->GetPrevious();
2555 }
2556
2557 bool Done() const { return instruction_ == nullptr; }
2558 HInstruction* Current() const { return instruction_; }
2559 void Advance() {
2560 instruction_ = next_;
2561 next_ = Done() ? nullptr : instruction_->GetPrevious();
2562 }
2563
2564 private:
2565 HInstruction* instruction_;
2566 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002567
2568 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002569};
2570
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002571class HVariableInputSizeInstruction : public HInstruction {
2572 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002573 using HInstruction::GetInputRecords; // Keep the const version visible.
2574 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2575 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2576 }
2577
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002578 void AddInput(HInstruction* input);
2579 void InsertInputAt(size_t index, HInstruction* input);
2580 void RemoveInputAt(size_t index);
2581
Igor Murashkind01745e2017-04-05 16:40:31 -07002582 // Removes all the inputs.
2583 // Also removes this instructions from each input's use list
2584 // (for non-environment uses only).
2585 void RemoveAllInputs();
2586
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002587 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302588 HVariableInputSizeInstruction(InstructionKind inst_kind,
2589 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002590 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002591 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002592 size_t number_of_inputs,
2593 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302594 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002595 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002596 HVariableInputSizeInstruction(InstructionKind inst_kind,
2597 DataType::Type type,
2598 SideEffects side_effects,
2599 uint32_t dex_pc,
2600 ArenaAllocator* allocator,
2601 size_t number_of_inputs,
2602 ArenaAllocKind kind)
2603 : HInstruction(inst_kind, type, side_effects, dex_pc),
2604 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002605
Artem Serovcced8ba2017-07-19 18:18:09 +01002606 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002607
Artem Serovcced8ba2017-07-19 18:18:09 +01002608 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002609};
2610
Vladimir Markof9f64412015-09-02 14:05:49 +01002611template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002612class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002613 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002614 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302615 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002616 HExpression<N>(InstructionKind kind,
2617 DataType::Type type,
2618 SideEffects side_effects,
2619 uint32_t dex_pc)
2620 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2621 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002622
Vladimir Marko372f10e2016-05-17 16:30:10 +01002623 using HInstruction::GetInputRecords; // Keep the const version visible.
2624 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2625 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002626 }
2627
Artem Serovcced8ba2017-07-19 18:18:09 +01002628 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002629 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002630
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002631 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002632 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002633
2634 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002635};
2636
Vladimir Markobd785672018-05-03 17:09:09 +01002637// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002638template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002639class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002640 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002641 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002642
Vladimir Markobd785672018-05-03 17:09:09 +01002643 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002644
Vladimir Marko372f10e2016-05-17 16:30:10 +01002645 using HInstruction::GetInputRecords; // Keep the const version visible.
2646 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2647 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002648 }
2649
Artem Serovcced8ba2017-07-19 18:18:09 +01002650 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002651 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002652
Vladimir Markof9f64412015-09-02 14:05:49 +01002653 private:
2654 friend class SsaBuilder;
2655};
2656
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002657// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2658// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002659class HReturnVoid FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002660 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002662 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302663 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002664
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002665 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002666
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002667 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002668
Artem Serovcced8ba2017-07-19 18:18:09 +01002669 protected:
2670 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002671};
2672
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002673// Represents dex's RETURN opcodes. A HReturn is a control flow
2674// instruction that branches to the exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002675class HReturn FINAL : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002676 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002677 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002678 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002679 SetRawInputAt(0, value);
2680 }
2681
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002682 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002683
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002684 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002685
Artem Serovcced8ba2017-07-19 18:18:09 +01002686 protected:
2687 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002688};
2689
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002690class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002691 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002692 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002693 uint32_t reg_number,
2694 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002696 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002697 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302698 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002699 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002700 SideEffects::None(),
2701 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002702 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002703 number_of_inputs,
2704 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002705 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002707 // Phis are constructed live and marked dead if conflicting or unused.
2708 // Individual steps of SsaBuilder should assume that if a phi has been
2709 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2710 SetPackedFlag<kFlagIsLive>(true);
2711 SetPackedFlag<kFlagCanBeNull>(true);
2712 }
2713
Artem Serovcced8ba2017-07-19 18:18:09 +01002714 bool IsClonable() const OVERRIDE { return true; }
2715
David Brazdildee58d62016-04-07 09:54:26 +00002716 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002717 static DataType::Type ToPhiType(DataType::Type type) {
2718 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002719 }
2720
2721 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2722
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002723 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002724 // Make sure that only valid type changes occur. The following are allowed:
2725 // (1) int -> float/ref (primitive type propagation),
2726 // (2) long -> double (primitive type propagation).
2727 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2729 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2730 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002731 SetPackedField<TypeField>(new_type);
2732 }
2733
2734 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2735 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2736
2737 uint32_t GetRegNumber() const { return reg_number_; }
2738
2739 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2740 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2741 bool IsDead() const { return !IsLive(); }
2742 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2743
Vladimir Markoe9004912016-06-16 16:50:52 +01002744 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002745 return other != nullptr
2746 && other->IsPhi()
2747 && other->AsPhi()->GetBlock() == GetBlock()
2748 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2749 }
2750
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002751 bool HasEquivalentPhi() const {
2752 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2753 return true;
2754 }
2755 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2756 return true;
2757 }
2758 return false;
2759 }
2760
David Brazdildee58d62016-04-07 09:54:26 +00002761 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2762 // An equivalent phi is a phi having the same dex register and type.
2763 // It assumes that phis with the same dex register are adjacent.
2764 HPhi* GetNextEquivalentPhiWithSameType() {
2765 HInstruction* next = GetNext();
2766 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2767 if (next->GetType() == GetType()) {
2768 return next->AsPhi();
2769 }
2770 next = next->GetNext();
2771 }
2772 return nullptr;
2773 }
2774
2775 DECLARE_INSTRUCTION(Phi);
2776
Artem Serovcced8ba2017-07-19 18:18:09 +01002777 protected:
2778 DEFAULT_COPY_CONSTRUCTOR(Phi);
2779
David Brazdildee58d62016-04-07 09:54:26 +00002780 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002781 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002782 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2783 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2784 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002785
David Brazdildee58d62016-04-07 09:54:26 +00002786 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002787};
2788
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002789// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002790// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002791// exit block.
Vladimir Markobd785672018-05-03 17:09:09 +01002792class HExit FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002793 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302794 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002795 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302796 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002797
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002798 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002799
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002800 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002801
Artem Serovcced8ba2017-07-19 18:18:09 +01002802 protected:
2803 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002804};
2805
2806// Jumps from one block to another.
Vladimir Markobd785672018-05-03 17:09:09 +01002807class HGoto FINAL : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002808 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302809 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002810 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302811 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002812
Artem Serovcced8ba2017-07-19 18:18:09 +01002813 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002814 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002815
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002816 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002817 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002818 }
2819
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002820 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002821
Artem Serovcced8ba2017-07-19 18:18:09 +01002822 protected:
2823 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002824};
2825
Roland Levillain9867bc72015-08-05 10:21:34 +01002826class HConstant : public HExpression<0> {
2827 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302828 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2829 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2830 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002831
2832 bool CanBeMoved() const OVERRIDE { return true; }
2833
Roland Levillain1a653882016-03-18 18:05:57 +00002834 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002835 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002836 // Is this constant 0 in the arithmetic sense?
2837 virtual bool IsArithmeticZero() const { return false; }
2838 // Is this constant a 0-bit pattern?
2839 virtual bool IsZeroBitPattern() const { return false; }
2840 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002841 virtual bool IsOne() const { return false; }
2842
David Brazdil77a48ae2015-09-15 12:34:04 +00002843 virtual uint64_t GetValueAsUint64() const = 0;
2844
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002845 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002846
Artem Serovcced8ba2017-07-19 18:18:09 +01002847 protected:
2848 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002849};
2850
Vladimir Markofcb503c2016-05-18 12:48:17 +01002851class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002852 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002853 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002854 return true;
2855 }
2856
David Brazdil77a48ae2015-09-15 12:34:04 +00002857 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2858
Roland Levillain9867bc72015-08-05 10:21:34 +01002859 size_t ComputeHashCode() const OVERRIDE { return 0; }
2860
Roland Levillain1a653882016-03-18 18:05:57 +00002861 // The null constant representation is a 0-bit pattern.
2862 virtual bool IsZeroBitPattern() const { return true; }
2863
Roland Levillain9867bc72015-08-05 10:21:34 +01002864 DECLARE_INSTRUCTION(NullConstant);
2865
Artem Serovcced8ba2017-07-19 18:18:09 +01002866 protected:
2867 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2868
Roland Levillain9867bc72015-08-05 10:21:34 +01002869 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302871 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2872 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002873
2874 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002875};
2876
2877// Constants of the type int. Those can be from Dex instructions, or
2878// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002879class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002880 public:
2881 int32_t GetValue() const { return value_; }
2882
David Brazdil9f389d42015-10-01 14:32:56 +01002883 uint64_t GetValueAsUint64() const OVERRIDE {
2884 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2885 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002886
Vladimir Marko372f10e2016-05-17 16:30:10 +01002887 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002888 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002889 return other->AsIntConstant()->value_ == value_;
2890 }
2891
2892 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2893
2894 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002895 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2896 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002897 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2898
Roland Levillain1a653882016-03-18 18:05:57 +00002899 // Integer constants are used to encode Boolean values as well,
2900 // where 1 means true and 0 means false.
2901 bool IsTrue() const { return GetValue() == 1; }
2902 bool IsFalse() const { return GetValue() == 0; }
2903
Roland Levillain9867bc72015-08-05 10:21:34 +01002904 DECLARE_INSTRUCTION(IntConstant);
2905
Artem Serovcced8ba2017-07-19 18:18:09 +01002906 protected:
2907 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2908
Roland Levillain9867bc72015-08-05 10:21:34 +01002909 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002910 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302911 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2912 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002913 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302914 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2915 value_(value ? 1 : 0) {
2916 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002917
2918 const int32_t value_;
2919
2920 friend class HGraph;
2921 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2922 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002923};
2924
Vladimir Markofcb503c2016-05-18 12:48:17 +01002925class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002926 public:
2927 int64_t GetValue() const { return value_; }
2928
David Brazdil77a48ae2015-09-15 12:34:04 +00002929 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2930
Vladimir Marko372f10e2016-05-17 16:30:10 +01002931 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002932 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002933 return other->AsLongConstant()->value_ == value_;
2934 }
2935
2936 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2937
2938 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002939 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2940 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002941 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2942
2943 DECLARE_INSTRUCTION(LongConstant);
2944
Artem Serovcced8ba2017-07-19 18:18:09 +01002945 protected:
2946 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2947
Roland Levillain9867bc72015-08-05 10:21:34 +01002948 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002949 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302950 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2951 value_(value) {
2952 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002953
2954 const int64_t value_;
2955
2956 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002957};
Dave Allison20dfc792014-06-16 20:44:29 -07002958
Vladimir Markofcb503c2016-05-18 12:48:17 +01002959class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002960 public:
2961 float GetValue() const { return value_; }
2962
2963 uint64_t GetValueAsUint64() const OVERRIDE {
2964 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2965 }
2966
Vladimir Marko372f10e2016-05-17 16:30:10 +01002967 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002968 DCHECK(other->IsFloatConstant()) << other->DebugName();
2969 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2970 }
2971
2972 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2973
2974 bool IsMinusOne() const OVERRIDE {
2975 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2976 }
Roland Levillain1a653882016-03-18 18:05:57 +00002977 bool IsArithmeticZero() const OVERRIDE {
2978 return std::fpclassify(value_) == FP_ZERO;
2979 }
2980 bool IsArithmeticPositiveZero() const {
2981 return IsArithmeticZero() && !std::signbit(value_);
2982 }
2983 bool IsArithmeticNegativeZero() const {
2984 return IsArithmeticZero() && std::signbit(value_);
2985 }
2986 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002987 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002988 }
2989 bool IsOne() const OVERRIDE {
2990 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2991 }
2992 bool IsNaN() const {
2993 return std::isnan(value_);
2994 }
2995
2996 DECLARE_INSTRUCTION(FloatConstant);
2997
Artem Serovcced8ba2017-07-19 18:18:09 +01002998 protected:
2999 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3000
Roland Levillain31dd3d62016-02-16 12:21:02 +00003001 private:
3002 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303003 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3004 value_(value) {
3005 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003006 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303007 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3008 value_(bit_cast<float, int32_t>(value)) {
3009 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003010
3011 const float value_;
3012
3013 // Only the SsaBuilder and HGraph can create floating-point constants.
3014 friend class SsaBuilder;
3015 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003016};
3017
Vladimir Markofcb503c2016-05-18 12:48:17 +01003018class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019 public:
3020 double GetValue() const { return value_; }
3021
3022 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3023
Vladimir Marko372f10e2016-05-17 16:30:10 +01003024 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003025 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3026 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3027 }
3028
3029 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3030
3031 bool IsMinusOne() const OVERRIDE {
3032 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3033 }
Roland Levillain1a653882016-03-18 18:05:57 +00003034 bool IsArithmeticZero() const OVERRIDE {
3035 return std::fpclassify(value_) == FP_ZERO;
3036 }
3037 bool IsArithmeticPositiveZero() const {
3038 return IsArithmeticZero() && !std::signbit(value_);
3039 }
3040 bool IsArithmeticNegativeZero() const {
3041 return IsArithmeticZero() && std::signbit(value_);
3042 }
3043 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003044 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003045 }
3046 bool IsOne() const OVERRIDE {
3047 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3048 }
3049 bool IsNaN() const {
3050 return std::isnan(value_);
3051 }
3052
3053 DECLARE_INSTRUCTION(DoubleConstant);
3054
Artem Serovcced8ba2017-07-19 18:18:09 +01003055 protected:
3056 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3057
Roland Levillain31dd3d62016-02-16 12:21:02 +00003058 private:
3059 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303060 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3061 value_(value) {
3062 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303064 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3065 value_(bit_cast<double, int64_t>(value)) {
3066 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003067
3068 const double value_;
3069
3070 // Only the SsaBuilder and HGraph can create floating-point constants.
3071 friend class SsaBuilder;
3072 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003073};
3074
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003075// Conditional branch. A block ending with an HIf instruction must have
3076// two successors.
Vladimir Markobd785672018-05-03 17:09:09 +01003077class HIf FINAL : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003078 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003079 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003080 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003081 SetRawInputAt(0, input);
3082 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003083
Artem Serovcced8ba2017-07-19 18:18:09 +01003084 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003085 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003086
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003087 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003088 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003089 }
3090
3091 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003092 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003093 }
3094
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003095 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003096
Artem Serovcced8ba2017-07-19 18:18:09 +01003097 protected:
3098 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003099};
3100
David Brazdilfc6a86a2015-06-26 10:33:45 +00003101
3102// Abstract instruction which marks the beginning and/or end of a try block and
3103// links it to the respective exception handlers. Behaves the same as a Goto in
3104// non-exceptional control flow.
3105// Normal-flow successor is stored at index zero, exception handlers under
3106// higher indices in no particular order.
Vladimir Markobd785672018-05-03 17:09:09 +01003107class HTryBoundary FINAL : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003108 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003109 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003110 kEntry,
3111 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003112 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003113 };
3114
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003115 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003116 : HExpression(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003117 SetPackedField<BoundaryKindField>(kind);
3118 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003119
3120 bool IsControlFlow() const OVERRIDE { return true; }
3121
3122 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003123 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003124
David Brazdild26a4112015-11-10 11:07:31 +00003125 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3126 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3127 }
3128
David Brazdilfc6a86a2015-06-26 10:33:45 +00003129 // Returns whether `handler` is among its exception handlers (non-zero index
3130 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003131 bool HasExceptionHandler(const HBasicBlock& handler) const {
3132 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003133 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003134 }
3135
3136 // If not present already, adds `handler` to its block's list of exception
3137 // handlers.
3138 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003139 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003140 GetBlock()->AddSuccessor(handler);
3141 }
3142 }
3143
Vladimir Markoa1de9182016-02-25 11:37:38 +00003144 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3145 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003146
David Brazdilffee3d32015-07-06 11:48:53 +01003147 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3148
David Brazdilfc6a86a2015-06-26 10:33:45 +00003149 DECLARE_INSTRUCTION(TryBoundary);
3150
Artem Serovcced8ba2017-07-19 18:18:09 +01003151 protected:
3152 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3153
David Brazdilfc6a86a2015-06-26 10:33:45 +00003154 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003155 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3156 static constexpr size_t kFieldBoundaryKindSize =
3157 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3158 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3159 kFieldBoundaryKind + kFieldBoundaryKindSize;
3160 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3161 "Too many packed fields.");
3162 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003163};
3164
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003165// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003166class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003167 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003168 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3169 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003170 HDeoptimize(ArenaAllocator* allocator,
3171 HInstruction* cond,
3172 DeoptimizationKind kind,
3173 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003174 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303175 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003176 SideEffects::All(),
3177 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003178 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003179 /* number_of_inputs */ 1,
3180 kArenaAllocMisc) {
3181 SetPackedFlag<kFieldCanBeMoved>(false);
3182 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003183 SetRawInputAt(0, cond);
3184 }
3185
Artem Serovcced8ba2017-07-19 18:18:09 +01003186 bool IsClonable() const OVERRIDE { return true; }
3187
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003188 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3189 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3190 // instead of `guard`.
3191 // We set CanTriggerGC to prevent any intermediate address to be live
3192 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003193 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003194 HInstruction* cond,
3195 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003196 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003197 uint32_t dex_pc)
3198 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303199 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003200 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003201 SideEffects::CanTriggerGC(),
3202 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003203 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003204 /* number_of_inputs */ 2,
3205 kArenaAllocMisc) {
3206 SetPackedFlag<kFieldCanBeMoved>(true);
3207 SetPackedField<DeoptimizeKindField>(kind);
3208 SetRawInputAt(0, cond);
3209 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003210 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003211
3212 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3213
3214 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3215 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3216 }
3217
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003218 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003219
Aart Bik75ff2c92018-04-21 01:28:11 +00003220 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003221
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003222 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003223
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003224 bool GuardsAnInput() const {
3225 return InputCount() == 2;
3226 }
3227
3228 HInstruction* GuardedInput() const {
3229 DCHECK(GuardsAnInput());
3230 return InputAt(1);
3231 }
3232
3233 void RemoveGuard() {
3234 RemoveInputAt(1);
3235 }
3236
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003237 DECLARE_INSTRUCTION(Deoptimize);
3238
Artem Serovcced8ba2017-07-19 18:18:09 +01003239 protected:
3240 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3241
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003242 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003243 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3244 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3245 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003246 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003247 static constexpr size_t kNumberOfDeoptimizePackedBits =
3248 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3249 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3250 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003251 using DeoptimizeKindField =
3252 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003253};
3254
Mingyao Yang063fc772016-08-02 11:02:54 -07003255// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3256// The compiled code checks this flag value in a guard before devirtualized call and
3257// if it's true, starts to do deoptimization.
3258// It has a 4-byte slot on stack.
3259// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003260class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003261 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003262 // CHA guards are only optimized in a separate pass and it has no side effects
3263 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003264 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303265 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003266 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303267 SideEffects::None(),
3268 dex_pc,
3269 allocator,
3270 0,
3271 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003272 }
3273
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003274 // We do all CHA guard elimination/motion in a single pass, after which there is no
3275 // further guard elimination/motion since a guard might have been used for justification
3276 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3277 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003278 bool CanBeMoved() const OVERRIDE { return false; }
3279
3280 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3281
Artem Serovcced8ba2017-07-19 18:18:09 +01003282 protected:
3283 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003284};
3285
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003286// Represents the ArtMethod that was passed as a first argument to
3287// the method. It is used by instructions that depend on it, like
3288// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003289class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003290 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003291 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303292 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3293 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003294
3295 DECLARE_INSTRUCTION(CurrentMethod);
3296
Artem Serovcced8ba2017-07-19 18:18:09 +01003297 protected:
3298 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003299};
3300
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003301// Fetches an ArtMethod from the virtual table or the interface method table
3302// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003303class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003304 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003305 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003306 kVTable,
3307 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003308 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003309 };
3310 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003311 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003312 TableKind kind,
3313 size_t index,
3314 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303315 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003316 index_(index) {
3317 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003318 SetRawInputAt(0, cls);
3319 }
3320
Artem Serovcced8ba2017-07-19 18:18:09 +01003321 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003322 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003323 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003324 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003325 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003326 }
3327
Vladimir Markoa1de9182016-02-25 11:37:38 +00003328 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003329 size_t GetIndex() const { return index_; }
3330
3331 DECLARE_INSTRUCTION(ClassTableGet);
3332
Artem Serovcced8ba2017-07-19 18:18:09 +01003333 protected:
3334 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3335
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003336 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003337 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003338 static constexpr size_t kFieldTableKindSize =
3339 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3340 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3341 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3342 "Too many packed fields.");
3343 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3344
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003345 // The index of the ArtMethod in the table.
3346 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003347};
3348
Mark Mendellfe57faa2015-09-18 09:26:15 -04003349// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3350// have one successor for each entry in the switch table, and the final successor
3351// will be the block containing the next Dex opcode.
Vladimir Markobd785672018-05-03 17:09:09 +01003352class HPackedSwitch FINAL : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003353 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003354 HPackedSwitch(int32_t start_value,
3355 uint32_t num_entries,
3356 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003357 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003358 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003359 start_value_(start_value),
3360 num_entries_(num_entries) {
3361 SetRawInputAt(0, input);
3362 }
3363
Artem Serovcced8ba2017-07-19 18:18:09 +01003364 bool IsClonable() const OVERRIDE { return true; }
3365
Mark Mendellfe57faa2015-09-18 09:26:15 -04003366 bool IsControlFlow() const OVERRIDE { return true; }
3367
3368 int32_t GetStartValue() const { return start_value_; }
3369
Vladimir Marko211c2112015-09-24 16:52:33 +01003370 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003371
3372 HBasicBlock* GetDefaultBlock() const {
3373 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003374 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003375 }
3376 DECLARE_INSTRUCTION(PackedSwitch);
3377
Artem Serovcced8ba2017-07-19 18:18:09 +01003378 protected:
3379 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3380
Mark Mendellfe57faa2015-09-18 09:26:15 -04003381 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003382 const int32_t start_value_;
3383 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003384};
3385
Roland Levillain88cb1752014-10-20 16:36:47 +01003386class HUnaryOperation : public HExpression<1> {
3387 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303388 HUnaryOperation(InstructionKind kind,
3389 DataType::Type result_type,
3390 HInstruction* input,
3391 uint32_t dex_pc = kNoDexPc)
3392 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003393 SetRawInputAt(0, input);
3394 }
3395
Artem Serovcced8ba2017-07-19 18:18:09 +01003396 // All of the UnaryOperation instructions are clonable.
3397 bool IsClonable() const OVERRIDE { return true; }
3398
Roland Levillain88cb1752014-10-20 16:36:47 +01003399 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003400 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003401
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003402 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003403 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003404 return true;
3405 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003406
Roland Levillain31dd3d62016-02-16 12:21:02 +00003407 // Try to statically evaluate `this` and return a HConstant
3408 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003409 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003410 HConstant* TryStaticEvaluation() const;
3411
3412 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003413 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3414 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003415 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3416 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003417
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003418 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003419
Artem Serovcced8ba2017-07-19 18:18:09 +01003420 protected:
3421 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003422};
3423
Dave Allison20dfc792014-06-16 20:44:29 -07003424class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003425 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303426 HBinaryOperation(InstructionKind kind,
3427 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003428 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003429 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003430 SideEffects side_effects = SideEffects::None(),
3431 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303432 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003433 SetRawInputAt(0, left);
3434 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003435 }
3436
Artem Serovcced8ba2017-07-19 18:18:09 +01003437 // All of the BinaryOperation instructions are clonable.
3438 bool IsClonable() const OVERRIDE { return true; }
3439
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003440 HInstruction* GetLeft() const { return InputAt(0); }
3441 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003442 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003443
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003444 virtual bool IsCommutative() const { return false; }
3445
3446 // Put constant on the right.
3447 // Returns whether order is changed.
3448 bool OrderInputsWithConstantOnTheRight() {
3449 HInstruction* left = InputAt(0);
3450 HInstruction* right = InputAt(1);
3451 if (left->IsConstant() && !right->IsConstant()) {
3452 ReplaceInput(right, 0);
3453 ReplaceInput(left, 1);
3454 return true;
3455 }
3456 return false;
3457 }
3458
3459 // Order inputs by instruction id, but favor constant on the right side.
3460 // This helps GVN for commutative ops.
3461 void OrderInputs() {
3462 DCHECK(IsCommutative());
3463 HInstruction* left = InputAt(0);
3464 HInstruction* right = InputAt(1);
3465 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3466 return;
3467 }
3468 if (OrderInputsWithConstantOnTheRight()) {
3469 return;
3470 }
3471 // Order according to instruction id.
3472 if (left->GetId() > right->GetId()) {
3473 ReplaceInput(right, 0);
3474 ReplaceInput(left, 1);
3475 }
3476 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003477
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003478 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003479 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003480 return true;
3481 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003482
Roland Levillain31dd3d62016-02-16 12:21:02 +00003483 // Try to statically evaluate `this` and return a HConstant
3484 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003485 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003486 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003487
3488 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003489 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3490 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003491 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3492 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003494 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3495 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003496 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3497 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003498 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3499 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003500 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003501 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3502 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003503
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003504 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003505 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003506 HConstant* GetConstantRight() const;
3507
3508 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003509 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003510 HInstruction* GetLeastConstantLeft() const;
3511
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003512 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003513
Artem Serovcced8ba2017-07-19 18:18:09 +01003514 protected:
3515 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003516};
3517
Mark Mendellc4701932015-04-10 13:18:51 -04003518// The comparison bias applies for floating point operations and indicates how NaN
3519// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003520enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003521 kNoBias, // bias is not applicable (i.e. for long operation)
3522 kGtBias, // return 1 for NaN comparisons
3523 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003524 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003525};
3526
Roland Levillain31dd3d62016-02-16 12:21:02 +00003527std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3528
Dave Allison20dfc792014-06-16 20:44:29 -07003529class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003530 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303531 HCondition(InstructionKind kind,
3532 HInstruction* first,
3533 HInstruction* second,
3534 uint32_t dex_pc = kNoDexPc)
3535 : HBinaryOperation(kind,
3536 DataType::Type::kBool,
3537 first,
3538 second,
3539 SideEffects::None(),
3540 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003541 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3542 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003543
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003544 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003545 // `instruction`, and disregard moves in between.
3546 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003547
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003548 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003549
3550 virtual IfCondition GetCondition() const = 0;
3551
Mark Mendellc4701932015-04-10 13:18:51 -04003552 virtual IfCondition GetOppositeCondition() const = 0;
3553
Vladimir Markoa1de9182016-02-25 11:37:38 +00003554 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003555 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3556
Vladimir Markoa1de9182016-02-25 11:37:38 +00003557 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3558 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003559
Vladimir Marko372f10e2016-05-17 16:30:10 +01003560 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003561 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003562 }
3563
Roland Levillain4fa13f62015-07-06 18:11:54 +01003564 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003565 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003566 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003567 if (if_cond == kCondNE) {
3568 return true;
3569 } else if (if_cond == kCondEQ) {
3570 return false;
3571 }
3572 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003573 }
3574
3575 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003576 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003577 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003578 if (if_cond == kCondEQ) {
3579 return true;
3580 } else if (if_cond == kCondNE) {
3581 return false;
3582 }
3583 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003584 }
3585
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003587 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003588 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003589 static constexpr size_t kFieldComparisonBiasSize =
3590 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3591 static constexpr size_t kNumberOfConditionPackedBits =
3592 kFieldComparisonBias + kFieldComparisonBiasSize;
3593 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3594 using ComparisonBiasField =
3595 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3596
Roland Levillain31dd3d62016-02-16 12:21:02 +00003597 template <typename T>
3598 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3599
3600 template <typename T>
3601 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003602 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003603 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3604 // Handle the bias.
3605 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3606 }
3607
3608 // Return an integer constant containing the result of a condition evaluated at compile time.
3609 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3610 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3611 }
3612
Artem Serovcced8ba2017-07-19 18:18:09 +01003613 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003614};
3615
3616// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003617class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003618 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003619 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303620 : HCondition(kEqual, first, second, dex_pc) {
3621 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003622
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003623 bool IsCommutative() const OVERRIDE { return true; }
3624
Vladimir Marko9e23df52015-11-10 17:14:35 +00003625 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3626 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003627 return MakeConstantCondition(true, GetDexPc());
3628 }
3629 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3630 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3631 }
3632 // In the following Evaluate methods, a HCompare instruction has
3633 // been merged into this HEqual instruction; evaluate it as
3634 // `Compare(x, y) == 0`.
3635 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3636 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3637 GetDexPc());
3638 }
3639 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3640 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3641 }
3642 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3643 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003644 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003645
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003646 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003647
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003648 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003649 return kCondEQ;
3650 }
3651
Mark Mendellc4701932015-04-10 13:18:51 -04003652 IfCondition GetOppositeCondition() const OVERRIDE {
3653 return kCondNE;
3654 }
3655
Artem Serovcced8ba2017-07-19 18:18:09 +01003656 protected:
3657 DEFAULT_COPY_CONSTRUCTOR(Equal);
3658
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003659 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003660 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003661};
3662
Vladimir Markofcb503c2016-05-18 12:48:17 +01003663class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003664 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303665 HNotEqual(HInstruction* first, HInstruction* second,
3666 uint32_t dex_pc = kNoDexPc)
3667 : HCondition(kNotEqual, first, second, dex_pc) {
3668 }
Dave Allison20dfc792014-06-16 20:44:29 -07003669
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003670 bool IsCommutative() const OVERRIDE { return true; }
3671
Vladimir Marko9e23df52015-11-10 17:14:35 +00003672 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3673 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003674 return MakeConstantCondition(false, GetDexPc());
3675 }
3676 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3677 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3678 }
3679 // In the following Evaluate methods, a HCompare instruction has
3680 // been merged into this HNotEqual instruction; evaluate it as
3681 // `Compare(x, y) != 0`.
3682 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3683 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3684 }
3685 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3686 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3687 }
3688 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3689 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003690 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003691
Dave Allison20dfc792014-06-16 20:44:29 -07003692 DECLARE_INSTRUCTION(NotEqual);
3693
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003694 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003695 return kCondNE;
3696 }
3697
Mark Mendellc4701932015-04-10 13:18:51 -04003698 IfCondition GetOppositeCondition() const OVERRIDE {
3699 return kCondEQ;
3700 }
3701
Artem Serovcced8ba2017-07-19 18:18:09 +01003702 protected:
3703 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3704
Dave Allison20dfc792014-06-16 20:44:29 -07003705 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003706 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003707};
3708
Vladimir Markofcb503c2016-05-18 12:48:17 +01003709class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003710 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303711 HLessThan(HInstruction* first, HInstruction* second,
3712 uint32_t dex_pc = kNoDexPc)
3713 : HCondition(kLessThan, first, second, dex_pc) {
3714 }
Dave Allison20dfc792014-06-16 20:44:29 -07003715
Roland Levillain9867bc72015-08-05 10:21:34 +01003716 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003717 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003718 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003719 // In the following Evaluate methods, a HCompare instruction has
3720 // been merged into this HLessThan instruction; evaluate it as
3721 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003722 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003723 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3724 }
3725 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3726 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3727 }
3728 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3729 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003730 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003731
Dave Allison20dfc792014-06-16 20:44:29 -07003732 DECLARE_INSTRUCTION(LessThan);
3733
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003734 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003735 return kCondLT;
3736 }
3737
Mark Mendellc4701932015-04-10 13:18:51 -04003738 IfCondition GetOppositeCondition() const OVERRIDE {
3739 return kCondGE;
3740 }
3741
Artem Serovcced8ba2017-07-19 18:18:09 +01003742 protected:
3743 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3744
Dave Allison20dfc792014-06-16 20:44:29 -07003745 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003746 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003747};
3748
Vladimir Markofcb503c2016-05-18 12:48:17 +01003749class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003750 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303751 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3752 uint32_t dex_pc = kNoDexPc)
3753 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3754 }
Dave Allison20dfc792014-06-16 20:44:29 -07003755
Roland Levillain9867bc72015-08-05 10:21:34 +01003756 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003757 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003758 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003759 // In the following Evaluate methods, a HCompare instruction has
3760 // been merged into this HLessThanOrEqual instruction; evaluate it as
3761 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003762 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003763 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3764 }
3765 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3766 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3767 }
3768 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3769 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003770 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003771
Dave Allison20dfc792014-06-16 20:44:29 -07003772 DECLARE_INSTRUCTION(LessThanOrEqual);
3773
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003774 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003775 return kCondLE;
3776 }
3777
Mark Mendellc4701932015-04-10 13:18:51 -04003778 IfCondition GetOppositeCondition() const OVERRIDE {
3779 return kCondGT;
3780 }
3781
Artem Serovcced8ba2017-07-19 18:18:09 +01003782 protected:
3783 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3784
Dave Allison20dfc792014-06-16 20:44:29 -07003785 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003786 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003787};
3788
Vladimir Markofcb503c2016-05-18 12:48:17 +01003789class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003790 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003791 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303792 : HCondition(kGreaterThan, first, second, dex_pc) {
3793 }
Dave Allison20dfc792014-06-16 20:44:29 -07003794
Roland Levillain9867bc72015-08-05 10:21:34 +01003795 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003796 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003797 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003798 // In the following Evaluate methods, a HCompare instruction has
3799 // been merged into this HGreaterThan instruction; evaluate it as
3800 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003801 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003802 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3803 }
3804 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3805 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3806 }
3807 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3808 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003809 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003810
Dave Allison20dfc792014-06-16 20:44:29 -07003811 DECLARE_INSTRUCTION(GreaterThan);
3812
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003813 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003814 return kCondGT;
3815 }
3816
Mark Mendellc4701932015-04-10 13:18:51 -04003817 IfCondition GetOppositeCondition() const OVERRIDE {
3818 return kCondLE;
3819 }
3820
Artem Serovcced8ba2017-07-19 18:18:09 +01003821 protected:
3822 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3823
Dave Allison20dfc792014-06-16 20:44:29 -07003824 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003825 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003826};
3827
Vladimir Markofcb503c2016-05-18 12:48:17 +01003828class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003829 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003830 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303831 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3832 }
Dave Allison20dfc792014-06-16 20:44:29 -07003833
Roland Levillain9867bc72015-08-05 10:21:34 +01003834 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003835 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003836 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003837 // In the following Evaluate methods, a HCompare instruction has
3838 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3839 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003840 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003841 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3842 }
3843 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3844 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3845 }
3846 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3847 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003848 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003849
Dave Allison20dfc792014-06-16 20:44:29 -07003850 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3851
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003852 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003853 return kCondGE;
3854 }
3855
Mark Mendellc4701932015-04-10 13:18:51 -04003856 IfCondition GetOppositeCondition() const OVERRIDE {
3857 return kCondLT;
3858 }
3859
Artem Serovcced8ba2017-07-19 18:18:09 +01003860 protected:
3861 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3862
Dave Allison20dfc792014-06-16 20:44:29 -07003863 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003864 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003865};
3866
Vladimir Markofcb503c2016-05-18 12:48:17 +01003867class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003868 public:
3869 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303870 : HCondition(kBelow, first, second, dex_pc) {
3871 }
Aart Bike9f37602015-10-09 11:15:55 -07003872
3873 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003874 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003875 }
3876 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003877 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3878 }
3879 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3880 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3881 LOG(FATAL) << DebugName() << " is not defined for float values";
3882 UNREACHABLE();
3883 }
3884 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3885 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3886 LOG(FATAL) << DebugName() << " is not defined for double values";
3887 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003888 }
3889
3890 DECLARE_INSTRUCTION(Below);
3891
3892 IfCondition GetCondition() const OVERRIDE {
3893 return kCondB;
3894 }
3895
3896 IfCondition GetOppositeCondition() const OVERRIDE {
3897 return kCondAE;
3898 }
3899
Artem Serovcced8ba2017-07-19 18:18:09 +01003900 protected:
3901 DEFAULT_COPY_CONSTRUCTOR(Below);
3902
Aart Bike9f37602015-10-09 11:15:55 -07003903 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003904 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003905 return MakeUnsigned(x) < MakeUnsigned(y);
3906 }
Aart Bike9f37602015-10-09 11:15:55 -07003907};
3908
Vladimir Markofcb503c2016-05-18 12:48:17 +01003909class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003910 public:
3911 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303912 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3913 }
Aart Bike9f37602015-10-09 11:15:55 -07003914
3915 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003916 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003917 }
3918 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003919 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3920 }
3921 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3922 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3923 LOG(FATAL) << DebugName() << " is not defined for float values";
3924 UNREACHABLE();
3925 }
3926 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3927 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3928 LOG(FATAL) << DebugName() << " is not defined for double values";
3929 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003930 }
3931
3932 DECLARE_INSTRUCTION(BelowOrEqual);
3933
3934 IfCondition GetCondition() const OVERRIDE {
3935 return kCondBE;
3936 }
3937
3938 IfCondition GetOppositeCondition() const OVERRIDE {
3939 return kCondA;
3940 }
3941
Artem Serovcced8ba2017-07-19 18:18:09 +01003942 protected:
3943 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3944
Aart Bike9f37602015-10-09 11:15:55 -07003945 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003946 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003947 return MakeUnsigned(x) <= MakeUnsigned(y);
3948 }
Aart Bike9f37602015-10-09 11:15:55 -07003949};
3950
Vladimir Markofcb503c2016-05-18 12:48:17 +01003951class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003952 public:
3953 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303954 : HCondition(kAbove, first, second, dex_pc) {
3955 }
Aart Bike9f37602015-10-09 11:15:55 -07003956
3957 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003958 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003959 }
3960 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003961 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3962 }
3963 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3964 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3965 LOG(FATAL) << DebugName() << " is not defined for float values";
3966 UNREACHABLE();
3967 }
3968 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3969 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3970 LOG(FATAL) << DebugName() << " is not defined for double values";
3971 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003972 }
3973
3974 DECLARE_INSTRUCTION(Above);
3975
3976 IfCondition GetCondition() const OVERRIDE {
3977 return kCondA;
3978 }
3979
3980 IfCondition GetOppositeCondition() const OVERRIDE {
3981 return kCondBE;
3982 }
3983
Artem Serovcced8ba2017-07-19 18:18:09 +01003984 protected:
3985 DEFAULT_COPY_CONSTRUCTOR(Above);
3986
Aart Bike9f37602015-10-09 11:15:55 -07003987 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003988 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003989 return MakeUnsigned(x) > MakeUnsigned(y);
3990 }
Aart Bike9f37602015-10-09 11:15:55 -07003991};
3992
Vladimir Markofcb503c2016-05-18 12:48:17 +01003993class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003994 public:
3995 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303996 : HCondition(kAboveOrEqual, first, second, dex_pc) {
3997 }
Aart Bike9f37602015-10-09 11:15:55 -07003998
3999 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004000 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004001 }
4002 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004003 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4004 }
4005 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4006 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4007 LOG(FATAL) << DebugName() << " is not defined for float values";
4008 UNREACHABLE();
4009 }
4010 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4011 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4012 LOG(FATAL) << DebugName() << " is not defined for double values";
4013 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004014 }
4015
4016 DECLARE_INSTRUCTION(AboveOrEqual);
4017
4018 IfCondition GetCondition() const OVERRIDE {
4019 return kCondAE;
4020 }
4021
4022 IfCondition GetOppositeCondition() const OVERRIDE {
4023 return kCondB;
4024 }
4025
Artem Serovcced8ba2017-07-19 18:18:09 +01004026 protected:
4027 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4028
Aart Bike9f37602015-10-09 11:15:55 -07004029 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004030 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004031 return MakeUnsigned(x) >= MakeUnsigned(y);
4032 }
Aart Bike9f37602015-10-09 11:15:55 -07004033};
Dave Allison20dfc792014-06-16 20:44:29 -07004034
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004035// Instruction to check how two inputs compare to each other.
4036// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004037class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004038 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004039 // Note that `comparison_type` is the type of comparison performed
4040 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004041 // HCompare instruction (which is always DataType::Type::kInt).
4042 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004043 HInstruction* first,
4044 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004045 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004046 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304047 : HBinaryOperation(kCompare,
4048 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004049 first,
4050 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004051 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004052 dex_pc) {
4053 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004054 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4055 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004056 }
4057
Roland Levillain9867bc72015-08-05 10:21:34 +01004058 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004059 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4060
4061 template <typename T>
4062 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004064 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4065 // Handle the bias.
4066 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4067 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004068
Roland Levillain9867bc72015-08-05 10:21:34 +01004069 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004070 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4071 // reach this code path when processing a freshly built HIR
4072 // graph. However HCompare integer instructions can be synthesized
4073 // by the instruction simplifier to implement IntegerCompare and
4074 // IntegerSignum intrinsics, so we have to handle this case.
4075 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004076 }
4077 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004078 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4079 }
4080 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4081 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4082 }
4083 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4084 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004085 }
4086
Vladimir Marko372f10e2016-05-17 16:30:10 +01004087 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004088 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004089 }
4090
Vladimir Markoa1de9182016-02-25 11:37:38 +00004091 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004092
Roland Levillain31dd3d62016-02-16 12:21:02 +00004093 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004094 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004095 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004096 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004097 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004098 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004100 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004101 // Comparisons do not require a runtime call in any back end.
4102 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004103 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004104
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004105 DECLARE_INSTRUCTION(Compare);
4106
Roland Levillain31dd3d62016-02-16 12:21:02 +00004107 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004108 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004109 static constexpr size_t kFieldComparisonBiasSize =
4110 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4111 static constexpr size_t kNumberOfComparePackedBits =
4112 kFieldComparisonBias + kFieldComparisonBiasSize;
4113 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4114 using ComparisonBiasField =
4115 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4116
Roland Levillain31dd3d62016-02-16 12:21:02 +00004117 // Return an integer constant containing the result of a comparison evaluated at compile time.
4118 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4119 DCHECK(value == -1 || value == 0 || value == 1) << value;
4120 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4121 }
4122
Artem Serovcced8ba2017-07-19 18:18:09 +01004123 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004124};
4125
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004126class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004127 public:
4128 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004129 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004130 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004131 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004132 bool finalizable,
4133 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304134 : HExpression(kNewInstance,
4135 DataType::Type::kReference,
4136 SideEffects::CanTriggerGC(),
4137 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004138 type_index_(type_index),
4139 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004140 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004141 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004142 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004143 }
4144
Artem Serovcced8ba2017-07-19 18:18:09 +01004145 bool IsClonable() const OVERRIDE { return true; }
4146
Andreas Gampea5b09a62016-11-17 15:21:22 -08004147 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004148 const DexFile& GetDexFile() const { return dex_file_; }
4149
4150 // Calls runtime so needs an environment.
4151 bool NeedsEnvironment() const OVERRIDE { return true; }
4152
Mingyao Yang062157f2016-03-02 10:15:36 -08004153 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4154 bool CanThrow() const OVERRIDE { return true; }
4155
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004156 bool NeedsChecks() const {
4157 return entrypoint_ == kQuickAllocObjectWithChecks;
4158 }
David Brazdil6de19382016-01-08 17:37:10 +00004159
Vladimir Markoa1de9182016-02-25 11:37:38 +00004160 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004161
4162 bool CanBeNull() const OVERRIDE { return false; }
4163
4164 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4165
4166 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4167 entrypoint_ = entrypoint;
4168 }
4169
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004170 HLoadClass* GetLoadClass() const {
4171 HInstruction* input = InputAt(0);
4172 if (input->IsClinitCheck()) {
4173 input = input->InputAt(0);
4174 }
4175 DCHECK(input->IsLoadClass());
4176 return input->AsLoadClass();
4177 }
4178
David Brazdil6de19382016-01-08 17:37:10 +00004179 bool IsStringAlloc() const;
4180
4181 DECLARE_INSTRUCTION(NewInstance);
4182
Artem Serovcced8ba2017-07-19 18:18:09 +01004183 protected:
4184 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4185
David Brazdil6de19382016-01-08 17:37:10 +00004186 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004187 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004188 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4189 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4190 "Too many packed fields.");
4191
Andreas Gampea5b09a62016-11-17 15:21:22 -08004192 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004193 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004194 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004195};
4196
Agi Csaki05f20562015-08-19 14:58:14 -07004197enum IntrinsicNeedsEnvironmentOrCache {
4198 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4199 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004200};
4201
Aart Bik5d75afe2015-12-14 11:57:01 -08004202enum IntrinsicSideEffects {
4203 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4204 kReadSideEffects, // Intrinsic may read heap memory.
4205 kWriteSideEffects, // Intrinsic may write heap memory.
4206 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4207};
4208
4209enum IntrinsicExceptions {
4210 kNoThrow, // Intrinsic does not throw any exceptions.
4211 kCanThrow // Intrinsic may throw exceptions.
4212};
4213
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004214class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004215 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004216 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004217
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004218 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004219 SetRawInputAt(index, argument);
4220 }
4221
Roland Levillain3e3d7332015-04-28 11:00:54 +01004222 // Return the number of arguments. This number can be lower than
4223 // the number of inputs returned by InputCount(), as some invoke
4224 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4225 // inputs at the end of their list of inputs.
4226 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4227
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004228 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4229
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004230 InvokeType GetInvokeType() const {
4231 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004232 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004233
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004234 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004235 return intrinsic_;
4236 }
4237
Aart Bik5d75afe2015-12-14 11:57:01 -08004238 void SetIntrinsic(Intrinsics intrinsic,
4239 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4240 IntrinsicSideEffects side_effects,
4241 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004242
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004243 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004244 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004245 }
4246
Aart Bikff7d89c2016-11-07 08:49:28 -08004247 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4248
Vladimir Markoa1de9182016-02-25 11:37:38 +00004249 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004250
Aart Bika8b8e9b2018-01-09 11:01:02 -08004251 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4252
4253 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4254
Aart Bik71bf7b42016-11-16 10:17:46 -08004255 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004256
Vladimir Marko372f10e2016-05-17 16:30:10 +01004257 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004258 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4259 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004260
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004261 uint32_t* GetIntrinsicOptimizations() {
4262 return &intrinsic_optimizations_;
4263 }
4264
4265 const uint32_t* GetIntrinsicOptimizations() const {
4266 return &intrinsic_optimizations_;
4267 }
4268
4269 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4270
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004271 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004272 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004273
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004274 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004275
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004276 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004277 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4278 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004279 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004280 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004281 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4282 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004283 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004284 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004285
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304286 HInvoke(InstructionKind kind,
4287 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004288 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004289 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004290 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004291 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004292 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004293 ArtMethod* resolved_method,
4294 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004295 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304296 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004297 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004298 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4299 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004300 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004301 number_of_arguments + number_of_other_inputs,
4302 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004303 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004304 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004305 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004306 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004307 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004308 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004309 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004310 }
4311
Artem Serovcced8ba2017-07-19 18:18:09 +01004312 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4313
Roland Levillain3e3d7332015-04-28 11:00:54 +01004314 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004315 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004316 const uint32_t dex_method_index_;
4317 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004318
4319 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4320 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004321};
4322
Vladimir Markofcb503c2016-05-18 12:48:17 +01004323class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004324 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004325 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004326 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004327 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004328 uint32_t dex_pc,
4329 uint32_t dex_method_index,
4330 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304331 : HInvoke(kInvokeUnresolved,
4332 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004333 number_of_arguments,
4334 0u /* number_of_other_inputs */,
4335 return_type,
4336 dex_pc,
4337 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004338 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004339 invoke_type) {
4340 }
4341
Artem Serovcced8ba2017-07-19 18:18:09 +01004342 bool IsClonable() const OVERRIDE { return true; }
4343
Calin Juravle175dc732015-08-25 15:42:32 +01004344 DECLARE_INSTRUCTION(InvokeUnresolved);
4345
Artem Serovcced8ba2017-07-19 18:18:09 +01004346 protected:
4347 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004348};
4349
Orion Hodsonac141392017-01-13 11:53:47 +00004350class HInvokePolymorphic FINAL : public HInvoke {
4351 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004352 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004353 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004354 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004355 uint32_t dex_pc,
4356 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304357 : HInvoke(kInvokePolymorphic,
4358 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004359 number_of_arguments,
4360 0u /* number_of_other_inputs */,
4361 return_type,
4362 dex_pc,
4363 dex_method_index,
4364 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304365 kVirtual) {
4366 }
Orion Hodsonac141392017-01-13 11:53:47 +00004367
Artem Serovcced8ba2017-07-19 18:18:09 +01004368 bool IsClonable() const OVERRIDE { return true; }
4369
Orion Hodsonac141392017-01-13 11:53:47 +00004370 DECLARE_INSTRUCTION(InvokePolymorphic);
4371
Artem Serovcced8ba2017-07-19 18:18:09 +01004372 protected:
4373 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004374};
4375
Vladimir Markofcb503c2016-05-18 12:48:17 +01004376class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004377 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004378 // Requirements of this method call regarding the class
4379 // initialization (clinit) check of its declaring class.
4380 enum class ClinitCheckRequirement {
4381 kNone, // Class already initialized.
4382 kExplicit, // Static call having explicit clinit check as last input.
4383 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004384 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004385 };
4386
Vladimir Marko58155012015-08-19 12:49:41 +00004387 // Determines how to load the target ArtMethod*.
4388 enum class MethodLoadKind {
4389 // Use a String init ArtMethod* loaded from Thread entrypoints.
4390 kStringInit,
4391
4392 // Use the method's own ArtMethod* loaded by the register allocator.
4393 kRecursive,
4394
Vladimir Marko65979462017-05-19 17:25:12 +01004395 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4396 // Used for boot image methods referenced by boot image code.
4397 kBootImageLinkTimePcRelative,
4398
Vladimir Marko58155012015-08-19 12:49:41 +00004399 // Use ArtMethod* at a known address, embed the direct address in the code.
4400 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4401 kDirectAddress,
4402
Vladimir Markob066d432018-01-03 13:14:37 +00004403 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4404 // Used for app->boot calls with relocatable image.
4405 kBootImageRelRo,
4406
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004407 // Load from an entry in the .bss section using a PC-relative load.
4408 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4409 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004410
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004411 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4412 // used when other kinds are unimplemented on a particular architecture.
4413 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004414 };
4415
4416 // Determines the location of the code pointer.
4417 enum class CodePtrLocation {
4418 // Recursive call, use local PC-relative call instruction.
4419 kCallSelf,
4420
Vladimir Marko58155012015-08-19 12:49:41 +00004421 // Use code pointer from the ArtMethod*.
4422 // Used when we don't know the target code. This is also the last-resort-kind used when
4423 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4424 kCallArtMethod,
4425 };
4426
4427 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004428 MethodLoadKind method_load_kind;
4429 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004430 // The method load data holds
4431 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4432 // Note that there are multiple string init methods, each having its own offset.
4433 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004434 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004435 };
4436
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004437 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004438 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004439 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004440 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004441 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004442 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004443 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004444 InvokeType invoke_type,
4445 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004446 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304447 : HInvoke(kInvokeStaticOrDirect,
4448 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004449 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004450 // There is potentially one extra argument for the HCurrentMethod node, and
4451 // potentially one other if the clinit check is explicit, and potentially
4452 // one other if the method is a string factory.
4453 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004454 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004455 return_type,
4456 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004457 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004458 resolved_method,
4459 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004460 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004461 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004462 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4463 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004464
Artem Serovcced8ba2017-07-19 18:18:09 +01004465 bool IsClonable() const OVERRIDE { return true; }
4466
Vladimir Markodc151b22015-10-15 18:02:30 +01004467 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004468 bool had_current_method_input = HasCurrentMethodInput();
4469 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4470
4471 // Using the current method is the default and once we find a better
4472 // method load kind, we should not go back to using the current method.
4473 DCHECK(had_current_method_input || !needs_current_method_input);
4474
4475 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004476 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4477 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004478 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004479 dispatch_info_ = dispatch_info;
4480 }
4481
Aart Bik6daebeb2017-04-03 14:35:41 -07004482 DispatchInfo GetDispatchInfo() const {
4483 return dispatch_info_;
4484 }
4485
Vladimir Markoc53c0792015-11-19 15:48:33 +00004486 void AddSpecialInput(HInstruction* input) {
4487 // We allow only one special input.
4488 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4489 DCHECK(InputCount() == GetSpecialInputIndex() ||
4490 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4491 InsertInputAt(GetSpecialInputIndex(), input);
4492 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004493
Vladimir Marko372f10e2016-05-17 16:30:10 +01004494 using HInstruction::GetInputRecords; // Keep the const version visible.
4495 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4496 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4497 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4498 DCHECK(!input_records.empty());
4499 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4500 HInstruction* last_input = input_records.back().GetInstruction();
4501 // Note: `last_input` may be null during arguments setup.
4502 if (last_input != nullptr) {
4503 // `last_input` is the last input of a static invoke marked as having
4504 // an explicit clinit check. It must either be:
4505 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4506 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4507 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4508 }
4509 }
4510 return input_records;
4511 }
4512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004513 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004514 // We access the method via the dex cache so we can't do an implicit null check.
4515 // TODO: for intrinsics we can generate implicit null checks.
4516 return false;
4517 }
4518
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004519 bool CanBeNull() const OVERRIDE {
Vladimir Markobd785672018-05-03 17:09:09 +01004520 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004521 }
4522
Vladimir Markoc53c0792015-11-19 15:48:33 +00004523 // Get the index of the special input, if any.
4524 //
David Brazdil6de19382016-01-08 17:37:10 +00004525 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4526 // method pointer; otherwise there may be one platform-specific special input,
4527 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004528 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004529 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004530
Vladimir Marko58155012015-08-19 12:49:41 +00004531 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4532 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4533 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004534 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004535 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004536 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004537 bool HasPcRelativeMethodLoadKind() const {
4538 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004539 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004540 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004541 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004542 bool HasCurrentMethodInput() const {
4543 // This function can be called only after the invoke has been fully initialized by the builder.
4544 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004545 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004546 return true;
4547 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004548 DCHECK(InputCount() == GetSpecialInputIndex() ||
4549 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004550 return false;
4551 }
4552 }
Vladimir Marko58155012015-08-19 12:49:41 +00004553
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004554 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004555 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004556 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004557 }
4558
4559 uint64_t GetMethodAddress() const {
4560 DCHECK(HasMethodAddress());
4561 return dispatch_info_.method_load_data;
4562 }
4563
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004564 const DexFile& GetDexFileForPcRelativeDexCache() const;
4565
Vladimir Markoa1de9182016-02-25 11:37:38 +00004566 ClinitCheckRequirement GetClinitCheckRequirement() const {
4567 return GetPackedField<ClinitCheckRequirementField>();
4568 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004569
Roland Levillain4c0eb422015-04-24 16:43:49 +01004570 // Is this instruction a call to a static method?
4571 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004572 return GetInvokeType() == kStatic;
4573 }
4574
4575 MethodReference GetTargetMethod() const {
4576 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004577 }
4578
Vladimir Markofbb184a2015-11-13 14:47:00 +00004579 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4580 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4581 // instruction; only relevant for static calls with explicit clinit check.
4582 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004583 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004584 size_t last_input_index = inputs_.size() - 1u;
4585 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004586 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004587 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004588 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004589 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004590 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004591 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004592 }
4593
4594 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004595 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004596 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004597 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004598 }
4599
4600 // Is this a call to a static method whose declaring class has an
4601 // implicit intialization check requirement?
4602 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004603 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004604 }
4605
Vladimir Markob554b5a2015-11-06 12:57:55 +00004606 // Does this method load kind need the current method as an input?
4607 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004608 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004609 }
4610
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004611 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004612
Artem Serovcced8ba2017-07-19 18:18:09 +01004613 protected:
4614 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4615
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004616 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004617 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004618 static constexpr size_t kFieldClinitCheckRequirementSize =
4619 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4620 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4621 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4622 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4623 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004624 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4625 kFieldClinitCheckRequirement,
4626 kFieldClinitCheckRequirementSize>;
4627
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004628 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004629 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004630 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004631};
Vladimir Markof64242a2015-12-01 14:58:23 +00004632std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004633std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004634
Vladimir Markofcb503c2016-05-18 12:48:17 +01004635class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004636 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004637 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004638 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004639 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004640 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004641 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004642 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004643 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304644 : HInvoke(kInvokeVirtual,
4645 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004646 number_of_arguments,
4647 0u,
4648 return_type,
4649 dex_pc,
4650 dex_method_index,
4651 resolved_method,
4652 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304653 vtable_index_(vtable_index) {
4654 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004655
Artem Serovcced8ba2017-07-19 18:18:09 +01004656 bool IsClonable() const OVERRIDE { return true; }
4657
Aart Bik71bf7b42016-11-16 10:17:46 -08004658 bool CanBeNull() const OVERRIDE {
4659 switch (GetIntrinsic()) {
4660 case Intrinsics::kThreadCurrentThread:
4661 case Intrinsics::kStringBufferAppend:
4662 case Intrinsics::kStringBufferToString:
4663 case Intrinsics::kStringBuilderAppend:
4664 case Intrinsics::kStringBuilderToString:
4665 return false;
4666 default:
4667 return HInvoke::CanBeNull();
4668 }
4669 }
4670
Calin Juravle641547a2015-04-21 22:08:51 +01004671 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004672 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004673 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004674 }
4675
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004676 uint32_t GetVTableIndex() const { return vtable_index_; }
4677
4678 DECLARE_INSTRUCTION(InvokeVirtual);
4679
Artem Serovcced8ba2017-07-19 18:18:09 +01004680 protected:
4681 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4682
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004683 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004684 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004685 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004686};
4687
Vladimir Markofcb503c2016-05-18 12:48:17 +01004688class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004689 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004690 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004691 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004693 uint32_t dex_pc,
4694 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004695 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004696 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304697 : HInvoke(kInvokeInterface,
4698 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004699 number_of_arguments,
4700 0u,
4701 return_type,
4702 dex_pc,
4703 dex_method_index,
4704 resolved_method,
4705 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304706 imt_index_(imt_index) {
4707 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004708
Artem Serovcced8ba2017-07-19 18:18:09 +01004709 bool IsClonable() const OVERRIDE { return true; }
4710
Calin Juravle641547a2015-04-21 22:08:51 +01004711 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004712 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004713 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004714 }
4715
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004716 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4717 // The assembly stub currently needs it.
4718 return true;
4719 }
4720
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004721 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004722
4723 DECLARE_INSTRUCTION(InvokeInterface);
4724
Artem Serovcced8ba2017-07-19 18:18:09 +01004725 protected:
4726 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4727
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004728 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004729 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004730 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004731};
4732
Vladimir Markofcb503c2016-05-18 12:48:17 +01004733class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004734 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004735 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304736 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004738 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004739
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004740 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004741
4742 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004743 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004744 }
4745 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004747 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004748 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4749 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4750 }
4751 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4752 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4753 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004754
Roland Levillain88cb1752014-10-20 16:36:47 +01004755 DECLARE_INSTRUCTION(Neg);
4756
Artem Serovcced8ba2017-07-19 18:18:09 +01004757 protected:
4758 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004759};
4760
Vladimir Markofcb503c2016-05-18 12:48:17 +01004761class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004762 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004763 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304764 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004765 SetRawInputAt(0, cls);
4766 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004767 }
4768
Artem Serovcced8ba2017-07-19 18:18:09 +01004769 bool IsClonable() const OVERRIDE { return true; }
4770
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004771 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004772 bool NeedsEnvironment() const OVERRIDE { return true; }
4773
Mingyao Yang0c365e62015-03-31 15:09:29 -07004774 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4775 bool CanThrow() const OVERRIDE { return true; }
4776
Calin Juravle10e244f2015-01-26 18:54:32 +00004777 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004778
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004779 HLoadClass* GetLoadClass() const {
4780 DCHECK(InputAt(0)->IsLoadClass());
4781 return InputAt(0)->AsLoadClass();
4782 }
4783
4784 HInstruction* GetLength() const {
4785 return InputAt(1);
4786 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004787
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004788 DECLARE_INSTRUCTION(NewArray);
4789
Artem Serovcced8ba2017-07-19 18:18:09 +01004790 protected:
4791 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004792};
4793
Vladimir Markofcb503c2016-05-18 12:48:17 +01004794class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004795 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004797 HInstruction* left,
4798 HInstruction* right,
4799 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304800 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4801 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004802
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004803 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004804
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004805 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004806
4807 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004808 return GetBlock()->GetGraph()->GetIntConstant(
4809 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004810 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004811 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004812 return GetBlock()->GetGraph()->GetLongConstant(
4813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004814 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004815 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4816 return GetBlock()->GetGraph()->GetFloatConstant(
4817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4818 }
4819 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4820 return GetBlock()->GetGraph()->GetDoubleConstant(
4821 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4822 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004823
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004824 DECLARE_INSTRUCTION(Add);
4825
Artem Serovcced8ba2017-07-19 18:18:09 +01004826 protected:
4827 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004828};
4829
Vladimir Markofcb503c2016-05-18 12:48:17 +01004830class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004831 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004832 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004833 HInstruction* left,
4834 HInstruction* right,
4835 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304836 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4837 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004838
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004839 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004840
4841 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004842 return GetBlock()->GetGraph()->GetIntConstant(
4843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004844 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 return GetBlock()->GetGraph()->GetLongConstant(
4847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004848 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004849 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4850 return GetBlock()->GetGraph()->GetFloatConstant(
4851 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4852 }
4853 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4854 return GetBlock()->GetGraph()->GetDoubleConstant(
4855 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4856 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004857
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004858 DECLARE_INSTRUCTION(Sub);
4859
Artem Serovcced8ba2017-07-19 18:18:09 +01004860 protected:
4861 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004862};
4863
Vladimir Markofcb503c2016-05-18 12:48:17 +01004864class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004865 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004866 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004867 HInstruction* left,
4868 HInstruction* right,
4869 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304870 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4871 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004872
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004873 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004874
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004875 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004876
4877 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004878 return GetBlock()->GetGraph()->GetIntConstant(
4879 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004880 }
4881 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004882 return GetBlock()->GetGraph()->GetLongConstant(
4883 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004884 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004885 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4886 return GetBlock()->GetGraph()->GetFloatConstant(
4887 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4888 }
4889 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4890 return GetBlock()->GetGraph()->GetDoubleConstant(
4891 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4892 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004893
4894 DECLARE_INSTRUCTION(Mul);
4895
Artem Serovcced8ba2017-07-19 18:18:09 +01004896 protected:
4897 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004898};
4899
Vladimir Markofcb503c2016-05-18 12:48:17 +01004900class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004901 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004902 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004903 HInstruction* left,
4904 HInstruction* right,
4905 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304906 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4907 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004908
Roland Levillain9867bc72015-08-05 10:21:34 +01004909 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004910 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004911 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004912 // Our graph structure ensures we never have 0 for `y` during
4913 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004914 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004915 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004916 return (y == -1) ? -x : x / y;
4917 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004918
Roland Levillain31dd3d62016-02-16 12:21:02 +00004919 template <typename T>
4920 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004921 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004922 return x / y;
4923 }
4924
Roland Levillain9867bc72015-08-05 10:21:34 +01004925 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004926 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004927 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004928 }
4929 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004930 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004931 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4932 }
4933 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4934 return GetBlock()->GetGraph()->GetFloatConstant(
4935 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4936 }
4937 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4938 return GetBlock()->GetGraph()->GetDoubleConstant(
4939 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004940 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004941
4942 DECLARE_INSTRUCTION(Div);
4943
Artem Serovcced8ba2017-07-19 18:18:09 +01004944 protected:
4945 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004946};
4947
Vladimir Markofcb503c2016-05-18 12:48:17 +01004948class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004949 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004950 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004951 HInstruction* left,
4952 HInstruction* right,
4953 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304954 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4955 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004956
Roland Levillain9867bc72015-08-05 10:21:34 +01004957 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004958 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004959 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004960 // Our graph structure ensures we never have 0 for `y` during
4961 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004962 DCHECK_NE(y, 0);
4963 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4964 return (y == -1) ? 0 : x % y;
4965 }
4966
Roland Levillain31dd3d62016-02-16 12:21:02 +00004967 template <typename T>
4968 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004969 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004970 return std::fmod(x, y);
4971 }
4972
Roland Levillain9867bc72015-08-05 10:21:34 +01004973 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004974 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004975 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004976 }
4977 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004978 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004979 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004980 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004981 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4982 return GetBlock()->GetGraph()->GetFloatConstant(
4983 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4984 }
4985 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4986 return GetBlock()->GetGraph()->GetDoubleConstant(
4987 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4988 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004989
4990 DECLARE_INSTRUCTION(Rem);
4991
Artem Serovcced8ba2017-07-19 18:18:09 +01004992 protected:
4993 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004994};
4995
Aart Bik1f8d51b2018-02-15 10:42:37 -08004996class HMin FINAL : public HBinaryOperation {
4997 public:
4998 HMin(DataType::Type result_type,
4999 HInstruction* left,
5000 HInstruction* right,
5001 uint32_t dex_pc)
5002 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5003
5004 bool IsCommutative() const OVERRIDE { return true; }
5005
5006 // Evaluation for integral values.
5007 template <typename T> static T ComputeIntegral(T x, T y) {
5008 return (x <= y) ? x : y;
5009 }
5010
5011 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5012 return GetBlock()->GetGraph()->GetIntConstant(
5013 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5014 }
5015 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5016 return GetBlock()->GetGraph()->GetLongConstant(
5017 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5018 }
5019 // TODO: Evaluation for floating-point values.
5020 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5021 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5022 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5023 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5024
5025 DECLARE_INSTRUCTION(Min);
5026
5027 protected:
5028 DEFAULT_COPY_CONSTRUCTOR(Min);
5029};
5030
5031class HMax FINAL : public HBinaryOperation {
5032 public:
5033 HMax(DataType::Type result_type,
5034 HInstruction* left,
5035 HInstruction* right,
5036 uint32_t dex_pc)
5037 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5038
5039 bool IsCommutative() const OVERRIDE { return true; }
5040
5041 // Evaluation for integral values.
5042 template <typename T> static T ComputeIntegral(T x, T y) {
5043 return (x >= y) ? x : y;
5044 }
5045
5046 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5047 return GetBlock()->GetGraph()->GetIntConstant(
5048 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5049 }
5050 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5051 return GetBlock()->GetGraph()->GetLongConstant(
5052 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5053 }
5054 // TODO: Evaluation for floating-point values.
5055 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5056 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5057 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5058 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5059
5060 DECLARE_INSTRUCTION(Max);
5061
5062 protected:
5063 DEFAULT_COPY_CONSTRUCTOR(Max);
5064};
5065
Aart Bik3dad3412018-02-28 12:01:46 -08005066class HAbs FINAL : public HUnaryOperation {
5067 public:
5068 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5069 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5070
5071 // Evaluation for integral values.
5072 template <typename T> static T ComputeIntegral(T x) {
5073 return x < 0 ? -x : x;
5074 }
5075
5076 // Evaluation for floating-point values.
5077 // Note, as a "quality of implementation", rather than pure "spec compliance",
5078 // we require that Math.abs() clears the sign bit (but changes nothing else)
5079 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5080 // http://b/30758343
5081 template <typename T, typename S> static T ComputeFP(T x) {
5082 S bits = bit_cast<S, T>(x);
5083 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5084 }
5085
5086 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5087 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5088 }
5089 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5090 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5091 }
5092 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5093 return GetBlock()->GetGraph()->GetFloatConstant(
5094 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5095 }
5096 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5097 return GetBlock()->GetGraph()->GetDoubleConstant(
5098 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5099 }
5100
5101 DECLARE_INSTRUCTION(Abs);
5102
5103 protected:
5104 DEFAULT_COPY_CONSTRUCTOR(Abs);
5105};
5106
Vladimir Markofcb503c2016-05-18 12:48:17 +01005107class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005108 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005109 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5110 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005111 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305112 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005113 SetRawInputAt(0, value);
5114 }
5115
5116 bool CanBeMoved() const OVERRIDE { return true; }
5117
Vladimir Marko372f10e2016-05-17 16:30:10 +01005118 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005119 return true;
5120 }
5121
5122 bool NeedsEnvironment() const OVERRIDE { return true; }
5123 bool CanThrow() const OVERRIDE { return true; }
5124
Calin Juravled0d48522014-11-04 16:40:20 +00005125 DECLARE_INSTRUCTION(DivZeroCheck);
5126
Artem Serovcced8ba2017-07-19 18:18:09 +01005127 protected:
5128 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005129};
5130
Vladimir Markofcb503c2016-05-18 12:48:17 +01005131class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005132 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005133 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005134 HInstruction* value,
5135 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005136 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305137 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005138 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5139 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005140 }
5141
Roland Levillain5b5b9312016-03-22 14:57:31 +00005142 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005143 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005144 return value << (distance & max_shift_distance);
5145 }
5146
5147 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005148 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005149 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005150 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005151 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005152 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005153 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005154 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005155 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5156 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5157 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5158 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005159 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005160 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5161 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005162 LOG(FATAL) << DebugName() << " is not defined for float values";
5163 UNREACHABLE();
5164 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005165 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5166 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005167 LOG(FATAL) << DebugName() << " is not defined for double values";
5168 UNREACHABLE();
5169 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005170
5171 DECLARE_INSTRUCTION(Shl);
5172
Artem Serovcced8ba2017-07-19 18:18:09 +01005173 protected:
5174 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005175};
5176
Vladimir Markofcb503c2016-05-18 12:48:17 +01005177class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005178 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005179 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005180 HInstruction* value,
5181 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005182 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305183 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005184 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5185 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005186 }
5187
Roland Levillain5b5b9312016-03-22 14:57:31 +00005188 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005189 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005190 return value >> (distance & max_shift_distance);
5191 }
5192
5193 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005194 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005195 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005196 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005197 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005198 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005199 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005200 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005201 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5202 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5203 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5204 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005205 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005206 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5207 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005208 LOG(FATAL) << DebugName() << " is not defined for float values";
5209 UNREACHABLE();
5210 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005211 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5212 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005213 LOG(FATAL) << DebugName() << " is not defined for double values";
5214 UNREACHABLE();
5215 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005216
5217 DECLARE_INSTRUCTION(Shr);
5218
Artem Serovcced8ba2017-07-19 18:18:09 +01005219 protected:
5220 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005221};
5222
Vladimir Markofcb503c2016-05-18 12:48:17 +01005223class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005224 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005225 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005226 HInstruction* value,
5227 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005228 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305229 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005230 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5231 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005232 }
5233
Roland Levillain5b5b9312016-03-22 14:57:31 +00005234 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005235 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005236 typedef typename std::make_unsigned<T>::type V;
5237 V ux = static_cast<V>(value);
5238 return static_cast<T>(ux >> (distance & max_shift_distance));
5239 }
5240
5241 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005242 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005243 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005244 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005245 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005246 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005247 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005248 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005249 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5250 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5251 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5252 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005253 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005254 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5255 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005256 LOG(FATAL) << DebugName() << " is not defined for float values";
5257 UNREACHABLE();
5258 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005259 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5260 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005261 LOG(FATAL) << DebugName() << " is not defined for double values";
5262 UNREACHABLE();
5263 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005264
5265 DECLARE_INSTRUCTION(UShr);
5266
Artem Serovcced8ba2017-07-19 18:18:09 +01005267 protected:
5268 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005269};
5270
Vladimir Markofcb503c2016-05-18 12:48:17 +01005271class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005272 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005273 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005274 HInstruction* left,
5275 HInstruction* right,
5276 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305277 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5278 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005279
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005280 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005281
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005282 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005283
5284 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005285 return GetBlock()->GetGraph()->GetIntConstant(
5286 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005287 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005288 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005289 return GetBlock()->GetGraph()->GetLongConstant(
5290 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005291 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005292 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5293 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5294 LOG(FATAL) << DebugName() << " is not defined for float values";
5295 UNREACHABLE();
5296 }
5297 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5298 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5299 LOG(FATAL) << DebugName() << " is not defined for double values";
5300 UNREACHABLE();
5301 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005302
5303 DECLARE_INSTRUCTION(And);
5304
Artem Serovcced8ba2017-07-19 18:18:09 +01005305 protected:
5306 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005307};
5308
Vladimir Markofcb503c2016-05-18 12:48:17 +01005309class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005310 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005311 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005312 HInstruction* left,
5313 HInstruction* right,
5314 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305315 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5316 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005317
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005318 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005319
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005320 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005321
5322 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005323 return GetBlock()->GetGraph()->GetIntConstant(
5324 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005325 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005326 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005327 return GetBlock()->GetGraph()->GetLongConstant(
5328 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005329 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005330 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5331 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5332 LOG(FATAL) << DebugName() << " is not defined for float values";
5333 UNREACHABLE();
5334 }
5335 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5336 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5337 LOG(FATAL) << DebugName() << " is not defined for double values";
5338 UNREACHABLE();
5339 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005340
5341 DECLARE_INSTRUCTION(Or);
5342
Artem Serovcced8ba2017-07-19 18:18:09 +01005343 protected:
5344 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005345};
5346
Vladimir Markofcb503c2016-05-18 12:48:17 +01005347class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005348 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005349 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005350 HInstruction* left,
5351 HInstruction* right,
5352 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305353 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5354 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005355
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005356 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005357
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005358 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005359
5360 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005361 return GetBlock()->GetGraph()->GetIntConstant(
5362 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005363 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005364 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005365 return GetBlock()->GetGraph()->GetLongConstant(
5366 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005367 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005368 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5369 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5370 LOG(FATAL) << DebugName() << " is not defined for float values";
5371 UNREACHABLE();
5372 }
5373 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5374 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5375 LOG(FATAL) << DebugName() << " is not defined for double values";
5376 UNREACHABLE();
5377 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005378
5379 DECLARE_INSTRUCTION(Xor);
5380
Artem Serovcced8ba2017-07-19 18:18:09 +01005381 protected:
5382 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005383};
5384
Vladimir Markofcb503c2016-05-18 12:48:17 +01005385class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005386 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005387 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305388 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005389 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5390 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005391 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005392
Roland Levillain5b5b9312016-03-22 14:57:31 +00005393 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005394 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005395 typedef typename std::make_unsigned<T>::type V;
5396 V ux = static_cast<V>(value);
5397 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005398 return static_cast<T>(ux);
5399 } else {
5400 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005401 return static_cast<T>(ux >> (distance & max_shift_value)) |
5402 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005403 }
5404 }
5405
Roland Levillain5b5b9312016-03-22 14:57:31 +00005406 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005407 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005408 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005409 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005410 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005411 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005412 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005413 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005414 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5415 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5416 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5417 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005418 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005419 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5420 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005421 LOG(FATAL) << DebugName() << " is not defined for float values";
5422 UNREACHABLE();
5423 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005424 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5425 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005426 LOG(FATAL) << DebugName() << " is not defined for double values";
5427 UNREACHABLE();
5428 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005429
5430 DECLARE_INSTRUCTION(Ror);
5431
Artem Serovcced8ba2017-07-19 18:18:09 +01005432 protected:
5433 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005434};
5435
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005436// The value of a parameter in this method. Its location depends on
5437// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005438class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005439 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005440 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005441 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005442 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005443 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005444 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305445 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005446 dex_file_(dex_file),
5447 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005448 index_(index) {
5449 SetPackedFlag<kFlagIsThis>(is_this);
5450 SetPackedFlag<kFlagCanBeNull>(!is_this);
5451 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005452
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005453 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005454 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005455 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005456 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005457
Vladimir Markoa1de9182016-02-25 11:37:38 +00005458 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5459 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005460
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005461 DECLARE_INSTRUCTION(ParameterValue);
5462
Artem Serovcced8ba2017-07-19 18:18:09 +01005463 protected:
5464 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5465
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005466 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005467 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005468 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005469 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5470 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5471 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5472 "Too many packed fields.");
5473
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005474 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005475 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005476 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005477 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005478 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005479};
5480
Vladimir Markofcb503c2016-05-18 12:48:17 +01005481class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005482 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005483 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305484 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5485 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005486
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005487 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005488 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005489 return true;
5490 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005491
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005492 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005493
5494 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005495 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005496 }
5497 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005498 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005499 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005500 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5501 LOG(FATAL) << DebugName() << " is not defined for float values";
5502 UNREACHABLE();
5503 }
5504 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5505 LOG(FATAL) << DebugName() << " is not defined for double values";
5506 UNREACHABLE();
5507 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005508
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005509 DECLARE_INSTRUCTION(Not);
5510
Artem Serovcced8ba2017-07-19 18:18:09 +01005511 protected:
5512 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005513};
5514
Vladimir Markofcb503c2016-05-18 12:48:17 +01005515class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005516 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005517 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305518 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5519 }
David Brazdil66d126e2015-04-03 16:02:44 +01005520
5521 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005522 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005523 return true;
5524 }
5525
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005526 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005527 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005528 return !x;
5529 }
5530
Roland Levillain9867bc72015-08-05 10:21:34 +01005531 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005532 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005533 }
5534 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5535 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005536 UNREACHABLE();
5537 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005538 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5539 LOG(FATAL) << DebugName() << " is not defined for float values";
5540 UNREACHABLE();
5541 }
5542 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5543 LOG(FATAL) << DebugName() << " is not defined for double values";
5544 UNREACHABLE();
5545 }
David Brazdil66d126e2015-04-03 16:02:44 +01005546
5547 DECLARE_INSTRUCTION(BooleanNot);
5548
Artem Serovcced8ba2017-07-19 18:18:09 +01005549 protected:
5550 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005551};
5552
Vladimir Markofcb503c2016-05-18 12:48:17 +01005553class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005554 public:
5555 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005556 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305557 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005558 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005559 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005560 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005561 }
5562
5563 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005564 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5565 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005566
5567 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005568 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5569 return true;
5570 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005571
Mark Mendelle82549b2015-05-06 10:55:34 -04005572 // Try to statically evaluate the conversion and return a HConstant
5573 // containing the result. If the input cannot be converted, return nullptr.
5574 HConstant* TryStaticEvaluation() const;
5575
Roland Levillaindff1f282014-11-05 14:15:05 +00005576 DECLARE_INSTRUCTION(TypeConversion);
5577
Artem Serovcced8ba2017-07-19 18:18:09 +01005578 protected:
5579 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005580};
5581
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005582static constexpr uint32_t kNoRegNumber = -1;
5583
Vladimir Markofcb503c2016-05-18 12:48:17 +01005584class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005585 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005586 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5587 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005588 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305589 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005590 SetRawInputAt(0, value);
5591 }
5592
Artem Serovcced8ba2017-07-19 18:18:09 +01005593 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005594 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005595 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005596 return true;
5597 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005598
Calin Juravle10e244f2015-01-26 18:54:32 +00005599 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005600
Calin Juravle10e244f2015-01-26 18:54:32 +00005601 bool CanThrow() const OVERRIDE { return true; }
5602
5603 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005604
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005605 DECLARE_INSTRUCTION(NullCheck);
5606
Artem Serovcced8ba2017-07-19 18:18:09 +01005607 protected:
5608 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005609};
5610
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005611// Embeds an ArtField and all the information required by the compiler. We cache
5612// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005613class FieldInfo : public ValueObject {
5614 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005615 FieldInfo(ArtField* field,
5616 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005617 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005618 bool is_volatile,
5619 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005620 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005621 const DexFile& dex_file)
5622 : field_(field),
5623 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005624 field_type_(field_type),
5625 is_volatile_(is_volatile),
5626 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005627 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005628 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005629
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005630 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005631 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005632 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005633 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005634 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005635 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005636 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005637
5638 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005639 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005640 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005641 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005642 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005643 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005644 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005645 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005646};
5647
Vladimir Markofcb503c2016-05-18 12:48:17 +01005648class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005649 public:
5650 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005651 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005653 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005654 bool is_volatile,
5655 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005656 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005657 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005658 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305659 : HExpression(kInstanceFieldGet,
5660 field_type,
5661 SideEffects::FieldReadOfType(field_type, is_volatile),
5662 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005663 field_info_(field,
5664 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005665 field_type,
5666 is_volatile,
5667 field_idx,
5668 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005669 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005670 SetRawInputAt(0, value);
5671 }
5672
Artem Serovcced8ba2017-07-19 18:18:09 +01005673 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005674 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005675
Vladimir Marko372f10e2016-05-17 16:30:10 +01005676 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5677 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005678 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005679 }
5680
Calin Juravle641547a2015-04-21 22:08:51 +01005681 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005682 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005683 }
5684
5685 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005686 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5687 }
5688
Calin Juravle52c48962014-12-16 17:02:57 +00005689 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005690 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005691 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005692 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005693
Vladimir Marko61b92282017-10-11 13:23:17 +01005694 void SetType(DataType::Type new_type) {
5695 DCHECK(DataType::IsIntegralType(GetType()));
5696 DCHECK(DataType::IsIntegralType(new_type));
5697 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5698 SetPackedField<TypeField>(new_type);
5699 }
5700
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005701 DECLARE_INSTRUCTION(InstanceFieldGet);
5702
Artem Serovcced8ba2017-07-19 18:18:09 +01005703 protected:
5704 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5705
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005706 private:
5707 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005708};
5709
Vladimir Markobd785672018-05-03 17:09:09 +01005710class HInstanceFieldSet FINAL : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005711 public:
5712 HInstanceFieldSet(HInstruction* object,
5713 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005714 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005715 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005716 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005717 bool is_volatile,
5718 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005719 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005720 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005721 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005722 : HExpression(kInstanceFieldSet,
5723 SideEffects::FieldWriteOfType(field_type, is_volatile),
5724 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005725 field_info_(field,
5726 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005727 field_type,
5728 is_volatile,
5729 field_idx,
5730 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005731 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005732 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005733 SetRawInputAt(0, object);
5734 SetRawInputAt(1, value);
5735 }
5736
Artem Serovcced8ba2017-07-19 18:18:09 +01005737 bool IsClonable() const OVERRIDE { return true; }
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
Calin Juravle52c48962014-12-16 17:02:57 +00005743 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005744 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005745 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005746 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005747 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005748 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5749 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005750
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005751 DECLARE_INSTRUCTION(InstanceFieldSet);
5752
Artem Serovcced8ba2017-07-19 18:18:09 +01005753 protected:
5754 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5755
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005756 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005757 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5758 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5759 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5760 "Too many packed fields.");
5761
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005762 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005763};
5764
Vladimir Markofcb503c2016-05-18 12:48:17 +01005765class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005766 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005767 HArrayGet(HInstruction* array,
5768 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005769 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005770 uint32_t dex_pc)
5771 : HArrayGet(array,
5772 index,
5773 type,
5774 SideEffects::ArrayReadOfType(type),
5775 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305776 /* is_string_char_at */ false) {
5777 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005778
5779 HArrayGet(HInstruction* array,
5780 HInstruction* index,
5781 DataType::Type type,
5782 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005783 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005784 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305785 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005786 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005787 SetRawInputAt(0, array);
5788 SetRawInputAt(1, index);
5789 }
5790
Artem Serovcced8ba2017-07-19 18:18:09 +01005791 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005792 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005793 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005794 return true;
5795 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005796 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005797 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005798 // Currently, unless the array is the result of NewArray, the array access is always
5799 // preceded by some form of null NullCheck necessary for the bounds check, usually
5800 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5801 // dynamic BCE. There are cases when these could be removed to produce better code.
5802 // If we ever add optimizations to do so we should allow an implicit check here
5803 // (as long as the address falls in the first page).
5804 //
5805 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5806 // a = cond ? new int[1] : null;
5807 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005808 return false;
5809 }
5810
David Brazdil4833f5a2015-12-16 10:37:39 +00005811 bool IsEquivalentOf(HArrayGet* other) const {
5812 bool result = (GetDexPc() == other->GetDexPc());
5813 if (kIsDebugBuild && result) {
5814 DCHECK_EQ(GetBlock(), other->GetBlock());
5815 DCHECK_EQ(GetArray(), other->GetArray());
5816 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005817 if (DataType::IsIntOrLongType(GetType())) {
5818 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005819 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005820 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5821 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005822 }
5823 }
5824 return result;
5825 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005826
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005827 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5828
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005829 HInstruction* GetArray() const { return InputAt(0); }
5830 HInstruction* GetIndex() const { return InputAt(1); }
5831
Vladimir Marko61b92282017-10-11 13:23:17 +01005832 void SetType(DataType::Type new_type) {
5833 DCHECK(DataType::IsIntegralType(GetType()));
5834 DCHECK(DataType::IsIntegralType(new_type));
5835 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5836 SetPackedField<TypeField>(new_type);
5837 }
5838
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005839 DECLARE_INSTRUCTION(ArrayGet);
5840
Artem Serovcced8ba2017-07-19 18:18:09 +01005841 protected:
5842 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5843
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005844 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005845 // We treat a String as an array, creating the HArrayGet from String.charAt()
5846 // intrinsic in the instruction simplifier. We can always determine whether
5847 // a particular HArrayGet is actually a String.charAt() by looking at the type
5848 // of the input but that requires holding the mutator lock, so we prefer to use
5849 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005850 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005851 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5852 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5853 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005854};
5855
Vladimir Markobd785672018-05-03 17:09:09 +01005856class HArraySet FINAL : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005857 public:
5858 HArraySet(HInstruction* array,
5859 HInstruction* index,
5860 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005861 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005862 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005863 : HArraySet(array,
5864 index,
5865 value,
5866 expected_component_type,
5867 // Make a best guess for side effects now, may be refined during SSA building.
5868 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305869 dex_pc) {
5870 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005871
5872 HArraySet(HInstruction* array,
5873 HInstruction* index,
5874 HInstruction* value,
5875 DataType::Type expected_component_type,
5876 SideEffects side_effects,
5877 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005878 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005879 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005880 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005881 SetPackedFlag<kFlagValueCanBeNull>(true);
5882 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005883 SetRawInputAt(0, array);
5884 SetRawInputAt(1, index);
5885 SetRawInputAt(2, value);
5886 }
5887
Artem Serovcced8ba2017-07-19 18:18:09 +01005888 bool IsClonable() const OVERRIDE { return true; }
5889
Calin Juravle77520bc2015-01-12 18:45:46 +00005890 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005891 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005892 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005893 }
5894
Mingyao Yang81014cb2015-06-02 03:16:27 -07005895 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005896 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005898 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005899 // TODO: Same as for ArrayGet.
5900 return false;
5901 }
5902
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005903 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005904 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005905 }
5906
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005907 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005908 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005909 }
5910
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005911 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005912 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005913 }
5914
Vladimir Markoa1de9182016-02-25 11:37:38 +00005915 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5916 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5917 bool StaticTypeOfArrayIsObjectArray() const {
5918 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5919 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005920
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005921 HInstruction* GetArray() const { return InputAt(0); }
5922 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005923 HInstruction* GetValue() const { return InputAt(2); }
5924
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005925 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005926 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5927 }
5928
5929 static DataType::Type GetComponentType(DataType::Type value_type,
5930 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005931 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005932 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005933 // be correct, we also check what is the value type. If it is a floating
5934 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005935 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005936 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005937 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005938 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005939
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005941 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005942 }
5943
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005944 static SideEffects ComputeSideEffects(DataType::Type type) {
5945 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005946 }
5947
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005948 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5949 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5950 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005951 }
5952
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005953 DECLARE_INSTRUCTION(ArraySet);
5954
Artem Serovcced8ba2017-07-19 18:18:09 +01005955 protected:
5956 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5957
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005958 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005959 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5960 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005961 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005962 static constexpr size_t kFlagNeedsTypeCheck =
5963 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5964 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005965 // Cached information for the reference_type_info_ so that codegen
5966 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005967 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5968 static constexpr size_t kNumberOfArraySetPackedBits =
5969 kFlagStaticTypeOfArrayIsObjectArray + 1;
5970 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5971 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005972 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005973};
5974
Vladimir Markofcb503c2016-05-18 12:48:17 +01005975class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005976 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005977 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305978 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005979 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005980 // Note that arrays do not change length, so the instruction does not
5981 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005982 SetRawInputAt(0, array);
5983 }
5984
Artem Serovcced8ba2017-07-19 18:18:09 +01005985 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00005986 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005987 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005988 return true;
5989 }
Calin Juravle641547a2015-04-21 22:08:51 +01005990 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5991 return obj == InputAt(0);
5992 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005993
Vladimir Markodce016e2016-04-28 13:10:02 +01005994 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5995
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005996 DECLARE_INSTRUCTION(ArrayLength);
5997
Artem Serovcced8ba2017-07-19 18:18:09 +01005998 protected:
5999 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6000
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006002 // We treat a String as an array, creating the HArrayLength from String.length()
6003 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6004 // determine whether a particular HArrayLength is actually a String.length() by
6005 // looking at the type of the input but that requires holding the mutator lock, so
6006 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006007 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006008 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6009 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6010 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006011};
6012
Vladimir Markofcb503c2016-05-18 12:48:17 +01006013class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006014 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006015 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6016 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006017 HBoundsCheck(HInstruction* index,
6018 HInstruction* length,
6019 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006020 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306021 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006022 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006023 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006024 SetRawInputAt(0, index);
6025 SetRawInputAt(1, length);
6026 }
6027
Artem Serovcced8ba2017-07-19 18:18:09 +01006028 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006029 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006030 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006031 return true;
6032 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006033
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006034 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006035
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006036 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006037
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006038 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006039
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006040 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006041
6042 DECLARE_INSTRUCTION(BoundsCheck);
6043
Artem Serovcced8ba2017-07-19 18:18:09 +01006044 protected:
6045 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6046
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006047 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006048 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006049};
6050
Vladimir Markobd785672018-05-03 17:09:09 +01006051class HSuspendCheck FINAL : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006052 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006053 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006054 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306055 slow_path_(nullptr) {
6056 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006057
Artem Serovcced8ba2017-07-19 18:18:09 +01006058 bool IsClonable() const OVERRIDE { return true; }
6059
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006060 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006061 return true;
6062 }
6063
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006064 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6065 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006066
6067 DECLARE_INSTRUCTION(SuspendCheck);
6068
Artem Serovcced8ba2017-07-19 18:18:09 +01006069 protected:
6070 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6071
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006072 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006073 // Only used for code generation, in order to share the same slow path between back edges
6074 // of a same loop.
6075 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006076};
6077
David Srbecky0cf44932015-12-09 14:09:59 +00006078// Pseudo-instruction which provides the native debugger with mapping information.
6079// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006080class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006081 public:
6082 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006083 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306084 }
David Srbecky0cf44932015-12-09 14:09:59 +00006085
6086 bool NeedsEnvironment() const OVERRIDE {
6087 return true;
6088 }
6089
6090 DECLARE_INSTRUCTION(NativeDebugInfo);
6091
Artem Serovcced8ba2017-07-19 18:18:09 +01006092 protected:
6093 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006094};
6095
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006096/**
6097 * Instruction to load a Class object.
6098 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006099class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006100 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006101 // Determines how to load the Class.
6102 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006103 // We cannot load this class. See HSharpening::SharpenLoadClass.
6104 kInvalid = -1,
6105
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006106 // Use the Class* from the method's own ArtMethod*.
6107 kReferrersClass,
6108
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006109 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006110 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006111 kBootImageLinkTimePcRelative,
6112
6113 // Use a known boot image Class* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006114 // Used for boot image classes referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006115 kBootImageAddress,
6116
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006117 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6118 // Used for boot image classes referenced by apps in AOT-compiled code (PIC).
6119 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006120
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006121 // Load from an entry in the .bss section using a PC-relative load.
6122 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6123 kBssEntry,
6124
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006125 // Load from the root table associated with the JIT compiled method.
6126 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006127
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006128 // Load using a simple runtime call. This is the fall-back load kind when
6129 // the codegen is unable to use another appropriate kind.
6130 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006131
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006132 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006133 };
6134
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006135 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006136 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006137 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006138 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006139 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006140 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006141 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006142 : HInstruction(kLoadClass,
6143 DataType::Type::kReference,
6144 SideEffectsForArchRuntimeCalls(),
6145 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006146 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006147 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006148 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006149 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006150 // Referrers class should not need access check. We never inline unverified
6151 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006152 DCHECK(!is_referrers_class || !needs_access_check);
6153
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006154 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006155 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006156 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006157 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006158 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006159 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006160 }
6161
Artem Serovcced8ba2017-07-19 18:18:09 +01006162 bool IsClonable() const OVERRIDE { return true; }
6163
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006164 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006166 LoadKind GetLoadKind() const {
6167 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006168 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006169
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006170 bool HasPcRelativeLoadKind() const {
6171 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6172 GetLoadKind() == LoadKind::kBootImageRelRo ||
6173 GetLoadKind() == LoadKind::kBssEntry;
6174 }
6175
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006176 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006177
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006178 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006179
Andreas Gampea5b09a62016-11-17 15:21:22 -08006180 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006181
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006182 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006183
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006184 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006185 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006186 }
6187
Calin Juravle0ba218d2015-05-19 18:46:01 +01006188 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006189 // The entrypoint the code generator is going to call does not do
6190 // clinit of the class.
6191 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006192 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006193 }
6194
6195 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006196 return NeedsAccessCheck() ||
6197 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006198 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006199 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006200 }
6201
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006202 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006203 return NeedsAccessCheck() ||
6204 MustGenerateClinitCheck() ||
6205 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6206 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6207 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006208 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006209 GetLoadKind() == LoadKind::kBssEntry) &&
6210 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006211 }
6212
Calin Juravleacf735c2015-02-12 15:25:22 +00006213 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006214 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6215 // Note: The is_exact flag from the return value should not be used.
6216 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6217 } else {
6218 return ReferenceTypeInfo::CreateInvalid();
6219 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006220 }
6221
Vladimir Marko175e7862018-03-27 09:03:13 +00006222 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6223 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6224 DCHECK(klass_ != nullptr);
6225 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006226 }
6227
Andreas Gampea5b09a62016-11-17 15:21:22 -08006228 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006229 const DexFile& GetDexFile() const { return dex_file_; }
6230
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006231 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006232 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006233 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006234
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006235 static SideEffects SideEffectsForArchRuntimeCalls() {
6236 return SideEffects::CanTriggerGC();
6237 }
6238
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006239 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006240 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006241 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006242 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006243
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006244 void MarkInBootImage() {
6245 SetPackedFlag<kFlagIsInBootImage>(true);
6246 }
6247
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006248 void AddSpecialInput(HInstruction* special_input);
6249
6250 using HInstruction::GetInputRecords; // Keep the const version visible.
6251 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6252 return ArrayRef<HUserRecord<HInstruction*>>(
6253 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6254 }
6255
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006256 Handle<mirror::Class> GetClass() const {
6257 return klass_;
6258 }
6259
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006260 DECLARE_INSTRUCTION(LoadClass);
6261
Artem Serovcced8ba2017-07-19 18:18:09 +01006262 protected:
6263 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6264
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006265 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006266 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006267 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006268 // Whether this instruction must generate the initialization check.
6269 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006270 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006271 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6272 static constexpr size_t kFieldLoadKindSize =
6273 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006274 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6275 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006276 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006277 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6278
6279 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006280 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006281 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006282 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006283 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006284 }
6285
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006286 void SetLoadKindInternal(LoadKind load_kind);
6287
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006288 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006289 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006290 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006291 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006292
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006293 // A type index and dex file where the class can be accessed. The dex file can be:
6294 // - The compiling method's dex file if the class is defined there too.
6295 // - The compiling method's dex file if the class is referenced there.
6296 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006297 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006298 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006299 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006300
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006301 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006302};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006303std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6304
6305// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006306inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6307 // The load kind should be determined before inserting the instruction to the graph.
6308 DCHECK(GetBlock() == nullptr);
6309 DCHECK(GetEnvironment() == nullptr);
6310 SetPackedField<LoadKindField>(load_kind);
6311 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6312 special_input_ = HUserRecord<HInstruction*>(nullptr);
6313 }
6314 if (!NeedsEnvironment()) {
6315 SetSideEffects(SideEffects::None());
6316 }
6317}
6318
6319// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006320inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006321 // The special input is used for PC-relative loads on some architectures,
6322 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006323 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006324 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006325 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006326 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006327 DCHECK(special_input_.GetInstruction() == nullptr);
6328 special_input_ = HUserRecord<HInstruction*>(special_input);
6329 special_input->AddUseAt(this, 0);
6330}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006331
Vladimir Marko372f10e2016-05-17 16:30:10 +01006332class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006333 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006334 // Determines how to load the String.
6335 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006336 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006337 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006338 kBootImageLinkTimePcRelative,
6339
6340 // Use a known boot image String* address, embedded in the code by the codegen.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006341 // Used for boot image strings referenced by apps in JIT- and AOT-compiled code (non-PIC).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006342 kBootImageAddress,
6343
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006344 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
6345 // Used for boot image strings referenced by apps in AOT-compiled code (PIC).
6346 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006347
Vladimir Markoaad75c62016-10-03 08:46:48 +00006348 // Load from an entry in the .bss section using a PC-relative load.
6349 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6350 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006351
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006352 // Load from the root table associated with the JIT compiled method.
6353 kJitTableAddress,
6354
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006355 // Load using a simple runtime call. This is the fall-back load kind when
6356 // the codegen is unable to use another appropriate kind.
6357 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006358
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006359 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006360 };
6361
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006362 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006363 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006364 const DexFile& dex_file,
6365 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006366 : HInstruction(kLoadString,
6367 DataType::Type::kReference,
6368 SideEffectsForArchRuntimeCalls(),
6369 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006370 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006371 string_index_(string_index),
6372 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006373 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006374 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006375
Artem Serovcced8ba2017-07-19 18:18:09 +01006376 bool IsClonable() const OVERRIDE { return true; }
6377
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006378 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006379
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006380 LoadKind GetLoadKind() const {
6381 return GetPackedField<LoadKindField>();
6382 }
6383
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006384 bool HasPcRelativeLoadKind() const {
6385 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6386 GetLoadKind() == LoadKind::kBootImageRelRo ||
6387 GetLoadKind() == LoadKind::kBssEntry;
6388 }
6389
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006390 const DexFile& GetDexFile() const {
6391 return dex_file_;
6392 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006393
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006394 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006395 return string_index_;
6396 }
6397
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006398 Handle<mirror::String> GetString() const {
6399 return string_;
6400 }
6401
6402 void SetString(Handle<mirror::String> str) {
6403 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006404 }
6405
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006406 bool CanBeMoved() const OVERRIDE { return true; }
6407
Vladimir Marko372f10e2016-05-17 16:30:10 +01006408 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006409
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006410 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006411
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006412 // Will call the runtime if we need to load the string through
6413 // the dex cache and the string is not guaranteed to be there yet.
6414 bool NeedsEnvironment() const OVERRIDE {
6415 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006416 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006417 load_kind == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006418 load_kind == LoadKind::kBootImageRelRo ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006419 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006420 return false;
6421 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006422 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006423 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006424
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006425 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006426 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006427 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006428
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006429 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006430 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006431
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006432 static SideEffects SideEffectsForArchRuntimeCalls() {
6433 return SideEffects::CanTriggerGC();
6434 }
6435
Vladimir Marko372f10e2016-05-17 16:30:10 +01006436 void AddSpecialInput(HInstruction* special_input);
6437
6438 using HInstruction::GetInputRecords; // Keep the const version visible.
6439 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6440 return ArrayRef<HUserRecord<HInstruction*>>(
6441 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006442 }
6443
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006444 DECLARE_INSTRUCTION(LoadString);
6445
Artem Serovcced8ba2017-07-19 18:18:09 +01006446 protected:
6447 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6448
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006449 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006450 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006451 static constexpr size_t kFieldLoadKindSize =
6452 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6453 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006454 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006455 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006456
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006457 void SetLoadKindInternal(LoadKind load_kind);
6458
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006459 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006460 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006461 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006462 HUserRecord<HInstruction*> special_input_;
6463
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006464 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006465 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006466
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006467 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006468};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006469std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6470
6471// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006472inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6473 // The load kind should be determined before inserting the instruction to the graph.
6474 DCHECK(GetBlock() == nullptr);
6475 DCHECK(GetEnvironment() == nullptr);
6476 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6477 SetPackedField<LoadKindField>(load_kind);
6478 if (load_kind != LoadKind::kRuntimeCall) {
6479 special_input_ = HUserRecord<HInstruction*>(nullptr);
6480 }
6481 if (!NeedsEnvironment()) {
6482 SetSideEffects(SideEffects::None());
6483 }
6484}
6485
6486// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006487inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006488 // The special input is used for PC-relative loads on some architectures,
6489 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006490 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006491 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006492 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006493 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006494 // HLoadString::GetInputRecords() returns an empty array at this point,
6495 // so use the GetInputRecords() from the base class to set the input record.
6496 DCHECK(special_input_.GetInstruction() == nullptr);
6497 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006498 special_input->AddUseAt(this, 0);
6499}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006500
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006501/**
6502 * Performs an initialization check on its Class object input.
6503 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006504class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006505 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006506 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006507 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306508 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006509 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006510 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006511 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006512 SetRawInputAt(0, constant);
6513 }
6514
Artem Serovcced8ba2017-07-19 18:18:09 +01006515 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006516 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006517 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006518 return true;
6519 }
6520
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006521 bool NeedsEnvironment() const OVERRIDE {
6522 // May call runtime to initialize the class.
6523 return true;
6524 }
6525
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006526 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006527
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006528 HLoadClass* GetLoadClass() const {
6529 DCHECK(InputAt(0)->IsLoadClass());
6530 return InputAt(0)->AsLoadClass();
6531 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006532
6533 DECLARE_INSTRUCTION(ClinitCheck);
6534
Artem Serovcced8ba2017-07-19 18:18:09 +01006535
6536 protected:
6537 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006538};
6539
Vladimir Markofcb503c2016-05-18 12:48:17 +01006540class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006541 public:
6542 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006543 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006544 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006545 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006546 bool is_volatile,
6547 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006548 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006549 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006550 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306551 : HExpression(kStaticFieldGet,
6552 field_type,
6553 SideEffects::FieldReadOfType(field_type, is_volatile),
6554 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006555 field_info_(field,
6556 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006557 field_type,
6558 is_volatile,
6559 field_idx,
6560 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006561 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006562 SetRawInputAt(0, cls);
6563 }
6564
Calin Juravle52c48962014-12-16 17:02:57 +00006565
Artem Serovcced8ba2017-07-19 18:18:09 +01006566 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006567 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006568
Vladimir Marko372f10e2016-05-17 16:30:10 +01006569 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6570 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006571 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006572 }
6573
6574 size_t ComputeHashCode() const OVERRIDE {
6575 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6576 }
6577
Calin Juravle52c48962014-12-16 17:02:57 +00006578 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006579 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006580 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006581 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006582
Vladimir Marko61b92282017-10-11 13:23:17 +01006583 void SetType(DataType::Type new_type) {
6584 DCHECK(DataType::IsIntegralType(GetType()));
6585 DCHECK(DataType::IsIntegralType(new_type));
6586 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6587 SetPackedField<TypeField>(new_type);
6588 }
6589
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006590 DECLARE_INSTRUCTION(StaticFieldGet);
6591
Artem Serovcced8ba2017-07-19 18:18:09 +01006592 protected:
6593 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6594
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006595 private:
6596 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006597};
6598
Vladimir Markobd785672018-05-03 17:09:09 +01006599class HStaticFieldSet FINAL : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006600 public:
6601 HStaticFieldSet(HInstruction* cls,
6602 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006603 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006604 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006605 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006606 bool is_volatile,
6607 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006608 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006609 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006610 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006611 : HExpression(kStaticFieldSet,
6612 SideEffects::FieldWriteOfType(field_type, is_volatile),
6613 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006614 field_info_(field,
6615 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006616 field_type,
6617 is_volatile,
6618 field_idx,
6619 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006620 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006621 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006622 SetRawInputAt(0, cls);
6623 SetRawInputAt(1, value);
6624 }
6625
Artem Serovcced8ba2017-07-19 18:18:09 +01006626 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006627 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006628 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006629 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006630 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006631
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006632 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006633 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6634 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006635
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006636 DECLARE_INSTRUCTION(StaticFieldSet);
6637
Artem Serovcced8ba2017-07-19 18:18:09 +01006638 protected:
6639 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6640
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006641 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006642 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6643 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6644 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6645 "Too many packed fields.");
6646
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006647 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006648};
6649
Vladimir Markofcb503c2016-05-18 12:48:17 +01006650class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006651 public:
6652 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006653 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006654 uint32_t field_index,
6655 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306656 : HExpression(kUnresolvedInstanceFieldGet,
6657 field_type,
6658 SideEffects::AllExceptGCDependency(),
6659 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006660 field_index_(field_index) {
6661 SetRawInputAt(0, obj);
6662 }
6663
Artem Serovcced8ba2017-07-19 18:18:09 +01006664 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006665 bool NeedsEnvironment() const OVERRIDE { return true; }
6666 bool CanThrow() const OVERRIDE { return true; }
6667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006668 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006669 uint32_t GetFieldIndex() const { return field_index_; }
6670
6671 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6672
Artem Serovcced8ba2017-07-19 18:18:09 +01006673 protected:
6674 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6675
Calin Juravlee460d1d2015-09-29 04:52:17 +01006676 private:
6677 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006678};
6679
Vladimir Markobd785672018-05-03 17:09:09 +01006680class HUnresolvedInstanceFieldSet FINAL : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006681 public:
6682 HUnresolvedInstanceFieldSet(HInstruction* obj,
6683 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006684 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006685 uint32_t field_index,
6686 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006687 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006688 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006689 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006690 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006691 SetRawInputAt(0, obj);
6692 SetRawInputAt(1, value);
6693 }
6694
Artem Serovcced8ba2017-07-19 18:18:09 +01006695 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006696 bool NeedsEnvironment() const OVERRIDE { return true; }
6697 bool CanThrow() const OVERRIDE { return true; }
6698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006699 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006700 uint32_t GetFieldIndex() const { return field_index_; }
6701
6702 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6703
Artem Serovcced8ba2017-07-19 18:18:09 +01006704 protected:
6705 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6706
Calin Juravlee460d1d2015-09-29 04:52:17 +01006707 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006708 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6709 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006710 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006711 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6712 kFieldFieldType + kFieldFieldTypeSize;
6713 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6714 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006715 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006716
Calin Juravlee460d1d2015-09-29 04:52:17 +01006717 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006718};
6719
Vladimir Markofcb503c2016-05-18 12:48:17 +01006720class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006721 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006722 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006723 uint32_t field_index,
6724 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306725 : HExpression(kUnresolvedStaticFieldGet,
6726 field_type,
6727 SideEffects::AllExceptGCDependency(),
6728 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006729 field_index_(field_index) {
6730 }
6731
Artem Serovcced8ba2017-07-19 18:18:09 +01006732 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006733 bool NeedsEnvironment() const OVERRIDE { return true; }
6734 bool CanThrow() const OVERRIDE { return true; }
6735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006736 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006737 uint32_t GetFieldIndex() const { return field_index_; }
6738
6739 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6740
Artem Serovcced8ba2017-07-19 18:18:09 +01006741 protected:
6742 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6743
Calin Juravlee460d1d2015-09-29 04:52:17 +01006744 private:
6745 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006746};
6747
Vladimir Markobd785672018-05-03 17:09:09 +01006748class HUnresolvedStaticFieldSet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006749 public:
6750 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006751 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006752 uint32_t field_index,
6753 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006754 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006755 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006756 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006757 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006758 SetRawInputAt(0, value);
6759 }
6760
Artem Serovcced8ba2017-07-19 18:18:09 +01006761 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006762 bool NeedsEnvironment() const OVERRIDE { return true; }
6763 bool CanThrow() const OVERRIDE { return true; }
6764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006765 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006766 uint32_t GetFieldIndex() const { return field_index_; }
6767
6768 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6769
Artem Serovcced8ba2017-07-19 18:18:09 +01006770 protected:
6771 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6772
Calin Juravlee460d1d2015-09-29 04:52:17 +01006773 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006774 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6775 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006776 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006777 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6778 kFieldFieldType + kFieldFieldTypeSize;
6779 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6780 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006782
Calin Juravlee460d1d2015-09-29 04:52:17 +01006783 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006784};
6785
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006786// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006787class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006788 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006789 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306790 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6791 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006792
David Brazdilbbd733e2015-08-18 17:48:17 +01006793 bool CanBeNull() const OVERRIDE { return false; }
6794
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006795 DECLARE_INSTRUCTION(LoadException);
6796
Artem Serovcced8ba2017-07-19 18:18:09 +01006797 protected:
6798 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006799};
6800
David Brazdilcb1c0552015-08-04 16:22:25 +01006801// Implicit part of move-exception which clears thread-local exception storage.
6802// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markobd785672018-05-03 17:09:09 +01006803class HClearException FINAL : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006804 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006805 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006806 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306807 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006808
6809 DECLARE_INSTRUCTION(ClearException);
6810
Artem Serovcced8ba2017-07-19 18:18:09 +01006811 protected:
6812 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006813};
6814
Vladimir Markobd785672018-05-03 17:09:09 +01006815class HThrow FINAL : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006816 public:
6817 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006818 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006819 SetRawInputAt(0, exception);
6820 }
6821
6822 bool IsControlFlow() const OVERRIDE { return true; }
6823
6824 bool NeedsEnvironment() const OVERRIDE { return true; }
6825
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006826 bool CanThrow() const OVERRIDE { return true; }
6827
Aart Bika8b8e9b2018-01-09 11:01:02 -08006828 bool AlwaysThrows() const OVERRIDE { return true; }
6829
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006830 DECLARE_INSTRUCTION(Throw);
6831
Artem Serovcced8ba2017-07-19 18:18:09 +01006832 protected:
6833 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006834};
6835
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006836/**
6837 * Implementation strategies for the code generator of a HInstanceOf
6838 * or `HCheckCast`.
6839 */
6840enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006841 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006842 kExactCheck, // Can do a single class compare.
6843 kClassHierarchyCheck, // Can just walk the super class chain.
6844 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6845 kInterfaceCheck, // No optimization yet when checking against an interface.
6846 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006847 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00006848 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006849 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006850};
6851
Roland Levillain86503782016-02-11 19:07:30 +00006852std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6853
Vladimir Marko175e7862018-03-27 09:03:13 +00006854// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
6855// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
6856class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006857 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00006858 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006859 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006860 HInstruction* object,
6861 HInstruction* target_class_or_null,
6862 TypeCheckKind check_kind,
6863 Handle<mirror::Class> klass,
6864 uint32_t dex_pc,
6865 ArenaAllocator* allocator,
6866 HIntConstant* bitstring_path_to_root,
6867 HIntConstant* bitstring_mask,
6868 SideEffects side_effects)
6869 : HVariableInputSizeInstruction(
6870 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01006871 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00006872 side_effects,
6873 dex_pc,
6874 allocator,
6875 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
6876 kArenaAllocTypeCheckInputs),
6877 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006878 SetPackedField<TypeCheckKindField>(check_kind);
6879 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006880 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006881 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00006882 SetRawInputAt(1, target_class_or_null);
6883 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
6884 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
6885 if (check_kind == TypeCheckKind::kBitstringCheck) {
6886 DCHECK(target_class_or_null->IsNullConstant());
6887 SetRawInputAt(2, bitstring_path_to_root);
6888 SetRawInputAt(3, bitstring_mask);
6889 } else {
6890 DCHECK(target_class_or_null->IsLoadClass());
6891 }
Vladimir Marko87584542017-12-12 17:47:52 +00006892 }
6893
6894 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00006895 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00006896 HInstruction* load_class = InputAt(1);
6897 DCHECK(load_class->IsLoadClass());
6898 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006899 }
6900
Vladimir Marko175e7862018-03-27 09:03:13 +00006901 uint32_t GetBitstringPathToRoot() const {
6902 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6903 HInstruction* path_to_root = InputAt(2);
6904 DCHECK(path_to_root->IsIntConstant());
6905 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
6906 }
6907
6908 uint32_t GetBitstringMask() const {
6909 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
6910 HInstruction* mask = InputAt(3);
6911 DCHECK(mask->IsIntConstant());
6912 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
6913 }
6914
Artem Serovcced8ba2017-07-19 18:18:09 +01006915 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006916 bool CanBeMoved() const OVERRIDE { return true; }
6917
Vladimir Marko175e7862018-03-27 09:03:13 +00006918 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6919 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
6920 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006921 }
6922
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00006923 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6924 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6925 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6926 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6927
Vladimir Marko175e7862018-03-27 09:03:13 +00006928 ReferenceTypeInfo GetTargetClassRTI() {
6929 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
6930 // Note: The is_exact flag from the return value should not be used.
6931 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6932 } else {
6933 return ReferenceTypeInfo::CreateInvalid();
6934 }
6935 }
6936
6937 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
6938 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6939 DCHECK(klass_ != nullptr);
6940 SetPackedFlag<kFlagValidTargetClassRTI>(true);
6941 }
6942
6943 Handle<mirror::Class> GetClass() const {
6944 return klass_;
6945 }
6946
6947 protected:
6948 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
6949
6950 private:
6951 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6952 static constexpr size_t kFieldTypeCheckKindSize =
6953 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6954 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6955 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
6956 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
6957 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6958 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
6959
6960 Handle<mirror::Class> klass_;
6961};
6962
6963class HInstanceOf FINAL : public HTypeCheckInstruction {
6964 public:
6965 HInstanceOf(HInstruction* object,
6966 HInstruction* target_class_or_null,
6967 TypeCheckKind check_kind,
6968 Handle<mirror::Class> klass,
6969 uint32_t dex_pc,
6970 ArenaAllocator* allocator,
6971 HIntConstant* bitstring_path_to_root,
6972 HIntConstant* bitstring_mask)
6973 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01006974 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00006975 object,
6976 target_class_or_null,
6977 check_kind,
6978 klass,
6979 dex_pc,
6980 allocator,
6981 bitstring_path_to_root,
6982 bitstring_mask,
6983 SideEffectsForArchRuntimeCalls(check_kind)) {}
6984
Vladimir Marko175e7862018-03-27 09:03:13 +00006985 bool NeedsEnvironment() const OVERRIDE {
6986 return CanCallRuntime(GetTypeCheckKind());
6987 }
6988
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006989 static bool CanCallRuntime(TypeCheckKind check_kind) {
6990 // Mips currently does runtime calls for any other checks.
6991 return check_kind != TypeCheckKind::kExactCheck;
6992 }
6993
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006994 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006995 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006996 }
6997
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006998 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006999
Artem Serovcced8ba2017-07-19 18:18:09 +01007000 protected:
7001 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007002};
7003
Vladimir Markofcb503c2016-05-18 12:48:17 +01007004class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007005 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007006 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307007 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007008 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7009 SetPackedFlag<kFlagUpperCanBeNull>(true);
7010 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007011 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007012 SetRawInputAt(0, input);
7013 }
7014
Artem Serovcced8ba2017-07-19 18:18:09 +01007015 bool IsClonable() const OVERRIDE { return true; }
7016
David Brazdilf5552582015-12-27 13:36:12 +00007017 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007018 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007019 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007020 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007021
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007022 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007023 DCHECK(GetUpperCanBeNull() || !can_be_null);
7024 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007025 }
7026
Vladimir Markoa1de9182016-02-25 11:37:38 +00007027 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007028
Calin Juravleb1498f62015-02-16 13:13:29 +00007029 DECLARE_INSTRUCTION(BoundType);
7030
Artem Serovcced8ba2017-07-19 18:18:09 +01007031 protected:
7032 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7033
Calin Juravleb1498f62015-02-16 13:13:29 +00007034 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007035 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7036 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007037 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007038 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7039 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7040 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7041
Calin Juravleb1498f62015-02-16 13:13:29 +00007042 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007043 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7044 // It is used to bound the type in cases like:
7045 // if (x instanceof ClassX) {
7046 // // uper_bound_ will be ClassX
7047 // }
David Brazdilf5552582015-12-27 13:36:12 +00007048 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007049};
7050
Vladimir Marko175e7862018-03-27 09:03:13 +00007051class HCheckCast FINAL : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007052 public:
7053 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007054 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007055 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007056 Handle<mirror::Class> klass,
7057 uint32_t dex_pc,
7058 ArenaAllocator* allocator,
7059 HIntConstant* bitstring_path_to_root,
7060 HIntConstant* bitstring_mask)
7061 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007062 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007063 object,
7064 target_class_or_null,
7065 check_kind,
7066 klass,
7067 dex_pc,
7068 allocator,
7069 bitstring_path_to_root,
7070 bitstring_mask,
7071 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007072
7073 bool NeedsEnvironment() const OVERRIDE {
7074 // Instruction may throw a CheckCastError.
7075 return true;
7076 }
7077
7078 bool CanThrow() const OVERRIDE { return true; }
7079
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007080 DECLARE_INSTRUCTION(CheckCast);
7081
Artem Serovcced8ba2017-07-19 18:18:09 +01007082 protected:
7083 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007084};
7085
Andreas Gampe26de38b2016-07-27 17:53:11 -07007086/**
7087 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7088 * @details We define the combined barrier types that are actually required
7089 * by the Java Memory Model, rather than using exactly the terminology from
7090 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7091 * primitives. Note that the JSR-133 cookbook generally does not deal with
7092 * store atomicity issues, and the recipes there are not always entirely sufficient.
7093 * The current recipe is as follows:
7094 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7095 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7096 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7097 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7098 * class has final fields.
7099 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7100 * store-to-memory instructions. Only generated together with non-temporal stores.
7101 */
7102enum MemBarrierKind {
7103 kAnyStore,
7104 kLoadAny,
7105 kStoreStore,
7106 kAnyAny,
7107 kNTStoreStore,
7108 kLastBarrierKind = kNTStoreStore
7109};
7110std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7111
Vladimir Markobd785672018-05-03 17:09:09 +01007112class HMemoryBarrier FINAL : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007113 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007114 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007115 : HExpression(kMemoryBarrier,
7116 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7117 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007118 SetPackedField<BarrierKindField>(barrier_kind);
7119 }
Calin Juravle27df7582015-04-17 19:12:31 +01007120
Artem Serovcced8ba2017-07-19 18:18:09 +01007121 bool IsClonable() const OVERRIDE { return true; }
7122
Vladimir Markoa1de9182016-02-25 11:37:38 +00007123 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007124
7125 DECLARE_INSTRUCTION(MemoryBarrier);
7126
Artem Serovcced8ba2017-07-19 18:18:09 +01007127 protected:
7128 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7129
Calin Juravle27df7582015-04-17 19:12:31 +01007130 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007131 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7132 static constexpr size_t kFieldBarrierKindSize =
7133 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7134 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7135 kFieldBarrierKind + kFieldBarrierKindSize;
7136 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7137 "Too many packed fields.");
7138 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007139};
7140
Igor Murashkind01745e2017-04-05 16:40:31 -07007141// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7142// the specified object(s), with respect to any subsequent store that might "publish"
7143// (i.e. make visible) the specified object to another thread.
7144//
7145// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7146// for all final fields (that were set) at the end of the invoked constructor.
7147//
7148// The constructor fence models the freeze actions for the final fields of an object
7149// being constructed (semantically at the end of the constructor). Constructor fences
7150// have a per-object affinity; two separate objects being constructed get two separate
7151// constructor fences.
7152//
7153// (Note: that if calling a super-constructor or forwarding to another constructor,
7154// the freezes would happen at the end of *that* constructor being invoked).
7155//
7156// The memory model guarantees that when the object being constructed is "published" after
7157// constructor completion (i.e. escapes the current thread via a store), then any final field
7158// writes must be observable on other threads (once they observe that publication).
7159//
7160// Further, anything written before the freeze, and read by dereferencing through the final field,
7161// must also be visible (so final object field could itself have an object with non-final fields;
7162// yet the freeze must also extend to them).
7163//
7164// Constructor example:
7165//
7166// class HasFinal {
7167// final int field; Optimizing IR for <init>()V:
7168// HasFinal() {
7169// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7170// // freeze(this.field); HConstructorFence(this)
7171// } HReturn
7172// }
7173//
7174// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7175// already-initialized classes; in that case the allocation must act as a "default-initializer"
7176// of the object which effectively writes the class pointer "final field".
7177//
7178// For example, we can model default-initialiation as roughly the equivalent of the following:
7179//
7180// class Object {
7181// private final Class header;
7182// }
7183//
7184// Java code: Optimizing IR:
7185//
7186// T new_instance<T>() {
7187// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7188// obj.header = T.class; // header write is done by above call.
7189// // freeze(obj.header) HConstructorFence(obj)
7190// return (T)obj;
7191// }
7192//
7193// See also:
7194// * CompilerDriver::RequiresConstructorBarrier
7195// * QuasiAtomic::ThreadFenceForConstructor
7196//
7197class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7198 // A fence has variable inputs because the inputs can be removed
7199 // after prepare_for_register_allocation phase.
7200 // (TODO: In the future a fence could freeze multiple objects
7201 // after merging two fences together.)
7202 public:
7203 // `fence_object` is the reference that needs to be protected for correct publication.
7204 //
7205 // It makes sense in the following situations:
7206 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7207 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7208 //
7209 // After construction the `fence_object` becomes the 0th input.
7210 // This is not an input in a real sense, but just a convenient place to stash the information
7211 // about the associated object.
7212 HConstructorFence(HInstruction* fence_object,
7213 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007214 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007215 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7216 // constraints described in the class header. We claim that these SideEffects constraints
7217 // enforce a superset of the real constraints.
7218 //
7219 // The ordering described above is conservatively modeled with SideEffects as follows:
7220 //
7221 // * To prevent reordering of the publication stores:
7222 // ----> "Reads of objects" is the initial SideEffect.
7223 // * For every primitive final field store in the constructor:
7224 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7225 // * If there are any stores to reference final fields in the constructor:
7226 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7227 // that are reachable from `fence_object` also need to be prevented for reordering
7228 // (and we do not want to do alias analysis to figure out what those stores are).
7229 //
7230 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7231 // even more conservative approach than the one described above, and prevents all of the
7232 // above reordering without analyzing any of the instructions in the constructor.
7233 //
7234 // If in a later phase we discover that there are no writes to reference final fields,
7235 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307236 : HVariableInputSizeInstruction(kConstructorFence,
7237 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007238 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007239 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007240 /* number_of_inputs */ 1,
7241 kArenaAllocConstructorFenceInputs) {
7242 DCHECK(fence_object != nullptr);
7243 SetRawInputAt(0, fence_object);
7244 }
7245
7246 // The object associated with this constructor fence.
7247 //
7248 // (Note: This will be null after the prepare_for_register_allocation phase,
7249 // as all constructor fence inputs are removed there).
7250 HInstruction* GetFenceObject() const {
7251 return InputAt(0);
7252 }
7253
7254 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7255 // - Delete `fence_use` from `this`'s use list.
7256 // - Delete `this` from `fence_use`'s inputs list.
7257 // - If the `fence_use` is dead, remove it from the graph.
7258 //
7259 // A fence is considered dead once it no longer has any uses
7260 // and all of the inputs are dead.
7261 //
7262 // This must *not* be called during/after prepare_for_register_allocation,
7263 // because that removes all the inputs to the fences but the fence is actually
7264 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007265 //
7266 // Returns how many HConstructorFence instructions were removed from graph.
7267 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007268
Igor Murashkindd018df2017-08-09 10:38:31 -07007269 // Combine all inputs of `this` and `other` instruction and remove
7270 // `other` from the graph.
7271 //
7272 // Inputs are unique after the merge.
7273 //
7274 // Requirement: `this` must not be the same as `other.
7275 void Merge(HConstructorFence* other);
7276
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007277 // Check if this constructor fence is protecting
7278 // an HNewInstance or HNewArray that is also the immediate
7279 // predecessor of `this`.
7280 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007281 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7282 // to be one of the inputs of `this`.
7283 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007284 // Returns the associated HNewArray or HNewInstance,
7285 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007286 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007287
Igor Murashkind01745e2017-04-05 16:40:31 -07007288 DECLARE_INSTRUCTION(ConstructorFence);
7289
Artem Serovcced8ba2017-07-19 18:18:09 +01007290 protected:
7291 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007292};
7293
Vladimir Markobd785672018-05-03 17:09:09 +01007294class HMonitorOperation FINAL : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007295 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007296 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007297 kEnter,
7298 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007299 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007300 };
7301
7302 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007303 : HExpression(kMonitorOperation,
7304 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7305 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007306 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007307 SetRawInputAt(0, object);
7308 }
7309
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007310 // Instruction may go into runtime, so we need an environment.
7311 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007312
7313 bool CanThrow() const OVERRIDE {
7314 // Verifier guarantees that monitor-exit cannot throw.
7315 // This is important because it allows the HGraphBuilder to remove
7316 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7317 return IsEnter();
7318 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007319
Vladimir Markoa1de9182016-02-25 11:37:38 +00007320 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7321 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007322
7323 DECLARE_INSTRUCTION(MonitorOperation);
7324
Artem Serovcced8ba2017-07-19 18:18:09 +01007325 protected:
7326 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7327
Calin Juravle52c48962014-12-16 17:02:57 +00007328 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007329 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7330 static constexpr size_t kFieldOperationKindSize =
7331 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7332 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7333 kFieldOperationKind + kFieldOperationKindSize;
7334 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7335 "Too many packed fields.");
7336 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007337};
7338
Vladimir Markofcb503c2016-05-18 12:48:17 +01007339class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007340 public:
7341 HSelect(HInstruction* condition,
7342 HInstruction* true_value,
7343 HInstruction* false_value,
7344 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307345 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007346 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7347
7348 // First input must be `true_value` or `false_value` to allow codegens to
7349 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7350 // that architectures which implement HSelect as a conditional move also
7351 // will not need to invert the condition.
7352 SetRawInputAt(0, false_value);
7353 SetRawInputAt(1, true_value);
7354 SetRawInputAt(2, condition);
7355 }
7356
Artem Serovcced8ba2017-07-19 18:18:09 +01007357 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007358 HInstruction* GetFalseValue() const { return InputAt(0); }
7359 HInstruction* GetTrueValue() const { return InputAt(1); }
7360 HInstruction* GetCondition() const { return InputAt(2); }
7361
7362 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007363 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7364 return true;
7365 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007366
7367 bool CanBeNull() const OVERRIDE {
7368 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7369 }
7370
7371 DECLARE_INSTRUCTION(Select);
7372
Artem Serovcced8ba2017-07-19 18:18:09 +01007373 protected:
7374 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007375};
7376
Vladimir Markof9f64412015-09-02 14:05:49 +01007377class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007378 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007379 MoveOperands(Location source,
7380 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007381 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007382 HInstruction* instruction)
7383 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007384
7385 Location GetSource() const { return source_; }
7386 Location GetDestination() const { return destination_; }
7387
7388 void SetSource(Location value) { source_ = value; }
7389 void SetDestination(Location value) { destination_ = value; }
7390
7391 // The parallel move resolver marks moves as "in-progress" by clearing the
7392 // destination (but not the source).
7393 Location MarkPending() {
7394 DCHECK(!IsPending());
7395 Location dest = destination_;
7396 destination_ = Location::NoLocation();
7397 return dest;
7398 }
7399
7400 void ClearPending(Location dest) {
7401 DCHECK(IsPending());
7402 destination_ = dest;
7403 }
7404
7405 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007406 DCHECK(source_.IsValid() || destination_.IsInvalid());
7407 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007408 }
7409
7410 // True if this blocks a move from the given location.
7411 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007412 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007413 }
7414
7415 // A move is redundant if it's been eliminated, if its source and
7416 // destination are the same, or if its destination is unneeded.
7417 bool IsRedundant() const {
7418 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7419 }
7420
7421 // We clear both operands to indicate move that's been eliminated.
7422 void Eliminate() {
7423 source_ = destination_ = Location::NoLocation();
7424 }
7425
7426 bool IsEliminated() const {
7427 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7428 return source_.IsInvalid();
7429 }
7430
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007431 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007432
Nicolas Geoffray90218252015-04-15 11:56:51 +01007433 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007434 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007435 }
7436
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007437 HInstruction* GetInstruction() const { return instruction_; }
7438
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007439 private:
7440 Location source_;
7441 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007442 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007443 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007444 // The instruction this move is assocatied with. Null when this move is
7445 // for moving an input in the expected locations of user (including a phi user).
7446 // This is only used in debug mode, to ensure we do not connect interval siblings
7447 // in the same parallel move.
7448 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007449};
7450
Roland Levillainc9285912015-12-18 10:38:42 +00007451std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7452
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007453static constexpr size_t kDefaultNumberOfMoves = 4;
7454
Vladimir Markobd785672018-05-03 17:09:09 +01007455class HParallelMove FINAL : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007456 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007457 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007458 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007459 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007460 moves_.reserve(kDefaultNumberOfMoves);
7461 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007462
Nicolas Geoffray90218252015-04-15 11:56:51 +01007463 void AddMove(Location source,
7464 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007465 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007466 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007467 DCHECK(source.IsValid());
7468 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007469 if (kIsDebugBuild) {
7470 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007471 for (const MoveOperands& move : moves_) {
7472 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007473 // Special case the situation where the move is for the spill slot
7474 // of the instruction.
7475 if ((GetPrevious() == instruction)
7476 || ((GetPrevious() == nullptr)
7477 && instruction->IsPhi()
7478 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007479 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007480 << "Doing parallel moves for the same instruction.";
7481 } else {
7482 DCHECK(false) << "Doing parallel moves for the same instruction.";
7483 }
7484 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007485 }
7486 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007487 for (const MoveOperands& move : moves_) {
7488 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007489 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007490 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007491 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007492 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007493 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007494 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007495 }
7496
Vladimir Marko225b6462015-09-28 12:17:40 +01007497 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007498 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007499 }
7500
Vladimir Marko225b6462015-09-28 12:17:40 +01007501 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007502
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007503 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007504
Artem Serovcced8ba2017-07-19 18:18:09 +01007505 protected:
7506 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7507
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007508 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007509 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007510};
7511
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007512// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7513// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7514// never used across anything that can trigger GC.
7515// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7516// So we represent it by the type `DataType::Type::kInt`.
7517class HIntermediateAddress FINAL : public HExpression<2> {
7518 public:
7519 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307520 : HExpression(kIntermediateAddress,
7521 DataType::Type::kInt32,
7522 SideEffects::DependsOnGC(),
7523 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007524 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7525 DataType::Size(DataType::Type::kReference))
7526 << "kPrimInt and kPrimNot have different sizes.";
7527 SetRawInputAt(0, base_address);
7528 SetRawInputAt(1, offset);
7529 }
7530
Artem Serovcced8ba2017-07-19 18:18:09 +01007531 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007532 bool CanBeMoved() const OVERRIDE { return true; }
7533 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7534 return true;
7535 }
7536 bool IsActualObject() const OVERRIDE { return false; }
7537
7538 HInstruction* GetBaseAddress() const { return InputAt(0); }
7539 HInstruction* GetOffset() const { return InputAt(1); }
7540
7541 DECLARE_INSTRUCTION(IntermediateAddress);
7542
Artem Serovcced8ba2017-07-19 18:18:09 +01007543 protected:
7544 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007545};
7546
7547
Mark Mendell0616ae02015-04-17 12:49:27 -04007548} // namespace art
7549
Aart Bikf8f5a162017-02-06 15:35:29 -08007550#include "nodes_vector.h"
7551
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007552#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7553#include "nodes_shared.h"
7554#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007555#ifdef ART_ENABLE_CODEGEN_mips
7556#include "nodes_mips.h"
7557#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007558#ifdef ART_ENABLE_CODEGEN_x86
7559#include "nodes_x86.h"
7560#endif
7561
7562namespace art {
7563
Igor Murashkin6ef45672017-08-08 13:59:55 -07007564class OptimizingCompilerStats;
7565
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007566class HGraphVisitor : public ValueObject {
7567 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007568 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7569 : stats_(stats),
7570 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007571 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007572
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007573 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007574 virtual void VisitBasicBlock(HBasicBlock* block);
7575
Roland Levillain633021e2014-10-01 14:12:25 +01007576 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007577 void VisitInsertionOrder();
7578
Roland Levillain633021e2014-10-01 14:12:25 +01007579 // Visit the graph following dominator tree reverse post-order.
7580 void VisitReversePostOrder();
7581
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007582 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007583
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007584 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007585#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007586 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7587
7588 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7589
7590#undef DECLARE_VISIT_INSTRUCTION
7591
Igor Murashkin6ef45672017-08-08 13:59:55 -07007592 protected:
7593 OptimizingCompilerStats* stats_;
7594
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007595 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007596 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007597
7598 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7599};
7600
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007601class HGraphDelegateVisitor : public HGraphVisitor {
7602 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007603 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7604 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007605 virtual ~HGraphDelegateVisitor() {}
7606
7607 // Visit functions that delegate to to super class.
7608#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007609 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007610
7611 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7612
7613#undef DECLARE_VISIT_INSTRUCTION
7614
7615 private:
7616 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7617};
7618
Artem Serovcced8ba2017-07-19 18:18:09 +01007619// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7620// and remove the old instruction.
7621HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7622
7623// Create a clone for each clonable instructions/phis and replace the original with the clone.
7624//
7625// Used for testing individual instruction cloner.
7626class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7627 public:
7628 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007629 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007630
7631 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7632 if (instruction->IsClonable()) {
7633 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007634 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007635 }
7636 }
7637
Artem Serov7f4aff62017-06-21 17:02:18 +01007638 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007639
7640 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007641 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007642
7643 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7644};
7645
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007646// Iterator over the blocks that art part of the loop. Includes blocks part
7647// of an inner loop. The order in which the blocks are iterated is on their
7648// block id.
7649class HBlocksInLoopIterator : public ValueObject {
7650 public:
7651 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7652 : blocks_in_loop_(info.GetBlocks()),
7653 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7654 index_(0) {
7655 if (!blocks_in_loop_.IsBitSet(index_)) {
7656 Advance();
7657 }
7658 }
7659
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007660 bool Done() const { return index_ == blocks_.size(); }
7661 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007662 void Advance() {
7663 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007664 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007665 if (blocks_in_loop_.IsBitSet(index_)) {
7666 break;
7667 }
7668 }
7669 }
7670
7671 private:
7672 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007673 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007674 size_t index_;
7675
7676 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7677};
7678
Mingyao Yang3584bce2015-05-19 16:01:59 -07007679// Iterator over the blocks that art part of the loop. Includes blocks part
7680// of an inner loop. The order in which the blocks are iterated is reverse
7681// post order.
7682class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7683 public:
7684 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7685 : blocks_in_loop_(info.GetBlocks()),
7686 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7687 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007688 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007689 Advance();
7690 }
7691 }
7692
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007693 bool Done() const { return index_ == blocks_.size(); }
7694 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007695 void Advance() {
7696 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007697 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7698 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007699 break;
7700 }
7701 }
7702 }
7703
7704 private:
7705 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007706 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007707 size_t index_;
7708
7709 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7710};
7711
Aart Bikf3e61ee2017-04-12 17:09:20 -07007712// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007713inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007714 if (constant->IsIntConstant()) {
7715 return constant->AsIntConstant()->GetValue();
7716 } else if (constant->IsLongConstant()) {
7717 return constant->AsLongConstant()->GetValue();
7718 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007719 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007720 return 0;
7721 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007722}
7723
Aart Bikf3e61ee2017-04-12 17:09:20 -07007724// Returns true iff instruction is an integral constant (and sets value on success).
7725inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7726 if (instruction->IsIntConstant()) {
7727 *value = instruction->AsIntConstant()->GetValue();
7728 return true;
7729 } else if (instruction->IsLongConstant()) {
7730 *value = instruction->AsLongConstant()->GetValue();
7731 return true;
7732 } else if (instruction->IsNullConstant()) {
7733 *value = 0;
7734 return true;
7735 }
7736 return false;
7737}
7738
Aart Bik0148de42017-09-05 09:25:01 -07007739// Returns true iff instruction is the given integral constant.
7740inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7741 int64_t val = 0;
7742 return IsInt64AndGet(instruction, &val) && val == value;
7743}
7744
7745// Returns true iff instruction is a zero bit pattern.
7746inline bool IsZeroBitPattern(HInstruction* instruction) {
7747 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7748}
7749
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007750// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007751#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007752 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7753 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7754#undef INSTRUCTION_TYPE_CHECK
7755
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007756// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007757#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7758 std::is_base_of<BaseType, H##type>::value,
7759#define INSTRUCTION_TYPE_CHECK(type, super) \
7760 inline bool HInstruction::Is##type() const { \
7761 DCHECK_LT(GetKind(), kLastInstructionKind); \
7762 using BaseType = H##type; \
7763 static constexpr bool results[] = { \
7764 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7765 }; \
7766 return results[static_cast<size_t>(GetKind())]; \
7767 }
7768
7769 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7770#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007771#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007772
7773#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007774 inline const H##type* HInstruction::As##type() const { \
7775 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7776 } \
7777 inline H##type* HInstruction::As##type() { \
7778 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7779 }
7780
Vladimir Markoa90dd512018-05-04 15:04:45 +01007781 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7782#undef INSTRUCTION_TYPE_CAST
7783
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007784
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007785// Create space in `blocks` for adding `number_of_new_blocks` entries
7786// starting at location `at`. Blocks after `at` are moved accordingly.
7787inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7788 size_t number_of_new_blocks,
7789 size_t after) {
7790 DCHECK_LT(after, blocks->size());
7791 size_t old_size = blocks->size();
7792 size_t new_size = old_size + number_of_new_blocks;
7793 blocks->resize(new_size);
7794 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7795}
7796
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007797/*
7798 * Hunt "under the hood" of array lengths (leading to array references),
7799 * null checks (also leading to array references), and new arrays
7800 * (leading to the actual length). This makes it more likely related
7801 * instructions become actually comparable.
7802 */
7803inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7804 while (instruction->IsArrayLength() ||
7805 instruction->IsNullCheck() ||
7806 instruction->IsNewArray()) {
7807 instruction = instruction->IsNewArray()
7808 ? instruction->AsNewArray()->GetLength()
7809 : instruction->InputAt(0);
7810 }
7811 return instruction;
7812}
7813
Artem Serov21c7e6f2017-07-27 16:04:42 +01007814void RemoveEnvironmentUses(HInstruction* instruction);
7815bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7816void ResetEnvironmentInputRecords(HInstruction* instruction);
7817
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007818} // namespace art
7819
7820#endif // ART_COMPILER_OPTIMIZING_NODES_H_