blob: 9da46206da3b782215aea60ebe7e9ab2e0938297 [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"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000037#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000038#include "handle.h"
39#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010040#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010041#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000042#include "method_reference.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
David Brazdild7558da2015-09-22 13:04:14 +01001275 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) \
1443 M(VecShl, VecBinaryOperation) \
1444 M(VecShr, VecBinaryOperation) \
1445 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001446 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001447 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001448 M(VecSADAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001449 M(VecLoad, VecMemoryOperation) \
1450 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001451
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001452/*
1453 * Instructions, shared across several (not all) architectures.
1454 */
1455#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1456#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1457#else
1458#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001459 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001460 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001461 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001462 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001463#endif
1464
Alexandre Ramesef20f712015-06-09 10:29:30 +01001465#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1466
1467#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1468
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001469#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001470#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001471#else
1472#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1473 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001474 M(MipsPackedSwitch, Instruction) \
1475 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001476#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001477
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001478#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1479
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001480#ifndef ART_ENABLE_CODEGEN_x86
1481#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1482#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001483#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1484 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001485 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001486 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001487 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001488#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001489
1490#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1491
1492#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1493 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001494 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001495 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1496 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001497 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001498 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001499 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1500 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1501
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001502#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1503 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001504 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001505 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001506 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001507 M(Invoke, Instruction) \
1508 M(VecOperation, Instruction) \
1509 M(VecUnaryOperation, VecOperation) \
1510 M(VecBinaryOperation, VecOperation) \
1511 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001512
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001513#define FOR_EACH_INSTRUCTION(M) \
1514 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1515 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1516
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001517#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001518FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1519#undef FORWARD_DECLARATION
1520
Vladimir Marko372f10e2016-05-17 16:30:10 +01001521#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001522 private: \
1523 H##type& operator=(const H##type&) = delete; \
1524 public: \
Vladimir Marko372f10e2016-05-17 16:30:10 +01001525 const char* DebugName() const OVERRIDE { return #type; } \
1526 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1527 return other->Is##type(); \
1528 } \
Artem Serovcced8ba2017-07-19 18:18:09 +01001529 HInstruction* Clone(ArenaAllocator* arena) const OVERRIDE { \
1530 DCHECK(IsClonable()); \
1531 return new (arena) H##type(*this->As##type()); \
1532 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001533 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001534
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001535#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001536 private: \
1537 H##type& operator=(const H##type&) = delete; \
1538 public: \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001539 bool Is##type() const { return As##type() != nullptr; } \
1540 const H##type* As##type() const { return this; } \
1541 H##type* As##type() { return this; }
1542
Artem Serovcced8ba2017-07-19 18:18:09 +01001543#define DEFAULT_COPY_CONSTRUCTOR(type) \
1544 explicit H##type(const H##type& other) = default;
1545
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001546template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001547class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1548 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001549 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001550 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001551 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001552 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001554 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001555 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001556
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001557 private:
David Brazdiled596192015-01-23 10:39:45 +00001558 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001559 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001560
1561 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001562 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001563
Vladimir Marko46817b82016-03-29 12:21:58 +01001564 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001565
1566 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1567};
1568
David Brazdiled596192015-01-23 10:39:45 +00001569template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001570using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001571
David Brazdil1abb4192015-02-17 18:33:36 +00001572// This class is used by HEnvironment and HInstruction classes to record the
1573// instructions they use and pointers to the corresponding HUseListNodes kept
1574// by the used instructions.
1575template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001576class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001577 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001578 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1579 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001580
Vladimir Marko46817b82016-03-29 12:21:58 +01001581 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1582 : HUserRecord(old_record.instruction_, before_use_node) {}
1583 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1584 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001585 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001586 }
1587
1588 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001589 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1590 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001591
1592 private:
1593 // Instruction used by the user.
1594 HInstruction* instruction_;
1595
Vladimir Marko46817b82016-03-29 12:21:58 +01001596 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1597 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001598};
1599
Vladimir Markoe9004912016-06-16 16:50:52 +01001600// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1601// This is used for HInstruction::GetInputs() to return a container wrapper providing
1602// HInstruction* values even though the underlying container has HUserRecord<>s.
1603struct HInputExtractor {
1604 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1605 return record.GetInstruction();
1606 }
1607 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1608 return record.GetInstruction();
1609 }
1610};
1611
1612using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1613using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1614
Aart Bik854a02b2015-07-14 16:07:00 -07001615/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001617 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 * For write/read dependences on fields/arrays, the dependence analysis uses
1619 * type disambiguation (e.g. a float field write cannot modify the value of an
1620 * integer field read) and the access type (e.g. a reference array write cannot
1621 * modify the value of a reference field read [although it may modify the
1622 * reference fetch prior to reading the field, which is represented by its own
1623 * write/read dependence]). The analysis makes conservative points-to
1624 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1625 * the same, and any reference read depends on any reference read without
1626 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001627 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 * The internal representation uses 38-bit and is described in the table below.
1629 * The first line indicates the side effect, and for field/array accesses the
1630 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 * The two numbered lines below indicate the bit position in the bitfield (read
1633 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001634 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1636 * +-------------+---------+---------+--------------+---------+---------+
1637 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1638 * | 3 |333333322|222222221| 1 |111111110|000000000|
1639 * | 7 |654321098|765432109| 8 |765432109|876543210|
1640 *
1641 * Note that, to ease the implementation, 'changes' bits are least significant
1642 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001643 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001644class SideEffects : public ValueObject {
1645 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001646 SideEffects() : flags_(0) {}
1647
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001648 static SideEffects None() {
1649 return SideEffects(0);
1650 }
1651
1652 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 return SideEffects(kAllChangeBits | kAllDependOnBits);
1654 }
1655
1656 static SideEffects AllChanges() {
1657 return SideEffects(kAllChangeBits);
1658 }
1659
1660 static SideEffects AllDependencies() {
1661 return SideEffects(kAllDependOnBits);
1662 }
1663
1664 static SideEffects AllExceptGCDependency() {
1665 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1666 }
1667
1668 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001669 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001670 }
1671
Aart Bik34c3ba92015-07-20 14:08:59 -07001672 static SideEffects AllWrites() {
1673 return SideEffects(kAllWrites);
1674 }
1675
1676 static SideEffects AllReads() {
1677 return SideEffects(kAllReads);
1678 }
1679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001681 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001682 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001683 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684 }
1685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001687 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001688 }
1689
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001690 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001691 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001692 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001693 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001694 }
1695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001697 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001698 }
1699
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001700 static SideEffects CanTriggerGC() {
1701 return SideEffects(1ULL << kCanTriggerGCBit);
1702 }
1703
1704 static SideEffects DependsOnGC() {
1705 return SideEffects(1ULL << kDependsOnGCBit);
1706 }
1707
Aart Bik854a02b2015-07-14 16:07:00 -07001708 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001709 SideEffects Union(SideEffects other) const {
1710 return SideEffects(flags_ | other.flags_);
1711 }
1712
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001713 SideEffects Exclusion(SideEffects other) const {
1714 return SideEffects(flags_ & ~other.flags_);
1715 }
1716
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001717 void Add(SideEffects other) {
1718 flags_ |= other.flags_;
1719 }
1720
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 bool Includes(SideEffects other) const {
1722 return (other.flags_ & flags_) == other.flags_;
1723 }
1724
1725 bool HasSideEffects() const {
1726 return (flags_ & kAllChangeBits);
1727 }
1728
1729 bool HasDependencies() const {
1730 return (flags_ & kAllDependOnBits);
1731 }
1732
1733 // Returns true if there are no side effects or dependencies.
1734 bool DoesNothing() const {
1735 return flags_ == 0;
1736 }
1737
Aart Bik854a02b2015-07-14 16:07:00 -07001738 // Returns true if something is written.
1739 bool DoesAnyWrite() const {
1740 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001741 }
1742
Aart Bik854a02b2015-07-14 16:07:00 -07001743 // Returns true if something is read.
1744 bool DoesAnyRead() const {
1745 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001746 }
1747
Aart Bik854a02b2015-07-14 16:07:00 -07001748 // Returns true if potentially everything is written and read
1749 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001750 bool DoesAllReadWrite() const {
1751 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1752 }
1753
Aart Bik854a02b2015-07-14 16:07:00 -07001754 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001755 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001756 }
1757
Roland Levillain0d5a2812015-11-13 10:07:31 +00001758 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001759 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001760 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1761 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001762 }
1763
1764 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001765 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001766 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001767 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001768 for (int s = kLastBit; s >= 0; s--) {
1769 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1770 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1771 // This is a bit for the GC side effect.
1772 if (current_bit_is_set) {
1773 flags += "GC";
1774 }
Aart Bik854a02b2015-07-14 16:07:00 -07001775 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001776 } else {
1777 // This is a bit for the array/field analysis.
1778 // The underscore character stands for the 'can trigger GC' bit.
1779 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1780 if (current_bit_is_set) {
1781 flags += kDebug[s];
1782 }
1783 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1784 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1785 flags += "|";
1786 }
1787 }
Aart Bik854a02b2015-07-14 16:07:00 -07001788 }
1789 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001790 }
1791
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001792 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001794 private:
1795 static constexpr int kFieldArrayAnalysisBits = 9;
1796
1797 static constexpr int kFieldWriteOffset = 0;
1798 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1799 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1800 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1801
1802 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1803
1804 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1805 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1806 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1807 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1808
1809 static constexpr int kLastBit = kDependsOnGCBit;
1810 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1811
1812 // Aliases.
1813
1814 static_assert(kChangeBits == kDependOnBits,
1815 "the 'change' bits should match the 'depend on' bits.");
1816
1817 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1818 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1819 static constexpr uint64_t kAllWrites =
1820 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1821 static constexpr uint64_t kAllReads =
1822 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001823
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001824 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001826 int shift;
1827 switch (type) {
1828 case DataType::Type::kReference: shift = 0; break;
1829 case DataType::Type::kBool: shift = 1; break;
1830 case DataType::Type::kInt8: shift = 2; break;
1831 case DataType::Type::kUint16: shift = 3; break;
1832 case DataType::Type::kInt16: shift = 4; break;
1833 case DataType::Type::kInt32: shift = 5; break;
1834 case DataType::Type::kInt64: shift = 6; break;
1835 case DataType::Type::kFloat32: shift = 7; break;
1836 case DataType::Type::kFloat64: shift = 8; break;
1837 default:
1838 LOG(FATAL) << "Unexpected data type " << type;
1839 UNREACHABLE();
1840 }
Aart Bik854a02b2015-07-14 16:07:00 -07001841 DCHECK_LE(kFieldWriteOffset, shift);
1842 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001843 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001844 }
1845
1846 // Private constructor on direct flags value.
1847 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1848
1849 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001850};
1851
David Brazdiled596192015-01-23 10:39:45 +00001852// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001853class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001854 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001855 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001856 size_t number_of_vregs,
1857 ArtMethod* method,
1858 uint32_t dex_pc,
1859 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001860 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1861 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001862 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001863 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001864 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001865 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001866 }
1867
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001868 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1869 const HEnvironment& to_copy,
1870 HInstruction* holder)
1871 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001872 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001873 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001874 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001875 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001876
Vladimir Markoec32f642017-06-02 10:51:55 +01001877 void AllocateLocations() {
1878 DCHECK(locations_.empty());
1879 locations_.resize(vregs_.size());
1880 }
1881
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001882 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001883 if (parent_ != nullptr) {
1884 parent_->SetAndCopyParentChain(allocator, parent);
1885 } else {
1886 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1887 parent_->CopyFrom(parent);
1888 if (parent->GetParent() != nullptr) {
1889 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1890 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001891 }
David Brazdiled596192015-01-23 10:39:45 +00001892 }
1893
Vladimir Marko69d310e2017-10-09 14:12:23 +01001894 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001895 void CopyFrom(HEnvironment* environment);
1896
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001897 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1898 // input to the loop phi instead. This is for inserting instructions that
1899 // require an environment (like HDeoptimization) in the loop pre-header.
1900 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001901
1902 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001903 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001904 }
1905
1906 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001907 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001908 }
1909
David Brazdil1abb4192015-02-17 18:33:36 +00001910 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001911
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001912 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001913
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001914 HEnvironment* GetParent() const { return parent_; }
1915
1916 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001917 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 }
1919
1920 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001921 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001922 }
1923
1924 uint32_t GetDexPc() const {
1925 return dex_pc_;
1926 }
1927
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001928 ArtMethod* GetMethod() const {
1929 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001930 }
1931
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001932 HInstruction* GetHolder() const {
1933 return holder_;
1934 }
1935
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001936
1937 bool IsFromInlinedInvoke() const {
1938 return GetParent() != nullptr;
1939 }
1940
David Brazdiled596192015-01-23 10:39:45 +00001941 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001942 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1943 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001944 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001945 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001946 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001947
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001948 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 HInstruction* const holder_;
1950
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001951 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001952
1953 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1954};
1955
Vladimir Markof9f64412015-09-02 14:05:49 +01001956class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001957 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301958#define DECLARE_KIND(type, super) k##type,
1959 enum InstructionKind {
1960 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1961 kLastInstructionKind
1962 };
1963#undef DECLARE_KIND
1964
1965 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001966 : previous_(nullptr),
1967 next_(nullptr),
1968 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001969 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001970 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001971 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001972 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001973 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001974 locations_(nullptr),
1975 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001976 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001977 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001978 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301979 SetPackedField<InstructionKindField>(kind);
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 Marko0ebe0d82017-09-21 22:50:39 +01002037 virtual DataType::Type GetType() const { return DataType::Type::kVoid; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002038
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002040
2041 uint32_t GetDexPc() const { return dex_pc_; }
2042
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002043 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002044
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002045 // Can the instruction throw?
2046 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2047 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002048 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002049
2050 // Does the instruction always throw an exception unconditionally?
2051 virtual bool AlwaysThrows() const { return false; }
2052
David Brazdilec16f792015-08-19 15:04:01 +01002053 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002054
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002055 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002056 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002057
Calin Juravle10e244f2015-01-26 18:54:32 +00002058 // Does not apply for all instructions, but having this at top level greatly
2059 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002060 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002061 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002063 return true;
2064 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002065
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002066 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002067 return false;
2068 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002069
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002070 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002071 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002072 }
2073
Calin Juravle2e768302015-07-28 14:41:11 +00002074 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002075
Calin Juravle61d544b2015-02-23 16:46:57 +00002076 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002077 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002078 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002079 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002080 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002081
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002082 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002083 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002084 // Note: fixup_end remains valid across push_front().
2085 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2086 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002087 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002088 uses_.push_front(*new_node);
2089 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002090 }
2091
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002092 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002093 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002094 // Note: env_fixup_end remains valid across push_front().
2095 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2096 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002097 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002098 env_uses_.push_front(*new_node);
2099 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002100 }
2101
David Brazdil1abb4192015-02-17 18:33:36 +00002102 void RemoveAsUserOfInput(size_t input) {
2103 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002104 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2105 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2106 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002107 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002108
Vladimir Marko372f10e2016-05-17 16:30:10 +01002109 void RemoveAsUserOfAllInputs() {
2110 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2111 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2112 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2113 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2114 }
2115 }
2116
David Brazdil1abb4192015-02-17 18:33:36 +00002117 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2118 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002119
Vladimir Marko46817b82016-03-29 12:21:58 +01002120 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2121 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2122 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002123 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002124 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002125 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002126
Aart Bikcc42be02016-10-20 16:14:16 -07002127 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002128 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002129 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002130 !CanThrow() &&
2131 !IsSuspendCheck() &&
2132 !IsControlFlow() &&
2133 !IsNativeDebugInfo() &&
2134 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002135 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002136 !IsMemoryBarrier() &&
2137 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002138 }
2139
Aart Bikcc42be02016-10-20 16:14:16 -07002140 bool IsDeadAndRemovable() const {
2141 return IsRemovable() && !HasUses();
2142 }
2143
Roland Levillain6c82d402014-10-13 16:10:27 +01002144 // Does this instruction strictly dominate `other_instruction`?
2145 // Returns false if this instruction and `other_instruction` are the same.
2146 // Aborts if this instruction and `other_instruction` are both phis.
2147 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002148
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002149 int GetId() const { return id_; }
2150 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002151
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002152 int GetSsaIndex() const { return ssa_index_; }
2153 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2154 bool HasSsaIndex() const { return ssa_index_ != -1; }
2155
2156 bool HasEnvironment() const { return environment_ != nullptr; }
2157 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002158 // Set the `environment_` field. Raw because this method does not
2159 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002160 void SetRawEnvironment(HEnvironment* environment) {
2161 DCHECK(environment_ == nullptr);
2162 DCHECK_EQ(environment->GetHolder(), this);
2163 environment_ = environment;
2164 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002165
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002166 void InsertRawEnvironment(HEnvironment* environment) {
2167 DCHECK(environment_ != nullptr);
2168 DCHECK_EQ(environment->GetHolder(), this);
2169 DCHECK(environment->GetParent() == nullptr);
2170 environment->parent_ = environment_;
2171 environment_ = environment;
2172 }
2173
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002174 void RemoveEnvironment();
2175
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002176 // Set the environment of this instruction, copying it from `environment`. While
2177 // copying, the uses lists are being updated.
2178 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002179 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002180 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002181 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002182 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002183 if (environment->GetParent() != nullptr) {
2184 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2185 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002186 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002188 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2189 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002190 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002191 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002192 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002193 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002194 if (environment->GetParent() != nullptr) {
2195 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2196 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002197 }
2198
Nicolas Geoffray39468442014-09-02 15:17:15 +01002199 // Returns the number of entries in the environment. Typically, that is the
2200 // number of dex registers in a method. It could be more in case of inlining.
2201 size_t EnvironmentSize() const;
2202
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002203 LocationSummary* GetLocations() const { return locations_; }
2204 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002205
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002206 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002207 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002208 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002209
Alexandre Rames188d4312015-04-09 18:30:21 +01002210 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2211 // uses of this instruction by `other` are *not* updated.
2212 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2213 ReplaceWith(other);
2214 other->ReplaceInput(this, use_index);
2215 }
2216
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002217 // Move `this` instruction before `cursor`
2218 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002219
Vladimir Markofb337ea2015-11-25 15:25:10 +00002220 // Move `this` before its first user and out of any loops. If there is no
2221 // out-of-loop user that dominates all other users, move the instruction
2222 // to the end of the out-of-loop common dominator of the user's blocks.
2223 //
2224 // This can be used only on non-throwing instructions with no side effects that
2225 // have at least one use but no environment uses.
2226 void MoveBeforeFirstUserAndOutOfLoops();
2227
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002228#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002229 bool Is##type() const; \
2230 const H##type* As##type() const; \
2231 H##type* As##type();
2232
2233 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2234#undef INSTRUCTION_TYPE_CHECK
2235
2236#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002237 bool Is##type() const { return (As##type() != nullptr); } \
2238 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002239 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002240 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002241#undef INSTRUCTION_TYPE_CHECK
2242
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
2267 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002268 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002269 return false;
2270 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002271
2272 // Returns whether any data encoded in the two instructions is equal.
2273 // This method does not look at the inputs. Both instructions must be
2274 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002275 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002276 return false;
2277 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002278
2279 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002280 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002281 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002282 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002283
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002284 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2285 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2286 // the virtual function because the __attribute__((__pure__)) doesn't really
2287 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302288 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002289
2290 virtual size_t ComputeHashCode() const {
2291 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002292 for (const HInstruction* input : GetInputs()) {
2293 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002294 }
2295 return result;
2296 }
2297
2298 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002299 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002300 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002301
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002302 size_t GetLifetimePosition() const { return lifetime_position_; }
2303 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2304 LiveInterval* GetLiveInterval() const { return live_interval_; }
2305 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2306 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2307
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002308 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2309
2310 // Returns whether the code generation of the instruction will require to have access
2311 // to the current method. Such instructions are:
2312 // (1): Instructions that require an environment, as calling the runtime requires
2313 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002314 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2315 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002316 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002317 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002318 }
2319
Vladimir Markodc151b22015-10-15 18:02:30 +01002320 // Returns whether the code generation of the instruction will require to have access
2321 // to the dex cache of the current method's declaring class via the current method.
2322 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002323
Mark Mendellc4701932015-04-10 13:18:51 -04002324 // Does this instruction have any use in an environment before
2325 // control flow hits 'other'?
2326 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2327
2328 // Remove all references to environment uses of this instruction.
2329 // The caller must ensure that this is safe to do.
2330 void RemoveEnvironmentUsers();
2331
Vladimir Markoa1de9182016-02-25 11:37:38 +00002332 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2333 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002334
David Brazdil1abb4192015-02-17 18:33:36 +00002335 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002336 // If set, the machine code for this instruction is assumed to be generated by
2337 // its users. Used by liveness analysis to compute use positions accordingly.
2338 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2339 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302340 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2341 static constexpr size_t kFieldInstructionKindSize =
2342 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
2343 static constexpr size_t kNumberOfGenericPackedBits =
2344 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002345 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2346
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302347 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2348 "Too many generic packed fields");
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)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002596
Artem Serovcced8ba2017-07-19 18:18:09 +01002597 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002598
Artem Serovcced8ba2017-07-19 18:18:09 +01002599 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002600};
2601
Vladimir Markof9f64412015-09-02 14:05:49 +01002602template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002603class HTemplateInstruction: public HInstruction {
2604 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302605 HTemplateInstruction<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2606 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002607 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002608
Vladimir Marko372f10e2016-05-17 16:30:10 +01002609 using HInstruction::GetInputRecords; // Keep the const version visible.
2610 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2611 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002612 }
2613
Artem Serovcced8ba2017-07-19 18:18:09 +01002614 protected:
2615 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<N>);
2616
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002617 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002618 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002619
2620 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002621};
2622
Vladimir Markof9f64412015-09-02 14:05:49 +01002623// HTemplateInstruction specialization for N=0.
2624template<>
2625class HTemplateInstruction<0>: public HInstruction {
2626 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302627 explicit HTemplateInstruction<0>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
2628 : HInstruction(kind, side_effects, dex_pc) {}
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002629
Vladimir Markof9f64412015-09-02 14:05:49 +01002630 virtual ~HTemplateInstruction() {}
2631
Vladimir Marko372f10e2016-05-17 16:30:10 +01002632 using HInstruction::GetInputRecords; // Keep the const version visible.
2633 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2634 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002635 }
2636
Artem Serovcced8ba2017-07-19 18:18:09 +01002637 protected:
2638 DEFAULT_COPY_CONSTRUCTOR(TemplateInstruction<0>);
2639
Vladimir Markof9f64412015-09-02 14:05:49 +01002640 private:
2641 friend class SsaBuilder;
2642};
2643
Dave Allison20dfc792014-06-16 20:44:29 -07002644template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002645class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002646 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302647 using HInstruction::InstructionKind;
2648 HExpression<N>(InstructionKind kind,
2649 DataType::Type type,
2650 SideEffects side_effects,
2651 uint32_t dex_pc)
2652 : HTemplateInstruction<N>(kind, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002653 this->template SetPackedField<TypeField>(type);
2654 }
Dave Allison20dfc792014-06-16 20:44:29 -07002655 virtual ~HExpression() {}
2656
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 DataType::Type GetType() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002658 return TypeField::Decode(this->GetPackedFields());
2659 }
Dave Allison20dfc792014-06-16 20:44:29 -07002660
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002661 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002662 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2663 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002664 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00002665 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2666 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2667 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002668 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Artem Serovcced8ba2017-07-19 18:18:09 +01002669 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Dave Allison20dfc792014-06-16 20:44:29 -07002670};
2671
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002672// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2673// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002674class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002675 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002676 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302677 : HTemplateInstruction(kReturnVoid, SideEffects::None(), dex_pc) {
2678 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002679
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002680 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002681
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002682 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002683
Artem Serovcced8ba2017-07-19 18:18:09 +01002684 protected:
2685 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002686};
2687
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002688// Represents dex's RETURN opcodes. A HReturn is a control flow
2689// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002690class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002691 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002692 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302693 : HTemplateInstruction(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002694 SetRawInputAt(0, value);
2695 }
2696
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002697 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002698
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002699 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002700
Artem Serovcced8ba2017-07-19 18:18:09 +01002701 protected:
2702 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002703};
2704
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002705class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002706 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002707 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002708 uint32_t reg_number,
2709 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002710 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002711 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002712 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302713 kPhi,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002714 SideEffects::None(),
2715 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002716 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002717 number_of_inputs,
2718 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002719 reg_number_(reg_number) {
2720 SetPackedField<TypeField>(ToPhiType(type));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002722 // Phis are constructed live and marked dead if conflicting or unused.
2723 // Individual steps of SsaBuilder should assume that if a phi has been
2724 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2725 SetPackedFlag<kFlagIsLive>(true);
2726 SetPackedFlag<kFlagCanBeNull>(true);
2727 }
2728
Artem Serovcced8ba2017-07-19 18:18:09 +01002729 bool IsClonable() const OVERRIDE { return true; }
2730
David Brazdildee58d62016-04-07 09:54:26 +00002731 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 static DataType::Type ToPhiType(DataType::Type type) {
2733 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002734 }
2735
2736 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002738 DataType::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2739 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002740 // Make sure that only valid type changes occur. The following are allowed:
2741 // (1) int -> float/ref (primitive type propagation),
2742 // (2) long -> double (primitive type propagation).
2743 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2745 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2746 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002747 SetPackedField<TypeField>(new_type);
2748 }
2749
2750 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2751 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2752
2753 uint32_t GetRegNumber() const { return reg_number_; }
2754
2755 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2756 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2757 bool IsDead() const { return !IsLive(); }
2758 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2759
Vladimir Markoe9004912016-06-16 16:50:52 +01002760 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002761 return other != nullptr
2762 && other->IsPhi()
2763 && other->AsPhi()->GetBlock() == GetBlock()
2764 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2765 }
2766
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002767 bool HasEquivalentPhi() const {
2768 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2769 return true;
2770 }
2771 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2772 return true;
2773 }
2774 return false;
2775 }
2776
David Brazdildee58d62016-04-07 09:54:26 +00002777 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2778 // An equivalent phi is a phi having the same dex register and type.
2779 // It assumes that phis with the same dex register are adjacent.
2780 HPhi* GetNextEquivalentPhiWithSameType() {
2781 HInstruction* next = GetNext();
2782 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2783 if (next->GetType() == GetType()) {
2784 return next->AsPhi();
2785 }
2786 next = next->GetNext();
2787 }
2788 return nullptr;
2789 }
2790
2791 DECLARE_INSTRUCTION(Phi);
2792
Artem Serovcced8ba2017-07-19 18:18:09 +01002793 protected:
2794 DEFAULT_COPY_CONSTRUCTOR(Phi);
2795
David Brazdildee58d62016-04-07 09:54:26 +00002796 private:
2797 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2798 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
David Brazdildee58d62016-04-07 09:54:26 +00002800 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2801 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2802 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2803 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
David Brazdildee58d62016-04-07 09:54:26 +00002805
David Brazdildee58d62016-04-07 09:54:26 +00002806 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002807};
2808
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002809// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002810// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002811// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002812class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002813 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302814 explicit HExit(uint32_t dex_pc = kNoDexPc)
2815 : HTemplateInstruction(kExit, SideEffects::None(), dex_pc) {
2816 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002817
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002818 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002819
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002820 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002821
Artem Serovcced8ba2017-07-19 18:18:09 +01002822 protected:
2823 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002824};
2825
2826// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002827class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002828 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302829 explicit HGoto(uint32_t dex_pc = kNoDexPc)
2830 : HTemplateInstruction(kGoto, SideEffects::None(), dex_pc) {
2831 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002832
Artem Serovcced8ba2017-07-19 18:18:09 +01002833 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002834 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002835
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002836 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002837 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002838 }
2839
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002840 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002841
Artem Serovcced8ba2017-07-19 18:18:09 +01002842 protected:
2843 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002844};
2845
Roland Levillain9867bc72015-08-05 10:21:34 +01002846class HConstant : public HExpression<0> {
2847 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302848 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2849 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2850 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002851
2852 bool CanBeMoved() const OVERRIDE { return true; }
2853
Roland Levillain1a653882016-03-18 18:05:57 +00002854 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002855 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002856 // Is this constant 0 in the arithmetic sense?
2857 virtual bool IsArithmeticZero() const { return false; }
2858 // Is this constant a 0-bit pattern?
2859 virtual bool IsZeroBitPattern() const { return false; }
2860 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002861 virtual bool IsOne() const { return false; }
2862
David Brazdil77a48ae2015-09-15 12:34:04 +00002863 virtual uint64_t GetValueAsUint64() const = 0;
2864
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002865 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002866
Artem Serovcced8ba2017-07-19 18:18:09 +01002867 protected:
2868 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002869};
2870
Vladimir Markofcb503c2016-05-18 12:48:17 +01002871class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002872 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002873 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002874 return true;
2875 }
2876
David Brazdil77a48ae2015-09-15 12:34:04 +00002877 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2878
Roland Levillain9867bc72015-08-05 10:21:34 +01002879 size_t ComputeHashCode() const OVERRIDE { return 0; }
2880
Roland Levillain1a653882016-03-18 18:05:57 +00002881 // The null constant representation is a 0-bit pattern.
2882 virtual bool IsZeroBitPattern() const { return true; }
2883
Roland Levillain9867bc72015-08-05 10:21:34 +01002884 DECLARE_INSTRUCTION(NullConstant);
2885
Artem Serovcced8ba2017-07-19 18:18:09 +01002886 protected:
2887 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2888
Roland Levillain9867bc72015-08-05 10:21:34 +01002889 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302891 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2892 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002893
2894 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002895};
2896
2897// Constants of the type int. Those can be from Dex instructions, or
2898// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002899class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002900 public:
2901 int32_t GetValue() const { return value_; }
2902
David Brazdil9f389d42015-10-01 14:32:56 +01002903 uint64_t GetValueAsUint64() const OVERRIDE {
2904 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2905 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002906
Vladimir Marko372f10e2016-05-17 16:30:10 +01002907 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002908 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002909 return other->AsIntConstant()->value_ == value_;
2910 }
2911
2912 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2913
2914 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002915 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2916 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002917 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2918
Roland Levillain1a653882016-03-18 18:05:57 +00002919 // Integer constants are used to encode Boolean values as well,
2920 // where 1 means true and 0 means false.
2921 bool IsTrue() const { return GetValue() == 1; }
2922 bool IsFalse() const { return GetValue() == 0; }
2923
Roland Levillain9867bc72015-08-05 10:21:34 +01002924 DECLARE_INSTRUCTION(IntConstant);
2925
Artem Serovcced8ba2017-07-19 18:18:09 +01002926 protected:
2927 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2928
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002930 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302931 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2932 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002933 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302934 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2935 value_(value ? 1 : 0) {
2936 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002937
2938 const int32_t value_;
2939
2940 friend class HGraph;
2941 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2942 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002943};
2944
Vladimir Markofcb503c2016-05-18 12:48:17 +01002945class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002946 public:
2947 int64_t GetValue() const { return value_; }
2948
David Brazdil77a48ae2015-09-15 12:34:04 +00002949 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2950
Vladimir Marko372f10e2016-05-17 16:30:10 +01002951 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002952 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002953 return other->AsLongConstant()->value_ == value_;
2954 }
2955
2956 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2957
2958 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002959 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2960 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002961 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2962
2963 DECLARE_INSTRUCTION(LongConstant);
2964
Artem Serovcced8ba2017-07-19 18:18:09 +01002965 protected:
2966 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2967
Roland Levillain9867bc72015-08-05 10:21:34 +01002968 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002969 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302970 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2971 value_(value) {
2972 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002973
2974 const int64_t value_;
2975
2976 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002977};
Dave Allison20dfc792014-06-16 20:44:29 -07002978
Vladimir Markofcb503c2016-05-18 12:48:17 +01002979class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002980 public:
2981 float GetValue() const { return value_; }
2982
2983 uint64_t GetValueAsUint64() const OVERRIDE {
2984 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2985 }
2986
Vladimir Marko372f10e2016-05-17 16:30:10 +01002987 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002988 DCHECK(other->IsFloatConstant()) << other->DebugName();
2989 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2990 }
2991
2992 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2993
2994 bool IsMinusOne() const OVERRIDE {
2995 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2996 }
Roland Levillain1a653882016-03-18 18:05:57 +00002997 bool IsArithmeticZero() const OVERRIDE {
2998 return std::fpclassify(value_) == FP_ZERO;
2999 }
3000 bool IsArithmeticPositiveZero() const {
3001 return IsArithmeticZero() && !std::signbit(value_);
3002 }
3003 bool IsArithmeticNegativeZero() const {
3004 return IsArithmeticZero() && std::signbit(value_);
3005 }
3006 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003007 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003008 }
3009 bool IsOne() const OVERRIDE {
3010 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3011 }
3012 bool IsNaN() const {
3013 return std::isnan(value_);
3014 }
3015
3016 DECLARE_INSTRUCTION(FloatConstant);
3017
Artem Serovcced8ba2017-07-19 18:18:09 +01003018 protected:
3019 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3020
Roland Levillain31dd3d62016-02-16 12:21:02 +00003021 private:
3022 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303023 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3024 value_(value) {
3025 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003026 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303027 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3028 value_(bit_cast<float, int32_t>(value)) {
3029 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003030
3031 const float value_;
3032
3033 // Only the SsaBuilder and HGraph can create floating-point constants.
3034 friend class SsaBuilder;
3035 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036};
3037
Vladimir Markofcb503c2016-05-18 12:48:17 +01003038class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003039 public:
3040 double GetValue() const { return value_; }
3041
3042 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3043
Vladimir Marko372f10e2016-05-17 16:30:10 +01003044 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003045 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3046 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3047 }
3048
3049 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
3050
3051 bool IsMinusOne() const OVERRIDE {
3052 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3053 }
Roland Levillain1a653882016-03-18 18:05:57 +00003054 bool IsArithmeticZero() const OVERRIDE {
3055 return std::fpclassify(value_) == FP_ZERO;
3056 }
3057 bool IsArithmeticPositiveZero() const {
3058 return IsArithmeticZero() && !std::signbit(value_);
3059 }
3060 bool IsArithmeticNegativeZero() const {
3061 return IsArithmeticZero() && std::signbit(value_);
3062 }
3063 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003064 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065 }
3066 bool IsOne() const OVERRIDE {
3067 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3068 }
3069 bool IsNaN() const {
3070 return std::isnan(value_);
3071 }
3072
3073 DECLARE_INSTRUCTION(DoubleConstant);
3074
Artem Serovcced8ba2017-07-19 18:18:09 +01003075 protected:
3076 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3077
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078 private:
3079 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303080 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3081 value_(value) {
3082 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003083 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303084 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3085 value_(bit_cast<double, int64_t>(value)) {
3086 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003087
3088 const double value_;
3089
3090 // Only the SsaBuilder and HGraph can create floating-point constants.
3091 friend class SsaBuilder;
3092 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003093};
3094
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003095// Conditional branch. A block ending with an HIf instruction must have
3096// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003097class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003098 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003099 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303100 : HTemplateInstruction(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003101 SetRawInputAt(0, input);
3102 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003103
Artem Serovcced8ba2017-07-19 18:18:09 +01003104 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003105 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003106
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003107 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003108 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003109 }
3110
3111 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003112 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003113 }
3114
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003115 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003116
Artem Serovcced8ba2017-07-19 18:18:09 +01003117 protected:
3118 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003119};
3120
David Brazdilfc6a86a2015-06-26 10:33:45 +00003121
3122// Abstract instruction which marks the beginning and/or end of a try block and
3123// links it to the respective exception handlers. Behaves the same as a Goto in
3124// non-exceptional control flow.
3125// Normal-flow successor is stored at index zero, exception handlers under
3126// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003127class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003128 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003129 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003130 kEntry,
3131 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003132 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003133 };
3134
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003135 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303136 : HTemplateInstruction(kTryBoundary, SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003137 SetPackedField<BoundaryKindField>(kind);
3138 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003139
3140 bool IsControlFlow() const OVERRIDE { return true; }
3141
3142 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003143 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003144
David Brazdild26a4112015-11-10 11:07:31 +00003145 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3146 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3147 }
3148
David Brazdilfc6a86a2015-06-26 10:33:45 +00003149 // Returns whether `handler` is among its exception handlers (non-zero index
3150 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003151 bool HasExceptionHandler(const HBasicBlock& handler) const {
3152 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003153 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003154 }
3155
3156 // If not present already, adds `handler` to its block's list of exception
3157 // handlers.
3158 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003159 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003160 GetBlock()->AddSuccessor(handler);
3161 }
3162 }
3163
Vladimir Markoa1de9182016-02-25 11:37:38 +00003164 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3165 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003166
David Brazdilffee3d32015-07-06 11:48:53 +01003167 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3168
David Brazdilfc6a86a2015-06-26 10:33:45 +00003169 DECLARE_INSTRUCTION(TryBoundary);
3170
Artem Serovcced8ba2017-07-19 18:18:09 +01003171 protected:
3172 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3173
David Brazdilfc6a86a2015-06-26 10:33:45 +00003174 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003175 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3176 static constexpr size_t kFieldBoundaryKindSize =
3177 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3178 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3179 kFieldBoundaryKind + kFieldBoundaryKindSize;
3180 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3181 "Too many packed fields.");
3182 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003183};
3184
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003185// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003186class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003187 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003188 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3189 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003190 HDeoptimize(ArenaAllocator* allocator,
3191 HInstruction* cond,
3192 DeoptimizationKind kind,
3193 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003194 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303195 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003196 SideEffects::All(),
3197 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003198 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003199 /* number_of_inputs */ 1,
3200 kArenaAllocMisc) {
3201 SetPackedFlag<kFieldCanBeMoved>(false);
3202 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003203 SetRawInputAt(0, cond);
3204 }
3205
Artem Serovcced8ba2017-07-19 18:18:09 +01003206 bool IsClonable() const OVERRIDE { return true; }
3207
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003208 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3209 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3210 // instead of `guard`.
3211 // We set CanTriggerGC to prevent any intermediate address to be live
3212 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003213 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003214 HInstruction* cond,
3215 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003216 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003217 uint32_t dex_pc)
3218 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303219 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003220 SideEffects::CanTriggerGC(),
3221 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003222 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003223 /* number_of_inputs */ 2,
3224 kArenaAllocMisc) {
3225 SetPackedFlag<kFieldCanBeMoved>(true);
3226 SetPackedField<DeoptimizeKindField>(kind);
3227 SetRawInputAt(0, cond);
3228 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003229 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003230
3231 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3232
3233 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3234 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3235 }
3236
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003237 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003238
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003239 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003240
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003241 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003243 DataType::Type GetType() const OVERRIDE {
3244 return GuardsAnInput() ? GuardedInput()->GetType() : DataType::Type::kVoid;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003245 }
3246
3247 bool GuardsAnInput() const {
3248 return InputCount() == 2;
3249 }
3250
3251 HInstruction* GuardedInput() const {
3252 DCHECK(GuardsAnInput());
3253 return InputAt(1);
3254 }
3255
3256 void RemoveGuard() {
3257 RemoveInputAt(1);
3258 }
3259
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003260 DECLARE_INSTRUCTION(Deoptimize);
3261
Artem Serovcced8ba2017-07-19 18:18:09 +01003262 protected:
3263 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3264
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003265 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003266 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3267 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3268 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003269 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003270 static constexpr size_t kNumberOfDeoptimizePackedBits =
3271 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3272 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3273 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003274 using DeoptimizeKindField =
3275 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003276};
3277
Mingyao Yang063fc772016-08-02 11:02:54 -07003278// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3279// The compiled code checks this flag value in a guard before devirtualized call and
3280// if it's true, starts to do deoptimization.
3281// It has a 4-byte slot on stack.
3282// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003283class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003284 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003285 // CHA guards are only optimized in a separate pass and it has no side effects
3286 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003287 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303288 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
3289 SideEffects::None(),
3290 dex_pc,
3291 allocator,
3292 0,
3293 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003294 }
3295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003296 DataType::Type GetType() const OVERRIDE { return DataType::Type::kInt32; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003297
3298 // We do all CHA guard elimination/motion in a single pass, after which there is no
3299 // further guard elimination/motion since a guard might have been used for justification
3300 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3301 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003302 bool CanBeMoved() const OVERRIDE { return false; }
3303
3304 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3305
Artem Serovcced8ba2017-07-19 18:18:09 +01003306 protected:
3307 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003308};
3309
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003310// Represents the ArtMethod that was passed as a first argument to
3311// the method. It is used by instructions that depend on it, like
3312// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003313class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003314 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003315 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303316 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3317 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003318
3319 DECLARE_INSTRUCTION(CurrentMethod);
3320
Artem Serovcced8ba2017-07-19 18:18:09 +01003321 protected:
3322 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003323};
3324
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003325// Fetches an ArtMethod from the virtual table or the interface method table
3326// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003327class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003328 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003329 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003330 kVTable,
3331 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003332 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003333 };
3334 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003335 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003336 TableKind kind,
3337 size_t index,
3338 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303339 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003340 index_(index) {
3341 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003342 SetRawInputAt(0, cls);
3343 }
3344
Artem Serovcced8ba2017-07-19 18:18:09 +01003345 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003346 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003347 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003348 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003349 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003350 }
3351
Vladimir Markoa1de9182016-02-25 11:37:38 +00003352 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003353 size_t GetIndex() const { return index_; }
3354
3355 DECLARE_INSTRUCTION(ClassTableGet);
3356
Artem Serovcced8ba2017-07-19 18:18:09 +01003357 protected:
3358 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3359
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003360 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003361 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3362 static constexpr size_t kFieldTableKindSize =
3363 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3364 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3365 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3366 "Too many packed fields.");
3367 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3368
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003369 // The index of the ArtMethod in the table.
3370 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003371};
3372
Mark Mendellfe57faa2015-09-18 09:26:15 -04003373// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3374// have one successor for each entry in the switch table, and the final successor
3375// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003376class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003377 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003378 HPackedSwitch(int32_t start_value,
3379 uint32_t num_entries,
3380 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003381 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303382 : HTemplateInstruction(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003383 start_value_(start_value),
3384 num_entries_(num_entries) {
3385 SetRawInputAt(0, input);
3386 }
3387
Artem Serovcced8ba2017-07-19 18:18:09 +01003388 bool IsClonable() const OVERRIDE { return true; }
3389
Mark Mendellfe57faa2015-09-18 09:26:15 -04003390 bool IsControlFlow() const OVERRIDE { return true; }
3391
3392 int32_t GetStartValue() const { return start_value_; }
3393
Vladimir Marko211c2112015-09-24 16:52:33 +01003394 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003395
3396 HBasicBlock* GetDefaultBlock() const {
3397 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003398 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003399 }
3400 DECLARE_INSTRUCTION(PackedSwitch);
3401
Artem Serovcced8ba2017-07-19 18:18:09 +01003402 protected:
3403 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3404
Mark Mendellfe57faa2015-09-18 09:26:15 -04003405 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003406 const int32_t start_value_;
3407 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003408};
3409
Roland Levillain88cb1752014-10-20 16:36:47 +01003410class HUnaryOperation : public HExpression<1> {
3411 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303412 HUnaryOperation(InstructionKind kind,
3413 DataType::Type result_type,
3414 HInstruction* input,
3415 uint32_t dex_pc = kNoDexPc)
3416 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003417 SetRawInputAt(0, input);
3418 }
3419
Artem Serovcced8ba2017-07-19 18:18:09 +01003420 // All of the UnaryOperation instructions are clonable.
3421 bool IsClonable() const OVERRIDE { return true; }
3422
Roland Levillain88cb1752014-10-20 16:36:47 +01003423 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003424 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003425
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003426 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003427 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003428 return true;
3429 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003430
Roland Levillain31dd3d62016-02-16 12:21:02 +00003431 // Try to statically evaluate `this` and return a HConstant
3432 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003433 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003434 HConstant* TryStaticEvaluation() const;
3435
3436 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003437 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3438 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003439 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3440 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003441
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003442 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003443
Artem Serovcced8ba2017-07-19 18:18:09 +01003444 protected:
3445 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003446};
3447
Dave Allison20dfc792014-06-16 20:44:29 -07003448class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003449 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303450 HBinaryOperation(InstructionKind kind,
3451 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003452 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003453 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003454 SideEffects side_effects = SideEffects::None(),
3455 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303456 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003457 SetRawInputAt(0, left);
3458 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003459 }
3460
Artem Serovcced8ba2017-07-19 18:18:09 +01003461 // All of the BinaryOperation instructions are clonable.
3462 bool IsClonable() const OVERRIDE { return true; }
3463
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003464 HInstruction* GetLeft() const { return InputAt(0); }
3465 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003466 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003467
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003468 virtual bool IsCommutative() const { return false; }
3469
3470 // Put constant on the right.
3471 // Returns whether order is changed.
3472 bool OrderInputsWithConstantOnTheRight() {
3473 HInstruction* left = InputAt(0);
3474 HInstruction* right = InputAt(1);
3475 if (left->IsConstant() && !right->IsConstant()) {
3476 ReplaceInput(right, 0);
3477 ReplaceInput(left, 1);
3478 return true;
3479 }
3480 return false;
3481 }
3482
3483 // Order inputs by instruction id, but favor constant on the right side.
3484 // This helps GVN for commutative ops.
3485 void OrderInputs() {
3486 DCHECK(IsCommutative());
3487 HInstruction* left = InputAt(0);
3488 HInstruction* right = InputAt(1);
3489 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3490 return;
3491 }
3492 if (OrderInputsWithConstantOnTheRight()) {
3493 return;
3494 }
3495 // Order according to instruction id.
3496 if (left->GetId() > right->GetId()) {
3497 ReplaceInput(right, 0);
3498 ReplaceInput(left, 1);
3499 }
3500 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003501
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003502 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003503 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003504 return true;
3505 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003506
Roland Levillain31dd3d62016-02-16 12:21:02 +00003507 // Try to statically evaluate `this` and return a HConstant
3508 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003509 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003510 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003511
3512 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003513 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3514 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003515 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3516 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003517 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003518 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3519 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003520 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3521 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003522 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3523 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003524 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003525 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3526 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003527
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003528 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003529 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003530 HConstant* GetConstantRight() const;
3531
3532 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003533 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003534 HInstruction* GetLeastConstantLeft() const;
3535
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003536 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003537
Artem Serovcced8ba2017-07-19 18:18:09 +01003538 protected:
3539 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003540};
3541
Mark Mendellc4701932015-04-10 13:18:51 -04003542// The comparison bias applies for floating point operations and indicates how NaN
3543// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003544enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003545 kNoBias, // bias is not applicable (i.e. for long operation)
3546 kGtBias, // return 1 for NaN comparisons
3547 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003548 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003549};
3550
Roland Levillain31dd3d62016-02-16 12:21:02 +00003551std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3552
Dave Allison20dfc792014-06-16 20:44:29 -07003553class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003554 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303555 HCondition(InstructionKind kind,
3556 HInstruction* first,
3557 HInstruction* second,
3558 uint32_t dex_pc = kNoDexPc)
3559 : HBinaryOperation(kind,
3560 DataType::Type::kBool,
3561 first,
3562 second,
3563 SideEffects::None(),
3564 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003565 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3566 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003567
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003568 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003569 // `instruction`, and disregard moves in between.
3570 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003571
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003572 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003573
3574 virtual IfCondition GetCondition() const = 0;
3575
Mark Mendellc4701932015-04-10 13:18:51 -04003576 virtual IfCondition GetOppositeCondition() const = 0;
3577
Vladimir Markoa1de9182016-02-25 11:37:38 +00003578 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003579 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3580
Vladimir Markoa1de9182016-02-25 11:37:38 +00003581 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3582 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003583
Vladimir Marko372f10e2016-05-17 16:30:10 +01003584 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003585 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003586 }
3587
Roland Levillain4fa13f62015-07-06 18:11:54 +01003588 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003589 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003590 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003591 if (if_cond == kCondNE) {
3592 return true;
3593 } else if (if_cond == kCondEQ) {
3594 return false;
3595 }
3596 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003597 }
3598
3599 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003600 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003601 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003602 if (if_cond == kCondEQ) {
3603 return true;
3604 } else if (if_cond == kCondNE) {
3605 return false;
3606 }
3607 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003608 }
3609
Roland Levillain31dd3d62016-02-16 12:21:02 +00003610 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003611 // Needed if we merge a HCompare into a HCondition.
3612 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3613 static constexpr size_t kFieldComparisonBiasSize =
3614 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3615 static constexpr size_t kNumberOfConditionPackedBits =
3616 kFieldComparisonBias + kFieldComparisonBiasSize;
3617 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3618 using ComparisonBiasField =
3619 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3620
Roland Levillain31dd3d62016-02-16 12:21:02 +00003621 template <typename T>
3622 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3623
3624 template <typename T>
3625 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003626 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003627 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3628 // Handle the bias.
3629 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3630 }
3631
3632 // Return an integer constant containing the result of a condition evaluated at compile time.
3633 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3634 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3635 }
3636
Artem Serovcced8ba2017-07-19 18:18:09 +01003637 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003638};
3639
3640// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003641class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003642 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003643 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303644 : HCondition(kEqual, first, second, dex_pc) {
3645 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003646
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003647 bool IsCommutative() const OVERRIDE { return true; }
3648
Vladimir Marko9e23df52015-11-10 17:14:35 +00003649 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3650 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003651 return MakeConstantCondition(true, GetDexPc());
3652 }
3653 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3654 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3655 }
3656 // In the following Evaluate methods, a HCompare instruction has
3657 // been merged into this HEqual instruction; evaluate it as
3658 // `Compare(x, y) == 0`.
3659 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3660 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3661 GetDexPc());
3662 }
3663 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3664 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3665 }
3666 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3667 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003668 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003669
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003670 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003671
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003672 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003673 return kCondEQ;
3674 }
3675
Mark Mendellc4701932015-04-10 13:18:51 -04003676 IfCondition GetOppositeCondition() const OVERRIDE {
3677 return kCondNE;
3678 }
3679
Artem Serovcced8ba2017-07-19 18:18:09 +01003680 protected:
3681 DEFAULT_COPY_CONSTRUCTOR(Equal);
3682
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003683 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003684 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003685};
3686
Vladimir Markofcb503c2016-05-18 12:48:17 +01003687class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003688 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303689 HNotEqual(HInstruction* first, HInstruction* second,
3690 uint32_t dex_pc = kNoDexPc)
3691 : HCondition(kNotEqual, first, second, dex_pc) {
3692 }
Dave Allison20dfc792014-06-16 20:44:29 -07003693
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003694 bool IsCommutative() const OVERRIDE { return true; }
3695
Vladimir Marko9e23df52015-11-10 17:14:35 +00003696 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3697 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003698 return MakeConstantCondition(false, GetDexPc());
3699 }
3700 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3701 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3702 }
3703 // In the following Evaluate methods, a HCompare instruction has
3704 // been merged into this HNotEqual instruction; evaluate it as
3705 // `Compare(x, y) != 0`.
3706 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3707 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3708 }
3709 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3710 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3711 }
3712 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3713 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003714 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003715
Dave Allison20dfc792014-06-16 20:44:29 -07003716 DECLARE_INSTRUCTION(NotEqual);
3717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003718 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003719 return kCondNE;
3720 }
3721
Mark Mendellc4701932015-04-10 13:18:51 -04003722 IfCondition GetOppositeCondition() const OVERRIDE {
3723 return kCondEQ;
3724 }
3725
Artem Serovcced8ba2017-07-19 18:18:09 +01003726 protected:
3727 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3728
Dave Allison20dfc792014-06-16 20:44:29 -07003729 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003730 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003731};
3732
Vladimir Markofcb503c2016-05-18 12:48:17 +01003733class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003734 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303735 HLessThan(HInstruction* first, HInstruction* second,
3736 uint32_t dex_pc = kNoDexPc)
3737 : HCondition(kLessThan, first, second, dex_pc) {
3738 }
Dave Allison20dfc792014-06-16 20:44:29 -07003739
Roland Levillain9867bc72015-08-05 10:21:34 +01003740 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003741 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003742 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 // In the following Evaluate methods, a HCompare instruction has
3744 // been merged into this HLessThan instruction; evaluate it as
3745 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003746 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003747 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3748 }
3749 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3750 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3751 }
3752 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3753 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003754 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003755
Dave Allison20dfc792014-06-16 20:44:29 -07003756 DECLARE_INSTRUCTION(LessThan);
3757
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003758 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003759 return kCondLT;
3760 }
3761
Mark Mendellc4701932015-04-10 13:18:51 -04003762 IfCondition GetOppositeCondition() const OVERRIDE {
3763 return kCondGE;
3764 }
3765
Artem Serovcced8ba2017-07-19 18:18:09 +01003766 protected:
3767 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3768
Dave Allison20dfc792014-06-16 20:44:29 -07003769 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003770 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003771};
3772
Vladimir Markofcb503c2016-05-18 12:48:17 +01003773class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003774 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303775 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3776 uint32_t dex_pc = kNoDexPc)
3777 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3778 }
Dave Allison20dfc792014-06-16 20:44:29 -07003779
Roland Levillain9867bc72015-08-05 10:21:34 +01003780 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003781 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003782 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003783 // In the following Evaluate methods, a HCompare instruction has
3784 // been merged into this HLessThanOrEqual instruction; evaluate it as
3785 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003786 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003787 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3788 }
3789 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3790 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3791 }
3792 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3793 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003794 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003795
Dave Allison20dfc792014-06-16 20:44:29 -07003796 DECLARE_INSTRUCTION(LessThanOrEqual);
3797
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003798 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003799 return kCondLE;
3800 }
3801
Mark Mendellc4701932015-04-10 13:18:51 -04003802 IfCondition GetOppositeCondition() const OVERRIDE {
3803 return kCondGT;
3804 }
3805
Artem Serovcced8ba2017-07-19 18:18:09 +01003806 protected:
3807 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3808
Dave Allison20dfc792014-06-16 20:44:29 -07003809 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003810 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003811};
3812
Vladimir Markofcb503c2016-05-18 12:48:17 +01003813class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003815 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303816 : HCondition(kGreaterThan, first, second, dex_pc) {
3817 }
Dave Allison20dfc792014-06-16 20:44:29 -07003818
Roland Levillain9867bc72015-08-05 10:21:34 +01003819 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003820 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003821 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003822 // In the following Evaluate methods, a HCompare instruction has
3823 // been merged into this HGreaterThan instruction; evaluate it as
3824 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003825 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003826 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3827 }
3828 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3829 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3830 }
3831 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3832 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003833 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003834
Dave Allison20dfc792014-06-16 20:44:29 -07003835 DECLARE_INSTRUCTION(GreaterThan);
3836
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003837 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003838 return kCondGT;
3839 }
3840
Mark Mendellc4701932015-04-10 13:18:51 -04003841 IfCondition GetOppositeCondition() const OVERRIDE {
3842 return kCondLE;
3843 }
3844
Artem Serovcced8ba2017-07-19 18:18:09 +01003845 protected:
3846 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3847
Dave Allison20dfc792014-06-16 20:44:29 -07003848 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003849 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003850};
3851
Vladimir Markofcb503c2016-05-18 12:48:17 +01003852class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003853 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003854 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303855 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3856 }
Dave Allison20dfc792014-06-16 20:44:29 -07003857
Roland Levillain9867bc72015-08-05 10:21:34 +01003858 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003859 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003860 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 // In the following Evaluate methods, a HCompare instruction has
3862 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3863 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003864 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003865 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3866 }
3867 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3868 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3869 }
3870 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3871 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003872 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003873
Dave Allison20dfc792014-06-16 20:44:29 -07003874 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003876 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003877 return kCondGE;
3878 }
3879
Mark Mendellc4701932015-04-10 13:18:51 -04003880 IfCondition GetOppositeCondition() const OVERRIDE {
3881 return kCondLT;
3882 }
3883
Artem Serovcced8ba2017-07-19 18:18:09 +01003884 protected:
3885 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3886
Dave Allison20dfc792014-06-16 20:44:29 -07003887 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003888 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003889};
3890
Vladimir Markofcb503c2016-05-18 12:48:17 +01003891class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003892 public:
3893 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303894 : HCondition(kBelow, first, second, dex_pc) {
3895 }
Aart Bike9f37602015-10-09 11:15:55 -07003896
3897 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003898 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003899 }
3900 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003901 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3902 }
3903 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3904 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3905 LOG(FATAL) << DebugName() << " is not defined for float values";
3906 UNREACHABLE();
3907 }
3908 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3909 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3910 LOG(FATAL) << DebugName() << " is not defined for double values";
3911 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003912 }
3913
3914 DECLARE_INSTRUCTION(Below);
3915
3916 IfCondition GetCondition() const OVERRIDE {
3917 return kCondB;
3918 }
3919
3920 IfCondition GetOppositeCondition() const OVERRIDE {
3921 return kCondAE;
3922 }
3923
Artem Serovcced8ba2017-07-19 18:18:09 +01003924 protected:
3925 DEFAULT_COPY_CONSTRUCTOR(Below);
3926
Aart Bike9f37602015-10-09 11:15:55 -07003927 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003928 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003929 return MakeUnsigned(x) < MakeUnsigned(y);
3930 }
Aart Bike9f37602015-10-09 11:15:55 -07003931};
3932
Vladimir Markofcb503c2016-05-18 12:48:17 +01003933class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003934 public:
3935 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303936 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3937 }
Aart Bike9f37602015-10-09 11:15:55 -07003938
3939 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003940 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003941 }
3942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003943 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3944 }
3945 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3946 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3947 LOG(FATAL) << DebugName() << " is not defined for float values";
3948 UNREACHABLE();
3949 }
3950 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3951 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3952 LOG(FATAL) << DebugName() << " is not defined for double values";
3953 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003954 }
3955
3956 DECLARE_INSTRUCTION(BelowOrEqual);
3957
3958 IfCondition GetCondition() const OVERRIDE {
3959 return kCondBE;
3960 }
3961
3962 IfCondition GetOppositeCondition() const OVERRIDE {
3963 return kCondA;
3964 }
3965
Artem Serovcced8ba2017-07-19 18:18:09 +01003966 protected:
3967 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
3968
Aart Bike9f37602015-10-09 11:15:55 -07003969 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003970 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003971 return MakeUnsigned(x) <= MakeUnsigned(y);
3972 }
Aart Bike9f37602015-10-09 11:15:55 -07003973};
3974
Vladimir Markofcb503c2016-05-18 12:48:17 +01003975class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003976 public:
3977 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303978 : HCondition(kAbove, first, second, dex_pc) {
3979 }
Aart Bike9f37602015-10-09 11:15:55 -07003980
3981 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003982 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003983 }
3984 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003985 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3986 }
3987 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3988 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3989 LOG(FATAL) << DebugName() << " is not defined for float values";
3990 UNREACHABLE();
3991 }
3992 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3993 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3994 LOG(FATAL) << DebugName() << " is not defined for double values";
3995 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003996 }
3997
3998 DECLARE_INSTRUCTION(Above);
3999
4000 IfCondition GetCondition() const OVERRIDE {
4001 return kCondA;
4002 }
4003
4004 IfCondition GetOppositeCondition() const OVERRIDE {
4005 return kCondBE;
4006 }
4007
Artem Serovcced8ba2017-07-19 18:18:09 +01004008 protected:
4009 DEFAULT_COPY_CONSTRUCTOR(Above);
4010
Aart Bike9f37602015-10-09 11:15:55 -07004011 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004012 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004013 return MakeUnsigned(x) > MakeUnsigned(y);
4014 }
Aart Bike9f37602015-10-09 11:15:55 -07004015};
4016
Vladimir Markofcb503c2016-05-18 12:48:17 +01004017class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004018 public:
4019 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304020 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4021 }
Aart Bike9f37602015-10-09 11:15:55 -07004022
4023 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004024 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004025 }
4026 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004027 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4028 }
4029 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4030 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4031 LOG(FATAL) << DebugName() << " is not defined for float values";
4032 UNREACHABLE();
4033 }
4034 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4035 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4036 LOG(FATAL) << DebugName() << " is not defined for double values";
4037 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004038 }
4039
4040 DECLARE_INSTRUCTION(AboveOrEqual);
4041
4042 IfCondition GetCondition() const OVERRIDE {
4043 return kCondAE;
4044 }
4045
4046 IfCondition GetOppositeCondition() const OVERRIDE {
4047 return kCondB;
4048 }
4049
Artem Serovcced8ba2017-07-19 18:18:09 +01004050 protected:
4051 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4052
Aart Bike9f37602015-10-09 11:15:55 -07004053 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004054 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004055 return MakeUnsigned(x) >= MakeUnsigned(y);
4056 }
Aart Bike9f37602015-10-09 11:15:55 -07004057};
Dave Allison20dfc792014-06-16 20:44:29 -07004058
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004059// Instruction to check how two inputs compare to each other.
4060// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004061class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004062 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004063 // Note that `comparison_type` is the type of comparison performed
4064 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004065 // HCompare instruction (which is always DataType::Type::kInt).
4066 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004067 HInstruction* first,
4068 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004069 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004070 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304071 : HBinaryOperation(kCompare,
4072 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004073 first,
4074 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004075 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004076 dex_pc) {
4077 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004078 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4079 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004080 }
4081
Roland Levillain9867bc72015-08-05 10:21:34 +01004082 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004083 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4084
4085 template <typename T>
4086 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004087 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004088 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4089 // Handle the bias.
4090 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4091 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004092
Roland Levillain9867bc72015-08-05 10:21:34 +01004093 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004094 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4095 // reach this code path when processing a freshly built HIR
4096 // graph. However HCompare integer instructions can be synthesized
4097 // by the instruction simplifier to implement IntegerCompare and
4098 // IntegerSignum intrinsics, so we have to handle this case.
4099 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004100 }
4101 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004102 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4103 }
4104 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4105 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4106 }
4107 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4108 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004109 }
4110
Vladimir Marko372f10e2016-05-17 16:30:10 +01004111 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004112 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004113 }
4114
Vladimir Markoa1de9182016-02-25 11:37:38 +00004115 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004116
Roland Levillain31dd3d62016-02-16 12:21:02 +00004117 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004118 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004119 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004120 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004121 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004122 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004124 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004125 // Comparisons do not require a runtime call in any back end.
4126 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004128
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004129 DECLARE_INSTRUCTION(Compare);
4130
Roland Levillain31dd3d62016-02-16 12:21:02 +00004131 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004132 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
4133 static constexpr size_t kFieldComparisonBiasSize =
4134 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4135 static constexpr size_t kNumberOfComparePackedBits =
4136 kFieldComparisonBias + kFieldComparisonBiasSize;
4137 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4138 using ComparisonBiasField =
4139 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4140
Roland Levillain31dd3d62016-02-16 12:21:02 +00004141 // Return an integer constant containing the result of a comparison evaluated at compile time.
4142 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4143 DCHECK(value == -1 || value == 0 || value == 1) << value;
4144 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4145 }
4146
Artem Serovcced8ba2017-07-19 18:18:09 +01004147 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004148};
4149
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00004150class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004151 public:
4152 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004153 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004154 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004155 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004156 bool finalizable,
4157 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304158 : HExpression(kNewInstance,
4159 DataType::Type::kReference,
4160 SideEffects::CanTriggerGC(),
4161 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004162 type_index_(type_index),
4163 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004164 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004165 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004166 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004167 }
4168
Artem Serovcced8ba2017-07-19 18:18:09 +01004169 bool IsClonable() const OVERRIDE { return true; }
4170
Andreas Gampea5b09a62016-11-17 15:21:22 -08004171 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004172 const DexFile& GetDexFile() const { return dex_file_; }
4173
4174 // Calls runtime so needs an environment.
4175 bool NeedsEnvironment() const OVERRIDE { return true; }
4176
Mingyao Yang062157f2016-03-02 10:15:36 -08004177 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
4178 bool CanThrow() const OVERRIDE { return true; }
4179
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004180 bool NeedsChecks() const {
4181 return entrypoint_ == kQuickAllocObjectWithChecks;
4182 }
David Brazdil6de19382016-01-08 17:37:10 +00004183
Vladimir Markoa1de9182016-02-25 11:37:38 +00004184 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004185
4186 bool CanBeNull() const OVERRIDE { return false; }
4187
4188 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4189
4190 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4191 entrypoint_ = entrypoint;
4192 }
4193
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004194 HLoadClass* GetLoadClass() const {
4195 HInstruction* input = InputAt(0);
4196 if (input->IsClinitCheck()) {
4197 input = input->InputAt(0);
4198 }
4199 DCHECK(input->IsLoadClass());
4200 return input->AsLoadClass();
4201 }
4202
David Brazdil6de19382016-01-08 17:37:10 +00004203 bool IsStringAlloc() const;
4204
4205 DECLARE_INSTRUCTION(NewInstance);
4206
Artem Serovcced8ba2017-07-19 18:18:09 +01004207 protected:
4208 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4209
David Brazdil6de19382016-01-08 17:37:10 +00004210 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004211 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004212 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4213 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4214 "Too many packed fields.");
4215
Andreas Gampea5b09a62016-11-17 15:21:22 -08004216 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004217 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004218 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004219};
4220
Agi Csaki05f20562015-08-19 14:58:14 -07004221enum IntrinsicNeedsEnvironmentOrCache {
4222 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4223 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004224};
4225
Aart Bik5d75afe2015-12-14 11:57:01 -08004226enum IntrinsicSideEffects {
4227 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4228 kReadSideEffects, // Intrinsic may read heap memory.
4229 kWriteSideEffects, // Intrinsic may write heap memory.
4230 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4231};
4232
4233enum IntrinsicExceptions {
4234 kNoThrow, // Intrinsic does not throw any exceptions.
4235 kCanThrow // Intrinsic may throw exceptions.
4236};
4237
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004238class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004239 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004240 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004241
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004242 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004243 SetRawInputAt(index, argument);
4244 }
4245
Roland Levillain3e3d7332015-04-28 11:00:54 +01004246 // Return the number of arguments. This number can be lower than
4247 // the number of inputs returned by InputCount(), as some invoke
4248 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4249 // inputs at the end of their list of inputs.
4250 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4251
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004252 DataType::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004253
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004254 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4255
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004256 InvokeType GetInvokeType() const {
4257 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004258 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004259
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004260 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004261 return intrinsic_;
4262 }
4263
Aart Bik5d75afe2015-12-14 11:57:01 -08004264 void SetIntrinsic(Intrinsics intrinsic,
4265 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4266 IntrinsicSideEffects side_effects,
4267 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004268
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004269 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004270 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004271 }
4272
Aart Bikff7d89c2016-11-07 08:49:28 -08004273 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4274
Vladimir Markoa1de9182016-02-25 11:37:38 +00004275 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004276
Aart Bika8b8e9b2018-01-09 11:01:02 -08004277 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4278
4279 bool AlwaysThrows() const OVERRIDE { return GetPackedFlag<kFlagAlwaysThrows>(); }
4280
Aart Bik71bf7b42016-11-16 10:17:46 -08004281 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004282
Vladimir Marko372f10e2016-05-17 16:30:10 +01004283 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004284 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4285 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004286
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004287 uint32_t* GetIntrinsicOptimizations() {
4288 return &intrinsic_optimizations_;
4289 }
4290
4291 const uint32_t* GetIntrinsicOptimizations() const {
4292 return &intrinsic_optimizations_;
4293 }
4294
4295 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4296
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004297 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004298 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004299
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004300 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004301
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004302 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004303 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4304 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004305 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4306 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004307 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004308 static constexpr size_t kFieldReturnTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004309 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00004310 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004311 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4312 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004313 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004314 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004315 using ReturnTypeField = BitField<DataType::Type, kFieldReturnType, kFieldReturnTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004316
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304317 HInvoke(InstructionKind kind,
4318 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004319 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004320 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004321 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004322 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004323 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004324 ArtMethod* resolved_method,
4325 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004326 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304327 kind,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004328 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4329 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004330 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004331 number_of_arguments + number_of_other_inputs,
4332 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004333 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004334 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004335 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004336 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004337 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004338 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004339 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004340 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004341 }
4342
Artem Serovcced8ba2017-07-19 18:18:09 +01004343 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4344
Roland Levillain3e3d7332015-04-28 11:00:54 +01004345 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004346 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004347 const uint32_t dex_method_index_;
4348 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004349
4350 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4351 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004352};
4353
Vladimir Markofcb503c2016-05-18 12:48:17 +01004354class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004355 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004356 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004357 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004358 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004359 uint32_t dex_pc,
4360 uint32_t dex_method_index,
4361 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304362 : HInvoke(kInvokeUnresolved,
4363 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004364 number_of_arguments,
4365 0u /* number_of_other_inputs */,
4366 return_type,
4367 dex_pc,
4368 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004369 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004370 invoke_type) {
4371 }
4372
Artem Serovcced8ba2017-07-19 18:18:09 +01004373 bool IsClonable() const OVERRIDE { return true; }
4374
Calin Juravle175dc732015-08-25 15:42:32 +01004375 DECLARE_INSTRUCTION(InvokeUnresolved);
4376
Artem Serovcced8ba2017-07-19 18:18:09 +01004377 protected:
4378 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004379};
4380
Orion Hodsonac141392017-01-13 11:53:47 +00004381class HInvokePolymorphic FINAL : public HInvoke {
4382 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004383 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004384 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004385 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004386 uint32_t dex_pc,
4387 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304388 : HInvoke(kInvokePolymorphic,
4389 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004390 number_of_arguments,
4391 0u /* number_of_other_inputs */,
4392 return_type,
4393 dex_pc,
4394 dex_method_index,
4395 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304396 kVirtual) {
4397 }
Orion Hodsonac141392017-01-13 11:53:47 +00004398
Artem Serovcced8ba2017-07-19 18:18:09 +01004399 bool IsClonable() const OVERRIDE { return true; }
4400
Orion Hodsonac141392017-01-13 11:53:47 +00004401 DECLARE_INSTRUCTION(InvokePolymorphic);
4402
Artem Serovcced8ba2017-07-19 18:18:09 +01004403 protected:
4404 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004405};
4406
Vladimir Markofcb503c2016-05-18 12:48:17 +01004407class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004408 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004409 // Requirements of this method call regarding the class
4410 // initialization (clinit) check of its declaring class.
4411 enum class ClinitCheckRequirement {
4412 kNone, // Class already initialized.
4413 kExplicit, // Static call having explicit clinit check as last input.
4414 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004415 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004416 };
4417
Vladimir Marko58155012015-08-19 12:49:41 +00004418 // Determines how to load the target ArtMethod*.
4419 enum class MethodLoadKind {
4420 // Use a String init ArtMethod* loaded from Thread entrypoints.
4421 kStringInit,
4422
4423 // Use the method's own ArtMethod* loaded by the register allocator.
4424 kRecursive,
4425
Vladimir Marko65979462017-05-19 17:25:12 +01004426 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4427 // Used for boot image methods referenced by boot image code.
4428 kBootImageLinkTimePcRelative,
4429
Vladimir Marko58155012015-08-19 12:49:41 +00004430 // Use ArtMethod* at a known address, embed the direct address in the code.
4431 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4432 kDirectAddress,
4433
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004434 // Load from an entry in the .bss section using a PC-relative load.
4435 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4436 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004437
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004438 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4439 // used when other kinds are unimplemented on a particular architecture.
4440 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004441 };
4442
4443 // Determines the location of the code pointer.
4444 enum class CodePtrLocation {
4445 // Recursive call, use local PC-relative call instruction.
4446 kCallSelf,
4447
Vladimir Marko58155012015-08-19 12:49:41 +00004448 // Use code pointer from the ArtMethod*.
4449 // Used when we don't know the target code. This is also the last-resort-kind used when
4450 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4451 kCallArtMethod,
4452 };
4453
4454 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004455 MethodLoadKind method_load_kind;
4456 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004457 // The method load data holds
4458 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4459 // Note that there are multiple string init methods, each having its own offset.
4460 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004461 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004462 };
4463
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004464 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004465 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004466 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004467 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004468 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004469 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004470 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004471 InvokeType invoke_type,
4472 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004473 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304474 : HInvoke(kInvokeStaticOrDirect,
4475 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004476 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004477 // There is potentially one extra argument for the HCurrentMethod node, and
4478 // potentially one other if the clinit check is explicit, and potentially
4479 // one other if the method is a string factory.
4480 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004481 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004482 return_type,
4483 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004484 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004485 resolved_method,
4486 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004487 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004488 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004489 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4490 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004491
Artem Serovcced8ba2017-07-19 18:18:09 +01004492 bool IsClonable() const OVERRIDE { return true; }
4493
Vladimir Markodc151b22015-10-15 18:02:30 +01004494 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004495 bool had_current_method_input = HasCurrentMethodInput();
4496 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4497
4498 // Using the current method is the default and once we find a better
4499 // method load kind, we should not go back to using the current method.
4500 DCHECK(had_current_method_input || !needs_current_method_input);
4501
4502 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004503 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4504 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004505 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004506 dispatch_info_ = dispatch_info;
4507 }
4508
Aart Bik6daebeb2017-04-03 14:35:41 -07004509 DispatchInfo GetDispatchInfo() const {
4510 return dispatch_info_;
4511 }
4512
Vladimir Markoc53c0792015-11-19 15:48:33 +00004513 void AddSpecialInput(HInstruction* input) {
4514 // We allow only one special input.
4515 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4516 DCHECK(InputCount() == GetSpecialInputIndex() ||
4517 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4518 InsertInputAt(GetSpecialInputIndex(), input);
4519 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004520
Vladimir Marko372f10e2016-05-17 16:30:10 +01004521 using HInstruction::GetInputRecords; // Keep the const version visible.
4522 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4523 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4524 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4525 DCHECK(!input_records.empty());
4526 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4527 HInstruction* last_input = input_records.back().GetInstruction();
4528 // Note: `last_input` may be null during arguments setup.
4529 if (last_input != nullptr) {
4530 // `last_input` is the last input of a static invoke marked as having
4531 // an explicit clinit check. It must either be:
4532 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4533 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4534 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4535 }
4536 }
4537 return input_records;
4538 }
4539
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004540 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004541 // We access the method via the dex cache so we can't do an implicit null check.
4542 // TODO: for intrinsics we can generate implicit null checks.
4543 return false;
4544 }
4545
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004546 bool CanBeNull() const OVERRIDE {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004547 return GetPackedField<ReturnTypeField>() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004548 }
4549
Vladimir Markoc53c0792015-11-19 15:48:33 +00004550 // Get the index of the special input, if any.
4551 //
David Brazdil6de19382016-01-08 17:37:10 +00004552 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4553 // method pointer; otherwise there may be one platform-specific special input,
4554 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004555 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004556 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004557
Vladimir Marko58155012015-08-19 12:49:41 +00004558 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4559 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4560 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004561 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004562 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004563 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004564 bool HasPcRelativeMethodLoadKind() const {
4565 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004566 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004567 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004568 bool HasCurrentMethodInput() const {
4569 // This function can be called only after the invoke has been fully initialized by the builder.
4570 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004571 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004572 return true;
4573 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004574 DCHECK(InputCount() == GetSpecialInputIndex() ||
4575 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004576 return false;
4577 }
4578 }
Vladimir Marko58155012015-08-19 12:49:41 +00004579
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004580 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004581 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004582 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004583 }
4584
4585 uint64_t GetMethodAddress() const {
4586 DCHECK(HasMethodAddress());
4587 return dispatch_info_.method_load_data;
4588 }
4589
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004590 const DexFile& GetDexFileForPcRelativeDexCache() const;
4591
Vladimir Markoa1de9182016-02-25 11:37:38 +00004592 ClinitCheckRequirement GetClinitCheckRequirement() const {
4593 return GetPackedField<ClinitCheckRequirementField>();
4594 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004595
Roland Levillain4c0eb422015-04-24 16:43:49 +01004596 // Is this instruction a call to a static method?
4597 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004598 return GetInvokeType() == kStatic;
4599 }
4600
4601 MethodReference GetTargetMethod() const {
4602 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004603 }
4604
Vladimir Markofbb184a2015-11-13 14:47:00 +00004605 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4606 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4607 // instruction; only relevant for static calls with explicit clinit check.
4608 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004609 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004610 size_t last_input_index = inputs_.size() - 1u;
4611 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004612 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004613 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004614 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004615 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004616 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004617 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004618 }
4619
4620 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004621 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004622 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004623 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004624 }
4625
4626 // Is this a call to a static method whose declaring class has an
4627 // implicit intialization check requirement?
4628 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004629 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004630 }
4631
Vladimir Markob554b5a2015-11-06 12:57:55 +00004632 // Does this method load kind need the current method as an input?
4633 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004634 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004635 }
4636
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004637 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004638
Artem Serovcced8ba2017-07-19 18:18:09 +01004639 protected:
4640 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4641
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004642 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004643 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004644 static constexpr size_t kFieldClinitCheckRequirementSize =
4645 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4646 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4647 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4648 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4649 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004650 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4651 kFieldClinitCheckRequirement,
4652 kFieldClinitCheckRequirementSize>;
4653
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004654 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004655 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004656 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004657};
Vladimir Markof64242a2015-12-01 14:58:23 +00004658std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004659std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004660
Vladimir Markofcb503c2016-05-18 12:48:17 +01004661class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004662 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004663 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004664 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004665 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004666 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004667 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004668 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004669 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304670 : HInvoke(kInvokeVirtual,
4671 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004672 number_of_arguments,
4673 0u,
4674 return_type,
4675 dex_pc,
4676 dex_method_index,
4677 resolved_method,
4678 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304679 vtable_index_(vtable_index) {
4680 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004681
Artem Serovcced8ba2017-07-19 18:18:09 +01004682 bool IsClonable() const OVERRIDE { return true; }
4683
Aart Bik71bf7b42016-11-16 10:17:46 -08004684 bool CanBeNull() const OVERRIDE {
4685 switch (GetIntrinsic()) {
4686 case Intrinsics::kThreadCurrentThread:
4687 case Intrinsics::kStringBufferAppend:
4688 case Intrinsics::kStringBufferToString:
4689 case Intrinsics::kStringBuilderAppend:
4690 case Intrinsics::kStringBuilderToString:
4691 return false;
4692 default:
4693 return HInvoke::CanBeNull();
4694 }
4695 }
4696
Calin Juravle641547a2015-04-21 22:08:51 +01004697 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004698 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004699 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004700 }
4701
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004702 uint32_t GetVTableIndex() const { return vtable_index_; }
4703
4704 DECLARE_INSTRUCTION(InvokeVirtual);
4705
Artem Serovcced8ba2017-07-19 18:18:09 +01004706 protected:
4707 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4708
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004709 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004710 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004711 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004712};
4713
Vladimir Markofcb503c2016-05-18 12:48:17 +01004714class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004715 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004716 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004717 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004718 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004719 uint32_t dex_pc,
4720 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004721 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004722 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304723 : HInvoke(kInvokeInterface,
4724 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004725 number_of_arguments,
4726 0u,
4727 return_type,
4728 dex_pc,
4729 dex_method_index,
4730 resolved_method,
4731 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304732 imt_index_(imt_index) {
4733 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004734
Artem Serovcced8ba2017-07-19 18:18:09 +01004735 bool IsClonable() const OVERRIDE { return true; }
4736
Calin Juravle641547a2015-04-21 22:08:51 +01004737 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004738 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004739 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004740 }
4741
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004742 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4743 // The assembly stub currently needs it.
4744 return true;
4745 }
4746
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004747 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004748
4749 DECLARE_INSTRUCTION(InvokeInterface);
4750
Artem Serovcced8ba2017-07-19 18:18:09 +01004751 protected:
4752 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4753
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004754 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004755 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004756 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004757};
4758
Vladimir Markofcb503c2016-05-18 12:48:17 +01004759class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004760 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004761 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304762 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004763 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004764 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004765
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004766 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004767
4768 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004769 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004770 }
4771 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004772 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004773 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004774 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4775 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4776 }
4777 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4778 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4779 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004780
Roland Levillain88cb1752014-10-20 16:36:47 +01004781 DECLARE_INSTRUCTION(Neg);
4782
Artem Serovcced8ba2017-07-19 18:18:09 +01004783 protected:
4784 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004785};
4786
Vladimir Markofcb503c2016-05-18 12:48:17 +01004787class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004788 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004789 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304790 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004791 SetRawInputAt(0, cls);
4792 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004793 }
4794
Artem Serovcced8ba2017-07-19 18:18:09 +01004795 bool IsClonable() const OVERRIDE { return true; }
4796
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004797 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004798 bool NeedsEnvironment() const OVERRIDE { return true; }
4799
Mingyao Yang0c365e62015-03-31 15:09:29 -07004800 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4801 bool CanThrow() const OVERRIDE { return true; }
4802
Calin Juravle10e244f2015-01-26 18:54:32 +00004803 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004804
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004805 HLoadClass* GetLoadClass() const {
4806 DCHECK(InputAt(0)->IsLoadClass());
4807 return InputAt(0)->AsLoadClass();
4808 }
4809
4810 HInstruction* GetLength() const {
4811 return InputAt(1);
4812 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004813
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004814 DECLARE_INSTRUCTION(NewArray);
4815
Artem Serovcced8ba2017-07-19 18:18:09 +01004816 protected:
4817 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004818};
4819
Vladimir Markofcb503c2016-05-18 12:48:17 +01004820class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004821 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004822 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004823 HInstruction* left,
4824 HInstruction* right,
4825 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304826 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4827 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004828
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004829 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004830
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004831 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004832
4833 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004834 return GetBlock()->GetGraph()->GetIntConstant(
4835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004836 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004837 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004838 return GetBlock()->GetGraph()->GetLongConstant(
4839 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004840 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004841 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4842 return GetBlock()->GetGraph()->GetFloatConstant(
4843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4844 }
4845 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4846 return GetBlock()->GetGraph()->GetDoubleConstant(
4847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4848 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004849
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004850 DECLARE_INSTRUCTION(Add);
4851
Artem Serovcced8ba2017-07-19 18:18:09 +01004852 protected:
4853 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004854};
4855
Vladimir Markofcb503c2016-05-18 12:48:17 +01004856class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004857 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004858 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004859 HInstruction* left,
4860 HInstruction* right,
4861 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304862 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4863 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004864
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004865 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004866
4867 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004868 return GetBlock()->GetGraph()->GetIntConstant(
4869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004870 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004871 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004872 return GetBlock()->GetGraph()->GetLongConstant(
4873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004874 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004875 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4876 return GetBlock()->GetGraph()->GetFloatConstant(
4877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4878 }
4879 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4880 return GetBlock()->GetGraph()->GetDoubleConstant(
4881 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4882 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004883
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004884 DECLARE_INSTRUCTION(Sub);
4885
Artem Serovcced8ba2017-07-19 18:18:09 +01004886 protected:
4887 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004888};
4889
Vladimir Markofcb503c2016-05-18 12:48:17 +01004890class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004891 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004892 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004893 HInstruction* left,
4894 HInstruction* right,
4895 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304896 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4897 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004898
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004899 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004900
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004901 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004902
4903 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004904 return GetBlock()->GetGraph()->GetIntConstant(
4905 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004906 }
4907 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004908 return GetBlock()->GetGraph()->GetLongConstant(
4909 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004910 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004911 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4912 return GetBlock()->GetGraph()->GetFloatConstant(
4913 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4914 }
4915 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4916 return GetBlock()->GetGraph()->GetDoubleConstant(
4917 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4918 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004919
4920 DECLARE_INSTRUCTION(Mul);
4921
Artem Serovcced8ba2017-07-19 18:18:09 +01004922 protected:
4923 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004924};
4925
Vladimir Markofcb503c2016-05-18 12:48:17 +01004926class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004927 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004928 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 HInstruction* left,
4930 HInstruction* right,
4931 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304932 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4933 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004934
Roland Levillain9867bc72015-08-05 10:21:34 +01004935 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004936 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004937 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004938 // Our graph structure ensures we never have 0 for `y` during
4939 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004940 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004941 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004942 return (y == -1) ? -x : x / y;
4943 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004944
Roland Levillain31dd3d62016-02-16 12:21:02 +00004945 template <typename T>
4946 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004947 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004948 return x / y;
4949 }
4950
Roland Levillain9867bc72015-08-05 10:21:34 +01004951 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004952 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004953 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004954 }
4955 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004956 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004957 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4958 }
4959 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4960 return GetBlock()->GetGraph()->GetFloatConstant(
4961 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4962 }
4963 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4964 return GetBlock()->GetGraph()->GetDoubleConstant(
4965 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004966 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004967
4968 DECLARE_INSTRUCTION(Div);
4969
Artem Serovcced8ba2017-07-19 18:18:09 +01004970 protected:
4971 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004972};
4973
Vladimir Markofcb503c2016-05-18 12:48:17 +01004974class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004975 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004976 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004977 HInstruction* left,
4978 HInstruction* right,
4979 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304980 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
4981 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004982
Roland Levillain9867bc72015-08-05 10:21:34 +01004983 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004984 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004985 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004986 // Our graph structure ensures we never have 0 for `y` during
4987 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004988 DCHECK_NE(y, 0);
4989 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4990 return (y == -1) ? 0 : x % y;
4991 }
4992
Roland Levillain31dd3d62016-02-16 12:21:02 +00004993 template <typename T>
4994 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004995 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004996 return std::fmod(x, y);
4997 }
4998
Roland Levillain9867bc72015-08-05 10:21:34 +01004999 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005000 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005001 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005002 }
5003 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005004 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005005 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005006 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005007 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
5008 return GetBlock()->GetGraph()->GetFloatConstant(
5009 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5010 }
5011 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
5012 return GetBlock()->GetGraph()->GetDoubleConstant(
5013 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5014 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005015
5016 DECLARE_INSTRUCTION(Rem);
5017
Artem Serovcced8ba2017-07-19 18:18:09 +01005018 protected:
5019 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005020};
5021
Aart Bik1f8d51b2018-02-15 10:42:37 -08005022class HMin FINAL : public HBinaryOperation {
5023 public:
5024 HMin(DataType::Type result_type,
5025 HInstruction* left,
5026 HInstruction* right,
5027 uint32_t dex_pc)
5028 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5029
5030 bool IsCommutative() const OVERRIDE { return true; }
5031
5032 // Evaluation for integral values.
5033 template <typename T> static T ComputeIntegral(T x, T y) {
5034 return (x <= y) ? x : y;
5035 }
5036
5037 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5038 return GetBlock()->GetGraph()->GetIntConstant(
5039 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5040 }
5041 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5042 return GetBlock()->GetGraph()->GetLongConstant(
5043 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5044 }
5045 // TODO: Evaluation for floating-point values.
5046 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5047 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5048 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5049 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5050
5051 DECLARE_INSTRUCTION(Min);
5052
5053 protected:
5054 DEFAULT_COPY_CONSTRUCTOR(Min);
5055};
5056
5057class HMax FINAL : public HBinaryOperation {
5058 public:
5059 HMax(DataType::Type result_type,
5060 HInstruction* left,
5061 HInstruction* right,
5062 uint32_t dex_pc)
5063 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5064
5065 bool IsCommutative() const OVERRIDE { return true; }
5066
5067 // Evaluation for integral values.
5068 template <typename T> static T ComputeIntegral(T x, T y) {
5069 return (x >= y) ? x : y;
5070 }
5071
5072 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
5073 return GetBlock()->GetGraph()->GetIntConstant(
5074 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5075 }
5076 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
5077 return GetBlock()->GetGraph()->GetLongConstant(
5078 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5079 }
5080 // TODO: Evaluation for floating-point values.
5081 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5082 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5083 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5084 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE { return nullptr; }
5085
5086 DECLARE_INSTRUCTION(Max);
5087
5088 protected:
5089 DEFAULT_COPY_CONSTRUCTOR(Max);
5090};
5091
Aart Bik3dad3412018-02-28 12:01:46 -08005092class HAbs FINAL : public HUnaryOperation {
5093 public:
5094 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5095 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5096
5097 // Evaluation for integral values.
5098 template <typename T> static T ComputeIntegral(T x) {
5099 return x < 0 ? -x : x;
5100 }
5101
5102 // Evaluation for floating-point values.
5103 // Note, as a "quality of implementation", rather than pure "spec compliance",
5104 // we require that Math.abs() clears the sign bit (but changes nothing else)
5105 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5106 // http://b/30758343
5107 template <typename T, typename S> static T ComputeFP(T x) {
5108 S bits = bit_cast<S, T>(x);
5109 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5110 }
5111
5112 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
5113 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5114 }
5115 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
5116 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5117 }
5118 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
5119 return GetBlock()->GetGraph()->GetFloatConstant(
5120 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5121 }
5122 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
5123 return GetBlock()->GetGraph()->GetDoubleConstant(
5124 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5125 }
5126
5127 DECLARE_INSTRUCTION(Abs);
5128
5129 protected:
5130 DEFAULT_COPY_CONSTRUCTOR(Abs);
5131};
5132
Vladimir Markofcb503c2016-05-18 12:48:17 +01005133class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005134 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005135 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
5136 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00005137 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305138 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005139 SetRawInputAt(0, value);
5140 }
5141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005142 DataType::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
Serguei Katkov8c0676c2015-08-03 13:55:33 +06005143
Calin Juravled0d48522014-11-04 16:40:20 +00005144 bool CanBeMoved() const OVERRIDE { return true; }
5145
Vladimir Marko372f10e2016-05-17 16:30:10 +01005146 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00005147 return true;
5148 }
5149
5150 bool NeedsEnvironment() const OVERRIDE { return true; }
5151 bool CanThrow() const OVERRIDE { return true; }
5152
Calin Juravled0d48522014-11-04 16:40:20 +00005153 DECLARE_INSTRUCTION(DivZeroCheck);
5154
Artem Serovcced8ba2017-07-19 18:18:09 +01005155 protected:
5156 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005157};
5158
Vladimir Markofcb503c2016-05-18 12:48:17 +01005159class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005160 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005161 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005162 HInstruction* value,
5163 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005164 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305165 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005166 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5167 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005168 }
5169
Roland Levillain5b5b9312016-03-22 14:57:31 +00005170 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005171 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005172 return value << (distance & max_shift_distance);
5173 }
5174
5175 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005176 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005177 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005178 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005179 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005180 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005181 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005182 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005183 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5184 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5185 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5186 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005187 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005188 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5189 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005190 LOG(FATAL) << DebugName() << " is not defined for float values";
5191 UNREACHABLE();
5192 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005193 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5194 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005195 LOG(FATAL) << DebugName() << " is not defined for double values";
5196 UNREACHABLE();
5197 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005198
5199 DECLARE_INSTRUCTION(Shl);
5200
Artem Serovcced8ba2017-07-19 18:18:09 +01005201 protected:
5202 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005203};
5204
Vladimir Markofcb503c2016-05-18 12:48:17 +01005205class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005206 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005207 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005208 HInstruction* value,
5209 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005210 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305211 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005212 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5213 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005214 }
5215
Roland Levillain5b5b9312016-03-22 14:57:31 +00005216 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005217 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005218 return value >> (distance & max_shift_distance);
5219 }
5220
5221 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005222 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005223 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005224 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005225 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005226 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005227 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005228 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005229 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5230 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5231 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5232 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005233 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005234 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5235 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005236 LOG(FATAL) << DebugName() << " is not defined for float values";
5237 UNREACHABLE();
5238 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005239 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5240 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005241 LOG(FATAL) << DebugName() << " is not defined for double values";
5242 UNREACHABLE();
5243 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005244
5245 DECLARE_INSTRUCTION(Shr);
5246
Artem Serovcced8ba2017-07-19 18:18:09 +01005247 protected:
5248 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005249};
5250
Vladimir Markofcb503c2016-05-18 12:48:17 +01005251class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005252 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005253 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005254 HInstruction* value,
5255 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005256 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305257 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005258 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5259 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005260 }
5261
Roland Levillain5b5b9312016-03-22 14:57:31 +00005262 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005263 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005264 typedef typename std::make_unsigned<T>::type V;
5265 V ux = static_cast<V>(value);
5266 return static_cast<T>(ux >> (distance & max_shift_distance));
5267 }
5268
5269 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005270 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005271 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005272 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005273 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01005274 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005275 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005276 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005277 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5278 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5279 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5280 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005281 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005282 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5283 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005284 LOG(FATAL) << DebugName() << " is not defined for float values";
5285 UNREACHABLE();
5286 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005287 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5288 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005289 LOG(FATAL) << DebugName() << " is not defined for double values";
5290 UNREACHABLE();
5291 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005292
5293 DECLARE_INSTRUCTION(UShr);
5294
Artem Serovcced8ba2017-07-19 18:18:09 +01005295 protected:
5296 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005297};
5298
Vladimir Markofcb503c2016-05-18 12:48:17 +01005299class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005300 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005301 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005302 HInstruction* left,
5303 HInstruction* right,
5304 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305305 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5306 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005307
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005308 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005309
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005310 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005311
5312 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005313 return GetBlock()->GetGraph()->GetIntConstant(
5314 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005315 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005316 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005317 return GetBlock()->GetGraph()->GetLongConstant(
5318 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005319 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005320 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5321 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5322 LOG(FATAL) << DebugName() << " is not defined for float values";
5323 UNREACHABLE();
5324 }
5325 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5326 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5327 LOG(FATAL) << DebugName() << " is not defined for double values";
5328 UNREACHABLE();
5329 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005330
5331 DECLARE_INSTRUCTION(And);
5332
Artem Serovcced8ba2017-07-19 18:18:09 +01005333 protected:
5334 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005335};
5336
Vladimir Markofcb503c2016-05-18 12:48:17 +01005337class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005338 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005339 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005340 HInstruction* left,
5341 HInstruction* right,
5342 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305343 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5344 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005345
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005346 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005347
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005348 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005349
5350 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005351 return GetBlock()->GetGraph()->GetIntConstant(
5352 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005353 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005354 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005355 return GetBlock()->GetGraph()->GetLongConstant(
5356 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005357 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005358 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5359 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5360 LOG(FATAL) << DebugName() << " is not defined for float values";
5361 UNREACHABLE();
5362 }
5363 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5364 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5365 LOG(FATAL) << DebugName() << " is not defined for double values";
5366 UNREACHABLE();
5367 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005368
5369 DECLARE_INSTRUCTION(Or);
5370
Artem Serovcced8ba2017-07-19 18:18:09 +01005371 protected:
5372 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005373};
5374
Vladimir Markofcb503c2016-05-18 12:48:17 +01005375class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005376 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005377 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005378 HInstruction* left,
5379 HInstruction* right,
5380 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305381 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5382 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005383
Mingyao Yangdc5ac732015-02-25 11:28:05 -08005384 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005385
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005386 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005387
5388 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005389 return GetBlock()->GetGraph()->GetIntConstant(
5390 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005391 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005392 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005393 return GetBlock()->GetGraph()->GetLongConstant(
5394 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005395 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005396 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5397 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5398 LOG(FATAL) << DebugName() << " is not defined for float values";
5399 UNREACHABLE();
5400 }
5401 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5402 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5403 LOG(FATAL) << DebugName() << " is not defined for double values";
5404 UNREACHABLE();
5405 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005406
5407 DECLARE_INSTRUCTION(Xor);
5408
Artem Serovcced8ba2017-07-19 18:18:09 +01005409 protected:
5410 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005411};
5412
Vladimir Markofcb503c2016-05-18 12:48:17 +01005413class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005414 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005415 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305416 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005417 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5418 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005419 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005420
Roland Levillain5b5b9312016-03-22 14:57:31 +00005421 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005422 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005423 typedef typename std::make_unsigned<T>::type V;
5424 V ux = static_cast<V>(value);
5425 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005426 return static_cast<T>(ux);
5427 } else {
5428 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005429 return static_cast<T>(ux >> (distance & max_shift_value)) |
5430 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005431 }
5432 }
5433
Roland Levillain5b5b9312016-03-22 14:57:31 +00005434 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005435 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005436 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005437 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005438 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005439 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005440 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005441 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005442 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5443 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5444 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5445 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005446 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005447 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5448 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005449 LOG(FATAL) << DebugName() << " is not defined for float values";
5450 UNREACHABLE();
5451 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005452 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5453 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005454 LOG(FATAL) << DebugName() << " is not defined for double values";
5455 UNREACHABLE();
5456 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005457
5458 DECLARE_INSTRUCTION(Ror);
5459
Artem Serovcced8ba2017-07-19 18:18:09 +01005460 protected:
5461 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005462};
5463
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005464// The value of a parameter in this method. Its location depends on
5465// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005466class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005467 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005468 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005469 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005470 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005471 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005472 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305473 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005474 dex_file_(dex_file),
5475 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005476 index_(index) {
5477 SetPackedFlag<kFlagIsThis>(is_this);
5478 SetPackedFlag<kFlagCanBeNull>(!is_this);
5479 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005480
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005481 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005482 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005483 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005484 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005485
Vladimir Markoa1de9182016-02-25 11:37:38 +00005486 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5487 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005488
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005489 DECLARE_INSTRUCTION(ParameterValue);
5490
Artem Serovcced8ba2017-07-19 18:18:09 +01005491 protected:
5492 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5493
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005494 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005495 // Whether or not the parameter value corresponds to 'this' argument.
5496 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5497 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5498 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5499 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5500 "Too many packed fields.");
5501
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005502 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005503 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005504 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005505 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005506 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005507};
5508
Vladimir Markofcb503c2016-05-18 12:48:17 +01005509class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005510 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305512 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5513 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005514
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005515 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005516 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005517 return true;
5518 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005519
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005520 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005521
5522 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005523 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005524 }
5525 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005526 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005527 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005528 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5529 LOG(FATAL) << DebugName() << " is not defined for float values";
5530 UNREACHABLE();
5531 }
5532 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5533 LOG(FATAL) << DebugName() << " is not defined for double values";
5534 UNREACHABLE();
5535 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005536
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005537 DECLARE_INSTRUCTION(Not);
5538
Artem Serovcced8ba2017-07-19 18:18:09 +01005539 protected:
5540 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005541};
5542
Vladimir Markofcb503c2016-05-18 12:48:17 +01005543class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005545 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305546 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5547 }
David Brazdil66d126e2015-04-03 16:02:44 +01005548
5549 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005550 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005551 return true;
5552 }
5553
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005554 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005555 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005556 return !x;
5557 }
5558
Roland Levillain9867bc72015-08-05 10:21:34 +01005559 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005560 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005561 }
5562 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5563 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005564 UNREACHABLE();
5565 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005566 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5567 LOG(FATAL) << DebugName() << " is not defined for float values";
5568 UNREACHABLE();
5569 }
5570 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5571 LOG(FATAL) << DebugName() << " is not defined for double values";
5572 UNREACHABLE();
5573 }
David Brazdil66d126e2015-04-03 16:02:44 +01005574
5575 DECLARE_INSTRUCTION(BooleanNot);
5576
Artem Serovcced8ba2017-07-19 18:18:09 +01005577 protected:
5578 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005579};
5580
Vladimir Markofcb503c2016-05-18 12:48:17 +01005581class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005582 public:
5583 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005584 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305585 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005586 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005587 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005588 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005589 }
5590
5591 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5593 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005594
5595 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005596 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5597 return true;
5598 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005599
Mark Mendelle82549b2015-05-06 10:55:34 -04005600 // Try to statically evaluate the conversion and return a HConstant
5601 // containing the result. If the input cannot be converted, return nullptr.
5602 HConstant* TryStaticEvaluation() const;
5603
Roland Levillaindff1f282014-11-05 14:15:05 +00005604 DECLARE_INSTRUCTION(TypeConversion);
5605
Artem Serovcced8ba2017-07-19 18:18:09 +01005606 protected:
5607 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005608};
5609
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005610static constexpr uint32_t kNoRegNumber = -1;
5611
Vladimir Markofcb503c2016-05-18 12:48:17 +01005612class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005613 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005614 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5615 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005616 HNullCheck(HInstruction* value, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305617 : HExpression(kNullCheck, value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005618 SetRawInputAt(0, value);
5619 }
5620
Artem Serovcced8ba2017-07-19 18:18:09 +01005621 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005622 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005623 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005624 return true;
5625 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005626
Calin Juravle10e244f2015-01-26 18:54:32 +00005627 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005628
Calin Juravle10e244f2015-01-26 18:54:32 +00005629 bool CanThrow() const OVERRIDE { return true; }
5630
5631 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005632
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005633 DECLARE_INSTRUCTION(NullCheck);
5634
Artem Serovcced8ba2017-07-19 18:18:09 +01005635 protected:
5636 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005637};
5638
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005639// Embeds an ArtField and all the information required by the compiler. We cache
5640// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005641class FieldInfo : public ValueObject {
5642 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005643 FieldInfo(ArtField* field,
5644 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005645 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005646 bool is_volatile,
5647 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005648 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005649 const DexFile& dex_file)
5650 : field_(field),
5651 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005652 field_type_(field_type),
5653 is_volatile_(is_volatile),
5654 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005655 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005656 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005657
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005658 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005659 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005660 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005661 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005662 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005663 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005664 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005665
5666 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005667 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005668 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005669 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005670 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005671 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005672 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005673 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005674};
5675
Vladimir Markofcb503c2016-05-18 12:48:17 +01005676class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005677 public:
5678 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005679 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005681 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005682 bool is_volatile,
5683 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005684 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005685 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005686 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305687 : HExpression(kInstanceFieldGet,
5688 field_type,
5689 SideEffects::FieldReadOfType(field_type, is_volatile),
5690 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005691 field_info_(field,
5692 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005693 field_type,
5694 is_volatile,
5695 field_idx,
5696 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005697 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005698 SetRawInputAt(0, value);
5699 }
5700
Artem Serovcced8ba2017-07-19 18:18:09 +01005701 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005702 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005703
Vladimir Marko372f10e2016-05-17 16:30:10 +01005704 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5705 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005706 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005707 }
5708
Calin Juravle641547a2015-04-21 22:08:51 +01005709 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005710 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005711 }
5712
5713 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005714 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5715 }
5716
Calin Juravle52c48962014-12-16 17:02:57 +00005717 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005718 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005719 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005720 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005721
Vladimir Marko61b92282017-10-11 13:23:17 +01005722 void SetType(DataType::Type new_type) {
5723 DCHECK(DataType::IsIntegralType(GetType()));
5724 DCHECK(DataType::IsIntegralType(new_type));
5725 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5726 SetPackedField<TypeField>(new_type);
5727 }
5728
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005729 DECLARE_INSTRUCTION(InstanceFieldGet);
5730
Artem Serovcced8ba2017-07-19 18:18:09 +01005731 protected:
5732 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5733
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005734 private:
5735 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005736};
5737
Vladimir Markofcb503c2016-05-18 12:48:17 +01005738class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005739 public:
5740 HInstanceFieldSet(HInstruction* object,
5741 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005742 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005743 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005744 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005745 bool is_volatile,
5746 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005747 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005748 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005749 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305750 : HTemplateInstruction(kInstanceFieldSet,
5751 SideEffects::FieldWriteOfType(field_type, is_volatile),
5752 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005753 field_info_(field,
5754 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005755 field_type,
5756 is_volatile,
5757 field_idx,
5758 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005759 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005760 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005761 SetRawInputAt(0, object);
5762 SetRawInputAt(1, value);
5763 }
5764
Artem Serovcced8ba2017-07-19 18:18:09 +01005765 bool IsClonable() const OVERRIDE { return true; }
5766
Calin Juravle641547a2015-04-21 22:08:51 +01005767 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005768 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005769 }
5770
Calin Juravle52c48962014-12-16 17:02:57 +00005771 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005772 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005774 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005775 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005776 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5777 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005778
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005779 DECLARE_INSTRUCTION(InstanceFieldSet);
5780
Artem Serovcced8ba2017-07-19 18:18:09 +01005781 protected:
5782 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5783
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005784 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005785 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5786 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5787 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5788 "Too many packed fields.");
5789
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005790 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005791};
5792
Vladimir Markofcb503c2016-05-18 12:48:17 +01005793class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005794 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005795 HArrayGet(HInstruction* array,
5796 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005797 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005798 uint32_t dex_pc)
5799 : HArrayGet(array,
5800 index,
5801 type,
5802 SideEffects::ArrayReadOfType(type),
5803 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305804 /* is_string_char_at */ false) {
5805 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005806
5807 HArrayGet(HInstruction* array,
5808 HInstruction* index,
5809 DataType::Type type,
5810 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005811 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005812 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305813 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005814 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005815 SetRawInputAt(0, array);
5816 SetRawInputAt(1, index);
5817 }
5818
Artem Serovcced8ba2017-07-19 18:18:09 +01005819 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005820 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005821 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005822 return true;
5823 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005824 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005825 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005826 // Currently, unless the array is the result of NewArray, the array access is always
5827 // preceded by some form of null NullCheck necessary for the bounds check, usually
5828 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5829 // dynamic BCE. There are cases when these could be removed to produce better code.
5830 // If we ever add optimizations to do so we should allow an implicit check here
5831 // (as long as the address falls in the first page).
5832 //
5833 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5834 // a = cond ? new int[1] : null;
5835 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005836 return false;
5837 }
5838
David Brazdil4833f5a2015-12-16 10:37:39 +00005839 bool IsEquivalentOf(HArrayGet* other) const {
5840 bool result = (GetDexPc() == other->GetDexPc());
5841 if (kIsDebugBuild && result) {
5842 DCHECK_EQ(GetBlock(), other->GetBlock());
5843 DCHECK_EQ(GetArray(), other->GetArray());
5844 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005845 if (DataType::IsIntOrLongType(GetType())) {
5846 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005847 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005848 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5849 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005850 }
5851 }
5852 return result;
5853 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005854
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005855 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5856
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005857 HInstruction* GetArray() const { return InputAt(0); }
5858 HInstruction* GetIndex() const { return InputAt(1); }
5859
Vladimir Marko61b92282017-10-11 13:23:17 +01005860 void SetType(DataType::Type new_type) {
5861 DCHECK(DataType::IsIntegralType(GetType()));
5862 DCHECK(DataType::IsIntegralType(new_type));
5863 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5864 SetPackedField<TypeField>(new_type);
5865 }
5866
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005867 DECLARE_INSTRUCTION(ArrayGet);
5868
Artem Serovcced8ba2017-07-19 18:18:09 +01005869 protected:
5870 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5871
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005872 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005873 // We treat a String as an array, creating the HArrayGet from String.charAt()
5874 // intrinsic in the instruction simplifier. We can always determine whether
5875 // a particular HArrayGet is actually a String.charAt() by looking at the type
5876 // of the input but that requires holding the mutator lock, so we prefer to use
5877 // a flag, so that code generators don't need to do the locking.
5878 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5879 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5880 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5881 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005882};
5883
Vladimir Markofcb503c2016-05-18 12:48:17 +01005884class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005885 public:
5886 HArraySet(HInstruction* array,
5887 HInstruction* index,
5888 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005889 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005890 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005891 : HArraySet(array,
5892 index,
5893 value,
5894 expected_component_type,
5895 // Make a best guess for side effects now, may be refined during SSA building.
5896 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305897 dex_pc) {
5898 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005899
5900 HArraySet(HInstruction* array,
5901 HInstruction* index,
5902 HInstruction* value,
5903 DataType::Type expected_component_type,
5904 SideEffects side_effects,
5905 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305906 : HTemplateInstruction(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005907 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005909 SetPackedFlag<kFlagValueCanBeNull>(true);
5910 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005911 SetRawInputAt(0, array);
5912 SetRawInputAt(1, index);
5913 SetRawInputAt(2, value);
5914 }
5915
Artem Serovcced8ba2017-07-19 18:18:09 +01005916 bool IsClonable() const OVERRIDE { return true; }
5917
Calin Juravle77520bc2015-01-12 18:45:46 +00005918 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005919 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005920 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005921 }
5922
Mingyao Yang81014cb2015-06-02 03:16:27 -07005923 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005924 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005925
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005926 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005927 // TODO: Same as for ArrayGet.
5928 return false;
5929 }
5930
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005931 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005932 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005933 }
5934
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005935 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005936 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005937 }
5938
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005939 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005940 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005941 }
5942
Vladimir Markoa1de9182016-02-25 11:37:38 +00005943 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5944 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5945 bool StaticTypeOfArrayIsObjectArray() const {
5946 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5947 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005948
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005949 HInstruction* GetArray() const { return InputAt(0); }
5950 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005951 HInstruction* GetValue() const { return InputAt(2); }
5952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005954 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
5955 }
5956
5957 static DataType::Type GetComponentType(DataType::Type value_type,
5958 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005959 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005960 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005961 // be correct, we also check what is the value type. If it is a floating
5962 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005963 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005964 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005965 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005966 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005967
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005968 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005970 }
5971
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005972 static SideEffects ComputeSideEffects(DataType::Type type) {
5973 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07005974 }
5975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
5977 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
5978 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005979 }
5980
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005981 DECLARE_INSTRUCTION(ArraySet);
5982
Artem Serovcced8ba2017-07-19 18:18:09 +01005983 protected:
5984 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
5985
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005986 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005987 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5988 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005989 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00005990 static constexpr size_t kFlagNeedsTypeCheck =
5991 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5992 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005993 // Cached information for the reference_type_info_ so that codegen
5994 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005995 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5996 static constexpr size_t kNumberOfArraySetPackedBits =
5997 kFlagStaticTypeOfArrayIsObjectArray + 1;
5998 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5999 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006000 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001};
6002
Vladimir Markofcb503c2016-05-18 12:48:17 +01006003class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006004 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006005 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306006 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006007 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006008 // Note that arrays do not change length, so the instruction does not
6009 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006010 SetRawInputAt(0, array);
6011 }
6012
Artem Serovcced8ba2017-07-19 18:18:09 +01006013 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle77520bc2015-01-12 18:45:46 +00006014 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006015 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006016 return true;
6017 }
Calin Juravle641547a2015-04-21 22:08:51 +01006018 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
6019 return obj == InputAt(0);
6020 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006021
Vladimir Markodce016e2016-04-28 13:10:02 +01006022 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6023
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006024 DECLARE_INSTRUCTION(ArrayLength);
6025
Artem Serovcced8ba2017-07-19 18:18:09 +01006026 protected:
6027 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6028
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006029 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006030 // We treat a String as an array, creating the HArrayLength from String.length()
6031 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6032 // determine whether a particular HArrayLength is actually a String.length() by
6033 // looking at the type of the input but that requires holding the mutator lock, so
6034 // we prefer to use a flag, so that code generators don't need to do the locking.
6035 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
6036 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6037 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6038 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006039};
6040
Vladimir Markofcb503c2016-05-18 12:48:17 +01006041class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006042 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006043 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
6044 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006045 HBoundsCheck(HInstruction* index,
6046 HInstruction* length,
6047 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006048 bool is_string_char_at = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306049 : HExpression(kBoundsCheck, index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006050 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006051 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006052 SetRawInputAt(0, index);
6053 SetRawInputAt(1, length);
6054 }
6055
Artem Serovcced8ba2017-07-19 18:18:09 +01006056 bool IsClonable() const OVERRIDE { return true; }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006057 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006058 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006059 return true;
6060 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006061
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006062 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006063
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006064 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006065
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006066 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006067
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006068 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006069
6070 DECLARE_INSTRUCTION(BoundsCheck);
6071
Artem Serovcced8ba2017-07-19 18:18:09 +01006072 protected:
6073 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6074
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006075 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006076 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006077};
6078
Vladimir Markofcb503c2016-05-18 12:48:17 +01006079class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006080 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006081 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306082 : HTemplateInstruction(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
6083 slow_path_(nullptr) {
6084 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006085
Artem Serovcced8ba2017-07-19 18:18:09 +01006086 bool IsClonable() const OVERRIDE { return true; }
6087
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006088 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006089 return true;
6090 }
6091
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006092 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6093 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006094
6095 DECLARE_INSTRUCTION(SuspendCheck);
6096
Artem Serovcced8ba2017-07-19 18:18:09 +01006097 protected:
6098 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6099
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006100 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006101 // Only used for code generation, in order to share the same slow path between back edges
6102 // of a same loop.
6103 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006104};
6105
David Srbecky0cf44932015-12-09 14:09:59 +00006106// Pseudo-instruction which provides the native debugger with mapping information.
6107// It ensures that we can generate line number and local variables at this point.
6108class HNativeDebugInfo : public HTemplateInstruction<0> {
6109 public:
6110 explicit HNativeDebugInfo(uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306111 : HTemplateInstruction<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
6112 }
David Srbecky0cf44932015-12-09 14:09:59 +00006113
6114 bool NeedsEnvironment() const OVERRIDE {
6115 return true;
6116 }
6117
6118 DECLARE_INSTRUCTION(NativeDebugInfo);
6119
Artem Serovcced8ba2017-07-19 18:18:09 +01006120 protected:
6121 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006122};
6123
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006124/**
6125 * Instruction to load a Class object.
6126 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006127class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006128 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006129 // Determines how to load the Class.
6130 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006131 // We cannot load this class. See HSharpening::SharpenLoadClass.
6132 kInvalid = -1,
6133
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006134 // Use the Class* from the method's own ArtMethod*.
6135 kReferrersClass,
6136
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006137 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006138 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006139 kBootImageLinkTimePcRelative,
6140
6141 // Use a known boot image Class* address, embedded in the code by the codegen.
6142 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006143 kBootImageAddress,
6144
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006145 // Use a PC-relative load from a boot image ClassTable mmapped into the .bss
6146 // of the oat file.
6147 kBootImageClassTable,
6148
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006149 // Load from an entry in the .bss section using a PC-relative load.
6150 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
6151 kBssEntry,
6152
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006153 // Load from the root table associated with the JIT compiled method.
6154 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006155
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006156 // Load using a simple runtime call. This is the fall-back load kind when
6157 // the codegen is unable to use another appropriate kind.
6158 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006159
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006160 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006161 };
6162
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006163 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006164 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006165 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006166 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006167 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006168 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006169 bool needs_access_check)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306170 : HInstruction(kLoadClass, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006171 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006172 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006173 dex_file_(dex_file),
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006174 klass_(klass),
6175 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006176 // Referrers class should not need access check. We never inline unverified
6177 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006178 DCHECK(!is_referrers_class || !needs_access_check);
6179
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006180 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006181 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006182 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006183 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006184 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006185 }
6186
Artem Serovcced8ba2017-07-19 18:18:09 +01006187 bool IsClonable() const OVERRIDE { return true; }
6188
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006189 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006190
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006191 LoadKind GetLoadKind() const {
6192 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006193 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006194
6195 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006196
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006197 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006198
Andreas Gampea5b09a62016-11-17 15:21:22 -08006199 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006200
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01006201 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006202
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006203 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006204 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006205 }
6206
Calin Juravle0ba218d2015-05-19 18:46:01 +01006207 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006208 // The entrypoint the code generator is going to call does not do
6209 // clinit of the class.
6210 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006211 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006212 }
6213
6214 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006215 return NeedsAccessCheck() ||
6216 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006217 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006218 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006219 }
6220
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006221 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00006222 return NeedsAccessCheck() ||
6223 MustGenerateClinitCheck() ||
6224 // If the class is in the boot image, the lookup in the runtime call cannot throw.
6225 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
6226 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006227 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006228 GetLoadKind() == LoadKind::kBssEntry) &&
6229 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006230 }
6231
Calin Juravleacf735c2015-02-12 15:25:22 +00006232 ReferenceTypeInfo GetLoadedClassRTI() {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006233 return loaded_class_rti_;
Calin Juravleacf735c2015-02-12 15:25:22 +00006234 }
6235
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006236 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
6237 // Make sure we only set exact types (the loaded class should never be merged).
6238 DCHECK(rti.IsExact());
6239 loaded_class_rti_ = rti;
Calin Juravleacf735c2015-02-12 15:25:22 +00006240 }
6241
Andreas Gampea5b09a62016-11-17 15:21:22 -08006242 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006243 const DexFile& GetDexFile() const { return dex_file_; }
6244
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006245 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006246 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006247 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006248
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006249 static SideEffects SideEffectsForArchRuntimeCalls() {
6250 return SideEffects::CanTriggerGC();
6251 }
6252
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006253 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006254 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006255 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006256 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006257
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006258 void MarkInBootImage() {
6259 SetPackedFlag<kFlagIsInBootImage>(true);
6260 }
6261
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006262 void AddSpecialInput(HInstruction* special_input);
6263
6264 using HInstruction::GetInputRecords; // Keep the const version visible.
6265 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6266 return ArrayRef<HUserRecord<HInstruction*>>(
6267 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6268 }
6269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006270 DataType::Type GetType() const OVERRIDE {
6271 return DataType::Type::kReference;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006272 }
6273
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006274 Handle<mirror::Class> GetClass() const {
6275 return klass_;
6276 }
6277
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006278 DECLARE_INSTRUCTION(LoadClass);
6279
Artem Serovcced8ba2017-07-19 18:18:09 +01006280 protected:
6281 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6282
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006283 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006284 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006285 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006286 // Whether this instruction must generate the initialization check.
6287 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006288 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006289 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6290 static constexpr size_t kFieldLoadKindSize =
6291 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006292 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006293 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006294 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6295
6296 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006297 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006298 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006299 load_kind == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006300 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006301 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006302 }
6303
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006304 void SetLoadKindInternal(LoadKind load_kind);
6305
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006306 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006307 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006308 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006309 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006310
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006311 // A type index and dex file where the class can be accessed. The dex file can be:
6312 // - The compiling method's dex file if the class is defined there too.
6313 // - The compiling method's dex file if the class is referenced there.
6314 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006315 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006316 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006317 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006318
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006319 Handle<mirror::Class> klass_;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006320
6321 ReferenceTypeInfo loaded_class_rti_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006322};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006323std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6324
6325// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006326inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6327 // The load kind should be determined before inserting the instruction to the graph.
6328 DCHECK(GetBlock() == nullptr);
6329 DCHECK(GetEnvironment() == nullptr);
6330 SetPackedField<LoadKindField>(load_kind);
6331 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6332 special_input_ = HUserRecord<HInstruction*>(nullptr);
6333 }
6334 if (!NeedsEnvironment()) {
6335 SetSideEffects(SideEffects::None());
6336 }
6337}
6338
6339// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006340inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006341 // The special input is used for PC-relative loads on some architectures,
6342 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006343 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006344 GetLoadKind() == LoadKind::kBootImageAddress ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006345 GetLoadKind() == LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006346 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006347 DCHECK(special_input_.GetInstruction() == nullptr);
6348 special_input_ = HUserRecord<HInstruction*>(special_input);
6349 special_input->AddUseAt(this, 0);
6350}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006351
Vladimir Marko372f10e2016-05-17 16:30:10 +01006352class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006353 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006354 // Determines how to load the String.
6355 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006356 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006357 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006358 kBootImageLinkTimePcRelative,
6359
6360 // Use a known boot image String* address, embedded in the code by the codegen.
6361 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006362 kBootImageAddress,
6363
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006364 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
6365 // of the oat file.
6366 kBootImageInternTable,
6367
Vladimir Markoaad75c62016-10-03 08:46:48 +00006368 // Load from an entry in the .bss section using a PC-relative load.
6369 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
6370 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006371
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006372 // Load from the root table associated with the JIT compiled method.
6373 kJitTableAddress,
6374
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006375 // Load using a simple runtime call. This is the fall-back load kind when
6376 // the codegen is unable to use another appropriate kind.
6377 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006378
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006379 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006380 };
6381
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006382 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006383 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006384 const DexFile& dex_file,
6385 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306386 : HInstruction(kLoadString, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006387 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006388 string_index_(string_index),
6389 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006390 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006391 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006392
Artem Serovcced8ba2017-07-19 18:18:09 +01006393 bool IsClonable() const OVERRIDE { return true; }
6394
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006395 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006396
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006397 LoadKind GetLoadKind() const {
6398 return GetPackedField<LoadKindField>();
6399 }
6400
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006401 const DexFile& GetDexFile() const {
6402 return dex_file_;
6403 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006404
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006405 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006406 return string_index_;
6407 }
6408
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006409 Handle<mirror::String> GetString() const {
6410 return string_;
6411 }
6412
6413 void SetString(Handle<mirror::String> str) {
6414 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006415 }
6416
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006417 bool CanBeMoved() const OVERRIDE { return true; }
6418
Vladimir Marko372f10e2016-05-17 16:30:10 +01006419 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006420
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006421 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006422
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006423 // Will call the runtime if we need to load the string through
6424 // the dex cache and the string is not guaranteed to be there yet.
6425 bool NeedsEnvironment() const OVERRIDE {
6426 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006427 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006428 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006429 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006430 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006431 return false;
6432 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006433 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006434 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006435
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006436 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006437 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006438 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006439
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07006440 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006441 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006442
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006443 static SideEffects SideEffectsForArchRuntimeCalls() {
6444 return SideEffects::CanTriggerGC();
6445 }
6446
Vladimir Marko372f10e2016-05-17 16:30:10 +01006447 void AddSpecialInput(HInstruction* special_input);
6448
6449 using HInstruction::GetInputRecords; // Keep the const version visible.
6450 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
6451 return ArrayRef<HUserRecord<HInstruction*>>(
6452 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006453 }
6454
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006455 DataType::Type GetType() const OVERRIDE {
6456 return DataType::Type::kReference;
Vladimir Marko372f10e2016-05-17 16:30:10 +01006457 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006458
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006459 DECLARE_INSTRUCTION(LoadString);
6460
Artem Serovcced8ba2017-07-19 18:18:09 +01006461 protected:
6462 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6463
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006464 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006465 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006466 static constexpr size_t kFieldLoadKindSize =
6467 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6468 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006469 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006470 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006471
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006472 void SetLoadKindInternal(LoadKind load_kind);
6473
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006474 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006475 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006476 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006477 HUserRecord<HInstruction*> special_input_;
6478
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006479 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006480 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006481
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006482 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006483};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006484std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6485
6486// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006487inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6488 // The load kind should be determined before inserting the instruction to the graph.
6489 DCHECK(GetBlock() == nullptr);
6490 DCHECK(GetEnvironment() == nullptr);
6491 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6492 SetPackedField<LoadKindField>(load_kind);
6493 if (load_kind != LoadKind::kRuntimeCall) {
6494 special_input_ = HUserRecord<HInstruction*>(nullptr);
6495 }
6496 if (!NeedsEnvironment()) {
6497 SetSideEffects(SideEffects::None());
6498 }
6499}
6500
6501// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006502inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006503 // The special input is used for PC-relative loads on some architectures,
6504 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006505 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006506 GetLoadKind() == LoadKind::kBootImageAddress ||
6507 GetLoadKind() == LoadKind::kBootImageInternTable ||
6508 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006509 // HLoadString::GetInputRecords() returns an empty array at this point,
6510 // so use the GetInputRecords() from the base class to set the input record.
6511 DCHECK(special_input_.GetInstruction() == nullptr);
6512 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006513 special_input->AddUseAt(this, 0);
6514}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006515
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006516/**
6517 * Performs an initialization check on its Class object input.
6518 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006519class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006520 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006521 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006522 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306523 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006524 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006525 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006526 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006527 SetRawInputAt(0, constant);
6528 }
6529
Artem Serovcced8ba2017-07-19 18:18:09 +01006530 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006531 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006532 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006533 return true;
6534 }
6535
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006536 bool NeedsEnvironment() const OVERRIDE {
6537 // May call runtime to initialize the class.
6538 return true;
6539 }
6540
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006541 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006542
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006543 HLoadClass* GetLoadClass() const {
6544 DCHECK(InputAt(0)->IsLoadClass());
6545 return InputAt(0)->AsLoadClass();
6546 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006547
6548 DECLARE_INSTRUCTION(ClinitCheck);
6549
Artem Serovcced8ba2017-07-19 18:18:09 +01006550
6551 protected:
6552 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006553};
6554
Vladimir Markofcb503c2016-05-18 12:48:17 +01006555class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006556 public:
6557 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006558 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006559 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006560 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006561 bool is_volatile,
6562 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006563 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006564 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006565 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306566 : HExpression(kStaticFieldGet,
6567 field_type,
6568 SideEffects::FieldReadOfType(field_type, is_volatile),
6569 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006570 field_info_(field,
6571 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006572 field_type,
6573 is_volatile,
6574 field_idx,
6575 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006576 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006577 SetRawInputAt(0, cls);
6578 }
6579
Calin Juravle52c48962014-12-16 17:02:57 +00006580
Artem Serovcced8ba2017-07-19 18:18:09 +01006581 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006582 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006583
Vladimir Marko372f10e2016-05-17 16:30:10 +01006584 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6585 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006586 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006587 }
6588
6589 size_t ComputeHashCode() const OVERRIDE {
6590 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6591 }
6592
Calin Juravle52c48962014-12-16 17:02:57 +00006593 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006594 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006595 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006596 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006597
Vladimir Marko61b92282017-10-11 13:23:17 +01006598 void SetType(DataType::Type new_type) {
6599 DCHECK(DataType::IsIntegralType(GetType()));
6600 DCHECK(DataType::IsIntegralType(new_type));
6601 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6602 SetPackedField<TypeField>(new_type);
6603 }
6604
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006605 DECLARE_INSTRUCTION(StaticFieldGet);
6606
Artem Serovcced8ba2017-07-19 18:18:09 +01006607 protected:
6608 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6609
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006610 private:
6611 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006612};
6613
Vladimir Markofcb503c2016-05-18 12:48:17 +01006614class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006615 public:
6616 HStaticFieldSet(HInstruction* cls,
6617 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006618 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006619 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006620 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006621 bool is_volatile,
6622 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006623 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006624 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006625 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306626 : HTemplateInstruction(kStaticFieldSet,
6627 SideEffects::FieldWriteOfType(field_type, is_volatile),
6628 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006629 field_info_(field,
6630 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006631 field_type,
6632 is_volatile,
6633 field_idx,
6634 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006635 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006636 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006637 SetRawInputAt(0, cls);
6638 SetRawInputAt(1, value);
6639 }
6640
Artem Serovcced8ba2017-07-19 18:18:09 +01006641 bool IsClonable() const OVERRIDE { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006642 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006643 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006644 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006645 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006646
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006647 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006648 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6649 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006650
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006651 DECLARE_INSTRUCTION(StaticFieldSet);
6652
Artem Serovcced8ba2017-07-19 18:18:09 +01006653 protected:
6654 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6655
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006656 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006657 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6658 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6659 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6660 "Too many packed fields.");
6661
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006662 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006663};
6664
Vladimir Markofcb503c2016-05-18 12:48:17 +01006665class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006666 public:
6667 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006668 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006669 uint32_t field_index,
6670 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306671 : HExpression(kUnresolvedInstanceFieldGet,
6672 field_type,
6673 SideEffects::AllExceptGCDependency(),
6674 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006675 field_index_(field_index) {
6676 SetRawInputAt(0, obj);
6677 }
6678
Artem Serovcced8ba2017-07-19 18:18:09 +01006679 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006680 bool NeedsEnvironment() const OVERRIDE { return true; }
6681 bool CanThrow() const OVERRIDE { return true; }
6682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006683 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006684 uint32_t GetFieldIndex() const { return field_index_; }
6685
6686 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6687
Artem Serovcced8ba2017-07-19 18:18:09 +01006688 protected:
6689 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6690
Calin Juravlee460d1d2015-09-29 04:52:17 +01006691 private:
6692 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006693};
6694
Vladimir Markofcb503c2016-05-18 12:48:17 +01006695class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006696 public:
6697 HUnresolvedInstanceFieldSet(HInstruction* obj,
6698 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006699 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006700 uint32_t field_index,
6701 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306702 : HTemplateInstruction(kUnresolvedInstanceFieldSet,
6703 SideEffects::AllExceptGCDependency(),
6704 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006705 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006706 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006707 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006708 SetRawInputAt(0, obj);
6709 SetRawInputAt(1, value);
6710 }
6711
Artem Serovcced8ba2017-07-19 18:18:09 +01006712 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006713 bool NeedsEnvironment() const OVERRIDE { return true; }
6714 bool CanThrow() const OVERRIDE { return true; }
6715
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006716 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006717 uint32_t GetFieldIndex() const { return field_index_; }
6718
6719 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6720
Artem Serovcced8ba2017-07-19 18:18:09 +01006721 protected:
6722 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6723
Calin Juravlee460d1d2015-09-29 04:52:17 +01006724 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006725 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6726 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006728 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6729 kFieldFieldType + kFieldFieldTypeSize;
6730 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6731 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006732 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006733
Calin Juravlee460d1d2015-09-29 04:52:17 +01006734 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006735};
6736
Vladimir Markofcb503c2016-05-18 12:48:17 +01006737class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006738 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006739 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006740 uint32_t field_index,
6741 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306742 : HExpression(kUnresolvedStaticFieldGet,
6743 field_type,
6744 SideEffects::AllExceptGCDependency(),
6745 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006746 field_index_(field_index) {
6747 }
6748
Artem Serovcced8ba2017-07-19 18:18:09 +01006749 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006750 bool NeedsEnvironment() const OVERRIDE { return true; }
6751 bool CanThrow() const OVERRIDE { return true; }
6752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006753 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006754 uint32_t GetFieldIndex() const { return field_index_; }
6755
6756 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6757
Artem Serovcced8ba2017-07-19 18:18:09 +01006758 protected:
6759 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6760
Calin Juravlee460d1d2015-09-29 04:52:17 +01006761 private:
6762 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006763};
6764
Vladimir Markofcb503c2016-05-18 12:48:17 +01006765class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006766 public:
6767 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006768 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006769 uint32_t field_index,
6770 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306771 : HTemplateInstruction(kUnresolvedStaticFieldSet,
6772 SideEffects::AllExceptGCDependency(),
6773 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006774 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006775 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006776 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006777 SetRawInputAt(0, value);
6778 }
6779
Artem Serovcced8ba2017-07-19 18:18:09 +01006780 bool IsClonable() const OVERRIDE { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006781 bool NeedsEnvironment() const OVERRIDE { return true; }
6782 bool CanThrow() const OVERRIDE { return true; }
6783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006784 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006785 uint32_t GetFieldIndex() const { return field_index_; }
6786
6787 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6788
Artem Serovcced8ba2017-07-19 18:18:09 +01006789 protected:
6790 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6791
Calin Juravlee460d1d2015-09-29 04:52:17 +01006792 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006793 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6794 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006795 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006796 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6797 kFieldFieldType + kFieldFieldTypeSize;
6798 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6799 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006800 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006801
Calin Juravlee460d1d2015-09-29 04:52:17 +01006802 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006803};
6804
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006805// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006806class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006808 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306809 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6810 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006811
David Brazdilbbd733e2015-08-18 17:48:17 +01006812 bool CanBeNull() const OVERRIDE { return false; }
6813
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006814 DECLARE_INSTRUCTION(LoadException);
6815
Artem Serovcced8ba2017-07-19 18:18:09 +01006816 protected:
6817 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006818};
6819
David Brazdilcb1c0552015-08-04 16:22:25 +01006820// Implicit part of move-exception which clears thread-local exception storage.
6821// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006822class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006823 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006824 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306825 : HTemplateInstruction(kClearException, SideEffects::AllWrites(), dex_pc) {
6826 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006827
6828 DECLARE_INSTRUCTION(ClearException);
6829
Artem Serovcced8ba2017-07-19 18:18:09 +01006830 protected:
6831 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006832};
6833
Vladimir Markofcb503c2016-05-18 12:48:17 +01006834class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006835 public:
6836 HThrow(HInstruction* exception, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306837 : HTemplateInstruction(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006838 SetRawInputAt(0, exception);
6839 }
6840
6841 bool IsControlFlow() const OVERRIDE { return true; }
6842
6843 bool NeedsEnvironment() const OVERRIDE { return true; }
6844
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006845 bool CanThrow() const OVERRIDE { return true; }
6846
Aart Bika8b8e9b2018-01-09 11:01:02 -08006847 bool AlwaysThrows() const OVERRIDE { return true; }
6848
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006849 DECLARE_INSTRUCTION(Throw);
6850
Artem Serovcced8ba2017-07-19 18:18:09 +01006851 protected:
6852 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006853};
6854
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006855/**
6856 * Implementation strategies for the code generator of a HInstanceOf
6857 * or `HCheckCast`.
6858 */
6859enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006860 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006861 kExactCheck, // Can do a single class compare.
6862 kClassHierarchyCheck, // Can just walk the super class chain.
6863 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6864 kInterfaceCheck, // No optimization yet when checking against an interface.
6865 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006866 kArrayCheck, // No optimization yet when checking against a generic array.
6867 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006868};
6869
Roland Levillain86503782016-02-11 19:07:30 +00006870std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6871
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006872class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006873 public:
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006874 HInstanceOf(HInstruction* object,
6875 HLoadClass* target_class,
6876 TypeCheckKind check_kind,
6877 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306878 : HExpression(kInstanceOf,
6879 DataType::Type::kBool,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006880 SideEffectsForArchRuntimeCalls(check_kind),
6881 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006882 SetPackedField<TypeCheckKindField>(check_kind);
6883 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006884 SetRawInputAt(0, object);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006885 SetRawInputAt(1, target_class);
Vladimir Marko87584542017-12-12 17:47:52 +00006886 }
6887
6888 HLoadClass* GetTargetClass() const {
6889 HInstruction* load_class = InputAt(1);
6890 DCHECK(load_class->IsLoadClass());
6891 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006892 }
6893
Artem Serovcced8ba2017-07-19 18:18:09 +01006894 bool IsClonable() const OVERRIDE { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006895 bool CanBeMoved() const OVERRIDE { return true; }
6896
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006897 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6898 return true;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006899 }
6900
Vladimir Markoeb0ebed2018-01-10 18:26:38 +00006901 bool NeedsEnvironment() const OVERRIDE {
6902 return CanCallRuntime(GetTypeCheckKind());
6903 }
6904
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006905 // Used only in code generation.
6906 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6907 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6908 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6909 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
6910
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006911 static bool CanCallRuntime(TypeCheckKind check_kind) {
6912 // Mips currently does runtime calls for any other checks.
6913 return check_kind != TypeCheckKind::kExactCheck;
6914 }
6915
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006916 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006917 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006918 }
6919
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006920 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006921
Artem Serovcced8ba2017-07-19 18:18:09 +01006922 protected:
6923 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006924
6925 private:
6926 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6927 static constexpr size_t kFieldTypeCheckKindSize =
6928 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6929 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6930 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6931 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6932 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006933};
6934
Vladimir Markofcb503c2016-05-18 12:48:17 +01006935class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006936 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006937 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306938 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006939 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6940 SetPackedFlag<kFlagUpperCanBeNull>(true);
6941 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006942 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00006943 SetRawInputAt(0, input);
6944 }
6945
Artem Serovcced8ba2017-07-19 18:18:09 +01006946 bool IsClonable() const OVERRIDE { return true; }
6947
David Brazdilf5552582015-12-27 13:36:12 +00006948 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006949 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006950 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006951 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006952
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006953 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006954 DCHECK(GetUpperCanBeNull() || !can_be_null);
6955 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006956 }
6957
Vladimir Markoa1de9182016-02-25 11:37:38 +00006958 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006959
Calin Juravleb1498f62015-02-16 13:13:29 +00006960 DECLARE_INSTRUCTION(BoundType);
6961
Artem Serovcced8ba2017-07-19 18:18:09 +01006962 protected:
6963 DEFAULT_COPY_CONSTRUCTOR(BoundType);
6964
Calin Juravleb1498f62015-02-16 13:13:29 +00006965 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006966 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6967 // is false then CanBeNull() cannot be true).
6968 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6969 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6970 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6971 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6972
Calin Juravleb1498f62015-02-16 13:13:29 +00006973 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006974 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6975 // It is used to bound the type in cases like:
6976 // if (x instanceof ClassX) {
6977 // // uper_bound_ will be ClassX
6978 // }
David Brazdilf5552582015-12-27 13:36:12 +00006979 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006980};
6981
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006982class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006983 public:
6984 HCheckCast(HInstruction* object,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006985 HLoadClass* target_class,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006986 TypeCheckKind check_kind,
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006987 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306988 : HTemplateInstruction(kCheckCast, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006989 SetPackedField<TypeCheckKindField>(check_kind);
6990 SetPackedFlag<kFlagMustDoNullCheck>(true);
6991 SetRawInputAt(0, object);
6992 SetRawInputAt(1, target_class);
6993 }
6994
6995 HLoadClass* GetTargetClass() const {
6996 HInstruction* load_class = InputAt(1);
6997 DCHECK(load_class->IsLoadClass());
6998 return load_class->AsLoadClass();
6999 }
7000
7001 bool IsClonable() const OVERRIDE { return true; }
7002 bool CanBeMoved() const OVERRIDE { return true; }
7003
7004 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7005 return true;
7006 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007007
7008 bool NeedsEnvironment() const OVERRIDE {
7009 // Instruction may throw a CheckCastError.
7010 return true;
7011 }
7012
7013 bool CanThrow() const OVERRIDE { return true; }
7014
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007015 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7016 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7017 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7018 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7019
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007020 DECLARE_INSTRUCTION(CheckCast);
7021
Artem Serovcced8ba2017-07-19 18:18:09 +01007022 protected:
7023 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007024
7025 private:
7026 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7027 static constexpr size_t kFieldTypeCheckKindSize =
7028 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7029 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7030 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
7031 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7032 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007033};
7034
Andreas Gampe26de38b2016-07-27 17:53:11 -07007035/**
7036 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7037 * @details We define the combined barrier types that are actually required
7038 * by the Java Memory Model, rather than using exactly the terminology from
7039 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7040 * primitives. Note that the JSR-133 cookbook generally does not deal with
7041 * store atomicity issues, and the recipes there are not always entirely sufficient.
7042 * The current recipe is as follows:
7043 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7044 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7045 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7046 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7047 * class has final fields.
7048 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7049 * store-to-memory instructions. Only generated together with non-temporal stores.
7050 */
7051enum MemBarrierKind {
7052 kAnyStore,
7053 kLoadAny,
7054 kStoreStore,
7055 kAnyAny,
7056 kNTStoreStore,
7057 kLastBarrierKind = kNTStoreStore
7058};
7059std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7060
Vladimir Markofcb503c2016-05-18 12:48:17 +01007061class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007062 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007063 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07007064 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307065 kMemoryBarrier,
7066 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7067 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007068 SetPackedField<BarrierKindField>(barrier_kind);
7069 }
Calin Juravle27df7582015-04-17 19:12:31 +01007070
Artem Serovcced8ba2017-07-19 18:18:09 +01007071 bool IsClonable() const OVERRIDE { return true; }
7072
Vladimir Markoa1de9182016-02-25 11:37:38 +00007073 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007074
7075 DECLARE_INSTRUCTION(MemoryBarrier);
7076
Artem Serovcced8ba2017-07-19 18:18:09 +01007077 protected:
7078 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7079
Calin Juravle27df7582015-04-17 19:12:31 +01007080 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007081 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7082 static constexpr size_t kFieldBarrierKindSize =
7083 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7084 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7085 kFieldBarrierKind + kFieldBarrierKindSize;
7086 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7087 "Too many packed fields.");
7088 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007089};
7090
Igor Murashkind01745e2017-04-05 16:40:31 -07007091// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7092// the specified object(s), with respect to any subsequent store that might "publish"
7093// (i.e. make visible) the specified object to another thread.
7094//
7095// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7096// for all final fields (that were set) at the end of the invoked constructor.
7097//
7098// The constructor fence models the freeze actions for the final fields of an object
7099// being constructed (semantically at the end of the constructor). Constructor fences
7100// have a per-object affinity; two separate objects being constructed get two separate
7101// constructor fences.
7102//
7103// (Note: that if calling a super-constructor or forwarding to another constructor,
7104// the freezes would happen at the end of *that* constructor being invoked).
7105//
7106// The memory model guarantees that when the object being constructed is "published" after
7107// constructor completion (i.e. escapes the current thread via a store), then any final field
7108// writes must be observable on other threads (once they observe that publication).
7109//
7110// Further, anything written before the freeze, and read by dereferencing through the final field,
7111// must also be visible (so final object field could itself have an object with non-final fields;
7112// yet the freeze must also extend to them).
7113//
7114// Constructor example:
7115//
7116// class HasFinal {
7117// final int field; Optimizing IR for <init>()V:
7118// HasFinal() {
7119// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7120// // freeze(this.field); HConstructorFence(this)
7121// } HReturn
7122// }
7123//
7124// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7125// already-initialized classes; in that case the allocation must act as a "default-initializer"
7126// of the object which effectively writes the class pointer "final field".
7127//
7128// For example, we can model default-initialiation as roughly the equivalent of the following:
7129//
7130// class Object {
7131// private final Class header;
7132// }
7133//
7134// Java code: Optimizing IR:
7135//
7136// T new_instance<T>() {
7137// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7138// obj.header = T.class; // header write is done by above call.
7139// // freeze(obj.header) HConstructorFence(obj)
7140// return (T)obj;
7141// }
7142//
7143// See also:
7144// * CompilerDriver::RequiresConstructorBarrier
7145// * QuasiAtomic::ThreadFenceForConstructor
7146//
7147class HConstructorFence FINAL : public HVariableInputSizeInstruction {
7148 // A fence has variable inputs because the inputs can be removed
7149 // after prepare_for_register_allocation phase.
7150 // (TODO: In the future a fence could freeze multiple objects
7151 // after merging two fences together.)
7152 public:
7153 // `fence_object` is the reference that needs to be protected for correct publication.
7154 //
7155 // It makes sense in the following situations:
7156 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7157 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7158 //
7159 // After construction the `fence_object` becomes the 0th input.
7160 // This is not an input in a real sense, but just a convenient place to stash the information
7161 // about the associated object.
7162 HConstructorFence(HInstruction* fence_object,
7163 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007164 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007165 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7166 // constraints described in the class header. We claim that these SideEffects constraints
7167 // enforce a superset of the real constraints.
7168 //
7169 // The ordering described above is conservatively modeled with SideEffects as follows:
7170 //
7171 // * To prevent reordering of the publication stores:
7172 // ----> "Reads of objects" is the initial SideEffect.
7173 // * For every primitive final field store in the constructor:
7174 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7175 // * If there are any stores to reference final fields in the constructor:
7176 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7177 // that are reachable from `fence_object` also need to be prevented for reordering
7178 // (and we do not want to do alias analysis to figure out what those stores are).
7179 //
7180 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7181 // even more conservative approach than the one described above, and prevents all of the
7182 // above reordering without analyzing any of the instructions in the constructor.
7183 //
7184 // If in a later phase we discover that there are no writes to reference final fields,
7185 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307186 : HVariableInputSizeInstruction(kConstructorFence,
7187 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007188 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007189 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007190 /* number_of_inputs */ 1,
7191 kArenaAllocConstructorFenceInputs) {
7192 DCHECK(fence_object != nullptr);
7193 SetRawInputAt(0, fence_object);
7194 }
7195
7196 // The object associated with this constructor fence.
7197 //
7198 // (Note: This will be null after the prepare_for_register_allocation phase,
7199 // as all constructor fence inputs are removed there).
7200 HInstruction* GetFenceObject() const {
7201 return InputAt(0);
7202 }
7203
7204 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7205 // - Delete `fence_use` from `this`'s use list.
7206 // - Delete `this` from `fence_use`'s inputs list.
7207 // - If the `fence_use` is dead, remove it from the graph.
7208 //
7209 // A fence is considered dead once it no longer has any uses
7210 // and all of the inputs are dead.
7211 //
7212 // This must *not* be called during/after prepare_for_register_allocation,
7213 // because that removes all the inputs to the fences but the fence is actually
7214 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007215 //
7216 // Returns how many HConstructorFence instructions were removed from graph.
7217 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007218
Igor Murashkindd018df2017-08-09 10:38:31 -07007219 // Combine all inputs of `this` and `other` instruction and remove
7220 // `other` from the graph.
7221 //
7222 // Inputs are unique after the merge.
7223 //
7224 // Requirement: `this` must not be the same as `other.
7225 void Merge(HConstructorFence* other);
7226
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007227 // Check if this constructor fence is protecting
7228 // an HNewInstance or HNewArray that is also the immediate
7229 // predecessor of `this`.
7230 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007231 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7232 // to be one of the inputs of `this`.
7233 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007234 // Returns the associated HNewArray or HNewInstance,
7235 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007236 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007237
Igor Murashkind01745e2017-04-05 16:40:31 -07007238 DECLARE_INSTRUCTION(ConstructorFence);
7239
Artem Serovcced8ba2017-07-19 18:18:09 +01007240 protected:
7241 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007242};
7243
Vladimir Markofcb503c2016-05-18 12:48:17 +01007244class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007245 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007246 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007247 kEnter,
7248 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007249 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007250 };
7251
7252 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007253 : HTemplateInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307254 kMonitorOperation,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007255 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7256 dex_pc) {
7257 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007258 SetRawInputAt(0, object);
7259 }
7260
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007261 // Instruction may go into runtime, so we need an environment.
7262 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007263
7264 bool CanThrow() const OVERRIDE {
7265 // Verifier guarantees that monitor-exit cannot throw.
7266 // This is important because it allows the HGraphBuilder to remove
7267 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7268 return IsEnter();
7269 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007270
Vladimir Markoa1de9182016-02-25 11:37:38 +00007271 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7272 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007273
7274 DECLARE_INSTRUCTION(MonitorOperation);
7275
Artem Serovcced8ba2017-07-19 18:18:09 +01007276 protected:
7277 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7278
Calin Juravle52c48962014-12-16 17:02:57 +00007279 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007280 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7281 static constexpr size_t kFieldOperationKindSize =
7282 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7283 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7284 kFieldOperationKind + kFieldOperationKindSize;
7285 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7286 "Too many packed fields.");
7287 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007288};
7289
Vladimir Markofcb503c2016-05-18 12:48:17 +01007290class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007291 public:
7292 HSelect(HInstruction* condition,
7293 HInstruction* true_value,
7294 HInstruction* false_value,
7295 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307296 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007297 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7298
7299 // First input must be `true_value` or `false_value` to allow codegens to
7300 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7301 // that architectures which implement HSelect as a conditional move also
7302 // will not need to invert the condition.
7303 SetRawInputAt(0, false_value);
7304 SetRawInputAt(1, true_value);
7305 SetRawInputAt(2, condition);
7306 }
7307
Artem Serovcced8ba2017-07-19 18:18:09 +01007308 bool IsClonable() const OVERRIDE { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007309 HInstruction* GetFalseValue() const { return InputAt(0); }
7310 HInstruction* GetTrueValue() const { return InputAt(1); }
7311 HInstruction* GetCondition() const { return InputAt(2); }
7312
7313 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01007314 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7315 return true;
7316 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007317
7318 bool CanBeNull() const OVERRIDE {
7319 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7320 }
7321
7322 DECLARE_INSTRUCTION(Select);
7323
Artem Serovcced8ba2017-07-19 18:18:09 +01007324 protected:
7325 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007326};
7327
Vladimir Markof9f64412015-09-02 14:05:49 +01007328class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007329 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007330 MoveOperands(Location source,
7331 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007332 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007333 HInstruction* instruction)
7334 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007335
7336 Location GetSource() const { return source_; }
7337 Location GetDestination() const { return destination_; }
7338
7339 void SetSource(Location value) { source_ = value; }
7340 void SetDestination(Location value) { destination_ = value; }
7341
7342 // The parallel move resolver marks moves as "in-progress" by clearing the
7343 // destination (but not the source).
7344 Location MarkPending() {
7345 DCHECK(!IsPending());
7346 Location dest = destination_;
7347 destination_ = Location::NoLocation();
7348 return dest;
7349 }
7350
7351 void ClearPending(Location dest) {
7352 DCHECK(IsPending());
7353 destination_ = dest;
7354 }
7355
7356 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007357 DCHECK(source_.IsValid() || destination_.IsInvalid());
7358 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007359 }
7360
7361 // True if this blocks a move from the given location.
7362 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007363 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007364 }
7365
7366 // A move is redundant if it's been eliminated, if its source and
7367 // destination are the same, or if its destination is unneeded.
7368 bool IsRedundant() const {
7369 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7370 }
7371
7372 // We clear both operands to indicate move that's been eliminated.
7373 void Eliminate() {
7374 source_ = destination_ = Location::NoLocation();
7375 }
7376
7377 bool IsEliminated() const {
7378 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7379 return source_.IsInvalid();
7380 }
7381
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007382 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007383
Nicolas Geoffray90218252015-04-15 11:56:51 +01007384 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007385 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007386 }
7387
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007388 HInstruction* GetInstruction() const { return instruction_; }
7389
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007390 private:
7391 Location source_;
7392 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007393 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007394 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007395 // The instruction this move is assocatied with. Null when this move is
7396 // for moving an input in the expected locations of user (including a phi user).
7397 // This is only used in debug mode, to ensure we do not connect interval siblings
7398 // in the same parallel move.
7399 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007400};
7401
Roland Levillainc9285912015-12-18 10:38:42 +00007402std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7403
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007404static constexpr size_t kDefaultNumberOfMoves = 4;
7405
Vladimir Markofcb503c2016-05-18 12:48:17 +01007406class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007407 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007408 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307409 : HTemplateInstruction(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007410 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007411 moves_.reserve(kDefaultNumberOfMoves);
7412 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007413
Nicolas Geoffray90218252015-04-15 11:56:51 +01007414 void AddMove(Location source,
7415 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007416 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007417 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007418 DCHECK(source.IsValid());
7419 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007420 if (kIsDebugBuild) {
7421 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007422 for (const MoveOperands& move : moves_) {
7423 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007424 // Special case the situation where the move is for the spill slot
7425 // of the instruction.
7426 if ((GetPrevious() == instruction)
7427 || ((GetPrevious() == nullptr)
7428 && instruction->IsPhi()
7429 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007430 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007431 << "Doing parallel moves for the same instruction.";
7432 } else {
7433 DCHECK(false) << "Doing parallel moves for the same instruction.";
7434 }
7435 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007436 }
7437 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007438 for (const MoveOperands& move : moves_) {
7439 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007440 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007441 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007442 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007443 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007444 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007445 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007446 }
7447
Vladimir Marko225b6462015-09-28 12:17:40 +01007448 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007449 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007450 }
7451
Vladimir Marko225b6462015-09-28 12:17:40 +01007452 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007454 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007455
Artem Serovcced8ba2017-07-19 18:18:09 +01007456 protected:
7457 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7458
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007459 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007460 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007461};
7462
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007463// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7464// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7465// never used across anything that can trigger GC.
7466// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7467// So we represent it by the type `DataType::Type::kInt`.
7468class HIntermediateAddress FINAL : public HExpression<2> {
7469 public:
7470 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307471 : HExpression(kIntermediateAddress,
7472 DataType::Type::kInt32,
7473 SideEffects::DependsOnGC(),
7474 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007475 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7476 DataType::Size(DataType::Type::kReference))
7477 << "kPrimInt and kPrimNot have different sizes.";
7478 SetRawInputAt(0, base_address);
7479 SetRawInputAt(1, offset);
7480 }
7481
Artem Serovcced8ba2017-07-19 18:18:09 +01007482 bool IsClonable() const OVERRIDE { return true; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007483 bool CanBeMoved() const OVERRIDE { return true; }
7484 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
7485 return true;
7486 }
7487 bool IsActualObject() const OVERRIDE { return false; }
7488
7489 HInstruction* GetBaseAddress() const { return InputAt(0); }
7490 HInstruction* GetOffset() const { return InputAt(1); }
7491
7492 DECLARE_INSTRUCTION(IntermediateAddress);
7493
Artem Serovcced8ba2017-07-19 18:18:09 +01007494 protected:
7495 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007496};
7497
7498
Mark Mendell0616ae02015-04-17 12:49:27 -04007499} // namespace art
7500
Aart Bikf8f5a162017-02-06 15:35:29 -08007501#include "nodes_vector.h"
7502
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007503#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7504#include "nodes_shared.h"
7505#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007506#ifdef ART_ENABLE_CODEGEN_mips
7507#include "nodes_mips.h"
7508#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007509#ifdef ART_ENABLE_CODEGEN_x86
7510#include "nodes_x86.h"
7511#endif
7512
7513namespace art {
7514
Igor Murashkin6ef45672017-08-08 13:59:55 -07007515class OptimizingCompilerStats;
7516
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007517class HGraphVisitor : public ValueObject {
7518 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007519 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7520 : stats_(stats),
7521 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007522 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007523
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007524 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007525 virtual void VisitBasicBlock(HBasicBlock* block);
7526
Roland Levillain633021e2014-10-01 14:12:25 +01007527 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007528 void VisitInsertionOrder();
7529
Roland Levillain633021e2014-10-01 14:12:25 +01007530 // Visit the graph following dominator tree reverse post-order.
7531 void VisitReversePostOrder();
7532
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007533 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007534
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007535 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007536#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007537 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7538
7539 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7540
7541#undef DECLARE_VISIT_INSTRUCTION
7542
Igor Murashkin6ef45672017-08-08 13:59:55 -07007543 protected:
7544 OptimizingCompilerStats* stats_;
7545
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007546 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007547 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007548
7549 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7550};
7551
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007552class HGraphDelegateVisitor : public HGraphVisitor {
7553 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007554 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7555 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007556 virtual ~HGraphDelegateVisitor() {}
7557
7558 // Visit functions that delegate to to super class.
7559#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00007560 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007561
7562 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7563
7564#undef DECLARE_VISIT_INSTRUCTION
7565
7566 private:
7567 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7568};
7569
Artem Serovcced8ba2017-07-19 18:18:09 +01007570// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7571// and remove the old instruction.
7572HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7573
7574// Create a clone for each clonable instructions/phis and replace the original with the clone.
7575//
7576// Used for testing individual instruction cloner.
7577class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7578 public:
7579 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007580 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007581
7582 void VisitInstruction(HInstruction* instruction) OVERRIDE {
7583 if (instruction->IsClonable()) {
7584 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007585 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007586 }
7587 }
7588
Artem Serov7f4aff62017-06-21 17:02:18 +01007589 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007590
7591 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007592 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007593
7594 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7595};
7596
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007597// Iterator over the blocks that art part of the loop. Includes blocks part
7598// of an inner loop. The order in which the blocks are iterated is on their
7599// block id.
7600class HBlocksInLoopIterator : public ValueObject {
7601 public:
7602 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7603 : blocks_in_loop_(info.GetBlocks()),
7604 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7605 index_(0) {
7606 if (!blocks_in_loop_.IsBitSet(index_)) {
7607 Advance();
7608 }
7609 }
7610
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007611 bool Done() const { return index_ == blocks_.size(); }
7612 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007613 void Advance() {
7614 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007615 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007616 if (blocks_in_loop_.IsBitSet(index_)) {
7617 break;
7618 }
7619 }
7620 }
7621
7622 private:
7623 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007624 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007625 size_t index_;
7626
7627 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7628};
7629
Mingyao Yang3584bce2015-05-19 16:01:59 -07007630// Iterator over the blocks that art part of the loop. Includes blocks part
7631// of an inner loop. The order in which the blocks are iterated is reverse
7632// post order.
7633class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7634 public:
7635 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7636 : blocks_in_loop_(info.GetBlocks()),
7637 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7638 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007639 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007640 Advance();
7641 }
7642 }
7643
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007644 bool Done() const { return index_ == blocks_.size(); }
7645 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007646 void Advance() {
7647 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007648 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7649 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007650 break;
7651 }
7652 }
7653 }
7654
7655 private:
7656 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007657 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007658 size_t index_;
7659
7660 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7661};
7662
Aart Bikf3e61ee2017-04-12 17:09:20 -07007663// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007664inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007665 if (constant->IsIntConstant()) {
7666 return constant->AsIntConstant()->GetValue();
7667 } else if (constant->IsLongConstant()) {
7668 return constant->AsLongConstant()->GetValue();
7669 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007670 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007671 return 0;
7672 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007673}
7674
Aart Bikf3e61ee2017-04-12 17:09:20 -07007675// Returns true iff instruction is an integral constant (and sets value on success).
7676inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7677 if (instruction->IsIntConstant()) {
7678 *value = instruction->AsIntConstant()->GetValue();
7679 return true;
7680 } else if (instruction->IsLongConstant()) {
7681 *value = instruction->AsLongConstant()->GetValue();
7682 return true;
7683 } else if (instruction->IsNullConstant()) {
7684 *value = 0;
7685 return true;
7686 }
7687 return false;
7688}
7689
Aart Bik0148de42017-09-05 09:25:01 -07007690// Returns true iff instruction is the given integral constant.
7691inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7692 int64_t val = 0;
7693 return IsInt64AndGet(instruction, &val) && val == value;
7694}
7695
7696// Returns true iff instruction is a zero bit pattern.
7697inline bool IsZeroBitPattern(HInstruction* instruction) {
7698 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7699}
7700
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007701#define INSTRUCTION_TYPE_CHECK(type, super) \
7702 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7703 inline const H##type* HInstruction::As##type() const { \
7704 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7705 } \
7706 inline H##type* HInstruction::As##type() { \
7707 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7708 }
7709
7710 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7711#undef INSTRUCTION_TYPE_CHECK
7712
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007713// Create space in `blocks` for adding `number_of_new_blocks` entries
7714// starting at location `at`. Blocks after `at` are moved accordingly.
7715inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7716 size_t number_of_new_blocks,
7717 size_t after) {
7718 DCHECK_LT(after, blocks->size());
7719 size_t old_size = blocks->size();
7720 size_t new_size = old_size + number_of_new_blocks;
7721 blocks->resize(new_size);
7722 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7723}
7724
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007725/*
7726 * Hunt "under the hood" of array lengths (leading to array references),
7727 * null checks (also leading to array references), and new arrays
7728 * (leading to the actual length). This makes it more likely related
7729 * instructions become actually comparable.
7730 */
7731inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7732 while (instruction->IsArrayLength() ||
7733 instruction->IsNullCheck() ||
7734 instruction->IsNewArray()) {
7735 instruction = instruction->IsNewArray()
7736 ? instruction->AsNewArray()->GetLength()
7737 : instruction->InputAt(0);
7738 }
7739 return instruction;
7740}
7741
Artem Serov21c7e6f2017-07-27 16:04:42 +01007742void RemoveEnvironmentUses(HInstruction* instruction);
7743bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7744void ResetEnvironmentInputRecords(HInstruction* instruction);
7745
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007746} // namespace art
7747
7748#endif // ART_COMPILER_OPTIMIZING_NODES_H_